Search in sources :

Example 6 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class XmlFieldWriter method createDocument.

private Document createDocument(Map<String, String> namespaces, String seedDocument) throws AtlasException {
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        if (namespaces != null && !namespaces.isEmpty()) {
            documentBuilderFactory.setNamespaceAware(true);
        }
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        if (seedDocument != null) {
            Document document = documentBuilder.parse(new ByteArrayInputStream(seedDocument.getBytes("UTF-8")));
            Element rootNode = document.getDocumentElement();
            // extract namespaces from seed document
            NamedNodeMap attributes = rootNode.getAttributes();
            if (attributes != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    Node n = attributes.item(i);
                    String nodeName = n.getNodeName();
                    if (nodeName != null && nodeName.startsWith("xmlns")) {
                        String namespaceAlias = "";
                        if (nodeName.contains(":")) {
                            namespaceAlias = nodeName.substring(nodeName.indexOf(":") + 1);
                        }
                        if (!namespaces.containsKey(namespaceAlias)) {
                            namespaces.put(namespaceAlias, n.getNodeValue());
                        }
                    }
                }
            }
            // rewrite root element to contain user-specified namespaces
            if (namespaces.size() > 0) {
                Element oldRootNode = rootNode;
                rootNode = (Element) oldRootNode.cloneNode(true);
                addNamespacesToElement(rootNode, namespaces);
                document.removeChild(oldRootNode);
                document.appendChild(rootNode);
            }
            return document;
        }
        return documentBuilder.newDocument();
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 7 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class XmlFieldWriter method write.

@Override
public void write(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    if (targetField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Now processing field p={} t={} v={}", targetField.getPath(), targetField.getFieldType(), targetField.getValue());
    }
    XmlPath path = new XmlPath(targetField.getPath());
    String lastSegment = path.getLastSegment();
    Element parentNode = null;
    String parentSegment = null;
    for (String segment : path.getSegments()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Now processing segment: {}", segment);
            LOG.debug("Parent element is currently: {}", XmlIOHelper.writeDocumentToString(true, parentNode));
        }
        if (parentNode == null) {
            // processing root node
            parentNode = document.getDocumentElement();
            String cleanedSegment = XmlPath.cleanPathSegment(segment);
            if (parentNode == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Creating root element with name: {}", cleanedSegment);
                }
                // no root node exists yet, create root node with this segment name;
                Element rootNode = createElement(segment);
                addNamespacesToElement(rootNode, namespaces);
                document.appendChild(rootNode);
                parentNode = rootNode;
            } else if (!(parentNode.getNodeName().equals(segment))) {
                // make sure root element's name matches.
                throw new AtlasException(String.format("Root element name '%s' does not match expected name '%s' from path: %s", parentNode.getNodeName(), segment, targetField.getPath()));
            }
            parentSegment = segment;
        } else {
            if (LOG.isDebugEnabled()) {
                if (segment.equals(lastSegment)) {
                    LOG.debug("Now processing field value segment: {}", segment);
                } else {
                    LOG.debug("Now processing parent segment: {}", segment);
                }
            }
            if (segment.equals(lastSegment) && targetField.getValue() == null) {
                break;
            }
            if (!XmlPath.isAttributeSegment(segment)) {
                // if current segment of path isn't attribute, it refers to a child element,
                // find it or create it..
                Element childNode = getChildNode(parentNode, parentSegment, segment);
                if (childNode == null) {
                    childNode = createParentNode(parentNode, parentSegment, segment);
                }
                parentNode = childNode;
                parentSegment = segment;
            }
            if (segment.equals(lastSegment)) {
                writeValue(parentNode, segment, targetField);
            }
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) Element(org.w3c.dom.Element) AtlasException(io.atlasmap.api.AtlasException)

Example 8 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class XmlIOHelper method writeDocumentToString.

public static String writeDocumentToString(boolean stripSpaces, Node node) throws AtlasException {
    try {
        if (node == null) {
            return "";
        }
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        String result = writer.getBuffer().toString();
        if (stripSpaces) {
            result = result.replaceAll("\n|\r", "");
            result = result.replaceAll("> *?<", "><");
        }
        return result;
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 9 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class JsonFieldReader method setDocument.

public void setDocument(String document) throws AtlasException {
    if (document == null || document.isEmpty()) {
        throw new AtlasException(new IllegalArgumentException("document cannot be null nor empty"));
    }
    try {
        JsonFactory factory = new JsonFactory();
        ObjectMapper mapper = new ObjectMapper();
        JsonParser parser = factory.createParser(document);
        this.rootNode = mapper.readTree(parser);
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) AtlasException(io.atlasmap.api.AtlasException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 10 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class JsonFieldReader method read.

@Override
public void read(AtlasInternalSession session) throws AtlasException {
    JsonField jsonField = JsonField.class.cast(session.head().getSourceField());
    if (rootNode == null) {
        throw new AtlasException("document is not set");
    }
    if (jsonField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'jsonField' cannot be null"));
    }
    JsonNode valueNode = null;
    AtlasPath path = new AtlasPath(jsonField.getPath());
    if (path.getSegments().size() >= 1) {
        if (rootNode.size() == 1 && !path.getSegments().get(0).startsWith(rootNode.fieldNames().next())) {
            // peel off a rooted object
            valueNode = rootNode.elements().next();
        } else {
            valueNode = rootNode;
        }
        // need to walk the path....
        for (String nodeName : path.getSegments()) {
            if (valueNode == null) {
                break;
            }
            valueNode = getValueNode(valueNode, nodeName);
        }
    }
    if (valueNode == null) {
        return;
    }
    if (valueNode.isNull()) {
        jsonField.setValue(null);
    // we can't detect field type if it's null node
    } else {
        if (jsonField.getFieldType() != null) {
            // mapping is overriding the fieldType
            try {
                Object convertedValue = conversionService.convertType(valueNode.asText(), jsonField.getFormat(), jsonField.getFieldType(), null);
                jsonField.setValue(convertedValue);
            } catch (AtlasConversionException e) {
                AtlasUtil.addAudit(session, jsonField.getDocId(), String.format("Failed to convert field value '%s' into type '%s'", valueNode.asText(), jsonField.getFieldType()), jsonField.getPath(), AuditStatus.ERROR, valueNode.asText());
            }
        } else {
            if (valueNode.isTextual()) {
                handleTextualNode(valueNode, jsonField);
            } else if (valueNode.isNumber()) {
                handleNumberNode(valueNode, jsonField);
            } else if (valueNode.isBoolean()) {
                handleBooleanNode(valueNode, jsonField);
            } else if (valueNode.isContainerNode()) {
                handleContainerNode(valueNode, jsonField);
            } else if (valueNode.isNull()) {
                jsonField.setValue(null);
            } else {
                LOG.warn(String.format("Detected unsupported json type for field p=%s docId=%s", jsonField.getPath(), jsonField.getDocId()));
                jsonField.setValue(valueNode.toString());
                jsonField.setFieldType(FieldType.UNSUPPORTED);
            }
        }
    }
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) AtlasException(io.atlasmap.api.AtlasException)

Aggregations

AtlasException (io.atlasmap.api.AtlasException)34 AtlasPath (io.atlasmap.core.AtlasPath)8 Method (java.lang.reflect.Method)7 AtlasConversionException (io.atlasmap.api.AtlasConversionException)6 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)6 JavaField (io.atlasmap.java.v2.JavaField)6 Field (io.atlasmap.v2.Field)6 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 TargetAddress (io.atlasmap.java.test.TargetAddress)3 TargetOrder (io.atlasmap.java.test.TargetOrder)3 TargetOrderArray (io.atlasmap.java.test.TargetOrderArray)3 TargetTestClass (io.atlasmap.java.test.TargetTestClass)3 TestListOrders (io.atlasmap.java.test.TestListOrders)3 AtlasModule (io.atlasmap.spi.AtlasModule)3 FieldType (io.atlasmap.v2.FieldType)3 LookupTable (io.atlasmap.v2.LookupTable)3 List (java.util.List)3 Element (org.w3c.dom.Element)3 JsonFactory (com.fasterxml.jackson.core.JsonFactory)2