Java Code Examples for javax.xml.validation.SchemaFactory#setFeature()

The following examples show how to use javax.xml.validation.SchemaFactory#setFeature() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MCRDOIBaseService.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
protected void initCommonProperties() {
    setUsername(requireNotEmptyProperty(CONFIG_USER_NAME));
    setPassword(requireNotEmptyProperty(CONFIG_PASSWORD));
    setTransformerID(requireNotEmptyProperty(CONFIG_TRANSFORMER));

    final MCRContentTransformer transformer = getTransformer();

    final Map<String, String> properties = getProperties();
    final String schemaURL = properties.getOrDefault(CONFIG_SCHEMA, getDefaultSchemaPath());
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schemaFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);

        URL localSchemaURL = MCRClassTools.getClassLoader().getResource(schemaURL);
        if (localSchemaURL == null) {
            throw new MCRConfigurationException(schemaURL + " was not found!");
        }
        setSchema(schemaFactory.newSchema(localSchemaURL));
    } catch (SAXException e) {
        throw new MCRConfigurationException("Error while loading " + getServiceID() + " schema!", e);
    }
}
 
Example 2
Source File: CatalogSupportBase.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void testValidation(boolean setUseCatalog, boolean useCatalog, String catalog,
        String xsd, LSResourceResolver resolver)
        throws Exception {

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    // use resolver or catalog if resolver = null
    if (resolver != null) {
        factory.setResourceResolver(resolver);
    }
    if (setUseCatalog) {
        factory.setFeature(XMLConstants.USE_CATALOG, useCatalog);
    }
    factory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);

    Schema schema = factory.newSchema(new StreamSource(new StringReader(xsd)));
    success("XMLSchema.dtd and datatypes.dtd are resolved.");
}
 
Example 3
Source File: Bug7143711Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testValidation_SAX_withSM() {
    System.out.println("Validation using SAX Source with security manager:");
    setSystemProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");

    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // should not allow
        factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, true);
        if ((boolean) factory.getFeature(ORACLE_FEATURE_SERVICE_MECHANISM)) {
            Assert.fail("should not override in secure mode");
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());

    } finally {
        clearSystemProperty(SAX_FACTORY_ID);
    }
}
 
Example 4
Source File: MCRTEIValidator.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private Schema getSchema(String path) throws SAXException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);
    schemaFactory.setErrorHandler(this);
    return schemaFactory.newSchema(MCRTEIValidator.class.getClassLoader().getResource(
        path));
}
 
Example 5
Source File: MCRDOIServiceTest.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private void loadSchema(String schemaURL) throws SAXException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);

    URL localSchemaURL = MCRClassTools.getClassLoader().getResource(schemaURL);
    if (localSchemaURL == null) {
        throw new MCRConfigurationException(schemaURL + " was not found!");
    }
    schemaFactory.newSchema(localSchemaURL);
}
 
Example 6
Source File: XmlUtil.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * We offer parameters for artemisInstance and artemisHome as they could be coming from the CLI or Maven Plugin
 */
public static <T> T decode(Class<T> clazz,
                           File configuration,
                           String artemisHome,
                           String artemisInstance,
                           URI artemisURIInstance) throws Exception {
   JAXBContext jaxbContext = JAXBContext.newInstance("org.apache.activemq.artemis.dto");

   Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
   SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
   sf.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);
   InputStream xsdStream = XmlUtil.class.getClassLoader().getResourceAsStream("org.apache.activemq/dto/activemq.xsd");
   StreamSource xsdSource = new StreamSource(xsdStream);
   Schema schema = sf.newSchema(xsdSource);
   unmarshaller.setSchema(schema);

   Properties props = new Properties(System.getProperties());
   if (artemisHome != null) {
      props.put("artemis.home", artemisHome);
   }

   if (artemisInstance != null) {
      props.put("artemis.instance", artemisInstance);
   }

   if (artemisURIInstance != null) {
      props.put("artemis.URI.instance", artemisURIInstance.toString());
   }

   XMLStreamReader reader = factory.createXMLStreamReader(new FileInputStream(configuration));

   reader = new PropertiesFilter(reader, props);

   return clazz.cast(unmarshaller.unmarshal(reader));
}
 
Example 7
Source File: CatalogSupportBase.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verifies Catalog Support for the Validator.
 * @param setUseCatalog1 a flag to indicate whether USE_CATALOG shall be set
 * on the factory.
 * @param setUseCatalog2 a flag to indicate whether USE_CATALOG shall be set
 * on the Validator.
 * @param source  the XML source
 * @param resolver1 a resolver to be set on the factory if specified
 * @param resolver2 a resolver to be set on the Validator if specified
 * @param catalog1 a catalog to be set on the factory if specified
 * @param catalog2 a catalog to be set on the Validator if specified
 */
public void testValidator(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog,
        Source source, LSResourceResolver resolver1, LSResourceResolver resolver2,
        String catalog1, String catalog2)
        throws Exception {

        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        if (setUseCatalog1) {
            schemaFactory.setFeature(XMLConstants.USE_CATALOG, useCatalog);
        }
        if (catalog1 != null) {
            schemaFactory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog1);
        }
        if (resolver1 != null) {
            schemaFactory.setResourceResolver(resolver1);
        }

        Schema schema = schemaFactory.newSchema();
        Validator validator = schema.newValidator();
        if (setUseCatalog2) {
            validator.setFeature(XMLConstants.USE_CATALOG, useCatalog);
        }
        if (catalog2 != null) {
            validator.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog2);
        }
        if (resolver2 != null) {
            validator.setResourceResolver(resolver2);
        }
        validator.validate(source);
}
 
Example 8
Source File: Bug6941169Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testValidation_SAX_withoutServiceMech() {
    System.out.println("Validation using SAX Source;  Service mechnism is turned off;  SAX Impl should be the default:");
    InputSource is = new InputSource(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
    SAXSource ss = new SAXSource(is);
    setSystemProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");
    long start = System.currentTimeMillis();
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
        Schema schema = factory.newSchema(new StreamSource(_xsd));
        Validator validator = schema.newValidator();
        validator.validate(ss, null);
    } catch (Exception e) {
        // e.printStackTrace();
        String error = e.getMessage();
        if (error.indexOf("javax.xml.parsers.FactoryConfigurationError: Provider MySAXFactoryImpl not found") > 0) {
            Assert.fail(e.getMessage());
        } else {
            System.out.println("Default impl is used");
        }

        // System.out.println(e.getMessage());

    }
    long end = System.currentTimeMillis();
    double elapsedTime = ((end - start));
    System.out.println("Time elapsed: " + elapsedTime);
    clearSystemProperty(SAX_FACTORY_ID);
}
 
Example 9
Source File: ValidationSupport.java    From FHIR with Apache License 2.0 5 votes vote down vote up
private static SchemaFactory createSchemaFactory() {
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schemaFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        return schemaFactory;
    } catch (Exception e) {
        throw new Error(e);
    }
}
 
Example 10
Source File: SchemaFactoryBuilder.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
protected void setSecurityFeature(SchemaFactory factory, String feature, Boolean value) throws Exception {
	factory.setFeature(feature, value);
}
 
Example 11
Source File: SchemaTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testValidation() throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setFeature("http://apache.org/xml/features/validate-annotations", true);
    factory.newSchema(new File(getClass().getResource("Bug8149915.xsd").getFile()));
}
 
Example 12
Source File: SchemaTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testValidation() throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setFeature("http://apache.org/xml/features/validate-annotations", true);
    factory.newSchema(new File(getClass().getResource("Bug8149915.xsd").getFile()));
}
 
Example 13
Source File: SchemaValidator.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Schema createSchema(String[] schemas) throws SAXException, IOException {

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        sf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);

        SchemaResourceResolver resourceResolver = new SchemaResourceResolver();

        sf.setResourceResolver(resourceResolver);

        Source[] sources = new Source[schemas.length];

        for (int i = 0; i < schemas.length; i++) {
            // need to validate the schema file
            Document doc = docBuilder.parse(schemas[i]);

            DOMSource stream = new DOMSource(doc, schemas[i]);

            sources[i] = stream;
        }
        return sf.newSchema(sources);

    }
 
Example 14
Source File: SchemaFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void testSetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
    SchemaFactory sf = newSchemaFactory();
    assertNotNull(sf);
    sf.setFeature(null, true);
}
 
Example 15
Source File: SchemaFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = SAXNotRecognizedException.class)
public void testSetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
    SchemaFactory sf = newSchemaFactory();
    sf.setFeature(UNRECOGNIZED_NAME, true);
}
 
Example 16
Source File: SchemaTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testValidation() throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setFeature("http://apache.org/xml/features/validate-annotations", true);
    factory.newSchema(new File(getClass().getResource("Bug8149915.xsd").getFile()));
}
 
Example 17
Source File: SchemaTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testValidation() throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setFeature("http://apache.org/xml/features/validate-annotations", true);
    factory.newSchema(new File(getClass().getResource("Bug8149915.xsd").getFile()));
}
 
Example 18
Source File: SchemaTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testValidation() throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setFeature("http://apache.org/xml/features/validate-annotations", true);
    factory.newSchema(new File(getClass().getResource("Bug8149915.xsd").getFile()));
}
 
Example 19
Source File: SchemaTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testValidation() throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setFeature("http://apache.org/xml/features/validate-annotations", true);
    factory.newSchema(new File(getClass().getResource("Bug8149915.xsd").getFile()));
}
 
Example 20
Source File: SchemaTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testValidation() throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setFeature("http://apache.org/xml/features/validate-annotations", true);
    factory.newSchema(new File(getClass().getResource("Bug8149915.xsd").getFile()));
}