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