Search in sources :

Example 71 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project ORCID-Source by ORCID.

the class JpaJaxbEntityAdapterToOrcidProfileTest method validateAgainstSchema.

private void validateAgainstSchema(OrcidMessage orcidMessage) throws SAXException, IOException {
    //We need to manually remove the visibility from the given and family names to match the schema
    removeVisibilityAttributeFromNames(orcidMessage);
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = factory.newSchema(getClass().getResource("/orcid-message-" + OrcidMessage.DEFAULT_VERSION + ".xsd"));
    Validator validator = schema.newValidator();
    validator.validate(orcidMessage.toSource());
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) Validator(javax.xml.validation.Validator)

Example 72 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project ORCID-Source by ORCID.

the class ValidateV2RC4SamplesTest method unmarshall.

private Object unmarshall(Reader reader, Class<?> type, String schemaPath) throws SAXException, URISyntaxException {
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        if (schemaPath != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(getClass().getResource(schemaPath).toURI()));
            unmarshaller.setSchema(schema);
        }
        return unmarshaller.unmarshal(reader);
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 73 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project ORCID-Source by ORCID.

the class ValidateV2RC3SamplesTest method unmarshall.

private Object unmarshall(Reader reader, Class<?> type, String schemaPath) throws SAXException, URISyntaxException {
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        if (schemaPath != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(getClass().getResource(schemaPath).toURI()));
            unmarshaller.setSchema(schema);
        }
        return unmarshaller.unmarshal(reader);
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 74 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project opennms by OpenNMS.

the class JAXBTest method testMarshall.

@Test
public void testMarshall() throws Exception {
    final String expectedXML = "" + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<component xmlns=\"http://xmlns.opennms.org/xsd/model/ncs\" type=\"Service\" foreignId=\"123\" foreignSource=\"NA-Service\">\n" + "    <name>CokeP2P</name>\n" + "    <component type=\"ServiceElement\" foreignId=\"8765\" foreignSource=\"NA-ServiceElement\">\n" + "        <name>PE1,SE1</name>\n" + "        <node foreignId=\"1111-PE1\" foreignSource=\"space\"/>\n" + "        <component type=\"ServiceElementComponent\" foreignId=\"8765,jnxVpnIf\" foreignSource=\"NA-SvcElemComp\">\n" + "            <name>jnxVpnIf</name>\n" + "            <upEventUei>uei.opennms.org/vendor/Juniper/traps/jnxVpnIfUp</upEventUei>\n" + "            <downEventUei>uei.opennms.org/vendor/Juniper/traps/jnxVpnIfDown</downEventUei>\n" + "            <attributes>\n" + "                <attribute>\n" + "                    <key>jnxVpnIfVpnType</key>\n" + "                    <value>5</value>\n" + "                </attribute>\n" + "                <attribute>\n" + "                    <key>jnxVpnIfVpnName</key>\n" + "                    <value>ge-1/0/2.50</value>\n" + "                </attribute>\n" + "            </attributes>\n" + "            <component type=\"ServiceElementComponent\" foreignId=\"8765,link\" foreignSource=\"NA-SvcElemComp\">\n" + "                <name>link</name>\n" + "                <upEventUei>uei.opennms.org/vendor/Juniper/traps/linkUp</upEventUei>\n" + "                <downEventUei>uei.opennms.org/vendor/Juniper/traps/linkDown</downEventUei>\n" + "                <attributes>\n" + "                    <attribute>\n" + "                        <key>linkName</key>\n" + "                        <value>ge-1/0/2</value>\n" + "                    </attribute>\n" + "                </attributes>\n" + "            </component>\n" + "        </component>\n" + "        <component type=\"ServiceElementComponent\" foreignId=\"8765,jnxVpnPw-vcid(50)\" foreignSource=\"NA-SvcElemComp\">\n" + "            <name>jnxVpnPw-vcid(50)</name>\n" + "            <upEventUei>uei.opennms.org/vendor/Juniper/traps/jnxVpnPwUp</upEventUei>\n" + "            <downEventUei>uei.opennms.org/vendor/Juniper/traps/jnxVpnPwDown</downEventUei>\n" + "            <dependenciesRequired>ANY</dependenciesRequired>\n" + "            <attributes>\n" + "                <attribute>\n" + "                    <key>jnxVpnPwVpnType</key>\n" + "                    <value>5</value>\n" + "                </attribute>\n" + "                <attribute>\n" + "                    <key>jnxVpnPwVpnName</key>\n" + "                    <value>ge-1/0/2.50</value>\n" + "                </attribute>\n" + "            </attributes>\n" + "            <component type=\"ServiceElementComponent\" foreignId=\"8765,lspA-PE1-PE2\" foreignSource=\"NA-SvcElemComp\">\n" + "                <name>lspA-PE1-PE2</name>\n" + "                <upEventUei>uei.opennms.org/vendor/Juniper/traps/mplsLspPathUp</upEventUei>\n" + "                <downEventUei>uei.opennms.org/vendor/Juniper/traps/mplsLspPathDown</downEventUei>\n" + "                <attributes>\n" + "                    <attribute>\n" + "                        <key>mplsLspName</key>\n" + "                        <value>lspA-PE1-PE2</value>\n" + "                    </attribute>\n" + "                </attributes>\n" + "            </component>\n" + "            <component type=\"ServiceElementComponent\" foreignId=\"8765,lspB-PE1-PE2\" foreignSource=\"NA-SvcElemComp\">\n" + "                <name>lspB-PE1-PE2</name>\n" + "                <upEventUei>uei.opennms.org/vendor/Juniper/traps/mplsLspPathUp</upEventUei>\n" + "                <downEventUei>uei.opennms.org/vendor/Juniper/traps/mplsLspPathDown</downEventUei>\n" + "                <attributes>\n" + "                    <attribute>\n" + "                        <key>mplsLspName</key>\n" + "                        <value>lspB-PE1-PE2</value>\n" + "                    </attribute>\n" + "                </attributes>\n" + "            </component>\n" + "        </component>\n" + "    </component>\n" + "    <component type=\"ServiceElement\" foreignId=\"9876\" foreignSource=\"NA-ServiceElement\">\n" + "        <name>PE2,SE1</name>\n" + "        <node foreignId=\"2222-PE2\" foreignSource=\"space\"/>\n" + "        <component type=\"ServiceElementComponent\" foreignId=\"9876,jnxVpnIf\" foreignSource=\"NA-SvcElemComp\">\n" + "            <name>jnxVpnIf</name>\n" + "            <upEventUei>uei.opennms.org/vendor/Juniper/traps/jnxVpnIfUp</upEventUei>\n" + "            <downEventUei>uei.opennms.org/vendor/Juniper/traps/jnxVpnIfDown</downEventUei>\n" + "            <attributes>\n" + "                <attribute>\n" + "                    <key>jnxVpnIfVpnType</key>\n" + "                    <value>5</value>\n" + "                </attribute>\n" + "                <attribute>\n" + "                    <key>jnxVpnIfVpnName</key>\n" + "                    <value>ge-3/1/4.50</value>\n" + "                </attribute>\n" + "            </attributes>\n" + "            <component type=\"ServiceElementComponent\" foreignId=\"9876,link\" foreignSource=\"NA-SvcElemComp\">\n" + "                <name>link</name>\n" + "                <upEventUei>uei.opennms.org/vendor/Juniper/traps/linkUp</upEventUei>\n" + "                <downEventUei>uei.opennms.org/vendor/Juniper/traps/linkDown</downEventUei>\n" + "                <attributes>\n" + "                    <attribute>\n" + "                        <key>linkName</key>\n" + "                        <value>ge-3/1/4</value>\n" + "                    </attribute>\n" + "                </attributes>\n" + "            </component>\n" + "        </component>\n" + "        <component type=\"ServiceElementComponent\" foreignId=\"9876,jnxVpnPw-vcid(50)\" foreignSource=\"NA-SvcElemComp\">\n" + "            <name>jnxVpnPw-vcid(50)</name>\n" + "            <upEventUei>uei.opennms.org/vendor/Juniper/traps/jnxVpnPwUp</upEventUei>\n" + "            <downEventUei>uei.opennms.org/vendor/Juniper/traps/jnxVpnPwDown</downEventUei>\n" + "            <dependenciesRequired>ANY</dependenciesRequired>\n" + "            <attributes>\n" + "                <attribute>\n" + "                    <key>jnxVpnPwVpnType</key>\n" + "                    <value>5</value>\n" + "                </attribute>\n" + "                <attribute>\n" + "                    <key>jnxVpnPwVpnName</key>\n" + "                    <value>ge-3/1/4.50</value>\n" + "                </attribute>\n" + "            </attributes>\n" + "            <component type=\"ServiceElementComponent\" foreignId=\"9876,lspA-PE2-PE1\" foreignSource=\"NA-SvcElemComp\">\n" + "                <name>lspA-PE2-PE1</name>\n" + "                <upEventUei>uei.opennms.org/vendor/Juniper/traps/mplsLspPathUp</upEventUei>\n" + "                <downEventUei>uei.opennms.org/vendor/Juniper/traps/mplsLspPathDown</downEventUei>\n" + "                <attributes>\n" + "                    <attribute>\n" + "                        <key>mplsLspName</key>\n" + "                        <value>lspA-PE2-PE1</value>\n" + "                    </attribute>\n" + "                </attributes>\n" + "            </component>\n" + "            <component type=\"ServiceElementComponent\" foreignId=\"9876,lspB-PE2-PE1\" foreignSource=\"NA-SvcElemComp\">\n" + "                <name>lspB-PE2-PE1</name>\n" + "                <upEventUei>uei.opennms.org/vendor/Juniper/traps/mplsLspPathUp</upEventUei>\n" + "                <downEventUei>uei.opennms.org/vendor/Juniper/traps/mplsLspPathDown</downEventUei>\n" + "                <attributes>\n" + "                    <attribute>\n" + "                        <key>mplsLspName</key>\n" + "                        <value>lspB-PE2-PE1</value>\n" + "                    </attribute>\n" + "                </attributes>\n" + "            </component>\n" + "        </component>\n" + "    </component>\n" + "</component>\n" + "";
    NCSComponent svc = new NCSBuilder("Service", "NA-Service", "123").setName("CokeP2P").pushComponent("ServiceElement", "NA-ServiceElement", "8765").setName("PE1,SE1").setNodeIdentity("space", "1111-PE1").pushComponent("ServiceElementComponent", "NA-SvcElemComp", "8765,jnxVpnIf").setName("jnxVpnIf").setUpEventUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnIfUp").setDownEventUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnIfDown").setAttribute("jnxVpnIfVpnType", "5").setAttribute("jnxVpnIfVpnName", "ge-1/0/2.50").pushComponent("ServiceElementComponent", "NA-SvcElemComp", "8765,link").setName("link").setUpEventUei("uei.opennms.org/vendor/Juniper/traps/linkUp").setDownEventUei("uei.opennms.org/vendor/Juniper/traps/linkDown").setAttribute("linkName", "ge-1/0/2").popComponent().popComponent().pushComponent("ServiceElementComponent", "NA-SvcElemComp", "8765,jnxVpnPw-vcid(50)").setName("jnxVpnPw-vcid(50)").setUpEventUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnPwUp").setDownEventUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnPwDown").setAttribute("jnxVpnPwVpnType", "5").setAttribute("jnxVpnPwVpnName", "ge-1/0/2.50").setDependenciesRequired(DependencyRequirements.ANY).pushComponent("ServiceElementComponent", "NA-SvcElemComp", "8765,lspA-PE1-PE2").setName("lspA-PE1-PE2").setUpEventUei("uei.opennms.org/vendor/Juniper/traps/mplsLspPathUp").setDownEventUei("uei.opennms.org/vendor/Juniper/traps/mplsLspPathDown").setAttribute("mplsLspName", "lspA-PE1-PE2").popComponent().pushComponent("ServiceElementComponent", "NA-SvcElemComp", "8765,lspB-PE1-PE2").setName("lspB-PE1-PE2").setUpEventUei("uei.opennms.org/vendor/Juniper/traps/mplsLspPathUp").setDownEventUei("uei.opennms.org/vendor/Juniper/traps/mplsLspPathDown").setAttribute("mplsLspName", "lspB-PE1-PE2").popComponent().popComponent().popComponent().pushComponent("ServiceElement", "NA-ServiceElement", "9876").setName("PE2,SE1").setNodeIdentity("space", "2222-PE2").pushComponent("ServiceElementComponent", "NA-SvcElemComp", "9876,jnxVpnIf").setName("jnxVpnIf").setUpEventUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnIfUp").setDownEventUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnIfDown").setAttribute("jnxVpnIfVpnType", "5").setAttribute("jnxVpnIfVpnName", "ge-3/1/4.50").pushComponent("ServiceElementComponent", "NA-SvcElemComp", "9876,link").setName("link").setUpEventUei("uei.opennms.org/vendor/Juniper/traps/linkUp").setDownEventUei("uei.opennms.org/vendor/Juniper/traps/linkDown").setAttribute("linkName", "ge-3/1/4").popComponent().popComponent().pushComponent("ServiceElementComponent", "NA-SvcElemComp", "9876,jnxVpnPw-vcid(50)").setName("jnxVpnPw-vcid(50)").setUpEventUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnPwUp").setDownEventUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnPwDown").setAttribute("jnxVpnPwVpnType", "5").setAttribute("jnxVpnPwVpnName", "ge-3/1/4.50").setDependenciesRequired(DependencyRequirements.ANY).pushComponent("ServiceElementComponent", "NA-SvcElemComp", "9876,lspA-PE2-PE1").setName("lspA-PE2-PE1").setUpEventUei("uei.opennms.org/vendor/Juniper/traps/mplsLspPathUp").setDownEventUei("uei.opennms.org/vendor/Juniper/traps/mplsLspPathDown").setAttribute("mplsLspName", "lspA-PE2-PE1").popComponent().pushComponent("ServiceElementComponent", "NA-SvcElemComp", "9876,lspB-PE2-PE1").setName("lspB-PE2-PE1").setUpEventUei("uei.opennms.org/vendor/Juniper/traps/mplsLspPathUp").setDownEventUei("uei.opennms.org/vendor/Juniper/traps/mplsLspPathDown").setAttribute("mplsLspName", "lspB-PE2-PE1").popComponent().popComponent().popComponent().get();
    // Create a Marshaller
    JAXBContext context = JAXBContext.newInstance(NCSComponent.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    // save the output in a byte array
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // marshall the output
    marshaller.marshal(svc, out);
    // verify its matches the expected results
    byte[] utf8 = out.toByteArray();
    String result = new String(utf8, StandardCharsets.UTF_8);
    assertXmlEquals(expectedXML, result);
    System.err.println(result);
    // unmarshall the generated XML
    URL xsd = getClass().getResource("/ncs-model.xsd");
    assertNotNull(xsd);
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = schemaFactory.newSchema(xsd);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    unmarshaller.setSchema(schema);
    Source source = new StreamSource(new ByteArrayInputStream(utf8));
    NCSComponent read = unmarshaller.unmarshal(source, NCSComponent.class).getValue();
    assertNotNull(read);
    // round trip back to XML and make sure we get the same thing
    ByteArrayOutputStream reout = new ByteArrayOutputStream();
    marshaller.marshal(read, reout);
    String roundTrip = new String(reout.toByteArray(), StandardCharsets.UTF_8);
    assertXmlEquals(expectedXML, roundTrip);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Marshaller(javax.xml.bind.Marshaller) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 75 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project opennms by OpenNMS.

the class XmlTest method validateXmlString.

protected void validateXmlString(final String xml) throws Exception {
    if (getSchemaFile() == null) {
        LOG.warn("skipping validation, schema file not set");
        return;
    }
    final SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final File schemaFile = new File(getSchemaFile());
    LOG.debug("Validating using schema file: {}", schemaFile);
    final Schema schema = schemaFactory.newSchema(schemaFile);
    final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setValidating(true);
    saxParserFactory.setNamespaceAware(true);
    saxParserFactory.setSchema(schema);
    assertTrue("make sure our SAX implementation can validate", saxParserFactory.isValidating());
    final Validator validator = schema.newValidator();
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());
    final Source source = new StreamSource(inputStream);
    validator.validate(source);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) File(java.io.File) Validator(javax.xml.validation.Validator) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SchemaFactory (javax.xml.validation.SchemaFactory)92 Schema (javax.xml.validation.Schema)72 StreamSource (javax.xml.transform.stream.StreamSource)47 Validator (javax.xml.validation.Validator)39 SAXException (org.xml.sax.SAXException)29 Source (javax.xml.transform.Source)28 IOException (java.io.IOException)20 URL (java.net.URL)18 DOMSource (javax.xml.transform.dom.DOMSource)18 JAXBContext (javax.xml.bind.JAXBContext)17 File (java.io.File)16 InputStream (java.io.InputStream)16 InputSource (org.xml.sax.InputSource)14 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Unmarshaller (javax.xml.bind.Unmarshaller)11 Test (org.junit.Test)10 Document (org.w3c.dom.Document)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8