Search in sources :

Example 21 with XmlDocument

use of io.atlasmap.xml.v2.XmlDocument in project atlasmap by atlasmap.

the class XmlInstanceInspectionTest method testInspectXmlStringAsSourceAttrsWithNamespace.

@Test
public void testInspectXmlStringAsSourceAttrsWithNamespace() throws Exception {
    final String source = "<data xmlns:y=\"http://y.namespace.com/\" y:intField='32000' longField='12421' stringField='abc' " + "booleanField='true' doubleField='12.0' shortField='1000' floatField='234.5f' y:charField='A' />";
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument xmlDocument = service.inspectXmlDocument(source);
    Assert.assertNotNull(xmlDocument);
    Assert.assertNotNull(xmlDocument.getFields());
    // check for namespace
    Assert.assertNotNull(xmlDocument.getXmlNamespaces());
    Assert.assertThat(xmlDocument.getXmlNamespaces().getXmlNamespace().size(), Is.is(1));
    XmlNamespace namespace = xmlDocument.getXmlNamespaces().getXmlNamespace().get(0);
    Assert.assertNotNull(namespace);
    Assert.assertEquals("y", namespace.getAlias());
    Assert.assertEquals("http://y.namespace.com/", namespace.getUri());
    Assert.assertNotNull(xmlDocument.getFields());
    Assert.assertThat(xmlDocument.getFields().getField().size(), Is.is(1));
    XmlComplexType root = (XmlComplexType) xmlDocument.getFields().getField().get(0);
    Assert.assertNotNull(root);
    Assert.assertThat(root.getXmlFields().getXmlField().size(), Is.is(8));
// debugFields(xmlDocument.getFields());
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlDocument(io.atlasmap.xml.v2.XmlDocument) Test(org.junit.Test)

Example 22 with XmlDocument

use of io.atlasmap.xml.v2.XmlDocument in project atlasmap by atlasmap.

the class XmlInstanceInspectionTest method testInspectXmlStringWithDefaultNamespace.

@Test
public void testInspectXmlStringWithDefaultNamespace() throws Exception {
    final String source = "<data xmlns=\"http://x.namespace.com/\">\n" + "     <intField>32000</intField>\n" + "     <longField>12421</longField>\n" + "     <stringField>abc</stringField>\n" + "     <booleanField>true</booleanField>\n" + "     <doubleField>12.0</doubleField>\n" + "     <shortField>1000</shortField>\n" + "     <floatField>234.5f</floatField>\n" + "     <charField>A</charField>\n" + "</data>";
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument xmlDocument = service.inspectXmlDocument(source);
    Assert.assertNotNull(xmlDocument);
    // check for namespace
    Assert.assertNotNull(xmlDocument.getXmlNamespaces());
    XmlNamespace namespace = xmlDocument.getXmlNamespaces().getXmlNamespace().get(0);
    Assert.assertThat(xmlDocument.getXmlNamespaces().getXmlNamespace().size(), Is.is(1));
    Assert.assertNotNull(namespace);
    Assert.assertNull(namespace.getAlias());
    Assert.assertEquals("http://x.namespace.com/", namespace.getUri());
    Assert.assertNotNull(xmlDocument.getFields());
    Assert.assertThat(xmlDocument.getFields().getField().size(), Is.is(1));
    XmlComplexType root = (XmlComplexType) xmlDocument.getFields().getField().get(0);
    Assert.assertNotNull(root);
    Assert.assertThat(root.getXmlFields().getXmlField().size(), Is.is(8));
// debugFields(xmlDocument.getFields());
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlDocument(io.atlasmap.xml.v2.XmlDocument) Test(org.junit.Test)

Example 23 with XmlDocument

use of io.atlasmap.xml.v2.XmlDocument in project atlasmap by atlasmap.

the class XmlSchemaInspectionMultipleNamespacesTest method testMultipleNoNamespaceRootSchema.

@Test
public void testMultipleNoNamespaceRootSchema() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/multiple-no-namespace-root-schema.xml").toFile();
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument answer = service.inspectSchema(schemaFile);
    Assert.assertEquals(1, answer.getXmlNamespaces().getXmlNamespace().size());
    XmlNamespace namespace = answer.getXmlNamespaces().getXmlNamespace().get(0);
    Assert.assertEquals("second", namespace.getAlias());
    Assert.assertEquals("io.atlasmap.xml.test:Second", namespace.getUri());
    Assert.assertEquals(null, namespace.isTargetNamespace());
    // Note that the FirstElement also appears here as it's a top level element of namespace ""
    Assert.assertEquals(2, answer.getFields().getField().size());
    XmlComplexType complex = XmlComplexType.class.cast(answer.getFields().getField().get(1));
    Assert.assertEquals("RootDocument", complex.getName());
    Assert.assertEquals(2, complex.getXmlFields().getXmlField().size());
    for (XmlField field : complex.getXmlFields().getXmlField()) {
        switch(field.getName()) {
            case "FirstElement":
                Assert.assertEquals(FieldType.COMPLEX, field.getFieldType());
                Assert.assertEquals("/RootDocument/FirstElement", field.getPath());
                List<XmlField> firstFields = XmlComplexType.class.cast(field).getXmlFields().getXmlField();
                Assert.assertEquals(1, firstFields.size());
                XmlField firstField = firstFields.get(0);
                Assert.assertEquals("FirstValue", firstField.getName());
                Assert.assertEquals(FieldType.STRING, firstField.getFieldType());
                Assert.assertEquals("/RootDocument/FirstElement/FirstValue", firstField.getPath());
                break;
            case "second:SecondElement":
                Assert.assertEquals(FieldType.COMPLEX, field.getFieldType());
                Assert.assertEquals("/RootDocument/second:SecondElement", field.getPath());
                List<XmlField> secondFields = XmlComplexType.class.cast(field).getXmlFields().getXmlField();
                Assert.assertEquals(1, secondFields.size());
                XmlField secondField = secondFields.get(0);
                Assert.assertEquals("second:SecondValue", secondField.getName());
                Assert.assertEquals(FieldType.STRING, secondField.getFieldType());
                Assert.assertEquals("/RootDocument/second:SecondElement/second:SecondValue", secondField.getPath());
                break;
            default:
                Assert.fail(String.format("Unknown field '%s'", field.getPath()));
        }
    }
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlField(io.atlasmap.xml.v2.XmlField) XmlDocument(io.atlasmap.xml.v2.XmlDocument) File(java.io.File) Test(org.junit.Test)

Example 24 with XmlDocument

use of io.atlasmap.xml.v2.XmlDocument in project atlasmap by atlasmap.

the class XmlSchemaInspectionMultipleNamespacesTest method testSyndesis.

@Test
public void testSyndesis() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/multiple-namespaces-syndesis.xml").toFile();
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument answer = service.inspectSchema(schemaFile);
    Assert.assertEquals(1, answer.getXmlNamespaces().getXmlNamespace().size());
    XmlNamespace namespace = answer.getXmlNamespaces().getXmlNamespace().get(0);
    Assert.assertEquals("tns", namespace.getAlias());
    Assert.assertEquals("http://syndesis.io/v1/swagger-connector-template/request", namespace.getUri());
    Assert.assertEquals(null, namespace.isTargetNamespace());
    List<Field> fields = answer.getFields().getField();
    Assert.assertEquals(1, fields.size());
    XmlComplexType complex = XmlComplexType.class.cast(fields.get(0));
    Assert.assertEquals("tns:request", complex.getName());
    List<XmlField> rootFields = complex.getXmlFields().getXmlField();
    Assert.assertEquals(1, rootFields.size());
    complex = XmlComplexType.class.cast(rootFields.get(0));
    Assert.assertEquals("tns:body", complex.getName());
    List<XmlField> bodyFields = complex.getXmlFields().getXmlField();
    Assert.assertEquals(1, bodyFields.size());
    complex = XmlComplexType.class.cast(bodyFields.get(0));
    Assert.assertEquals("Pet", complex.getName());
    List<XmlField> petFields = complex.getXmlFields().getXmlField();
    Assert.assertEquals(6, petFields.size());
    for (XmlField xmlField : petFields) {
        switch(xmlField.getName()) {
            case "id":
                Assert.assertEquals(FieldType.DECIMAL, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/id", xmlField.getPath());
                break;
            case "Category":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/Category", xmlField.getPath());
                List<XmlField> categoryFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(2, categoryFields.size());
                for (XmlField categoryField : categoryFields) {
                    switch(categoryField.getName()) {
                        case "id":
                            Assert.assertEquals(FieldType.DECIMAL, categoryField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/Category/id", categoryField.getPath());
                            break;
                        case "name":
                            Assert.assertEquals(FieldType.STRING, categoryField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/Category/name", categoryField.getPath());
                            break;
                        default:
                            Assert.fail(String.format("Unknown field '%s'", categoryField.getPath()));
                    }
                }
                break;
            case "name":
                Assert.assertEquals(FieldType.STRING, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/name", xmlField.getPath());
                break;
            case "photoUrl":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/photoUrl", xmlField.getPath());
                List<XmlField> photoUrlFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(1, photoUrlFields.size());
                XmlField photoUrlField = photoUrlFields.get(0);
                Assert.assertEquals(FieldType.STRING, photoUrlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/photoUrl/photoUrl", photoUrlField.getPath());
                break;
            case "tag":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/tag", xmlField.getPath());
                List<XmlField> tagFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(1, tagFields.size());
                XmlField tagField = tagFields.get(0);
                Assert.assertEquals(FieldType.COMPLEX, tagField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/tag/Tag", tagField.getPath());
                List<XmlField> tagTagFields = XmlComplexType.class.cast(tagField).getXmlFields().getXmlField();
                Assert.assertEquals(2, tagTagFields.size());
                for (XmlField tagTagField : tagTagFields) {
                    switch(tagTagField.getName()) {
                        case "id":
                            Assert.assertEquals(FieldType.DECIMAL, tagTagField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/tag/Tag/id", tagTagField.getPath());
                            break;
                        case "name":
                            Assert.assertEquals(FieldType.STRING, tagTagField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/tag/Tag/name", tagTagField.getPath());
                            break;
                        default:
                            Assert.fail(String.format("Unknown field '%s'", tagTagField.getPath()));
                    }
                }
                break;
            case "status":
                Assert.assertEquals(FieldType.STRING, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/status", xmlField.getPath());
                break;
            default:
                Assert.fail(String.format("Unknown field '%s'", xmlField.getPath()));
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlField(io.atlasmap.xml.v2.XmlField) XmlDocument(io.atlasmap.xml.v2.XmlDocument) File(java.io.File) Test(org.junit.Test)

Example 25 with XmlDocument

use of io.atlasmap.xml.v2.XmlDocument in project atlasmap by atlasmap.

the class SchemaInspector method doInspect.

private void doInspect(InputStream is) throws Exception {
    xmlDocument = AtlasXmlModelFactory.createXmlDocument();
    Fields fields = new Fields();
    xmlDocument.setFields(fields);
    namespaceContext = new AtlasXmlNamespaceContext();
    rootNamespace = null;
    XSOMParser parser = new XSOMParser(SAXParserFactory.newInstance());
    parser.setAnnotationParser(new DomAnnotationParserFactory());
    parser.setErrorHandler(new XSOMErrorHandler());
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = dbf.newDocumentBuilder().parse(is);
    Element root = doc.getDocumentElement();
    if (root == null) {
        throw new XmlInspectionException("XML schema document is empty");
    } else if ("SchemaSet".equals(root.getLocalName())) {
        XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(namespaceContext);
        NodeList subSchemas = (NodeList) xpath.evaluate(String.format("/%s:SchemaSet/%s:AdditionalSchemas/%s:schema", NS_PREFIX_SCHEMASET, NS_PREFIX_SCHEMASET, NS_PREFIX_XMLSCHEMA), doc, XPathConstants.NODESET);
        for (int i = 0; i < subSchemas.getLength(); i++) {
            Element e = (Element) subSchemas.item(i);
            inheritNamespaces(e, false);
            parser.parse(toInputStream(transformer, e));
        }
        Element rootSchema = (Element) xpath.evaluate(String.format("/%s:SchemaSet/%s:schema", NS_PREFIX_SCHEMASET, NS_PREFIX_XMLSCHEMA), doc, XPathConstants.NODE);
        if (rootSchema == null) {
            throw new XmlInspectionException("The root schema '/SchemaSet/schema' must be specified once and only once");
        }
        rootNamespace = getTargetNamespace(rootSchema);
        if (rootNamespace != null && !rootNamespace.isEmpty()) {
            namespaceContext.add("tns", rootNamespace);
        }
        inheritNamespaces(rootSchema, true);
        parser.parse(toInputStream(transformer, rootSchema));
    } else if ("schema".equals(root.getLocalName())) {
        parser.parse(toInputStream(transformer, root));
        rootNamespace = getTargetNamespace(root);
        if (rootNamespace != null && !rootNamespace.isEmpty()) {
            namespaceContext.add("tns", rootNamespace);
        }
    } else {
        throw new XmlInspectionException(String.format("Unsupported document element '%s': root element must be 'schema' or 'SchemaSet'", root.getLocalName()));
    }
    XSSchemaSet schemaSet = parser.getResult();
    printSchemaSet(schemaSet);
    populateNamespaces();
}
Also used : XPath(javax.xml.xpath.XPath) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) Transformer(javax.xml.transform.Transformer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XmlDocument(io.atlasmap.xml.v2.XmlDocument) XmlFields(io.atlasmap.xml.v2.XmlFields) Fields(io.atlasmap.v2.Fields) DomAnnotationParserFactory(com.sun.xml.xsom.util.DomAnnotationParserFactory)

Aggregations

XmlDocument (io.atlasmap.xml.v2.XmlDocument)25 Test (org.junit.Test)22 XmlComplexType (io.atlasmap.xml.v2.XmlComplexType)20 XmlNamespace (io.atlasmap.xml.v2.XmlNamespace)10 File (java.io.File)10 XmlField (io.atlasmap.xml.v2.XmlField)8 Document (org.w3c.dom.Document)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Field (io.atlasmap.v2.Field)2 XmlInspectionService (io.atlasmap.xml.inspect.XmlInspectionService)2 XmlInspectionResponse (io.atlasmap.xml.v2.XmlInspectionResponse)2 IOException (java.io.IOException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 XSSchemaSet (com.sun.xml.xsom.XSSchemaSet)1 XSOMParser (com.sun.xml.xsom.parser.XSOMParser)1 DomAnnotationParserFactory (com.sun.xml.xsom.util.DomAnnotationParserFactory)1 Fields (io.atlasmap.v2.Fields)1 InspectionType (io.atlasmap.xml.v2.InspectionType)1 Restriction (io.atlasmap.xml.v2.Restriction)1 XmlFields (io.atlasmap.xml.v2.XmlFields)1