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

The following examples show how to use javax.xml.validation.ValidatorHandler#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: EdfiRecordParser.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
/**
 * Parse and Validate an XML represented by the input stream against provided XSD and reports validation issues.
 *
 * @param input XML to parse and validate
 * @param vHandler Validator handler
 * @throws IOException If a IO error occurs during XML parsing.
 * @throws XmlParseException If a SAX error occurs during XML parsing.
 */
protected void parseAndValidate(InputStream input, ValidatorHandler vHandler) throws XmlParseException, IOException {
    vHandler.setErrorHandler(this);

    InputSource is = new InputSource(new InputStreamReader(input, "UTF-8"));
    is.setEncoding("UTF-8");

    try {
        XMLReader parser = XMLReaderFactory.createXMLReader();
        parser.setContentHandler(vHandler);
        parser.setErrorHandler(this);

        vHandler.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);

        // Commented out the following line, as Java 1.6.0_45 throws an exception on this.
        //parser.setFeature("http://apache.org/xml/features/validation/id-idref-checking", false);
        parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
        parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

        parser.parse(is);
    } catch (SAXException e) {
        throw new XmlParseException(e.getMessage(), e);
    }
}
 
Example 2
Source File: EdfiRecordParserImpl2.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
private void parseAndValidate(InputStream input, Schema schema) throws XmlParseException, IOException {
    ValidatorHandler vHandler = schema.newValidatorHandler();
    vHandler.setContentHandler(this);
    vHandler.setErrorHandler(this);

    InputSource is = new InputSource(new InputStreamReader(input, "UTF-8"));
    is.setEncoding("UTF-8");

    try {
        XMLReader parser = XMLReaderFactory.createXMLReader();
        parser.setContentHandler(vHandler);
        parser.setErrorHandler(this);

        vHandler.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);

        parser.setFeature("http://apache.org/xml/features/validation/id-idref-checking", false);
        parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
        parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

        parser.parse(is);
    } catch (SAXException e) {
        throw new XmlParseException(e.getMessage(), e);
    }
}
 
Example 3
Source File: Bug4970400.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test1() throws Exception {
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler();
    validatorHandler.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
    validatorHandler.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
}
 
Example 4
Source File: Bug4970383.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler();
    try {
        validatorHandler.setFeature(null, false);
        Assert.fail("should report an error");
    } catch (NullPointerException e) {
        ; // expected
    }
}
 
Example 5
Source File: ValidatorHandlerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
    ValidatorHandler validatorHandler = getValidatorHandler();
    assertFalse(validatorHandler.getFeature(FEATURE_NAME), "The feature should be false by default.");

    validatorHandler.setFeature(FEATURE_NAME, true);
    assertTrue(validatorHandler.getFeature(FEATURE_NAME), "The feature should be false by default.");

}
 
Example 6
Source File: ValidatorHandlerTest.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 {
    ValidatorHandler validatorHandler = getValidatorHandler();
    assertNotNull(validatorHandler);
    validatorHandler.setFeature(null, true);
}