Search in sources :

Example 1 with XmlEnumField

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;
    }
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) XmlEnumField(io.atlasmap.xml.v2.XmlEnumField) FieldGroup(io.atlasmap.v2.FieldGroup) XmlField(io.atlasmap.xml.v2.XmlField) XmlEnumField(io.atlasmap.xml.v2.XmlEnumField) AtlasException(io.atlasmap.api.AtlasException) XmlNamespaces(io.atlasmap.xml.v2.XmlNamespaces)

Example 2 with XmlEnumField

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());
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlField(io.atlasmap.xml.v2.XmlField) XmlEnumField(io.atlasmap.xml.v2.XmlEnumField) XmlDocument(io.atlasmap.xml.v2.XmlDocument) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with XmlEnumField

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;
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) XmlEnumField(io.atlasmap.xml.v2.XmlEnumField) XmlField(io.atlasmap.xml.v2.XmlField) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) AtlasException(io.atlasmap.api.AtlasException) XmlSegmentContext(io.atlasmap.xml.core.XmlPath.XmlSegmentContext) Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) XmlEnumField(io.atlasmap.xml.v2.XmlEnumField) AtlasPath(io.atlasmap.core.AtlasPath)

Example 4 with XmlEnumField

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);
    }
}
Also used : XSType(com.sun.xml.xsom.XSType) XSFacet(com.sun.xml.xsom.XSFacet) XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XSRestrictionSimpleType(com.sun.xml.xsom.XSRestrictionSimpleType) XmlEnumFields(io.atlasmap.xml.v2.XmlEnumFields) XmlEnumField(io.atlasmap.xml.v2.XmlEnumField) XmlField(io.atlasmap.xml.v2.XmlField) List(java.util.List)

Aggregations

XmlEnumField (io.atlasmap.xml.v2.XmlEnumField)4 XmlField (io.atlasmap.xml.v2.XmlField)4 AtlasException (io.atlasmap.api.AtlasException)2 Field (io.atlasmap.v2.Field)2 FieldGroup (io.atlasmap.v2.FieldGroup)2 XmlComplexType (io.atlasmap.xml.v2.XmlComplexType)2 XSFacet (com.sun.xml.xsom.XSFacet)1 XSRestrictionSimpleType (com.sun.xml.xsom.XSRestrictionSimpleType)1 XSType (com.sun.xml.xsom.XSType)1 AtlasPath (io.atlasmap.core.AtlasPath)1 XmlSegmentContext (io.atlasmap.xml.core.XmlPath.XmlSegmentContext)1 XmlDocument (io.atlasmap.xml.v2.XmlDocument)1 XmlEnumFields (io.atlasmap.xml.v2.XmlEnumFields)1 XmlNamespaces (io.atlasmap.xml.v2.XmlNamespaces)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.jupiter.api.Test)1 Element (org.w3c.dom.Element)1