Search in sources :

Example 11 with Fields

use of io.atlasmap.v2.Fields in project atlasmap by atlasmap.

the class BaseXmlInspectionServiceTest method debugFields.

protected void debugFields(Fields xmlFields) {
    for (Field field : xmlFields.getField()) {
        Assert.assertTrue(field instanceof XmlField);
        XmlField xmlField = (XmlField) field;
        printXmlField(xmlField);
        if (xmlField instanceof XmlComplexType) {
            debugFields(((XmlComplexType) xmlField).getXmlFields());
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlField(io.atlasmap.xml.v2.XmlField)

Example 12 with Fields

use of io.atlasmap.v2.Fields in project atlasmap by atlasmap.

the class XmlSchemaInspectionMultipleNamespacesTest method testSyndesis.

@Test
public void testSyndesis() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/multiple-namespaces-syndesis.xml").toFile();
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument answer = service.inspectSchema(schemaFile);
    Assert.assertEquals(1, answer.getXmlNamespaces().getXmlNamespace().size());
    XmlNamespace namespace = answer.getXmlNamespaces().getXmlNamespace().get(0);
    Assert.assertEquals("tns", namespace.getAlias());
    Assert.assertEquals("http://syndesis.io/v1/swagger-connector-template/request", namespace.getUri());
    Assert.assertEquals(null, namespace.isTargetNamespace());
    List<Field> fields = answer.getFields().getField();
    Assert.assertEquals(1, fields.size());
    XmlComplexType complex = XmlComplexType.class.cast(fields.get(0));
    Assert.assertEquals("tns:request", complex.getName());
    List<XmlField> rootFields = complex.getXmlFields().getXmlField();
    Assert.assertEquals(1, rootFields.size());
    complex = XmlComplexType.class.cast(rootFields.get(0));
    Assert.assertEquals("tns:body", complex.getName());
    List<XmlField> bodyFields = complex.getXmlFields().getXmlField();
    Assert.assertEquals(1, bodyFields.size());
    complex = XmlComplexType.class.cast(bodyFields.get(0));
    Assert.assertEquals("Pet", complex.getName());
    List<XmlField> petFields = complex.getXmlFields().getXmlField();
    Assert.assertEquals(6, petFields.size());
    for (XmlField xmlField : petFields) {
        switch(xmlField.getName()) {
            case "id":
                Assert.assertEquals(FieldType.DECIMAL, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/id", xmlField.getPath());
                break;
            case "Category":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/Category", xmlField.getPath());
                List<XmlField> categoryFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(2, categoryFields.size());
                for (XmlField categoryField : categoryFields) {
                    switch(categoryField.getName()) {
                        case "id":
                            Assert.assertEquals(FieldType.DECIMAL, categoryField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/Category/id", categoryField.getPath());
                            break;
                        case "name":
                            Assert.assertEquals(FieldType.STRING, categoryField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/Category/name", categoryField.getPath());
                            break;
                        default:
                            Assert.fail(String.format("Unknown field '%s'", categoryField.getPath()));
                    }
                }
                break;
            case "name":
                Assert.assertEquals(FieldType.STRING, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/name", xmlField.getPath());
                break;
            case "photoUrl":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/photoUrl", xmlField.getPath());
                List<XmlField> photoUrlFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(1, photoUrlFields.size());
                XmlField photoUrlField = photoUrlFields.get(0);
                Assert.assertEquals(FieldType.STRING, photoUrlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/photoUrl/photoUrl", photoUrlField.getPath());
                break;
            case "tag":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/tag", xmlField.getPath());
                List<XmlField> tagFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(1, tagFields.size());
                XmlField tagField = tagFields.get(0);
                Assert.assertEquals(FieldType.COMPLEX, tagField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/tag/Tag", tagField.getPath());
                List<XmlField> tagTagFields = XmlComplexType.class.cast(tagField).getXmlFields().getXmlField();
                Assert.assertEquals(2, tagTagFields.size());
                for (XmlField tagTagField : tagTagFields) {
                    switch(tagTagField.getName()) {
                        case "id":
                            Assert.assertEquals(FieldType.DECIMAL, tagTagField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/tag/Tag/id", tagTagField.getPath());
                            break;
                        case "name":
                            Assert.assertEquals(FieldType.STRING, tagTagField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/tag/Tag/name", tagTagField.getPath());
                            break;
                        default:
                            Assert.fail(String.format("Unknown field '%s'", tagTagField.getPath()));
                    }
                }
                break;
            case "status":
                Assert.assertEquals(FieldType.STRING, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/status", xmlField.getPath());
                break;
            default:
                Assert.fail(String.format("Unknown field '%s'", xmlField.getPath()));
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlField(io.atlasmap.xml.v2.XmlField) XmlDocument(io.atlasmap.xml.v2.XmlDocument) File(java.io.File) Test(org.junit.Test)

Example 13 with Fields

use of io.atlasmap.v2.Fields in project atlasmap by atlasmap.

the class SchemaInspector method doInspect.

private void doInspect(InputStream is) throws Exception {
    xmlDocument = AtlasXmlModelFactory.createXmlDocument();
    Fields fields = new Fields();
    xmlDocument.setFields(fields);
    namespaceContext = new AtlasXmlNamespaceContext();
    rootNamespace = null;
    XSOMParser parser = new XSOMParser(SAXParserFactory.newInstance());
    parser.setAnnotationParser(new DomAnnotationParserFactory());
    parser.setErrorHandler(new XSOMErrorHandler());
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = dbf.newDocumentBuilder().parse(is);
    Element root = doc.getDocumentElement();
    if (root == null) {
        throw new XmlInspectionException("XML schema document is empty");
    } else if ("SchemaSet".equals(root.getLocalName())) {
        XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(namespaceContext);
        NodeList subSchemas = (NodeList) xpath.evaluate(String.format("/%s:SchemaSet/%s:AdditionalSchemas/%s:schema", NS_PREFIX_SCHEMASET, NS_PREFIX_SCHEMASET, NS_PREFIX_XMLSCHEMA), doc, XPathConstants.NODESET);
        for (int i = 0; i < subSchemas.getLength(); i++) {
            Element e = (Element) subSchemas.item(i);
            inheritNamespaces(e, false);
            parser.parse(toInputStream(transformer, e));
        }
        Element rootSchema = (Element) xpath.evaluate(String.format("/%s:SchemaSet/%s:schema", NS_PREFIX_SCHEMASET, NS_PREFIX_XMLSCHEMA), doc, XPathConstants.NODE);
        if (rootSchema == null) {
            throw new XmlInspectionException("The root schema '/SchemaSet/schema' must be specified once and only once");
        }
        rootNamespace = getTargetNamespace(rootSchema);
        if (rootNamespace != null && !rootNamespace.isEmpty()) {
            namespaceContext.add("tns", rootNamespace);
        }
        inheritNamespaces(rootSchema, true);
        parser.parse(toInputStream(transformer, rootSchema));
    } else if ("schema".equals(root.getLocalName())) {
        parser.parse(toInputStream(transformer, root));
        rootNamespace = getTargetNamespace(root);
        if (rootNamespace != null && !rootNamespace.isEmpty()) {
            namespaceContext.add("tns", rootNamespace);
        }
    } else {
        throw new XmlInspectionException(String.format("Unsupported document element '%s': root element must be 'schema' or 'SchemaSet'", root.getLocalName()));
    }
    XSSchemaSet schemaSet = parser.getResult();
    printSchemaSet(schemaSet);
    populateNamespaces();
}
Also used : XPath(javax.xml.xpath.XPath) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) Transformer(javax.xml.transform.Transformer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XmlDocument(io.atlasmap.xml.v2.XmlDocument) XmlFields(io.atlasmap.xml.v2.XmlFields) Fields(io.atlasmap.v2.Fields) DomAnnotationParserFactory(com.sun.xml.xsom.util.DomAnnotationParserFactory)

Example 14 with Fields

use of io.atlasmap.v2.Fields in project atlasmap by atlasmap.

the class SchemaInspectorTest method inspectJsonSchemaGeo.

// examples from json-schema.org
@Test
public void inspectJsonSchemaGeo() throws Exception {
    final String schema = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/geo.json")));
    JsonDocument document = inspectionService.inspectJsonSchema(schema);
    List<Field> fields = document.getFields().getField();
    JsonField f = (JsonField) fields.get(0);
    assertEquals("latitude", f.getName());
    assertEquals("/latitude", f.getPath());
    assertEquals(FieldType.NUMBER, f.getFieldType());
    f = (JsonField) fields.get(1);
    assertEquals("longitude", f.getName());
    assertEquals("/longitude", f.getPath());
    assertEquals(FieldType.NUMBER, f.getFieldType());
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonField(io.atlasmap.json.v2.JsonField) JsonDocument(io.atlasmap.json.v2.JsonDocument) Test(org.junit.Test)

Example 15 with Fields

use of io.atlasmap.v2.Fields in project atlasmap by atlasmap.

the class SchemaInspectorTest method doInspectJsonSchemaCard.

private void doInspectJsonSchemaCard(String schema) throws Exception {
    JsonDocument document = inspectionService.inspectJsonSchema(schema);
    List<Field> fields = document.getFields().getField();
    JsonField f = (JsonField) fields.get(0);
    assertEquals("fn", f.getName());
    assertEquals("/fn", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(1);
    assertEquals("familyName", f.getName());
    assertEquals("/familyName", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(2);
    assertEquals("givenName", f.getName());
    assertEquals("/givenName", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(3);
    assertEquals("additionalName", f.getName());
    assertEquals("/additionalName", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    assertEquals(CollectionType.LIST, f.getCollectionType());
    f = (JsonField) fields.get(4);
    assertEquals("honorificPrefix", f.getName());
    assertEquals("/honorificPrefix", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    assertEquals(CollectionType.LIST, f.getCollectionType());
    f = (JsonField) fields.get(5);
    assertEquals("honorificSuffix", f.getName());
    assertEquals("/honorificSuffix", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    assertEquals(CollectionType.LIST, f.getCollectionType());
    f = (JsonField) fields.get(6);
    assertEquals("nickname", f.getName());
    assertEquals("/nickname", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(7);
    assertEquals("url", f.getName());
    assertEquals("/url", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonComplexType) fields.get(8);
    assertEquals("email", f.getName());
    assertEquals("/email", f.getPath());
    assertEquals(FieldType.COMPLEX, f.getFieldType());
    List<JsonField> emailfields = ((JsonComplexType) f).getJsonFields().getJsonField();
    f = emailfields.get(0);
    assertEquals("type", f.getName());
    assertEquals("/email/type", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = emailfields.get(1);
    assertEquals("value", f.getName());
    assertEquals("/email/value", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonComplexType) fields.get(9);
    assertEquals("tel", f.getName());
    assertEquals("/tel", f.getPath());
    assertEquals(FieldType.COMPLEX, f.getFieldType());
    List<JsonField> telfields = ((JsonComplexType) f).getJsonFields().getJsonField();
    f = telfields.get(0);
    assertEquals("type", f.getName());
    assertEquals("/tel/type", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = telfields.get(1);
    assertEquals("value", f.getName());
    assertEquals("/tel/value", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonComplexType) fields.get(10);
    assertEquals("adr", f.getName());
    assertEquals("/adr", f.getPath());
    assertEquals(FieldType.COMPLEX, f.getFieldType());
    List<JsonField> addrfields = ((JsonComplexType) f).getJsonFields().getJsonField();
    f = addrfields.get(0);
    assertEquals("post-office-box", f.getName());
    assertEquals("/adr/post-office-box", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = addrfields.get(1);
    assertEquals("extended-address", f.getName());
    assertEquals("/adr/extended-address", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = addrfields.get(2);
    assertEquals("street-address", f.getName());
    assertEquals("/adr/street-address", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = addrfields.get(3);
    assertEquals("locality", f.getName());
    assertEquals("/adr/locality", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = addrfields.get(4);
    assertEquals("region", f.getName());
    assertEquals("/adr/region", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = addrfields.get(5);
    assertEquals("postal-code", f.getName());
    assertEquals("/adr/postal-code", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = addrfields.get(6);
    assertEquals("country-name", f.getName());
    assertEquals("/adr/country-name", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonComplexType) fields.get(11);
    assertEquals("geo", f.getName());
    assertEquals("/geo", f.getPath());
    assertEquals(FieldType.COMPLEX, f.getFieldType());
    List<JsonField> geofields = ((JsonComplexType) f).getJsonFields().getJsonField();
    f = geofields.get(0);
    assertEquals("latitude", f.getName());
    assertEquals("/geo/latitude", f.getPath());
    assertEquals(FieldType.NUMBER, f.getFieldType());
    f = geofields.get(1);
    assertEquals("longitude", f.getName());
    assertEquals("/geo/longitude", f.getPath());
    assertEquals(FieldType.NUMBER, f.getFieldType());
    f = (JsonField) fields.get(12);
    assertEquals("tz", f.getName());
    assertEquals("/tz", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(13);
    assertEquals("photo", f.getName());
    assertEquals("/photo", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(14);
    assertEquals("logo", f.getName());
    assertEquals("/logo", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(15);
    assertEquals("sound", f.getName());
    assertEquals("/sound", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(16);
    assertEquals("bday", f.getName());
    assertEquals("/bday", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(17);
    assertEquals("title", f.getName());
    assertEquals("/title", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(18);
    assertEquals("role", f.getName());
    assertEquals("/role", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonComplexType) fields.get(19);
    assertEquals("org", f.getName());
    assertEquals("/org", f.getPath());
    assertEquals(FieldType.COMPLEX, f.getFieldType());
    List<JsonField> orgfields = ((JsonComplexType) f).getJsonFields().getJsonField();
    f = orgfields.get(0);
    assertEquals("organizationName", f.getName());
    assertEquals("/org/organizationName", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = orgfields.get(1);
    assertEquals("organizationUnit", f.getName());
    assertEquals("/org/organizationUnit", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonField(io.atlasmap.json.v2.JsonField) JsonDocument(io.atlasmap.json.v2.JsonDocument)

Aggregations

Field (io.atlasmap.v2.Field)10 Test (org.junit.Test)6 JsonDocument (io.atlasmap.json.v2.JsonDocument)5 JsonField (io.atlasmap.json.v2.JsonField)5 Fields (io.atlasmap.v2.Fields)4 XmlComplexType (io.atlasmap.xml.v2.XmlComplexType)3 XmlDocument (io.atlasmap.xml.v2.XmlDocument)3 XmlField (io.atlasmap.xml.v2.XmlField)3 File (java.io.File)3 Record (org.jooq.Record)3 DefaultDataTypeDefinition (org.jooq.util.DefaultDataTypeDefinition)3 Rdb$fields (org.jooq.util.firebird.rdb.tables.Rdb$fields)3 JavaField (io.atlasmap.java.v2.JavaField)2 AtlasMapping (io.atlasmap.v2.AtlasMapping)2 BaseMapping (io.atlasmap.v2.BaseMapping)2 Mapping (io.atlasmap.v2.Mapping)2 XmlFields (io.atlasmap.xml.v2.XmlFields)2 XmlNamespace (io.atlasmap.xml.v2.XmlNamespace)2 ArrayList (java.util.ArrayList)2 ColumnDefinition (org.jooq.util.ColumnDefinition)2