Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 136 additions & 104 deletions schemas/Metals/v0.1.1/stylesheet.xsl

Large diffs are not rendered by default.

76 changes: 74 additions & 2 deletions schemas/Metals/v0.1.1/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,43 @@
"OtherMechanicalTests": "Other Mechanical Tests",
"NonDestructiveTests": "Non-Destructive Tests",
"OtherProductTests": "Other Product Tests",
"Attachments": "Attachments"
"Attachments": "Attachments",

"ProductName": "Product Name",
"ToleranceStandard": "Tolerance Standard",
"Specification": "Specification",
"DiameterTolerance": "Diameter Tolerance",
"FlangeThicknessTolerance": "Flange Thickness Tolerance",
"FlangeWidthTolerance": "Flange Width Tolerance",
"HeightTolerance": "Height Tolerance",
"InnerDiameterTolerance": "Inner Diameter Tolerance",
"LengthTolerance": "Length Tolerance",
"OuterDiameterTolerance": "Outer Diameter Tolerance",
"ThicknessTolerance": "Thickness Tolerance",
"WallThicknessTolerance": "Wall Thickness Tolerance",
"WebThicknessTolerance": "Web Thickness Tolerance",
"WidthTolerance": "Width Tolerance",
"Lot": "Lot",
"Stage": "Stage",
"Atmosphere": "Atmosphere",
"Property": "Property",
"Symbol": "Symbol",
"Min": "Min",
"Max": "Max",
"Status": "Status",
"TargetOrMinimum": "Target/Min",
"Test": "Test",
"ResultAndLimits": "Result / limits",
"SequenceNumber": "No.",
"Average": "Average",
"StdDev": "Std Dev",
"SpecMin": "Spec Min",
"SpecMax": "Spec Max",
"Parameter": "Parameter",
"MicroPurityIso4967": "Micro-purity acc. ISO 4967 - actual (max) per type",
"Specimen": "Specimen",
"PositionShort": "Pos.",
"ValidatedBy": "Validated by"
}
},
"DE": {
Expand Down Expand Up @@ -811,7 +847,43 @@
"OtherMechanicalTests": "Andere mechanische Prüfungen",
"NonDestructiveTests": "Zerstörungsfreie Prüfungen",
"OtherProductTests": "Andere Produktprüfungen",
"Attachments": "Anlagen"
"Attachments": "Anlagen",

"ProductName": "Produktname",
"ToleranceStandard": "Toleranznorm",
"Specification": "Spezifikation",
"DiameterTolerance": "Durchmessertoleranz",
"FlangeThicknessTolerance": "Flanschdickentoleranz",
"FlangeWidthTolerance": "Flanschbreitentoleranz",
"HeightTolerance": "Höhentoleranz",
"InnerDiameterTolerance": "Innendurchmessertoleranz",
"LengthTolerance": "Längentoleranz",
"OuterDiameterTolerance": "Außendurchmessertoleranz",
"ThicknessTolerance": "Dickentoleranz",
"WallThicknessTolerance": "Wanddickentoleranz",
"WebThicknessTolerance": "Stegdickentoleranz",
"WidthTolerance": "Breitentoleranz",
"Lot": "Los",
"Stage": "Stufe",
"Atmosphere": "Atmosphäre",
"Property": "Eigenschaft",
"Symbol": "Symbol",
"Min": "Min",
"Max": "Max",
"Status": "Status",
"TargetOrMinimum": "Soll/Min",
"Test": "Prüfung",
"ResultAndLimits": "Ergebnis mit Grenzwerten",
"SequenceNumber": "Nr.",
"Average": "Durchschnitt",
"StdDev": "Std.-Abw.",
"SpecMin": "Spez. Min",
"SpecMax": "Spez. Max",
"Parameter": "Parameter",
"MicroPurityIso4967": "Mikroreinheit nach ISO 4967 - Ist (max) je Typ",
"Specimen": "Probekörper",
"PositionShort": "Pos.",
"ValidatedBy": "Validiert von"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public JsonNode load() throws IOException {
ObjectNode resourceTranslations = loadTranslationsFromResource(resource);
JsonMerger.deepMerge(resourceTranslations, translations, null);
}
return translations;
// Translation keys become XML element names under Root/Translations; Metals' keys include
// enum values such as "1/4T" that are not legal names (material-identity/schemas#331).
return XmlKeySanitizer.sanitize(translations);
}

private ObjectNode loadTranslationsFromResource(Resource resource) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,9 @@ public WatermarkManager(String text) {
*/
public static String resolveText(String translationFilePattern, String primaryLanguage) {
String lang = primaryLanguage.toUpperCase(Locale.ROOT);
// Broaden "translations*.json" to "translation*.json" so Metals' singular
// translation.json (unmatched by the render pipeline's pattern) is found too.
String pattern = translationFilePattern.replace("translations*", "translation*");
try {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
for (Resource resource : resolver.getResources("classpath*:" + pattern)) {
for (Resource resource : resolver.getResources("classpath*:" + translationFilePattern)) {
JsonNode tree;
try (InputStream is = resource.getInputStream()) {
tree = objectMapper.readTree(is);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.materialidentity.schemaservice;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.HashSet;
import java.util.Set;

/**
* Rewrites JSON object keys so the document survives the XmlMapper JSON → XML conversion in
* {@link XsltTransformer}, which writes keys verbatim as element names. Metals' translation.json
* carries enum values such as {@code 1/4T} or {@code Quenching and Tempering} as keys; loading it
* as {@code Root/Translations} unsanitized produces XML that does not parse
* (material-identity/schemas#331). Any character that is illegal in an XML name becomes
* {@code _}, and a key that cannot start an XML name gets a {@code _} prefix. Legal keys pass
* through unchanged. Ported from material-identity/schema's {@code :legacy} module.
*/
public final class XmlKeySanitizer {

private XmlKeySanitizer() {
}

/** A deep copy of {@code node} with every object key rewritten to a legal XML element name. */
public static JsonNode sanitize(JsonNode node) {
if (node.isObject()) {
// A key that is already legal owns its name; a rewritten key that lands on a taken
// name moves aside with a "_" prefix. Otherwise "a b" before "a_b" would take "a_b"
// and a lookup of a_b would read the wrong value. Key order is preserved.
Set<String> taken = new HashSet<>();
node.fieldNames().forEachRemaining(key -> {
if (sanitizeKey(key).equals(key)) {
taken.add(key);
}
});
ObjectNode out = JsonNodeFactory.instance.objectNode();
var fields = node.fields();
while (fields.hasNext()) {
var entry = fields.next();
String key = sanitizeKey(entry.getKey());
if (!key.equals(entry.getKey())) {
while (taken.contains(key)) {
key = "_" + key;
}
taken.add(key);
}
out.set(key, sanitize(entry.getValue()));
}
return out;
}
if (node.isArray()) {
ArrayNode out = JsonNodeFactory.instance.arrayNode(node.size());
node.forEach(element -> out.add(sanitize(element)));
return out;
}
return node;
}

/**
* Rewrite one key: illegal characters → {@code _}, and a leading character that is only legal
* mid-name (a digit, {@code -}, {@code .}) keeps its value behind a {@code _} prefix.
*/
static String sanitizeKey(String key) {
StringBuilder sb = new StringBuilder(key.length() + 1);
for (int i = 0; i < key.length(); i++) {
char c = key.charAt(i);
boolean legal = Character.isLetterOrDigit(c) || c == '-' || c == '.' || c == '_';
sb.append(legal ? c : '_');
}
if (sb.isEmpty() || !(Character.isLetter(sb.charAt(0)) || sb.charAt(0) == '_')) {
sb.insert(0, '_');
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
public final class SchemaControllerConstants {
// render endpoint constants
public static final String SCHEMAS_FOLDER_NAME = "schemas";
public static final String JSON_TRANSLATIONS_FILE_NAME_PATTERN = "translations*.json";
// Singular prefix so Metals' translation.json matches alongside every other family's
// translations.json (material-identity/schemas#331).
public static final String JSON_TRANSLATIONS_FILE_NAME_PATTERN = "translation*.json";
public static final String XSLT_FILE_NAME = "stylesheet.xsl";
public static final String DEFAULT_PDF_ATTACHMENT_CERT_FILE_NAME = "dmp.json";
public static final String DEFAULT_PDF_ATTACHMENT_CERT_FILE_EXTENSION = ".json";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.materialidentity.schemaservice;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.materialidentity.schemaservice.config.SchemaControllerConstants;

/**
* The render pipeline loads Metals' singular translation.json as Root/Translations
* (material-identity/schemas#331), with keys made legal XML names, and the test-mode watermark
* still resolves the same text from it.
*/
class TranslationLoaderTest {

private static final ObjectMapper MAPPER = new ObjectMapper();

private static String pattern(String family, String version) {
return Paths.get("schemas", family, version,
SchemaControllerConstants.JSON_TRANSLATIONS_FILE_NAME_PATTERN).toString();
}

@Test
void loadsMetalsSingularTranslationFile() throws Exception {
JsonNode labels = new TranslationLoader(pattern("Metals", "v0.1.1"), new String[] { "EN" })
.load().get("DigitalMaterialPassport");
assertEquals("Cast Number", labels.get("CastNumber").asText());
}

@Test
void englishOnlyCertificateGetsEnglishLabelsOnly() throws Exception {
JsonNode labels = new TranslationLoader(pattern("Metals", "v0.1.1"), new String[] { "EN" })
.load().get("DigitalMaterialPassport");
assertEquals("Heat Number", labels.get("HeatNumber").asText());
assertFalse(labels.get("HeatNumber").asText().contains(" / "));
}

@Test
void bilingualCertificateJoinsLabelsInLanguageOrder() throws Exception {
JsonNode labels = new TranslationLoader(pattern("Metals", "v0.1.1"), new String[] { "EN", "DE" })
.load().get("DigitalMaterialPassport");
assertEquals("Cast Number / Gussnummer", labels.get("CastNumber").asText());
}

@Test
void enumValueKeysBecomeLegalXmlNames() throws Exception {
JsonNode labels = new TranslationLoader(pattern("Metals", "v0.1.1"), new String[] { "EN" })
.load().get("DigitalMaterialPassport");
assertEquals("1/4 Thickness", labels.get("_1_4T").asText());
assertEquals("Quenching and Tempering", labels.get("Quenching_and_Tempering").asText());
assertFalse(labels.has("1/4T"));
}

@Test
void sanitizingLeavesOtherFamiliesUnchanged() throws Exception {
for (String[] familyVersion : new String[][] {
{ "CoA", "v1.1.0" }, { "EN10168", "v0.5.0" }, { "ForestrySource", "v1.1.0" } }) {
JsonNode loaded = new TranslationLoader(pattern(familyVersion[0], familyVersion[1]),
new String[] { "EN", "DE" }).load();
assertTrue(loaded.size() > 0, familyVersion[0] + " translations should load");
assertEquals(loaded, XmlKeySanitizer.sanitize(loaded),
familyVersion[0] + " keys must already be legal XML names");
}
}

@Test
void metalsWatermarkTextIsUnchanged() {
String metals = pattern("Metals", "v0.1.1");
assertEquals("PREVIEW — NOT A VALID CERTIFICATE", WatermarkManager.resolveText(metals, "EN"));
assertEquals("VORSCHAU — KEIN GÜLTIGES ZERTIFIKAT", WatermarkManager.resolveText(metals, "DE"));
}

@Test
void sanitizerRewritesOnlyIllegalKeys() throws Exception {
assertEquals("CastNumber", XmlKeySanitizer.sanitizeKey("CastNumber"));
assertEquals("_1_4T", XmlKeySanitizer.sanitizeKey("1/4T"));
assertEquals("L-T", XmlKeySanitizer.sanitizeKey("L-T"));
assertEquals("a_b", XmlKeySanitizer.sanitizeKey("a b"));
assertEquals("ns_name", XmlKeySanitizer.sanitizeKey("ns:name"));
assertEquals("_", XmlKeySanitizer.sanitizeKey(""));

JsonNode out = XmlKeySanitizer.sanitize(MAPPER.readTree("""
{ "a b": "x", "a_b": "y", "list": [ { "1st": 1 } ], "keep": "v a l / u e" }
"""));
assertEquals(4, out.size());
// "a_b" is already legal and keeps its name and value; the rewritten "a b" moves aside.
assertEquals("y", out.get("a_b").asText());
assertEquals("x", out.get("_a_b").asText());
assertEquals(1, out.at("/list/0/_1st").asInt());
assertEquals("v a l / u e", out.get("keep").asText());
}
}
Binary file modified test/fixtures/Metals/v0.1.1/valid_address_fields.pdf
Binary file not shown.
Binary file modified test/fixtures/Metals/v0.1.1/valid_backward_compatibility.pdf
Binary file not shown.
Binary file modified test/fixtures/Metals/v0.1.1/valid_basic_certificate.pdf
Binary file not shown.
Loading
Loading