Code example for Validator

Methods: setErrorHandlervalidate

0
        InputStream xsdStream = SdkRepository.getXsdStream(version);
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsdStream));
        Validator validator = schema.newValidator();
        if (handler != null) {
            validator.setErrorHandler(handler);
        } 
 
        return validator;
    } 
 
    /** An helper that validates a string against an expected regexp. */ 
    private void assertRegex(String expectedRegexp, String actualString) {
        assertNotNull(actualString);
        assertTrue(
                String.format("Regexp Assertion Failed:\nExpected: %s\nActual: %s\n",
                        expectedRegexp, actualString),
                actualString.matches(expectedRegexp));
    } 
 
    // --- Tests ------------ 
 
    /** Validate a valid sample using namespace version 1 using an InputStream */ 
    public void testValidateLocalRepositoryFile1() throws Exception { 
        InputStream xmlStream = this.getClass().getResourceAsStream(
                    "/com/android/sdklib/testdata/repository_sample_1.xml"); 
        Source source = new StreamSource(xmlStream);
 
        CaptureErrorHandler handler = new CaptureErrorHandler();
        Validator validator = getValidator(1, handler);
        validator.validate(source);
        handler.verify();
    } 
 
    /** Validate a valid sample using namespace version 1 using an InputStream */ 
    public void testValidateLocalRepositoryFile2() throws Exception {