use of io.atlasmap.xml.v2.XmlEnumField in project atlasmap by atlasmap.
the class XmlFieldReader method read.
@Override
public Field read(AtlasInternalSession session) throws AtlasException {
Field field = session.head().getSourceField();
if (document == null) {
AtlasUtil.addAudit(session, field, String.format("Cannot read field '%s' of document '%s', document is null", field.getPath(), field.getDocId()), AuditStatus.ERROR, null);
return field;
}
if (field == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
}
if (!(field instanceof XmlField) && !(field instanceof FieldGroup) && !(field instanceof XmlEnumField)) {
throw new AtlasException(String.format("Unsupported field type '%s'", field.getClass()));
}
seedDocumentNamespaces(document);
if (LOG.isDebugEnabled()) {
LOG.debug("Reading source value for field: " + field.getPath());
}
Optional<XmlNamespaces> xmlNamespaces = getSourceNamespaces(session, field);
XmlPath path = new XmlPath(field.getPath());
List<Field> fields = getFieldsForPath(session, xmlNamespaces, document.getDocumentElement(), field, path, 0);
if (path.hasCollection() && !path.isIndexedCollection()) {
FieldGroup fieldGroup = AtlasModelFactory.createFieldGroupFrom(field, true);
fieldGroup.getField().addAll(fields);
session.head().setSourceField(fieldGroup);
return fieldGroup;
} else if (fields.size() == 1) {
field.setValue(fields.get(0).getValue());
return field;
} else {
return field;
}
}
use of io.atlasmap.xml.v2.XmlEnumField in project atlasmap by atlasmap.
the class XmlSchemaInspectionTest method testInspectSchemaFileComplex.
@Test
public void testInspectSchemaFileComplex() throws Exception {
File schemaFile = Paths.get("src/test/resources/inspect/complex-schema.xsd").toFile();
XmlInspectionService service = new XmlInspectionService();
XmlDocument xmlDocument = service.inspectSchema(schemaFile);
assertNotNull(xmlDocument);
assertNotNull(xmlDocument.getFields());
assertEquals(1, xmlDocument.getFields().getField().size());
XmlComplexType root = (XmlComplexType) xmlDocument.getFields().getField().get(0);
assertNotNull(root);
assertEquals(10, root.getXmlFields().getXmlField().size());
XmlField enumField = root.getXmlFields().getXmlField().get(9);
assertEquals("enumField", enumField.getName());
assertEquals(FieldType.COMPLEX, enumField.getFieldType());
XmlComplexType enumComplex = (XmlComplexType) enumField;
assertTrue(enumComplex.isEnumeration());
List<XmlEnumField> enumFields = enumComplex.getXmlEnumFields().getXmlEnumField();
assertEquals(6, enumFields.size());
assertEquals("aaa", enumFields.get(0).getName());
assertEquals("fff", enumFields.get(5).getName());
// debugFields(xmlDocument.getFields());
}
use of io.atlasmap.xml.v2.XmlEnumField in project atlasmap by atlasmap.
the class XmlFieldReader method getFieldsForPath.
private List<Field> getFieldsForPath(AtlasInternalSession session, Optional<XmlNamespaces> xmlNamespaces, Element node, Field field, XmlPath path, int depth) throws AtlasException {
List<Field> fields = new ArrayList<>();
List<XmlSegmentContext> segments = path.getXmlSegments(false);
if (segments.size() < depth) {
throw new AtlasException(String.format("depth '%s' exceeds segment size '%s'", depth, segments.size()));
}
if (segments.size() == depth) {
if (!(field instanceof XmlEnumField) && field.getFieldType() == FieldType.COMPLEX) {
FieldGroup group = (FieldGroup) field;
populateChildFields(session, xmlNamespaces, node, group, path);
fields.add(group);
} else {
XmlField xmlField = new XmlField();
AtlasXmlModelFactory.copyField(field, xmlField, true);
if (field instanceof XmlEnumField && xmlField.getFieldType() == FieldType.COMPLEX) {
// enum has COMPLEX by default
xmlField.setFieldType(FieldType.STRING);
}
copyValue(session, xmlNamespaces, segments.get(depth - 1), node, xmlField);
// reset index for subfields
xmlField.setIndex(null);
fields.add(xmlField);
}
return fields;
}
// segments.size() > depth
XmlSegmentContext segment = segments.get(depth);
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing segment: " + segment.getName());
}
if (depth == 0) {
if (segment.getName().startsWith(XmlIOHelper.getNodeNameWithoutNamespaceAlias(node))) {
Optional<String> rootNamespace = Optional.empty();
if (segment.getNamespace() != null) {
rootNamespace = getNamespace(xmlNamespaces, segment.getNamespace());
}
if (!rootNamespace.isPresent() || rootNamespace.get().equals(node.getNamespaceURI())) {
// "/XOA/contact<>/firstName", skip.
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping root segment: " + segment);
}
if (segments.size() > 1) {
depth = 1;
segment = segments.get(depth);
}
}
}
}
if (segment.isAttribute() && segments.size() == depth + 1) {
// if last segment is attribute
List<Field> attrFields = getFieldsForPath(session, xmlNamespaces, node, field, path, depth + 1);
fields.addAll(attrFields);
return fields;
}
String fieldName = segment.getName();
String fieldNamespace = segment.getNamespace();
Optional<String> namespace = getNamespace(xmlNamespaces, fieldNamespace);
List<Element> children = XmlIOHelper.getChildrenWithNameStripAlias(fieldName, namespace, node);
if (children == null || children.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping source value set, couldn't find children with name '" + fieldName + "', for segment: " + segment);
}
return fields;
}
if (segment.getCollectionType() == CollectionType.NONE) {
List<Field> childFields = getFieldsForPath(session, xmlNamespaces, children.get(0), field, path, depth + 1);
fields.addAll(childFields);
return fields;
}
// collection
Integer index = segment.getCollectionIndex();
if (index != null) {
if (index < children.size()) {
List<Field> arrayFields = getFieldsForPath(session, xmlNamespaces, children.get(index), field, path, depth + 1);
fields.addAll(arrayFields);
} else if (LOG.isDebugEnabled()) {
LOG.debug("Skipping source value set, children list can't fit index " + index + ", children list size: " + children.size());
}
} else {
// if index not included, iterate over all
for (int i = 0; i < children.size(); i++) {
Field itemField;
if (field instanceof FieldGroup) {
itemField = AtlasXmlModelFactory.cloneFieldGroup((FieldGroup) field);
AtlasPath.setCollectionIndexRecursively((FieldGroup) itemField, depth + 1, i);
} else {
itemField = AtlasXmlModelFactory.cloneField((XmlField) field, false);
AtlasPath itemPath = new AtlasPath(field.getPath());
itemPath.setCollectionIndex(depth + 1, i);
itemField.setPath(itemPath.toString());
}
List<Field> arrayFields = getFieldsForPath(session, xmlNamespaces, children.get(i), itemField, new XmlPath(itemField.getPath()), depth + 1);
fields.addAll(arrayFields);
}
}
return fields;
}
use of io.atlasmap.xml.v2.XmlEnumField in project atlasmap by atlasmap.
the class XmlSchemaInspector method printElement.
private void printElement(XSElementDecl element, XmlComplexType parentXmlComplexType, CollectionType collectionType, Set<String> cachedComplexType) throws Exception {
String parentPath = parentXmlComplexType.getPath();
String elementName = getNameNS(element);
String typeName = getNameNS(element.getType());
XSType elementType = element.getType();
if (elementType == null) {
return;
}
if (elementType.isComplexType()) {
XmlComplexType complexType = getXmlComplexType();
String path = parentPath + "/" + elementName + getCollectionPathSuffix(collectionType);
complexType.setName(elementName);
complexType.setPath(path);
complexType.setCollectionType(collectionType);
parentXmlComplexType.getXmlFields().getXmlField().add(complexType);
if (typeName != null && !typeName.isEmpty() && cachedComplexType.contains(typeName)) {
complexType.setStatus(FieldStatus.CACHED);
} else if (typeName != null) {
cachedComplexType.add(typeName);
}
if (complexType.getStatus() != FieldStatus.CACHED) {
printComplexType(element.getType().asComplexType(), complexType, cachedComplexType);
if (LOG.isTraceEnabled()) {
LOG.trace("Element: {}/{}", parentPath, getNameNS(element));
}
}
} else if (elementType.asSimpleType() != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Element: {}/{}", parentPath, getNameNS(element));
}
XSRestrictionSimpleType restrictionType = elementType.asSimpleType().asRestriction();
List<XSFacet> enumerations = restrictionType != null ? restrictionType.getFacets("enumeration") : null;
if (enumerations != null && !enumerations.isEmpty()) {
XmlComplexType complexType = getXmlComplexType();
String path = parentPath + "/" + elementName + getCollectionPathSuffix(collectionType);
complexType.setName(elementName);
complexType.setPath(path);
complexType.setCollectionType(collectionType);
complexType.setEnumeration(true);
parentXmlComplexType.getXmlFields().getXmlField().add(complexType);
XmlEnumFields enums = new XmlEnumFields();
complexType.setXmlEnumFields(enums);
for (XSFacet enumFacet : enumerations) {
XmlEnumField f = new XmlEnumField();
f.setName(enumFacet.getValue().toString());
enums.getXmlEnumField().add(f);
}
return;
}
XmlField xmlField = AtlasXmlModelFactory.createXmlField();
xmlField.setName(elementName);
xmlField.setPath(parentPath + "/" + elementName + getCollectionPathSuffix(collectionType));
xmlField.setCollectionType(collectionType);
parentXmlComplexType.getXmlFields().getXmlField().add(xmlField);
if (element.getDefaultValue() != null) {
xmlField.setValue(element.getDefaultValue());
} else if (element.getFixedValue() != null) {
xmlField.setValue(element.getFixedValue());
}
XSRestrictionSimpleType typeRestriction = element.getType().asSimpleType().asRestriction();
if (typeRestriction != null) {
xmlField.setFieldType(XS_TYPE_TO_FIELD_TYPE_MAP.get(typeRestriction.getBaseType().getName()));
mapRestrictions(xmlField, typeRestriction);
}
printSimpleType(element.getType().asSimpleType(), xmlField);
}
}
Aggregations