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