Search in sources :

Example 6 with XmlNamespace

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

the class XmlSchemaInspectionMultipleNamespacesTest method testMultipleNoNamespaceSchemas.

@Test
public void testMultipleNoNamespaceSchemas() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/multiple-no-namespace-schemas.xml").toFile();
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument answer = service.inspectSchema(schemaFile);
    assertEquals(1, answer.getXmlNamespaces().getXmlNamespace().size());
    XmlNamespace namespace = answer.getXmlNamespaces().getXmlNamespace().get(0);
    assertEquals("tns", namespace.getAlias());
    assertEquals("io.atlasmap.xml.test:Root", namespace.getUri());
    assertEquals(null, namespace.isTargetNamespace());
    assertEquals(1, answer.getFields().getField().size());
    XmlComplexType complex = XmlComplexType.class.cast(answer.getFields().getField().get(0));
    assertEquals("tns:RootDocument", complex.getName());
    assertEquals(2, complex.getXmlFields().getXmlField().size());
    for (XmlField field : complex.getXmlFields().getXmlField()) {
        switch(field.getName()) {
            case "FirstElement":
                assertEquals(FieldType.COMPLEX, field.getFieldType());
                assertEquals("/tns:RootDocument/FirstElement", field.getPath());
                List<XmlField> firstFields = XmlComplexType.class.cast(field).getXmlFields().getXmlField();
                assertEquals(1, firstFields.size());
                XmlField firstField = firstFields.get(0);
                assertEquals("FirstValue", firstField.getName());
                assertEquals(FieldType.STRING, firstField.getFieldType());
                assertEquals("/tns:RootDocument/FirstElement/FirstValue", firstField.getPath());
                break;
            case "SecondElement":
                assertEquals(FieldType.COMPLEX, field.getFieldType());
                assertEquals("/tns:RootDocument/SecondElement", field.getPath());
                List<XmlField> secondFields = XmlComplexType.class.cast(field).getXmlFields().getXmlField();
                assertEquals(1, secondFields.size());
                XmlField secondField = secondFields.get(0);
                assertEquals("SecondValue", secondField.getName());
                assertEquals(FieldType.STRING, secondField.getFieldType());
                assertEquals("/tns:RootDocument/SecondElement/SecondValue", secondField.getPath());
                break;
            default:
                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.jupiter.api.Test)

Example 7 with XmlNamespace

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

the class XmlSchemaInspectionTest method testInspectSchemaFileWithNamespace.

@Test
public void testInspectSchemaFileWithNamespace() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/simple-namespace-schema.xsd").toFile();
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument xmlDocument = service.inspectSchema(schemaFile);
    assertNotNull(xmlDocument);
    assertNotNull(xmlDocument.getFields());
    assertEquals(1, xmlDocument.getFields().getField().size());
    assertNotNull(xmlDocument.getXmlNamespaces());
    assertEquals(1, xmlDocument.getXmlNamespaces().getXmlNamespace().size());
    XmlNamespace namespace = xmlDocument.getXmlNamespaces().getXmlNamespace().get(0);
    assertEquals("tns", namespace.getAlias());
    assertEquals("http://example.com/", namespace.getUri());
// debugFields(xmlDocument.getFields());
}
Also used : XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlDocument(io.atlasmap.xml.v2.XmlDocument) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 8 with XmlNamespace

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

the class XmlSchemaInspector method populateNamespaces.

private void populateNamespaces() {
    for (Entry<String, String> entry : namespaceContext.getNamespaceMap().entrySet()) {
        String prefix = entry.getKey();
        String uri = entry.getValue();
        if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) || AtlasXmlConstants.ATLAS_XML_SCHEMASET_NAMESPACE.equals(uri)) {
            continue;
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("adding a namespace >>> prefix={}, uri={}", prefix, uri);
        }
        if (xmlDocument.getXmlNamespaces() == null) {
            xmlDocument.setXmlNamespaces(new XmlNamespaces());
        }
        XmlNamespace namespace = new XmlNamespace();
        namespace.setAlias(prefix);
        namespace.setUri(uri);
        xmlDocument.getXmlNamespaces().getXmlNamespace().add(namespace);
    }
}
Also used : XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlNamespaces(io.atlasmap.xml.v2.XmlNamespaces)

Example 9 with XmlNamespace

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

the class XmlModule method processPreTargetExecution.

@Override
public void processPreTargetExecution(AtlasInternalSession session) throws AtlasException {
    XmlNamespaces xmlNs = null;
    String template = null;
    DataSource ds = getDataSource();
    if (ds instanceof XmlDataSource) {
        xmlNs = ((XmlDataSource) ds).getXmlNamespaces();
        template = ((XmlDataSource) ds).getTemplate();
    }
    Map<String, String> nsMap = new HashMap<String, String>();
    if (xmlNs != null && xmlNs.getXmlNamespace() != null && !xmlNs.getXmlNamespace().isEmpty()) {
        for (XmlNamespace ns : xmlNs.getXmlNamespace()) {
            nsMap.put(ns.getAlias(), ns.getUri());
        }
    }
    XmlFieldWriter writer = new XmlFieldWriter(getClassLoader(), nsMap, template);
    session.setFieldWriter(getDocId(), writer);
    if (LOG.isDebugEnabled()) {
        LOG.debug("{}: processPreTargetExcution completed", getDocId());
    }
}
Also used : HashMap(java.util.HashMap) XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlFieldWriter(io.atlasmap.xml.core.XmlFieldWriter) XmlNamespaces(io.atlasmap.xml.v2.XmlNamespaces) DataSource(io.atlasmap.v2.DataSource) XmlDataSource(io.atlasmap.xml.v2.XmlDataSource) XmlDataSource(io.atlasmap.xml.v2.XmlDataSource)

Example 10 with XmlNamespace

use of io.atlasmap.xml.v2.XmlNamespace 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);
    assertEquals(1, answer.getXmlNamespaces().getXmlNamespace().size());
    XmlNamespace namespace = answer.getXmlNamespaces().getXmlNamespace().get(0);
    assertEquals("tns", namespace.getAlias());
    assertEquals("http://syndesis.io/v1/swagger-connector-template/request", namespace.getUri());
    assertEquals(null, namespace.isTargetNamespace());
    List<Field> fields = answer.getFields().getField();
    assertEquals(1, fields.size());
    XmlComplexType complex = XmlComplexType.class.cast(fields.get(0));
    assertEquals("tns:request", complex.getName());
    List<XmlField> rootFields = complex.getXmlFields().getXmlField();
    assertEquals(1, rootFields.size());
    complex = XmlComplexType.class.cast(rootFields.get(0));
    assertEquals("tns:body", complex.getName());
    List<XmlField> bodyFields = complex.getXmlFields().getXmlField();
    assertEquals(1, bodyFields.size());
    complex = XmlComplexType.class.cast(bodyFields.get(0));
    assertEquals("Pet", complex.getName());
    List<XmlField> petFields = complex.getXmlFields().getXmlField();
    assertEquals(6, petFields.size());
    for (XmlField xmlField : petFields) {
        switch(xmlField.getName()) {
            case "id":
                assertEquals(FieldType.DECIMAL, xmlField.getFieldType());
                assertEquals("/tns:request/tns:body/Pet/id", xmlField.getPath());
                break;
            case "Category":
                assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                assertEquals("/tns:request/tns:body/Pet/Category", xmlField.getPath());
                List<XmlField> categoryFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                assertEquals(2, categoryFields.size());
                for (XmlField categoryField : categoryFields) {
                    switch(categoryField.getName()) {
                        case "id":
                            assertEquals(FieldType.DECIMAL, categoryField.getFieldType());
                            assertEquals("/tns:request/tns:body/Pet/Category/id", categoryField.getPath());
                            break;
                        case "name":
                            assertEquals(FieldType.STRING, categoryField.getFieldType());
                            assertEquals("/tns:request/tns:body/Pet/Category/name", categoryField.getPath());
                            break;
                        default:
                            fail(String.format("Unknown field '%s'", categoryField.getPath()));
                    }
                }
                break;
            case "name":
                assertEquals(FieldType.STRING, xmlField.getFieldType());
                assertEquals("/tns:request/tns:body/Pet/name", xmlField.getPath());
                break;
            case "photoUrl":
                assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                assertEquals("/tns:request/tns:body/Pet/photoUrl", xmlField.getPath());
                List<XmlField> photoUrlFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                assertEquals(1, photoUrlFields.size());
                XmlField photoUrlField = photoUrlFields.get(0);
                assertEquals(FieldType.STRING, photoUrlField.getFieldType());
                assertEquals("/tns:request/tns:body/Pet/photoUrl/photoUrl<>", photoUrlField.getPath());
                assertEquals(CollectionType.LIST, photoUrlField.getCollectionType());
                break;
            case "tag":
                assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                assertEquals("/tns:request/tns:body/Pet/tag", xmlField.getPath());
                List<XmlField> tagFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                assertEquals(1, tagFields.size());
                XmlField tagField = tagFields.get(0);
                assertEquals(FieldType.COMPLEX, tagField.getFieldType());
                assertEquals("/tns:request/tns:body/Pet/tag/Tag<>", tagField.getPath());
                assertEquals(CollectionType.LIST, tagField.getCollectionType());
                List<XmlField> tagTagFields = XmlComplexType.class.cast(tagField).getXmlFields().getXmlField();
                assertEquals(2, tagTagFields.size());
                for (XmlField tagTagField : tagTagFields) {
                    switch(tagTagField.getName()) {
                        case "id":
                            assertEquals(FieldType.DECIMAL, tagTagField.getFieldType());
                            assertEquals("/tns:request/tns:body/Pet/tag/Tag<>/id", tagTagField.getPath());
                            break;
                        case "name":
                            assertEquals(FieldType.STRING, tagTagField.getFieldType());
                            assertEquals("/tns:request/tns:body/Pet/tag/Tag<>/name", tagTagField.getPath());
                            break;
                        default:
                            fail(String.format("Unknown field '%s'", tagTagField.getPath()));
                    }
                }
                break;
            case "status":
                assertEquals(FieldType.STRING, xmlField.getFieldType());
                assertEquals("/tns:request/tns:body/Pet/status", xmlField.getPath());
                break;
            default:
                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.jupiter.api.Test)

Aggregations

XmlNamespace (io.atlasmap.xml.v2.XmlNamespace)15 XmlDocument (io.atlasmap.xml.v2.XmlDocument)9 Test (org.junit.jupiter.api.Test)9 XmlComplexType (io.atlasmap.xml.v2.XmlComplexType)8 XmlNamespaces (io.atlasmap.xml.v2.XmlNamespaces)6 File (java.io.File)6 XmlField (io.atlasmap.xml.v2.XmlField)5 DataSource (io.atlasmap.v2.DataSource)2 Field (io.atlasmap.v2.Field)2 XmlDataSource (io.atlasmap.xml.v2.XmlDataSource)2 AtlasMapping (io.atlasmap.v2.AtlasMapping)1 XmlFieldWriter (io.atlasmap.xml.core.XmlFieldWriter)1 Restriction (io.atlasmap.xml.v2.Restriction)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1