Search in sources :

Example 1 with XMLSchemaLoader

use of org.apache.xerces.impl.xs.XMLSchemaLoader in project iaf by ibissource.

the class TestDomTreeAligner method testFiles.

@Override
public void testFiles(String schemaFile, String namespace, String rootElement, String inputFile, boolean potentialCompactionProblems, String expectedFailureReason) throws Exception {
    URL schemaUrl = getSchemaURL(schemaFile);
    String xsdUri = schemaUrl.toExternalForm();
    Schema schema = Utils.getSchemaFromResource(schemaUrl);
    XMLSchemaLoader xsLoader = new XMLSchemaLoader();
    XSModel xsModel = xsLoader.loadURI(xsdUri);
    List<XSModel> schemaInformation = new LinkedList<XSModel>();
    schemaInformation.add(xsModel);
    String xmlString = getTestFile(inputFile + ".xml");
    boolean expectValid = expectedFailureReason == null;
    assertEquals("valid XML", expectValid, Utils.validate(schemaUrl, xmlString));
    ValidatorHandler validator = schema.newValidatorHandler();
    DomTreeAligner aligner = new DomTreeAligner(validator, schemaInformation);
    // Xml2Json xml2json = new Xml2Json(aligner, false);
    validator.setContentHandler(aligner);
    // aligner.setContentHandler(xml2json);
    Document domIn = Utils.string2Dom(xmlString);
    System.out.println("input DOM [" + Utils.dom2String1(domIn) + "]");
    // System.out.println("xmlString "+xmlString);
    // System.out.println("dom in "+ToStringBuilder.reflectionToString(domIn.getDocumentElement()));
    Source source = aligner.asSource(domIn);
    System.out.println();
    System.out.println("start aligning " + inputFile);
    String xmlAct = Utils.source2String(source);
    System.out.println("xml aligned via source=" + xmlAct);
    assertNotNull("xmlAct is null", xmlAct);
    assertTrue("XML is not aligned", Utils.validate(schemaUrl, xmlAct));
// InputSource is = new InputSource(new StringReader(xmlString));
// try {
// //       	String jsonOut=xml2json.toString();
// System.out.println("jsonOut="+jsonOut);
// if (!expectValid) {
// fail("expected to fail");
// }
// } catch (Exception e) {
// if (expectValid) {
// e.printStackTrace();
// fail(e.getMessage());
// }
// }
// String xmlString=getTestXml(xml);
// String xsdUri=Utils.class.getResource(xsd).toExternalForm();
// 
// assertEquals("valid XML", expectValid, Utils.validate(namespace, xsdUri, xmlString));
// 
// Document dom = Utils.string2Dom(xmlString);
// Utils.clean(dom);
// 
// XMLReader parser = new SAXParser();
// Schema schema=Utils.getSchemaFromResource(namespace, xsdUri);
// ValidatorHandler validator = schema.newValidatorHandler();
// XmlAligner instance = implementation.newInstance();
// instance.setPsviProvider((PSVIProvider)validator);
// 
// parser.setContentHandler(validator);
// validator.setContentHandler(instance);
// 
// System.out.println();
// System.out.println("start aligning "+xml);
// instance.parse(dom.getDocumentElement());
// System.out.println("alignedXml="+Utils.dom2String1(dom));
// assertTrue("valid aligned XML", Utils.validate(namespace, xsdUri, dom)); // only if dom  itself is modified...
// testXmlAligner(instance, dom.getDocumentElement(), namespace, xsdUri);
// JSONObject json=Utils.xml2Json(xmlString);
// System.out.println("JSON:"+json);
// String jsonString = json.toString();
// 
// jsonString=jsonString.replaceAll(",\"xmlns\":\"[^\"]*\"", "");
// System.out.println("JSON with fixed xmlns:"+jsonString);
// 
// String xmlFromJson = Utils.json2Xml(Utils.string2Json(jsonString));
// if (StringUtils.isNotEmpty(namespace)) {
// xmlFromJson=xmlFromJson.replaceFirst(">", " xmlns=\""+namespace+"\">");
// }
// System.out.println("start aligning xml from json "+xmlFromJson);
// Document domFromJson = Utils.string2Dom(xmlFromJson);
// instance.startParse(domFromJson.getDocumentElement());
}
Also used : ValidatorHandler(javax.xml.validation.ValidatorHandler) XMLSchemaLoader(org.apache.xerces.impl.xs.XMLSchemaLoader) Schema(javax.xml.validation.Schema) XSModel(org.apache.xerces.xs.XSModel) Document(org.w3c.dom.Document) URL(java.net.URL) LinkedList(java.util.LinkedList) Source(javax.xml.transform.Source)

Example 2 with XMLSchemaLoader

use of org.apache.xerces.impl.xs.XMLSchemaLoader in project iaf by ibissource.

the class DomTreeAligner method translate.

public static String translate(Document xmlIn, URL schemaURL) throws SAXException, IOException {
    // create the ValidatorHandler
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(schemaURL);
    ValidatorHandler validatorHandler = schema.newValidatorHandler();
    // create the XSModel
    XMLSchemaLoader xsLoader = new XMLSchemaLoader();
    XSModel xsModel = xsLoader.loadURI(schemaURL.toExternalForm());
    List<XSModel> schemaInformation = new LinkedList<XSModel>();
    schemaInformation.add(xsModel);
    // create the validator, setup the chain
    DomTreeAligner dta = new DomTreeAligner(validatorHandler, schemaInformation);
    Source source = dta.asSource(xmlIn);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    String xml = null;
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(source, result);
        writer.flush();
        xml = writer.toString();
    } catch (TransformerConfigurationException e) {
        SAXException se = new SAXException(e);
        se.initCause(e);
        throw se;
    } catch (TransformerException e) {
        SAXException se = new SAXException(e);
        se.initCause(e);
        throw se;
    }
    return xml;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) ValidatorHandler(javax.xml.validation.ValidatorHandler) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Schema(javax.xml.validation.Schema) LinkedList(java.util.LinkedList) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) XMLSchemaLoader(org.apache.xerces.impl.xs.XMLSchemaLoader) XSModel(org.apache.xerces.xs.XSModel) TransformerException(javax.xml.transform.TransformerException)

Example 3 with XMLSchemaLoader

use of org.apache.xerces.impl.xs.XMLSchemaLoader in project iaf by ibissource.

the class Json2Xml method translate.

public static String translate(JsonStructure json, URL schemaURL, boolean compactJsonArrays, String rootElement, boolean strictSyntax, boolean deepSearch, String targetNamespace, Map<String, Object> overrideValues) throws SAXException, IOException {
    // create the ValidatorHandler
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(schemaURL);
    ValidatorHandler validatorHandler = schema.newValidatorHandler();
    // create the XSModel
    XMLSchemaLoader xsLoader = new XMLSchemaLoader();
    XSModel xsModel = xsLoader.loadURI(schemaURL.toExternalForm());
    List<XSModel> schemaInformation = new LinkedList<XSModel>();
    schemaInformation.add(xsModel);
    // create the validator, setup the chain
    Json2Xml j2x = new Json2Xml(validatorHandler, schemaInformation, compactJsonArrays, rootElement, strictSyntax);
    if (overrideValues != null) {
        j2x.setOverrideValues(overrideValues);
    }
    if (targetNamespace != null) {
        // if (DEBUG) System.out.println("setting targetNamespace ["+targetNamespace+"]");
        j2x.setTargetNamespace(targetNamespace);
    }
    j2x.setDeepSearch(deepSearch);
    Source source = j2x.asSource(json);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    String xml = null;
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(source, result);
        writer.flush();
        xml = writer.toString();
    } catch (TransformerConfigurationException e) {
        SAXException se = new SAXException(e);
        se.initCause(e);
        throw se;
    } catch (TransformerException e) {
        SAXException se = new SAXException(e);
        se.initCause(e);
        throw se;
    }
    return xml;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) ValidatorHandler(javax.xml.validation.ValidatorHandler) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Schema(javax.xml.validation.Schema) JsonString(javax.json.JsonString) LinkedList(java.util.LinkedList) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) XMLSchemaLoader(org.apache.xerces.impl.xs.XMLSchemaLoader) XSModel(org.apache.xerces.xs.XSModel) TransformerException(javax.xml.transform.TransformerException)

Example 4 with XMLSchemaLoader

use of org.apache.xerces.impl.xs.XMLSchemaLoader in project iaf by ibissource.

the class JavaxValidationContext method getXSModels.

protected List<XSModel> getXSModels(List<nl.nn.adapterframework.validation.Schema> schemas) throws IOException, XMLStreamException, ConfigurationException {
    List<XSModel> result = new ArrayList<XSModel>();
    XMLSchemaLoader xsLoader = new XMLSchemaLoader();
    for (nl.nn.adapterframework.validation.Schema schema : schemas) {
        XSModel xsModel = xsLoader.loadURI(schema.getSystemId());
        result.add(xsModel);
    }
    return result;
}
Also used : XMLSchemaLoader(org.apache.xerces.impl.xs.XMLSchemaLoader) ArrayList(java.util.ArrayList) XSModel(org.apache.xerces.xs.XSModel)

Example 5 with XMLSchemaLoader

use of org.apache.xerces.impl.xs.XMLSchemaLoader in project iaf by ibissource.

the class Properties2Xml method translate.

public static String translate(Map<String, String> data, URL schemaURL, String rootElement, String targetNamespace) throws SAXException, IOException {
    // create the ValidatorHandler
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(schemaURL);
    ValidatorHandler validatorHandler = schema.newValidatorHandler();
    // create the XSModel
    XMLSchemaLoader xsLoader = new XMLSchemaLoader();
    XSModel xsModel = xsLoader.loadURI(schemaURL.toExternalForm());
    List<XSModel> schemaInformation = new LinkedList<XSModel>();
    schemaInformation.add(xsModel);
    // create the validator, setup the chain
    Properties2Xml p2x = new Properties2Xml(validatorHandler, schemaInformation, rootElement);
    if (targetNamespace != null) {
        p2x.setTargetNamespace(targetNamespace);
    }
    Source source = p2x.asSource(data);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    String xml = null;
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(source, result);
        writer.flush();
        xml = writer.toString();
    } catch (TransformerConfigurationException e) {
        SAXException se = new SAXException(e);
        se.initCause(e);
        throw se;
    } catch (TransformerException e) {
        SAXException se = new SAXException(e);
        se.initCause(e);
        throw se;
    }
    return xml;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) ValidatorHandler(javax.xml.validation.ValidatorHandler) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Schema(javax.xml.validation.Schema) LinkedList(java.util.LinkedList) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) XMLSchemaLoader(org.apache.xerces.impl.xs.XMLSchemaLoader) XSModel(org.apache.xerces.xs.XSModel) TransformerException(javax.xml.transform.TransformerException)

Aggregations

XMLSchemaLoader (org.apache.xerces.impl.xs.XMLSchemaLoader)5 XSModel (org.apache.xerces.xs.XSModel)5 LinkedList (java.util.LinkedList)4 Source (javax.xml.transform.Source)4 Schema (javax.xml.validation.Schema)4 ValidatorHandler (javax.xml.validation.ValidatorHandler)4 StringWriter (java.io.StringWriter)3 Transformer (javax.xml.transform.Transformer)3 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)3 TransformerException (javax.xml.transform.TransformerException)3 TransformerFactory (javax.xml.transform.TransformerFactory)3 StreamResult (javax.xml.transform.stream.StreamResult)3 SchemaFactory (javax.xml.validation.SchemaFactory)3 SAXException (org.xml.sax.SAXException)3 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 JsonString (javax.json.JsonString)1 Document (org.w3c.dom.Document)1