use of javax.xml.validation.Schema 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());
}
use of javax.xml.validation.Schema 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);
}
}
use of javax.xml.validation.Schema 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);
}
}
use of javax.xml.validation.Schema 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);
}
use of javax.xml.validation.Schema 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);
}
Aggregations