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);
}
}
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);
}
}
}
}
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);
}
}
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);
}
}
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);
}
}
}
}
Aggregations