use of javax.xml.validation.Schema in project languagetool by languagetool-org.
the class XMLValidator method getValidator.
private Validator getValidator(URL xmlSchema) throws SAXException {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(xmlSchema);
Validator validator = schema.newValidator();
validator.setErrorHandler(new ErrorHandler());
return validator;
}
use of javax.xml.validation.Schema in project head by mifos.
the class MenuParser method parse.
/**
* Method to parse xml and return crude menu
*
* @return array of crude Menu objects
*/
public static Menu[] parse() throws SystemException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
SchemaFactory schfactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schfactory.setErrorHandler(null);
Schema schema = schfactory.newSchema(new StreamSource(MifosResourceUtil.getClassPathResourceAsStream(FilePaths.MENUSCHEMA)));
factory.setNamespaceAware(false);
factory.setValidating(false);
factory.setSchema(schema);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(MifosResourceUtil.getClassPathResourceAsStream(FilePaths.MENUPATH));
NodeList tabNodeList = document.getElementsByTagName(MenuConstants.TOPMENUTAB);
Menu[] leftMenus = new Menu[tabNodeList.getLength()];
for (int i = 0; i < tabNodeList.getLength(); i++) {
leftMenus[i] = new Menu();
leftMenus[i].setTopMenuTabName(((Element) tabNodeList.item(i)).getAttribute(MenuConstants.NAME));
leftMenus[i].setMenuGroups(createMenuGroup(tabNodeList.item(i)));
String menuHeading = ((Element) tabNodeList.item(i)).getElementsByTagName(MenuConstants.LEFTMENULABEL).item(0).getFirstChild().getTextContent().trim();
leftMenus[i].setMenuHeading(menuHeading);
}
return leftMenus;
} catch (SAXParseException spe) {
throw new MenuParseException(spe);
} catch (SAXException sxe) {
throw new MenuParseException(sxe);
} catch (ParserConfigurationException pce) {
throw new MenuParseException(pce);
} catch (IOException ioe) {
throw new MenuParseException(ioe);
}
}
use of javax.xml.validation.Schema in project head by mifos.
the class TableTagParser method parser.
public Table parser(String file) throws TableTagParseException {
Table table = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
SchemaFactory schfactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schfactory.setErrorHandler(null);
Schema schema = schfactory.newSchema(new StreamSource(MifosResourceUtil.getClassPathResourceAsURIString(FilePaths.CUSTOMTABLETAGXSD)));
factory.setNamespaceAware(false);
factory.setValidating(false);
factory.setSchema(schema);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(null);
Document document = builder.parse(MifosResourceUtil.getClassPathResourceAsURIString(file));
table = createTable(document);
} catch (Exception e) {
throw new TableTagParseException(e);
}
return table;
}
use of javax.xml.validation.Schema in project OpenAttestation by OpenAttestation.
the class XML method parseDocumentElement.
/**
* Example schema locations:
* http://docs.oasis-open.org/security/saml/v2.0/saml-schema-protocol-2.0.xsd
* http://docs.oasis-open.org/security/saml/v2.0/saml-schema-assertion-2.0.xsd
* http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/xenc-schema.xsd
* http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd
*
* @param xml
* @return
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public Element parseDocumentElement(String xml) throws ParserConfigurationException, SAXException, IOException {
ClasspathResourceResolver resolver = new ClasspathResourceResolver();
resolver.setResourcePackage(schemaPackageName);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setResourceResolver(resolver);
Source[] schemaSources = new Source[schemaLocations.size()];
int i = 0;
InputStream inStream = null;
Schema schema;
try {
for (String schemaLocation : schemaLocations) {
inStream = resolver.findResource(schemaLocation);
// if(in == null) log.debug("parseDocumentElement - InputStream is NULL");
// else log.debug("parseDocumentElement - InputStream OK");
schemaSources[i] = new StreamSource(inStream);
log.debug("parseDocumentElement - schemaSources[" + i + "] :" + schemaSources[i].getSystemId());
i++;
}
// if(schemaSources!=null) log.debug("parseDocumentElement - schemaSources.length: " + schemaSources.length);
// else log.debug("parseDocumentElement - schemaSources is null");
schema = schemaFactory.newSchema(schemaSources);
} catch (Exception e) {
schema = null;
log.error(e.getMessage());
} finally {
if (inStream != null)
inStream.close();
}
// Validator validator = schema.newValidator();
// validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes())));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
// bug #1038 prevent XXE
factory.setExpandEntityReferences(false);
// bug #1038 prevent XXE
factory.setXIncludeAware(false);
// fix for javax.xml.crypto.dsig.XMLSignatureException: javax.xml.crypto.URIReferenceException: com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID HostTrustAssertion
factory.setSchema(schema);
// ParserConfigurationException
DocumentBuilder builder = factory.newDocumentBuilder();
try (ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes())) {
// SAXException, IOException
Element document = builder.parse(in).getDocumentElement();
return document;
}
}
use of javax.xml.validation.Schema in project honeycomb by altamiracorp.
the class ConfigurationParser method checkValidConfig.
/**
* Performs validation on the configuration content supplied by the
* configuration supplier against the schema document provided by the
* validation supplier. Throws Runtime exception if validation fails.
*
* @param configSupplier The supplier that provides the configuration to inspect, not null
* @param schemaSupplier The supplier that provides the schema used to inspect the configuration, not null
*/
private static void checkValidConfig(final InputSupplier<? extends InputStream> configSupplier, final InputSupplier<? extends InputStream> schemaSupplier) {
try {
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Schema schema = schemaFactory.newSchema(new StreamSource(schemaSupplier.getInput()));
final Validator validator = schema.newValidator();
validator.validate(new StreamSource(configSupplier.getInput()));
} catch (Exception e) {
logger.error("Unable to validate honeycomb configuration.", e);
throw new RuntimeException("Exception while validating honeycomb configuration.", e);
}
}
Aggregations