Search in sources :

Example 11 with XmlDocument

use of io.atlasmap.xml.v2.XmlDocument in project atlasmap by atlasmap.

the class XmlSchemaInspectionTest method testInspectSchemaString.

@Test
public void testInspectSchemaString() throws Exception {
    final String source = "<xs:schema attributeFormDefault=\"unqualified\" elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" + "  <xs:element name=\"data\">\n" + "    <xs:complexType>\n" + "      <xs:sequence>\n" + "        <xs:element type=\"xs:int\" name=\"intField\"/>\n" + "        <xs:element type=\"xs:long\" name=\"longField\"/>\n" + "        <xs:element type=\"xs:string\" name=\"stringField\"/>\n" + "        <xs:element type=\"xs:boolean\" name=\"booleanField\"/>\n" + "        <xs:element type=\"xs:double\" name=\"doubleField\"/>\n" + "        <xs:element type=\"xs:short\" name=\"shortField\"/>\n" + "        <xs:element type=\"xs:float\" name=\"floatField\"/>\n" + "        <xs:element type=\"xs:string\" name=\"charField\"/>\n" + "      </xs:sequence>\n" + "    </xs:complexType>\n" + "  </xs:element>\n" + "</xs:schema>";
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument xmlDocument = service.inspectSchema(source);
    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(8));
// debugFields(xmlDocument.getFields());
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlDocument(io.atlasmap.xml.v2.XmlDocument) Test(org.junit.Test)

Example 12 with XmlDocument

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

Example 13 with XmlDocument

use of io.atlasmap.xml.v2.XmlDocument in project atlasmap by atlasmap.

the class XmlSchemaInspectionTest method testInspectSchemaFileWithNamespace.

@Test
public void testInspectSchemaFileWithNamespace() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/simple-namespace-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));
    Assert.assertNotNull(xmlDocument.getXmlNamespaces());
    Assert.assertThat(xmlDocument.getXmlNamespaces().getXmlNamespace().size(), Is.is(1));
    XmlNamespace namespace = xmlDocument.getXmlNamespaces().getXmlNamespace().get(0);
    Assert.assertThat(namespace.getAlias(), Is.is("tns"));
    Assert.assertThat(namespace.getUri(), Is.is("http://example.com/"));
// debugFields(xmlDocument.getFields());
}
Also used : XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlDocument(io.atlasmap.xml.v2.XmlDocument) File(java.io.File) Test(org.junit.Test)

Example 14 with XmlDocument

use of io.atlasmap.xml.v2.XmlDocument in project atlasmap by atlasmap.

the class XmlService method getClass.

@GET
@Path("/inspect")
@Produces(MediaType.APPLICATION_JSON)
public Response getClass(@QueryParam("uri") String uri, @QueryParam("type") String type) {
    XmlDocument d = null;
    try {
        if (type == null) {
            throw new Exception("uri and type parameters must be specified");
        }
        InspectionType inspectType = InspectionType.valueOf(type);
        XmlInspectionService s = new XmlInspectionService();
        switch(inspectType) {
            case INSTANCE:
                d = s.inspectXmlDocument(new File(uri));
                break;
            case SCHEMA:
                d = s.inspectSchema(new File(uri));
                break;
            default:
                throw new Exception("Unknown type specified: " + type);
        }
    } catch (Exception e) {
        LOG.error("Error inspecting xml: " + e.getMessage(), e);
    }
    return Response.ok().entity(toJson(d)).build();
}
Also used : InspectionType(io.atlasmap.xml.v2.InspectionType) XmlInspectionService(io.atlasmap.xml.inspect.XmlInspectionService) XmlDocument(io.atlasmap.xml.v2.XmlDocument) File(java.io.File) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) ApplicationPath(javax.ws.rs.ApplicationPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 15 with XmlDocument

use of io.atlasmap.xml.v2.XmlDocument in project atlasmap by atlasmap.

the class XmlSchemaInspectionMultipleNamespacesTest method testMultipleNamespaces.

@Test
public void testMultipleNamespaces() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/multiple-namespaces-schemaset.xml").toFile();
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument answer = service.inspectSchema(schemaFile);
    Assert.assertEquals(3, answer.getXmlNamespaces().getXmlNamespace().size());
    for (XmlNamespace namespace : answer.getXmlNamespaces().getXmlNamespace()) {
        switch(namespace.getAlias()) {
            case "tns":
                Assert.assertEquals("io.atlasmap.xml.test:Root", namespace.getUri());
                Assert.assertEquals(null, namespace.isTargetNamespace());
                break;
            case "first":
                Assert.assertEquals("io.atlasmap.xml.test:First", namespace.getUri());
                Assert.assertEquals(null, namespace.isTargetNamespace());
                break;
            case "second":
                Assert.assertEquals("io.atlasmap.xml.test:Second", namespace.getUri());
                Assert.assertEquals(null, namespace.isTargetNamespace());
                break;
            default:
                Assert.fail(String.format("Unknown alias '%s'", namespace.getAlias()));
        }
    }
    List<Field> fields = answer.getFields().getField();
    Assert.assertEquals(1, fields.size());
    XmlComplexType complex = XmlComplexType.class.cast(fields.get(0));
    Assert.assertEquals("tns:RootDocument", complex.getName());
    List<XmlField> rootFields = complex.getXmlFields().getXmlField();
    Assert.assertEquals(4, rootFields.size());
    for (XmlField xmlField : rootFields) {
        switch(xmlField.getName()) {
            case "tns:Name":
                Assert.assertEquals(FieldType.STRING, xmlField.getFieldType());
                Assert.assertEquals("/tns:RootDocument/tns:Name", xmlField.getPath());
                break;
            case "tns:Value":
                Assert.assertEquals(FieldType.STRING, xmlField.getFieldType());
                Assert.assertEquals("/tns:RootDocument/tns:Value", xmlField.getPath());
                break;
            case "first:FirstElement":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:RootDocument/first:FirstElement", xmlField.getPath());
                List<XmlField> firstFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(2, firstFields.size());
                for (XmlField firstField : firstFields) {
                    switch(firstField.getName()) {
                        case "first:Name":
                            Assert.assertEquals(FieldType.STRING, firstField.getFieldType());
                            Assert.assertEquals("/tns:RootDocument/first:FirstElement/first:Name", firstField.getPath());
                            break;
                        case "first:Value":
                            Assert.assertEquals(FieldType.STRING, firstField.getFieldType());
                            Assert.assertEquals("/tns:RootDocument/first:FirstElement/first:Value", firstField.getPath());
                            break;
                        default:
                            Assert.fail(String.format("Unknown field '%s'", firstField.getPath()));
                    }
                }
                break;
            case "second:SecondElement":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:RootDocument/second:SecondElement", xmlField.getPath());
                List<XmlField> secondFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(2, secondFields.size());
                for (XmlField secondField : secondFields) {
                    switch(secondField.getName()) {
                        case "second:Name":
                            Assert.assertEquals(FieldType.STRING, secondField.getFieldType());
                            Assert.assertEquals("/tns:RootDocument/second:SecondElement/second:Name", secondField.getPath());
                            break;
                        case "second:Value":
                            Assert.assertEquals(FieldType.STRING, secondField.getFieldType());
                            Assert.assertEquals("/tns:RootDocument/second:SecondElement/second:Value", secondField.getPath());
                            break;
                        default:
                            Assert.fail(String.format("Unknown field '%s'", secondField.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)

Aggregations

XmlDocument (io.atlasmap.xml.v2.XmlDocument)25 Test (org.junit.Test)22 XmlComplexType (io.atlasmap.xml.v2.XmlComplexType)20 XmlNamespace (io.atlasmap.xml.v2.XmlNamespace)10 File (java.io.File)10 XmlField (io.atlasmap.xml.v2.XmlField)8 Document (org.w3c.dom.Document)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Field (io.atlasmap.v2.Field)2 XmlInspectionService (io.atlasmap.xml.inspect.XmlInspectionService)2 XmlInspectionResponse (io.atlasmap.xml.v2.XmlInspectionResponse)2 IOException (java.io.IOException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 XSSchemaSet (com.sun.xml.xsom.XSSchemaSet)1 XSOMParser (com.sun.xml.xsom.parser.XSOMParser)1 DomAnnotationParserFactory (com.sun.xml.xsom.util.DomAnnotationParserFactory)1 Fields (io.atlasmap.v2.Fields)1 InspectionType (io.atlasmap.xml.v2.InspectionType)1 Restriction (io.atlasmap.xml.v2.Restriction)1 XmlFields (io.atlasmap.xml.v2.XmlFields)1