use of javax.xml.validation.Validator 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.Validator 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.Validator 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.Validator in project oxCore by GluuFederation.
the class XMLValidator method validateMetadata.
/**
* @param stream
* @param validationSchema
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @return GluuErrorHandler
*/
public static GluuErrorHandler validateMetadata(InputStream stream, Schema validationSchema) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
// Fix XXE vulnerability
newFactory.setXIncludeAware(false);
newFactory.setExpandEntityReferences(false);
newFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
newFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
newFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
newFactory.setCoalescing(false);
newFactory.setExpandEntityReferences(true);
newFactory.setIgnoringComments(false);
newFactory.setIgnoringElementContentWhitespace(false);
newFactory.setNamespaceAware(true);
newFactory.setValidating(false);
DocumentBuilder xmlParser = newFactory.newDocumentBuilder();
Document xmlDoc = xmlParser.parse(stream);
Validator validator = validationSchema.newValidator();
GluuErrorHandler handler = new GluuErrorHandler();
validator.setErrorHandler(handler);
validator.validate(new DOMSource(xmlDoc));
return handler;
}
use of javax.xml.validation.Validator 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