use of javax.xml.validation.Schema in project maven-plugins by apache.
the class DefaultChangesSchemaValidator method compileJAXPSchema.
/**
* @param uriSchema
* @return Schema
* @throws Exception
*/
private Schema compileJAXPSchema(String uriSchema) throws IOException, SAXException, NullPointerException {
InputStream in = null;
try {
in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uriSchema);
if (in == null) {
throw new NullPointerException(" impossible to load schema with path " + uriSchema);
}
//newInstance de SchemaFactory not ThreadSafe
final Schema schema = SchemaFactory.newInstance(W3C_XML_SCHEMA).newSchema(new StreamSource(in));
in.close();
in = null;
return schema;
} finally {
IOUtil.close(in);
}
}
use of javax.xml.validation.Schema in project oxCore by GluuFederation.
the class FacesConfigPopulator method isValidFacesConfig.
/**
* Validates *.faces-config.xml file
*
* @param xml
* @return
*/
private boolean isValidFacesConfig(InputStream xml) {
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setResourceResolver((LSResourceResolver) DbfFactory.FACES_ENTITY_RESOLVER);
InputStream xsd = this.getClass().getResourceAsStream(FACES_2_2_XSD);
Schema schema = factory.newSchema(new StreamSource(xsd));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xml));
return true;
} catch (Exception ex) {
return false;
}
}
use of javax.xml.validation.Schema in project midpoint by Evolveum.
the class TestMisc method test200ExportUsers.
@Test
public void test200ExportUsers() throws Exception {
final String TEST_NAME = "test200ExportUsers";
TestUtil.displayTestTile(this, TEST_NAME);
// GIVEN
Task task = taskManager.createTaskInstance(TestMisc.class.getName() + "." + TEST_NAME);
OperationResult result = task.getResult();
// WHEN
List<PrismObject<UserType>> users = modelService.searchObjects(UserType.class, null, SelectorOptions.createCollection(new ItemPath(), GetOperationOptions.createRaw()), task, result);
// THEN
result.computeStatus();
display("Search users result", result);
TestUtil.assertSuccess(result);
assertEquals("Unexpected number of users", 5, users.size());
for (PrismObject<UserType> user : users) {
display("Exporting user", user);
assertNotNull("Null definition in " + user, user.getDefinition());
display("Definition", user.getDefinition());
String xmlString = prismContext.serializeObjectToString(user, PrismContext.LANG_XML);
display("Exported user", xmlString);
Document xmlDocument = DOMUtil.parseDocument(xmlString);
Schema javaxSchema = prismContext.getSchemaRegistry().getJavaxSchema();
Validator validator = javaxSchema.newValidator();
validator.setResourceResolver(prismContext.getEntityResolver());
validator.validate(new DOMSource(xmlDocument));
PrismObject<Objectable> parsedUser = prismContext.parseObject(xmlString);
assertTrue("Re-parsed user is not equal to original: " + user, user.equals(parsedUser));
}
}
use of javax.xml.validation.Schema in project uPortal by Jasig.
the class BaseXsltDataUpgraderTest method testXsltUpgrade.
protected void testXsltUpgrade(final Resource xslResource, final PortalDataKey dataKey, final Resource inputResource, final Resource expectedResultResource, final Resource xsdResource) throws Exception {
final XmlUtilities xmlUtilities = new XmlUtilitiesImpl() {
@Override
public Templates getTemplates(Resource stylesheet) throws TransformerConfigurationException, IOException {
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
return transformerFactory.newTemplates(new StreamSource(stylesheet.getInputStream()));
}
};
final XsltDataUpgrader xsltDataUpgrader = new XsltDataUpgrader();
xsltDataUpgrader.setPortalDataKey(dataKey);
xsltDataUpgrader.setXslResource(xslResource);
xsltDataUpgrader.setXmlUtilities(xmlUtilities);
xsltDataUpgrader.afterPropertiesSet();
//Create XmlEventReader (what the JaxbPortalDataHandlerService has)
final XMLInputFactory xmlInputFactory = xmlUtilities.getXmlInputFactory();
final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(inputResource.getInputStream());
final Node sourceNode = xmlUtilities.convertToDom(xmlEventReader);
final DOMSource source = new DOMSource(sourceNode);
final DOMResult result = new DOMResult();
xsltDataUpgrader.upgradeData(source, result);
//XSD Validation
final String resultString = XmlUtilitiesImpl.toString(result.getNode());
if (xsdResource != null) {
final Schema schema = this.loadSchema(new Resource[] { xsdResource }, XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Validator validator = schema.newValidator();
try {
validator.validate(new StreamSource(new StringReader(resultString)));
} catch (Exception e) {
throw new XmlTestException("Failed to validate XSLT output against provided XSD", resultString, e);
}
}
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setNormalizeWhitespace(true);
try {
Diff d = new Diff(new InputStreamReader(expectedResultResource.getInputStream()), new StringReader(resultString));
assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
} catch (Exception e) {
throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", resultString, e);
} catch (Error e) {
throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", resultString, e);
}
}
use of javax.xml.validation.Schema in project webservices-axiom by apache.
the class ValidateSample method validate.
// START SNIPPET: sax
public void validate(InputStream in, URL schemaUrl) throws Exception {
SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(in, "UTF-8");
SOAPEnvelope envelope = builder.getSOAPEnvelope();
OMElement bodyContent = envelope.getBody().getFirstElement();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaUrl);
Validator validator = schema.newValidator();
validator.validate(bodyContent.getSAXSource(true));
}
Aggregations