-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab79bf3
commit 3060672
Showing
19 changed files
with
6,949 additions
and
7,312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/org/cip4/xjdf/json/openapi/SchemaReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
Oops, something went wrong.