Search in sources :

Example 41 with XmlField

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

the class XmlFieldReaderTest method testReadDocumentWithMultipleNamespaceComplex.

@Test
public void testReadDocumentWithMultipleNamespaceComplex() throws Exception {
    String doc = getDocumentString("src/test/resources/complex_example_ns.xml");
    reader.setDocument(doc, true);
    XmlField xmlField = AtlasXmlModelFactory.createXmlField();
    xmlField.setPath("/orders/order[2]/id[1]/@y:custId");
    assertNull(xmlField.getValue());
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(xmlField);
    reader.read(session);
    assertNotNull(xmlField.getValue());
    assertThat(xmlField.getValue(), is("b"));
}
Also used : Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) XmlField(io.atlasmap.xml.v2.XmlField) Test(org.junit.Test)

Example 42 with XmlField

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

the class XmlFieldReaderTest method testReadDocumentSetAttributeValueComplex.

@Test
public void testReadDocumentSetAttributeValueComplex() throws Exception {
    String doc = getDocumentString("src/test/resources/complex_example.xml");
    reader.setDocument(doc, false);
    XmlField xmlField = AtlasXmlModelFactory.createXmlField();
    xmlField.setPath("/orders/order[2]/id[1]/@custId");
    assertNull(xmlField.getValue());
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(xmlField);
    reader.read(session);
    assertNotNull(xmlField.getValue());
    assertThat(xmlField.getValue(), is("b"));
}
Also used : Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) XmlField(io.atlasmap.xml.v2.XmlField) Test(org.junit.Test)

Example 43 with XmlField

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

the class SchemaInspector method printElement.

private void printElement(XSElementDecl element, String root, XmlComplexType xmlComplexType, CollectionType collectionType) throws Exception {
    String rootName = root;
    if (element.getType().isComplexType()) {
        XmlComplexType complexType = getXmlComplexType();
        rootName = rootName + "/" + getNameNS(element);
        complexType.setName(getNameNS(element));
        complexType.setPath(rootName);
        complexType.setCollectionType(collectionType);
        xmlComplexType.getXmlFields().getXmlField().add(complexType);
        printComplexType(element.getType().asComplexType(), rootName, complexType);
    } else {
        if (element.getType() != null && element.getType().asSimpleType() != null) {
            XmlField xmlField = AtlasXmlModelFactory.createXmlField();
            xmlField.setName(getNameNS(element));
            xmlField.setPath(rootName + "/" + getNameNS(element));
            xmlComplexType.getXmlFields().getXmlField().add(xmlField);
            if (element.getDefaultValue() != null) {
                xmlField.setValue(element.getDefaultValue());
            } else if (element.getFixedValue() != null) {
                xmlField.setValue(element.getFixedValue());
            }
            XSRestrictionSimpleType typeRestriction = element.getType().asSimpleType().asRestriction();
            if (typeRestriction != null) {
                xmlField.setFieldType(XS_TYPE_TO_FIELD_TYPE_MAP.get(typeRestriction.getBaseType().getName()));
                mapRestrictions(xmlField, typeRestriction);
            }
            printSimpleType(element.getType().asSimpleType(), xmlField);
        }
    }
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XSRestrictionSimpleType(com.sun.xml.xsom.XSRestrictionSimpleType) XmlField(io.atlasmap.xml.v2.XmlField)

Example 44 with XmlField

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

the class SchemaInspector method printAttributes.

private void printAttributes(XSComplexType xsComplexType, String rootName, XmlComplexType xmlComplexType) {
    Collection<? extends XSAttributeUse> c = xsComplexType.getDeclaredAttributeUses();
    for (XSAttributeUse aC : c) {
        XmlField xmlField = AtlasXmlModelFactory.createXmlField();
        XSAttributeDecl attributeDecl = aC.getDecl();
        xmlField.setName(getNameNS(attributeDecl));
        if (attributeDecl.getDefaultValue() != null) {
            xmlField.setValue(attributeDecl.getDefaultValue().value);
        } else if (attributeDecl.getFixedValue() != null) {
            xmlField.setValue(attributeDecl.getFixedValue().value);
        }
        xmlField.setPath(rootName + "/" + "@" + getNameNS(attributeDecl));
        FieldType attrType = getFieldType(attributeDecl.getType().getName());
        xmlField.setFieldType(attrType);
        if (xmlField.getFieldType() == null) {
            // check the simple types in the schema....
            XSSimpleType simpleType = xsComplexType.getRoot().getSimpleType(xsComplexType.getTargetNamespace(), attributeDecl.getType().getName());
            if (simpleType != null) {
                FieldType fieldType = getFieldType(simpleType.getBaseType().getName());
                xmlField.setFieldType(fieldType);
                xmlField.setTypeName(attributeDecl.getType().getName());
                if (simpleType.asRestriction() != null) {
                    mapRestrictions(xmlField, simpleType.asRestriction());
                }
            } else {
                // cannot figure it out....
                xmlField.setFieldType(FieldType.UNSUPPORTED);
            }
        }
        xmlComplexType.getXmlFields().getXmlField().add(xmlField);
    }
}
Also used : XSSimpleType(com.sun.xml.xsom.XSSimpleType) XmlField(io.atlasmap.xml.v2.XmlField) XSAttributeDecl(com.sun.xml.xsom.XSAttributeDecl) XSAttributeUse(com.sun.xml.xsom.XSAttributeUse) FieldType(io.atlasmap.v2.FieldType)

Example 45 with XmlField

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

the class SchemaInspector method mapSimpleRestrictionToRestriction.

private void mapSimpleRestrictionToRestriction(SimpleTypeRestriction simpleTypeRestriction, XmlField xmlField) {
    for (Field field : SimpleTypeRestriction.class.getDeclaredFields()) {
        field.setAccessible(true);
        try {
            // do we even care about this restriction?
            if (typeRestrictionExists(field.getName())) {
                Object value = field.get(simpleTypeRestriction);
                if (value != null) {
                    Restriction restriction = new Restriction();
                    restriction.setValue((String) value);
                    restriction.setType(RestrictionType.fromValue(field.getName()));
                    xmlField.getRestrictions().getRestriction().add(restriction);
                }
            }
        } catch (IllegalAccessException e) {
        // eat it...
        }
    }
}
Also used : XmlField(io.atlasmap.xml.v2.XmlField) Field(java.lang.reflect.Field) Restriction(io.atlasmap.xml.v2.Restriction)

Aggregations

XmlField (io.atlasmap.xml.v2.XmlField)46 Test (org.junit.Test)31 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)21 Head (io.atlasmap.spi.AtlasInternalSession.Head)21 XmlComplexType (io.atlasmap.xml.v2.XmlComplexType)11 XmlDocument (io.atlasmap.xml.v2.XmlDocument)8 AtlasMapping (io.atlasmap.v2.AtlasMapping)7 Mapping (io.atlasmap.v2.Mapping)7 Field (io.atlasmap.v2.Field)6 XmlNamespace (io.atlasmap.xml.v2.XmlNamespace)6 File (java.io.File)6 Validation (io.atlasmap.v2.Validation)4 FieldType (io.atlasmap.v2.FieldType)3 Restriction (io.atlasmap.xml.v2.Restriction)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 AtlasMappingUtil (io.atlasmap.core.AtlasMappingUtil)2 DefaultAtlasConversionService (io.atlasmap.core.DefaultAtlasConversionService)2 AtlasModuleDetail (io.atlasmap.spi.AtlasModuleDetail)2 AtlasModuleMode (io.atlasmap.spi.AtlasModuleMode)2