Skip to content

Commit

Permalink
Update to latest XJDF schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Hartmann authored and MichelHartmann committed Dec 19, 2024
1 parent ab79bf3 commit 3060672
Show file tree
Hide file tree
Showing 19 changed files with 6,949 additions and 7,312 deletions.
7 changes: 3 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ dependencies {
implementation("com.charleskorn.kaml:kaml:0.53.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.4.2")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.7.0")
testImplementation("com.google.jimfs:jimfs:1.2")
swaggerUI("org.webjars:swagger-ui:3.10.0")
implementation(kotlin("stdlib-jdk8"))
implementation("org.cip4.lib.jdf:JDFLibJ-JSON:1.1.015")
implementation("org.cip4.lib.jdf:JDFLibJ:2.1.7.+")
implementation("org.cip4.lib.jdf:JDFLibJ-JSON:1.2.1")
implementation("org.cip4.lib.jdf:JDFLibJ:2.1.8.+")
implementation ("com.googlecode.json-simple:json-simple:1.1.1")
implementation("com.networknt:json-schema-validator:1.0.81")
implementation("com.networknt:json-schema-validator:1.5.4")
implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.15.2")
compileOnly("org.projectlombok:lombok:1.18.24")
Expand Down
15 changes: 1 addition & 14 deletions src/main/java/org/cip4/xjdf/json/openapi/ComplexType.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private void applyChoicePolymorphism(Schema schema, TypeTranslator nameTranslato
propertiesSchema.properties(new Schemas(
Collections.singletonMap(
"Name",
new Schema().constValue(getDiscriminator(reference))
new Schema().constValue(reference)
)
)
);
Expand All @@ -209,17 +209,4 @@ private void applyChoicePolymorphism(Schema schema, TypeTranslator nameTranslato
schema.items().discriminator(new Discriminator("Name"));
schema.items().required(Collections.singletonList("Name"));
}

private String getDiscriminator(String type) {
return switch (type) {
case "AuditCreated" -> "Created";
case "AuditNotification" -> "Notification";
case "AuditProcessRun" -> "ProcessRun";
case "AuditResource" -> "Resource";
case "AuditStatus" -> "Status";
case "Glue" -> "Glue";
case "Media" -> "Media";
default -> throw new RuntimeException("No known discriminator for '" + type + "'");
};
}
}
26 changes: 6 additions & 20 deletions src/main/java/org/cip4/xjdf/json/openapi/JsonSchemaConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
Expand All @@ -28,23 +24,13 @@ public class JsonSchemaConverter {
private final Document doc;
private final XPath xPath;

public JsonSchemaConverter(InputStream sourceXsd) {
this.doc = readXml(sourceXsd);
XPathFactory xpFactory = XPathFactory.newInstance();
this.xPath = xpFactory.newXPath();
this.xPath.setNamespaceContext(new XsdNamespaceContext());
}
public static final String XJDF_SCHEMA = "https://schema.cip4.org/jdfschema_2_2/xjdf.json";
public static final String XJMF_SCHEMA = "https://schema.cip4.org/jdfschema_2_2/xjmf.json";

private Document readXml(InputStream sourceXsd) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
InputSource xmlInput = new InputSource(sourceXsd);
return dBuilder.parse(xmlInput);
} catch (Exception e) {
throw new RuntimeException("Error reading XML input", e);
}
public JsonSchemaConverter(InputStream sourceXsd) {
SchemaReader reader = new SchemaReader();
this.doc = reader.readXml(sourceXsd);
this.xPath = reader.getXPath();
}

public TypeMap convert(OutputStream xjdfOutputStream, OutputStream xjmfOutputStream) {
Expand Down
25 changes: 4 additions & 21 deletions src/main/java/org/cip4/xjdf/json/openapi/OpenApiConverter.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package org.cip4.xjdf.json.openapi;

import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import org.cip4.xjdf.json.openapi.model.Info;
import org.cip4.xjdf.json.openapi.model.OpenApi;
import org.cip4.xjdf.json.openapi.model.Schemas;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
Expand All @@ -22,24 +17,12 @@ public class OpenApiConverter {

private final Document doc;
private final XPath xPath;
public static final String SCHEMA = "https://schema.cip4.org/jdfschema_2_2/xjmf.yaml";

public OpenApiConverter(InputStream sourceXsd) {
this.doc = readXml(sourceXsd);
XPathFactory xpFactory = XPathFactory.newInstance();
this.xPath = xpFactory.newXPath();
this.xPath.setNamespaceContext(new XsdNamespaceContext());
}

private Document readXml(InputStream sourceXsd) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
InputSource xmlInput = new InputSource(sourceXsd);
return dBuilder.parse(xmlInput);
} catch (Exception e) {
throw new RuntimeException("Error reading XML input", e);
}
SchemaReader reader = new SchemaReader();
this.doc = reader.readXml(sourceXsd);
this.xPath = reader.getXPath();
}

public void convert(TypeMap typeMap, OutputStream outputStream) {
Expand Down
100 changes: 100 additions & 0 deletions src/main/java/org/cip4/xjdf/json/openapi/SchemaReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.cip4.xjdf.json.openapi;

import lombok.SneakyThrows;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.InputStream;
import java.io.StringWriter;

public class SchemaReader {

private static XPath xPath;

public Document readXml(InputStream sourceXsd) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
InputSource xmlInput = new InputSource(sourceXsd);
return preprocess(dbFactory.newDocumentBuilder().parse(xmlInput));
} catch (Exception e) {
throw new RuntimeException("Error reading XML input", e);
}
}

public XPath getXPath() {
if (xPath == null) {
XPathFactory xpFactory = XPathFactory.newInstance();
xPath = xpFactory.newXPath();
xPath.setNamespaceContext(new XsdNamespaceContext());
}
return xPath;
}

@SneakyThrows
private Document preprocess(Document document) {
XPath xPath = getXPath();

// remove deprecated glue element (https://cip4.atlassian.net/browse/JDF-889)
Node glue = (Node) xPath.evaluate(
"//xs:complexType[@name=\"BoxFoldingParams\"]//xs:choice/xs:element[@ref=\"Glue\"]",
document,
XPathConstants.NODE
);
if (glue != null) {
glue.getParentNode().removeChild(glue);
}

// simplify trivial choice (https://cip4.atlassian.net/browse/JDF-889)
NodeList simpleChoices = (NodeList) xPath.evaluate(
"//xs:choice[count(xs:element | xs:any)=1]",
document,
XPathConstants.NODESET
);
for (int i = 0; i < simpleChoices.getLength(); i++) {
Node simpleChoice = simpleChoices.item(i);
Node onlyChoice = (Node) xPath.evaluate(
"xs:element | xs:any",
simpleChoice,
XPathConstants.NODE
);
if (simpleChoice.getAttributes() != null) {
NamedNodeMap attrs = simpleChoice.getAttributes();
if (attrs.getNamedItem("minOccurs") != null) {
Node attr = attrs.removeNamedItem("minOccurs");
document.importNode(attr, true);
onlyChoice.getAttributes().setNamedItem(attr);
}
if (attrs.getNamedItem("maxOccurs") != null) {
Node attr = attrs.removeNamedItem("maxOccurs");
document.importNode(attr, true);
onlyChoice.getAttributes().setNamedItem(attr);
}
}

simpleChoice.getParentNode().replaceChild(onlyChoice, simpleChoice);
}

return document;
}

@SneakyThrows
public String getStringFromDocument(Document doc) {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}

}
Loading

0 comments on commit 3060672

Please sign in to comment.