Search in sources :

Example 1 with SegmentContext

use of io.atlasmap.core.AtlasPath.SegmentContext 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)

Example 2 with SegmentContext

use of io.atlasmap.core.AtlasPath.SegmentContext in project atlasmap by atlasmap.

the class JsonModule method getCollectionSize.

@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
    // TODO could this use FieldReader?
    Object document = session.getSourceDocument(getDocId());
    // make this a JSON document
    JsonFactory jsonFactory = new JsonFactory();
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        JsonParser parser = jsonFactory.createParser(document.toString());
        JsonNode rootNode = objectMapper.readTree(parser);
        ObjectNode parentNode = (ObjectNode) rootNode;
        String parentSegment = "[root node]";
        for (SegmentContext sc : new AtlasPath(field.getPath()).getSegmentContexts(false)) {
            JsonNode currentNode = JsonFieldWriter.getChildNode(parentNode, parentSegment, sc.getSegment());
            if (currentNode == null) {
                return 0;
            }
            if (AtlasPath.isCollectionSegment(sc.getSegment())) {
                if (currentNode != null && currentNode.isArray()) {
                    return currentNode.size();
                }
                return 0;
            }
            parentNode = (ObjectNode) currentNode;
        }
    } catch (IOException e) {
        throw new AtlasException(e.getMessage(), e);
    }
    return 0;
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonFactory(com.fasterxml.jackson.core.JsonFactory) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) AtlasException(io.atlasmap.api.AtlasException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 3 with SegmentContext

use of io.atlasmap.core.AtlasPath.SegmentContext in project atlasmap by atlasmap.

the class XmlModule method getCollectionSize.

@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
    // TODO could this use FieldReader?
    try {
        Object sourceObject = session.getSourceDocument(getDocId());
        Document document = getDocument((String) sourceObject, false);
        Element parentNode = document.getDocumentElement();
        for (SegmentContext sc : new XmlPath(field.getPath()).getSegmentContexts(false)) {
            if (sc.getPrev() == null) {
                // "/XOA/contact<>/firstName", skip.
                continue;
            }
            String childrenElementName = XmlPath.cleanPathSegment(sc.getSegment());
            String namespaceAlias = XmlPath.getNamespace(sc.getSegment());
            if (namespaceAlias != null && !"".equals(namespaceAlias)) {
                childrenElementName = namespaceAlias + ":" + childrenElementName;
            }
            List<Element> children = XmlIOHelper.getChildrenWithName(childrenElementName, parentNode);
            if (children == null || children.isEmpty()) {
                return 0;
            }
            if (XmlPath.isCollectionSegment(sc.getSegment())) {
                return children.size();
            }
            parentNode = children.get(0);
        }
        return 0;
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : XmlPath(io.atlasmap.xml.core.XmlPath) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) AtlasException(io.atlasmap.api.AtlasException) TransformerException(javax.xml.transform.TransformerException) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasValidationException(io.atlasmap.api.AtlasValidationException) AtlasException(io.atlasmap.api.AtlasException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 4 with SegmentContext

use of io.atlasmap.core.AtlasPath.SegmentContext in project atlasmap by atlasmap.

the class AtlasPathTest method testGetSegmentContexts.

@Test
public void testGetSegmentContexts() {
    AtlasPath path = new AtlasPath("/orders");
    assertNotNull(path.getSegmentContexts(true));
    List<SegmentContext> segmentContexts = path.getSegmentContexts(false);
    assertNotNull(segmentContexts);
    assertEquals(2, segmentContexts.size());
    SegmentContext segmentContext = segmentContexts.get(1);
    assertNotNull(segmentContext.getSegment());
    assertNotNull(segmentContext.getSegmentIndex());
    assertNotNull(segmentContext.getSegmentPath());
    assertNull(segmentContext.getNext());
    assertNotNull(segmentContext.getPrev());
    assertNotNull(segmentContext.getPathUtil());
    assertNotNull(segmentContext.toString());
    assertFalse(segmentContext.hasChild());
    assertTrue(segmentContext.hasParent());
    segmentContext = segmentContexts.get(0);
    assertTrue(segmentContext.hasChild());
    assertFalse(segmentContext.hasParent());
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) Test(org.junit.Test)

Example 5 with SegmentContext

use of io.atlasmap.core.AtlasPath.SegmentContext in project atlasmap by atlasmap.

the class JavaWriterUtilTest method runInstantiateObjectTest.

public void runInstantiateObjectTest(Class<?> clz, boolean createWrapperArray) throws Exception {
    SegmentContext sc = new SegmentContext();
    sc.setSegment("blah[52]");
    sc.setSegmentPath("/blah[52]");
    JavaWriterUtil writerUtil = new JavaWriterUtil(DefaultAtlasConversionService.getInstance());
    Object o = writerUtil.instantiateObject(clz, sc, createWrapperArray);
    assertNotNull(o);
    if (createWrapperArray) {
        assertTrue(o.getClass().isArray());
        assertEquals(53, Array.getLength(o));
        assertEquals(clz, o.getClass().getComponentType());
    } else {
        assertEquals(clz, o.getClass());
    }
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext)

Aggregations

SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)7 AtlasException (io.atlasmap.api.AtlasException)4 AtlasPath (io.atlasmap.core.AtlasPath)3 AtlasConversionException (io.atlasmap.api.AtlasConversionException)2 Field (io.atlasmap.v2.Field)2 IOException (java.io.IOException)2 Element (org.w3c.dom.Element)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 AtlasValidationException (io.atlasmap.api.AtlasValidationException)1 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)1 JavaField (io.atlasmap.java.v2.JavaField)1 FieldType (io.atlasmap.v2.FieldType)1 LookupTable (io.atlasmap.v2.LookupTable)1 XmlPath (io.atlasmap.xml.core.XmlPath)1 XmlField (io.atlasmap.xml.v2.XmlField)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1