use of javax.xml.validation.SchemaFactory in project OpenClinica by OpenClinica.
the class XmlSchemaValidationHelper method validateAgainstSchema.
public void validateAgainstSchema(String xml, InputStream xsdFile) {
try {
// parse an XML document into a DOM tree
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder parser = builderFactory.newDocumentBuilder();
Document document = parser.parse(new InputSource(new StringReader(xml)));
// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(xsdFile);
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance, which can be used to validate an
// instance document
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(document));
} catch (FileNotFoundException ex) {
throw new OpenClinicaSystemException("File was not found", ex.getCause());
} catch (IOException ioe) {
// ioe.printStackTrace();
throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
} catch (SAXParseException spe) {
// spe.printStackTrace();
throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
} catch (SAXException e) {
throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
} catch (ParserConfigurationException pce) {
throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
}
}
use of javax.xml.validation.SchemaFactory in project OpenClinica by OpenClinica.
the class XmlSchemaValidationHelper method validateAgainstSchema.
public void validateAgainstSchema(File xmlFile, File xsdFile) {
try {
// parse an XML document into a DOM tree
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder parser = builderFactory.newDocumentBuilder();
Document document = parser.parse(xmlFile);
// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(xsdFile);
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance, which can be used to validate an
// instance document
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(document));
} catch (FileNotFoundException ex) {
throw new OpenClinicaSystemException("File was not found", ex.getCause());
} catch (IOException ioe) {
throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
} catch (SAXParseException spe) {
//spe.printStackTrace();
throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
} catch (SAXException e) {
throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
} catch (ParserConfigurationException pce) {
throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
}
}
use of javax.xml.validation.SchemaFactory in project voltdb by VoltDB.
the class CatalogPasswordScrambler method getDeployment.
public static DeploymentType getDeployment(File sourceFH) {
try {
JAXBContext jc = JAXBContext.newInstance("org.voltdb.compiler.deploymentfile");
// This schema shot the sheriff.
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(DependencyPair.class.getResource("compiler/DeploymentFileSchema.xsd"));
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setSchema(schema);
@SuppressWarnings("unchecked") JAXBElement<DeploymentType> result = (JAXBElement<DeploymentType>) unmarshaller.unmarshal(sourceFH);
DeploymentType deployment = result.getValue();
return deployment;
} catch (JAXBException e) {
// Convert some linked exceptions to more friendly errors.
if (e.getLinkedException() instanceof java.io.FileNotFoundException) {
System.err.println(e.getLinkedException().getMessage());
return null;
} else if (e.getLinkedException() instanceof org.xml.sax.SAXParseException) {
System.err.println("Error schema validating deployment.xml file. " + e.getLinkedException().getMessage());
return null;
} else {
throw new RuntimeException(e);
}
} catch (SAXException e) {
System.err.println("Error schema validating deployment.xml file. " + e.getMessage());
return null;
}
}
use of javax.xml.validation.SchemaFactory in project jangaroo-tools by CoreMedia.
the class ExmlValidator method setupSAXParser.
private SAXParser setupSAXParser() throws IOException {
try {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
ExmlSchemaResolver exmlSchemaResolver = new ExmlSchemaResolver();
schemaFactory.setResourceResolver(exmlSchemaResolver);
List<Source> schemas = new ArrayList<Source>();
schemas.add(new StreamSource(getClass().getResourceAsStream(Exmlc.EXML_SCHEMA_LOCATION), "exml"));
schemas.add(new StreamSource(getClass().getResourceAsStream(Exmlc.EXML_UNTYPED_SCHEMA_LOCATION), "untyped"));
Collection<ExmlSchemaSource> exmlSchemaSources = exmlSchemaSourceByNamespace.values();
for (ExmlSchemaSource exmlSchemaSource : exmlSchemaSources) {
schemas.add(exmlSchemaSource.newStreamSource());
}
Schema exmlSchema = schemaFactory.newSchema(schemas.toArray(new Source[schemas.size()]));
final SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setNamespaceAware(true);
saxFactory.setSchema(exmlSchema);
SAXParser saxParser = saxFactory.newSAXParser();
saxParser.getXMLReader().setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
//To change body of implemented methods use File | Settings | File Templates.
return null;
}
});
return saxParser;
} catch (ParserConfigurationException e) {
throw new IllegalStateException("A default dom builder should be provided.", e);
} catch (SAXParseException e) {
// SAX parser error while parsing EXML schemas: log only, will cause error or warning, depending on configuration:
logSAXParseException(null, e, true);
return null;
} catch (SAXException e) {
throw new IllegalStateException("SAX parser does not support validation.", e);
}
}
use of javax.xml.validation.SchemaFactory in project jangaroo-tools by CoreMedia.
the class ValidateSchemas method validateUntypedSchema.
@Test
public void validateUntypedSchema() throws IOException, SAXException, URISyntaxException {
String schemaLang = "http://www.w3.org/2001/XMLSchema";
// get validation driver:
SchemaFactory factory = SchemaFactory.newInstance(schemaLang);
// create schema by reading it from an XSD file:
factory.newSchema(new StreamSource(getFile("/net/jangaroo/exml/schemas/untyped.xsd")));
}
Aggregations