use of javax.xml.validation.Schema in project head by mifos.
the class ChartOfAccountsConfig method load.
/**
* Factory method which loads the Chart of Accounts configuration from the
* given filename. Given XML filename will be validated against
* {@link FilePaths#CHART_OF_ACCOUNTS_SCHEMA}.
*
* @param chartOfAccountsXml
* a relative path to the Chart of Accounts configuration file.
*/
public static ChartOfAccountsConfig load(String chartOfAccountsXml) throws ConfigurationException {
ChartOfAccountsConfig instance = null;
Document document = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = dbf.newDocumentBuilder();
if (FilePaths.CHART_OF_ACCOUNTS_DEFAULT.equals(chartOfAccountsXml)) {
// default chart of accounts
document = parser.parse(MifosResourceUtil.getClassPathResourceAsStream(chartOfAccountsXml));
} else {
// custom chart of accounts
document = parser.parse(MifosResourceUtil.getFile(chartOfAccountsXml));
}
// create a SchemaFactory capable of understanding XML schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load an XML schema
ClassPathResource schemaFileResource = new ClassPathResource(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
Source schemaFile = null;
if (schemaFileResource.exists()) {
InputStream in = ChartOfAccountsConfig.class.getClassLoader().getResourceAsStream(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
schemaFile = new StreamSource(in);
} else {
schemaFile = new StreamSource(MifosResourceUtil.getFile(FilePaths.CHART_OF_ACCOUNTS_SCHEMA));
}
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance and validate document
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
} catch (IOException e) {
throw new ConfigurationException(e);
} catch (SAXException e) {
throw new ConfigurationException(e);
} catch (ParserConfigurationException e) {
throw new ConfigurationException(e);
}
instance = new ChartOfAccountsConfig();
instance.coaDocument = document;
return instance;
}
use of javax.xml.validation.Schema in project hibernate-orm by hibernate.
the class XmlParserHelper method getSchema.
public Schema getSchema(String schemaResource) throws XmlParsingException {
Schema schema = SCHEMA_CACHE.get(schemaResource);
if (schema != null) {
return schema;
}
schema = loadSchema(schemaResource);
Schema previous = SCHEMA_CACHE.putIfAbsent(schemaResource, schema);
return previous != null ? previous : schema;
}
use of javax.xml.validation.Schema in project hibernate-orm by hibernate.
the class XmlParserHelper method loadSchema.
private Schema loadSchema(String schemaName) throws XmlParsingException {
Schema schema = null;
URL schemaUrl = this.getClass().getClassLoader().getResource(schemaName);
if (schemaUrl == null) {
return schema;
}
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
schema = sf.newSchema(schemaUrl);
} catch (SAXException e) {
throw new XmlParsingException("Unable to create schema for " + schemaName + ": " + e.getMessage(), e);
}
return schema;
}
use of javax.xml.validation.Schema in project hibernate-orm by hibernate.
the class JpaDescriptorParser method loadEntityMappings.
private void loadEntityMappings(Collection<String> mappingFileNames) {
for (String mappingFile : mappingFileNames) {
InputStream stream = xmlParserHelper.getInputStreamForResource(mappingFile);
if (stream == null) {
continue;
}
EntityMappings mapping = null;
try {
Schema schema = xmlParserHelper.getSchema(ORM_SCHEMA);
mapping = xmlParserHelper.getJaxbRoot(stream, EntityMappings.class, schema);
} catch (XmlParsingException e) {
context.logMessage(Diagnostic.Kind.WARNING, "Unable to parse " + mappingFile + ": " + e.getMessage());
}
if (mapping != null) {
entityMappings.add(mapping);
}
try {
stream.close();
} catch (IOException e) {
// eat it
}
}
}
use of javax.xml.validation.Schema in project hibernate-orm by hibernate.
the class JpaDescriptorParser method getPersistence.
private Persistence getPersistence() {
Persistence persistence = null;
String persistenceXmlLocation = context.getPersistenceXmlLocation();
InputStream stream = xmlParserHelper.getInputStreamForResource(persistenceXmlLocation);
if (stream == null) {
return null;
}
try {
Schema schema = xmlParserHelper.getSchema(PERSISTENCE_SCHEMA);
persistence = xmlParserHelper.getJaxbRoot(stream, Persistence.class, schema);
} catch (XmlParsingException e) {
context.logMessage(Diagnostic.Kind.WARNING, "Unable to parse persistence.xml: " + e.getMessage());
}
try {
stream.close();
} catch (IOException e) {
// eat it
}
return persistence;
}
Aggregations