Example usage for javax.xml.validation SchemaFactory setProperty

List of usage examples for javax.xml.validation SchemaFactory setProperty

Introduction

In this page you can find the example usage for javax.xml.validation SchemaFactory setProperty.

Prototype

public void setProperty(String name, Object object) throws SAXNotRecognizedException, SAXNotSupportedException 

Source Link

Document

Set the value of a property.

Usage

From source file:ca.uhn.fhir.validation.SchemaBaseValidator.java

private Schema loadSchema(String theVersion, String theSchemaName) {
    String key = theVersion + "-" + theSchemaName;

    synchronized (myKeyToSchema) {
        Schema schema = myKeyToSchema.get(key);
        if (schema != null) {
            return schema;
        }/*from ww  w .  ja  va2  s.  c o  m*/

        Source baseSource = loadXml(null, theSchemaName);

        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schemaFactory.setResourceResolver(new MyResourceResolver());

        try {
            try {
                /*
                 * See https://github.com/jamesagnew/hapi-fhir/issues/339
                 * https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Processing
                 */
                schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
            } catch (SAXNotRecognizedException snex) {
                ourLog.warn("Jaxp 1.5 Support not found.", snex);
            }
            schema = schemaFactory.newSchema(new Source[] { baseSource });
        } catch (SAXException e) {
            throw new ConfigurationException("Could not load/parse schema file: " + theSchemaName, e);
        }
        myKeyToSchema.put(key, schema);
        return schema;
    }
}