Search in sources :

Example 11 with XmlField

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

the class XmlFieldWriterTest method readFromFile.

private AtlasInternalSession readFromFile(String fieldPath, FieldType fieldType, Path path) throws Exception {
    String input = new String(Files.readAllBytes(path));
    reader.setDocument(input, false);
    XmlField xmlField = AtlasXmlModelFactory.createXmlField();
    xmlField.setPath(fieldPath);
    xmlField.setPrimitive(Boolean.TRUE);
    xmlField.setFieldType(fieldType);
    assertNull(xmlField.getValue());
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(xmlField);
    Audits audits = new Audits();
    when(session.getAudits()).thenReturn(audits);
    reader.read(session);
    return session;
}
Also used : Head(io.atlasmap.spi.AtlasInternalSession.Head) Audits(io.atlasmap.v2.Audits) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) XmlField(io.atlasmap.xml.v2.XmlField)

Example 12 with XmlField

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

the class XmlFieldWriterTest method writeValue.

public void writeValue(String path, Object value) throws Exception {
    if (writer == null) {
        createWriter();
    }
    XmlField xmlField = AtlasXmlModelFactory.createXmlField();
    xmlField.setPath(path);
    xmlField.setValue(value);
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(mock(Field.class));
    when(session.head().getTargetField()).thenReturn(xmlField);
    writer.write(session);
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) XmlField(io.atlasmap.xml.v2.XmlField)

Example 13 with XmlField

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

the class BaseXmlInspectionServiceTest method printXmlField.

protected void printXmlField(XmlField xmlField) {
    System.out.println("Name --> " + xmlField.getName());
    System.out.println("Path --> " + xmlField.getPath());
    System.out.println("Value --> " + xmlField.getValue());
    if (xmlField.getFieldType() != null) {
        System.out.println("Type --> " + xmlField.getFieldType().name());
    }
    if (xmlField.getTypeName() != null) {
        System.out.println("Type Name --> " + xmlField.getTypeName());
    }
    if (xmlField.getCollectionType() != null) {
        System.out.println("Collection Type --> " + xmlField.getCollectionType().name());
    }
    if (xmlField.getRestrictions() != null && !xmlField.getRestrictions().getRestriction().isEmpty()) {
        for (Restriction restriction : xmlField.getRestrictions().getRestriction()) {
            if (restriction != null) {
                System.out.println("Restriction Type--> " + restriction.getType());
                System.out.println("Restriction Type Value--> " + restriction.getValue());
            }
        }
    }
    System.out.println();
}
Also used : Restriction(io.atlasmap.xml.v2.Restriction)

Example 14 with XmlField

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

the class XmlSchemaInspectionTest method testInspectShipOrderSchemaFile.

@Test
public void testInspectShipOrderSchemaFile() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/ship-order-schema.xsd").toFile();
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument xmlDocument = service.inspectSchema(schemaFile);
    Assert.assertNotNull(xmlDocument);
    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(4));
    // @orderId
    XmlField orderIdAttr = root.getXmlFields().getXmlField().get(0);
    Assert.assertNotNull(orderIdAttr);
    Assert.assertThat(orderIdAttr.getName(), Is.is("orderid"));
    Assert.assertThat(orderIdAttr.getValue(), Is.is("2"));
    Assert.assertThat(orderIdAttr.getPath(), Is.is("/shiporder/@orderid"));
    Assert.assertThat(orderIdAttr.getFieldType(), Is.is(FieldType.STRING));
    // orderperson
    XmlField orderPerson = root.getXmlFields().getXmlField().get(1);
    Assert.assertNotNull(orderPerson);
    Assert.assertThat(orderPerson.getName(), Is.is("orderperson"));
    Assert.assertNull(orderPerson.getValue());
    Assert.assertThat(orderPerson.getPath(), Is.is("/shiporder/orderperson"));
    Assert.assertThat(orderPerson.getFieldType(), Is.is(FieldType.STRING));
    // shipTo
    XmlField shipTo = root.getXmlFields().getXmlField().get(2);
    Assert.assertNotNull(shipTo);
    Assert.assertTrue(shipTo instanceof XmlComplexType);
    Assert.assertThat(((XmlComplexType) shipTo).getXmlFields().getXmlField().size(), Is.is(4));
    // item
    XmlField item = root.getXmlFields().getXmlField().get(3);
    Assert.assertNotNull(item);
    Assert.assertTrue(item instanceof XmlComplexType);
    Assert.assertThat(((XmlComplexType) item).getXmlFields().getXmlField().size(), Is.is(4));
// debugFields(xmlDocument.getFields());
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlField(io.atlasmap.xml.v2.XmlField) XmlDocument(io.atlasmap.xml.v2.XmlDocument) File(java.io.File) Test(org.junit.Test)

Example 15 with XmlField

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

the class XmlFieldReader method read.

public void read(AtlasInternalSession session) throws AtlasException {
    if (document == null) {
        throw new AtlasException(new IllegalArgumentException("'document' cannot be null"));
    }
    Field field = session.head().getSourceField();
    if (field == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
    }
    seedDocumentNamespaces(document);
    XmlField xmlField = XmlField.class.cast(field);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Reading source value for field: " + xmlField.getPath());
    }
    if (document == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'document' cannot be null"));
    }
    if (xmlField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'xmlField' cannot be null"));
    }
    Element parentNode = document.getDocumentElement();
    for (SegmentContext sc : new XmlPath(xmlField.getPath()).getSegmentContexts(false)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Now processing segment: " + sc.getSegment());
            LOG.debug("Parent element is currently: " + XmlIOHelper.writeDocumentToString(true, parentNode));
        }
        if (sc.getPrev() == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Skipping root segment: " + sc);
            }
            // "/XOA/contact<>/firstName", skip.
            continue;
        }
        if (!XmlPath.isAttributeSegment(sc.getSegment())) {
            String childrenElementName = XmlPath.cleanPathSegment(sc.getSegment());
            String namespaceAlias = XmlPath.getNamespace(sc.getSegment());
            if (namespaceAlias != null && !"".equals(namespaceAlias)) {
                childrenElementName = namespaceAlias + ":" + childrenElementName;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Looking for children elements with name: " + childrenElementName);
            }
            List<Element> children = XmlIOHelper.getChildrenWithName(childrenElementName, parentNode);
            if (children == null || children.isEmpty()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Skipping source value set, couldn't find children with name '" + childrenElementName + "', for segment: " + sc);
                }
                return;
            }
            parentNode = children.get(0);
            if (XmlPath.isCollectionSegment(sc.getSegment())) {
                int index = XmlPath.indexOfSegment(sc.getSegment());
                if (index >= children.size()) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Skipping source value set, children list can't fit index " + index + ", children list size: " + children.size());
                    }
                    return;
                }
                parentNode = children.get(index);
            }
        }
        if (sc.getNext() == null) {
            // last segment.
            String value = parentNode.getTextContent();
            if (XmlPath.isAttributeSegment(sc.getSegment())) {
                String attributeName = XmlPath.getAttribute(sc.getSegment());
                value = parentNode.getAttribute(attributeName);
            }
            if (value == null) {
                return;
            }
            if (xmlField.getFieldType() == null) {
                xmlField.setValue(value);
                xmlField.setFieldType(FieldType.STRING);
            } else {
                Object convertedValue;
                try {
                    convertedValue = conversionService.convertType(value, xmlField.getFormat(), xmlField.getFieldType(), null);
                    xmlField.setValue(convertedValue);
                } catch (AtlasConversionException e) {
                    AtlasUtil.addAudit(session, xmlField.getDocId(), String.format("Failed to convert field value '%s' into type '%s'", value, xmlField.getFieldType()), xmlField.getPath(), AuditStatus.ERROR, value);
                }
            }
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) AtlasConversionException(io.atlasmap.api.AtlasConversionException) XmlField(io.atlasmap.xml.v2.XmlField) Element(org.w3c.dom.Element) AtlasException(io.atlasmap.api.AtlasException)

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