use of javax.xml.validation.Schema in project openblocks by mikaelhg.
the class WorkspaceController method validate.
/**
* Validates the code blocks document against the schema
* @param document The document to check
* @throws RuntimeException If the validation failed
*/
private void validate(Document document) {
try {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaUrl = ClassLoader.getSystemResource("edu/mit/blocks/codeblocks/codeblocks.xsd");
Schema schema = schemaFactory.newSchema(schemaUrl);
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of javax.xml.validation.Schema in project orientdb by orientechnologies.
the class StopSAXParser method parse.
/**
* Parse the persistence.xml files referenced by the URLs in the collection
*
* @param persistenceXml
* @return A collection of parsed persistence units.
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
public static Collection<? extends PersistenceUnitInfo> parse(URL persistenceXml) {
InputStream is = null;
try {
// Buffer the InputStream so we can mark it, though we'll be in
// trouble if we have to read more than 8192 characters before finding
// the schema!
is = new BufferedInputStream(persistenceXml.openStream());
JPAVersion jpaVersion = getSchemaVersion(is);
Schema schema = getSchema(jpaVersion);
if (schema == null) {
throw new PersistenceException("Schema is unknown");
}
// Get back to the beginning of the stream
is = new BufferedInputStream(persistenceXml.openStream());
parserFactory.setNamespaceAware(true);
int endIndex = persistenceXml.getPath().length() - PERSISTENCE_XML_BASE_NAME.length();
URL persistenceXmlRoot = new URL("file://" + persistenceXml.getFile().substring(0, endIndex));
return getPersistenceUnits(is, persistenceXmlRoot, jpaVersion);
} catch (Exception e) {
throw new PersistenceException("Something goes wrong while parsing persistence.xml", e);
} finally {
if (is != null)
try {
is.close();
} catch (IOException e) {
// No logging necessary, just consume
}
}
}
use of javax.xml.validation.Schema in project camel by apache.
the class XAdESSignaturePropertiesTest method validateAgainstSchema.
private void validateAgainstSchema(Document doc) throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schema1 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/XAdES.xsd"));
Source schema2 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/xmldsig-core-schema.xsd"));
Schema schema = factory.newSchema(new Source[] { schema2, schema1 });
Validator validator = schema.newValidator();
validator.validate(new DOMSource(doc));
}
use of javax.xml.validation.Schema in project camel by apache.
the class XmlSignerProcessor method getSchemaForSigner.
protected Schema getSchemaForSigner(Message message, ValidatorErrorHandler errorHandler) throws XmlSignatureException, SAXException, IOException {
Schema schema;
String schemaResourceUri = getSchemaResourceUri(message);
if (schemaResourceUri == null) {
schema = null;
} else {
schema = getSchema(message);
}
return schema;
}
use of javax.xml.validation.Schema in project camel by apache.
the class XmlSignerProcessor method getMessageBodyNode.
protected Node getMessageBodyNode(Message message) throws Exception {
//NOPMD
InputStream is = message.getMandatoryBody(InputStream.class);
Boolean isPlainText = isPlainText(message);
Node node;
if (isPlainText != null && isPlainText) {
node = getTextNode(message, is);
} else {
ValidatorErrorHandler errorHandler = new DefaultValidationErrorHandler();
Schema schema = getSchemaForSigner(message, errorHandler);
Document doc = parseInput(is, getConfiguration().getDisallowDoctypeDecl(), schema, errorHandler);
// throws ValidationException
errorHandler.handleErrors(message.getExchange(), schema, null);
node = doc.getDocumentElement();
LOG.debug("Root element of document to be signed: {}", node);
}
return node;
}
Aggregations