public ValidatorHandler newValidator() { synchronized(this) { if(schema==null) { try { // do not disable secure processing - these are well-known schemas SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false); schema = allowExternalAccess(sf, "file", false).newSchema(source); } catch (SAXException e) { // we make sure that the schema is correct before we ship. throw new AssertionError(e); } } } ValidatorHandler handler = schema.newValidatorHandler(); return handler; }
/** * Applies the additional binding customizations. */ public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) { if(topLevel!=null) { this.errorReceiver = errorReceiver; Unmarshaller u = BindInfo.getCustomizationUnmarshaller(); this.unmarshaller = u.getUnmarshallerHandler(); ValidatorHandler v = BindInfo.bindingFileSchema.newValidator(); v.setErrorHandler(errorReceiver); loader = new ForkContentHandler(v,unmarshaller); topLevel.applyAll(schema.getSchemas()); this.loader = null; this.unmarshaller = null; this.errorReceiver = null; } }
@Test public void test1() throws Exception { SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler(); try { validatorHandler.getFeature("unknown1234"); Assert.fail("SAXNotRecognizedException was not thrown."); } catch (SAXNotRecognizedException e) { ; // expected } if (!validatorHandler.getFeature("http://xml.org/sax/features/namespace-prefixes")) { // as expected System.out.println("getFeature(namespace-prefixes): OK"); } else { Assert.fail("Expected false, returned true."); } }
public ValidatorHandler newValidator() { synchronized(this) { if(schema==null) { try { schema = SchemaFactory.newInstance(WellKnownNamespace.XML_SCHEMA).newSchema(source); } catch (SAXException e) { // we make sure that the schema is correct before we ship. throw new AssertionError(e); } } } ValidatorHandler handler = schema.newValidatorHandler(); // fixValidatorBug6246922(handler); return handler; }
/** * Applies the additional binding customizations. */ public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) { if(topLevel!=null) { this.errorReceiver = errorReceiver; UnmarshallerImpl u = BindInfo.getJAXBContext().createUnmarshaller(); this.unmarshaller = u.getUnmarshallerHandler(); ValidatorHandler v = BindInfo.bindingFileSchema.newValidator(); v.setErrorHandler(errorReceiver); loader = new ForkContentHandler(v,unmarshaller); topLevel.applyAll(schema.getSchemas()); this.loader = null; this.unmarshaller = null; this.errorReceiver = null; } }
public static void translate(String xml, URL schemaURL, DocumentContainer documentContainer) throws SAXException, IOException { // create the ValidatorHandler SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(schemaURL); ValidatorHandler validatorHandler = schema.newValidatorHandler(); // create the parser, setup the chain XMLReader parser = new SAXParser(); XmlAligner aligner = new XmlAligner(validatorHandler); XmlTo<DocumentContainer> xml2object = new XmlTo<DocumentContainer>(aligner, documentContainer); parser.setContentHandler(validatorHandler); aligner.setContentHandler(xml2object); // start translating InputSource is = new InputSource(new StringReader(xml)); parser.parse(is); }
/** * 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); } }
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); } }
private XmlConfigHandler parseAndAssertInstance(String xmlInstance) throws SAXException, IOException { InputStream schemaStream = XmlConfigHandlerTest.class.getResourceAsStream(CONFIG_TEST_2_XSD); XMLSchemaFactory xmlSchemaFactory = new XMLSchemaFactory(); Schema schema = xmlSchemaFactory.newSchema(new StreamSource(schemaStream)); ValidatorHandler validatorHandler = schema.newValidatorHandler(); final TypeInfoProvider typeInfoProvider = validatorHandler.getTypeInfoProvider(); XmlConfigHandler xmlConfigHandler = new XmlConfigHandler(typeInfoProvider); validatorHandler.setContentHandler(xmlConfigHandler); XMLReader parser = XMLReaderFactory.createXMLReader(); parser.setContentHandler(validatorHandler); InputStream xmlStream = XmlConfigHandlerTest.class.getResourceAsStream(xmlInstance); parser.parse(new InputSource(xmlStream)); assertInstance(validatorHandler, xmlConfigHandler, xmlInstance); return xmlConfigHandler; }
private void assertInstance(ValidatorHandler validatorHandler, XmlConfigHandler xmlConfigHandler, String xmlInstance) throws SAXException, IOException { Map<String,Property> properties = xmlConfigHandler.getProperties(); Assert.assertEquals(4, properties.size()); Assert.assertEquals("port", properties.get("port").getName()); Assert.assertEquals("100", properties.get("port").getValues().get(0).getValue()); Assert.assertEquals("ref", properties.get("ref").getName()); Assert.assertEquals("FileRepHandler", properties.get("ref").getValues().get(0).getValue()); Assert.assertEquals("com.avast.cloud.RequestHandler", (properties.get("ref").getValues().get(0)).getRefType()); Assert.assertEquals("list", properties.get("list").getName()); Assert.assertEquals("aaa", properties.get("list").getValues().get(0).getValue()); Assert.assertEquals("bbb", properties.get("list").getValues().get(1).getValue()); Assert.assertEquals("ccc", properties.get("list").getValues().get(2).getValue()); Assert.assertEquals("map", properties.get("map").getName()); Assert.assertEquals("a", ((MapEntry)properties.get("map").getValues().get(0)).getKey()); Assert.assertEquals("1", properties.get("map").getValues().get(0).getValue()); Assert.assertEquals("b", ((MapEntry)properties.get("map").getValues().get(1)).getKey()); Assert.assertEquals("2", properties.get("map").getValues().get(1).getValue()); Assert.assertEquals("c", ((MapEntry)properties.get("map").getValues().get(2)).getKey()); Assert.assertEquals("3", properties.get("map").getValues().get(2).getValue()); }
private TypeInfo readTypeForProperty(final String propertyName) throws SAXException, IOException { InputStream schemaStream = XmlConfigHandlerTest.class.getResourceAsStream(CONFIG_TEST_2_XSD); XMLSchemaFactory xmlSchemaFactory = new XMLSchemaFactory(); Schema schema = xmlSchemaFactory.newSchema(new StreamSource(schemaStream)); final AtomicReference<TypeInfo> propertyTypeInfo = new AtomicReference<TypeInfo>(); ValidatorHandler validatorHandler = schema.newValidatorHandler(); final TypeInfoProvider typeInfoProvider = validatorHandler.getTypeInfoProvider(); validatorHandler.setContentHandler(new DefaultHandler() { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (propertyName.equals(localName)) { propertyTypeInfo.set(typeInfoProvider.getElementTypeInfo()); } } }); XMLReader parser = XMLReaderFactory.createXMLReader(); parser.setContentHandler(validatorHandler); InputStream xmlStream = XmlConfigHandlerTest.class.getResourceAsStream(CONFIG_TEST_2_XML); parser.parse(new InputSource(xmlStream)); return propertyTypeInfo.get(); }
/** * Parses an xml config file and returns a Config object. * * @param xmlFile * The xml config file which is passed by the user to annotation processing * @return * A non null Config object */ private Config parseAndGetConfig (File xmlFile, ErrorHandler errorHandler, boolean disableSecureProcessing) throws SAXException, IOException { XMLReader reader; try { SAXParserFactory factory = XmlFactory.createParserFactory(disableSecureProcessing); reader = factory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException e) { // in practice this will never happen throw new Error(e); } NGCCRuntimeEx runtime = new NGCCRuntimeEx(errorHandler); // set up validator ValidatorHandler validator = configSchema.newValidator(); validator.setErrorHandler(errorHandler); // the validator will receive events first, then the parser. reader.setContentHandler(new ForkContentHandler(validator,runtime)); reader.setErrorHandler(errorHandler); Config config = new Config(runtime); runtime.setRootHandler(config); reader.parse(new InputSource(xmlFile.toURL().toExternalForm())); runtime.reset(); return config; }
public ValidatorHandler newValidator() { if (schema==null) { synchronized (this) { if (schema == null) { ResourceResolver resourceResolver = null; try (InputStream is = clazz.getResourceAsStream(resourceName)) { StreamSource source = new StreamSource(is); source.setSystemId(resourceName); // do not disable secure processing - these are well-known schemas SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false); SchemaFactory schemaFactory = allowExternalAccess(sf, "file", false); if (createResolver) { resourceResolver = new ResourceResolver(clazz); schemaFactory.setResourceResolver(resourceResolver); } schema = schemaFactory.newSchema(source); } catch (IOException | SAXException e) { InternalError ie = new InternalError(e.getMessage()); ie.initCause(e); throw ie; } finally { if (resourceResolver != null) resourceResolver.closeStreams(); } } } } return schema.newValidatorHandler(); }
@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); }
@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 } }
private ValidatorHandler createValidatorHandler(String xsd) throws SAXException { SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); StringReader reader = new StringReader(xsd); StreamSource xsdSource = new StreamSource(reader); Schema schema = schemaFactory.newSchema(xsdSource); return schema.newValidatorHandler(); }
@Test public void test1() throws Exception { SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler(); try { validatorHandler.getFeature(null); Assert.fail(); } catch (NullPointerException e) { e.printStackTrace(); ; // as expected } }
@Test public void test() throws Exception { XMLReader xmlReader = createXMLReader(); final ValidatorHandler validatorHandler = createValidatorHandler(XSD); xmlReader.setContentHandler(validatorHandler); DefaultHandler handler = new DefaultHandler() { public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (!"ns:test".equals(qName)) { return; } TypeInfoProvider infoProvider = validatorHandler.getTypeInfoProvider(); if (infoProvider == null) { throw new SAXException("Can't obtain TypeInfoProvider object."); } int index = attributes.getIndex("id"); if (index == -1) { throw new SAXException("The attribute 'id' is not in the list."); } Assert.assertTrue(infoProvider.isSpecified(index)); index = attributes.getIndex("date"); if (index == -1) { throw new SAXException("The attribute 'date' is not in the list."); } Assert.assertFalse(infoProvider.isSpecified(index)); System.out.println("OK"); } }; validatorHandler.setContentHandler(handler); parse(xmlReader, XML); }
private ValidatorHandler createValidatorHandler(String xsd) throws SAXException { SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xsd.getBytes())); StreamSource xsdSource = new StreamSource(reader); Schema schema = schemaFactory.newSchema(xsdSource); return schema.newValidatorHandler(); }
@Test public void test1() throws Exception { XMLReader xmlReader = createXMLReader(); final ValidatorHandler validatorHandler = createValidatorHandler(XSD); xmlReader.setContentHandler(validatorHandler); DefaultHandler handler = new DefaultHandler() { public void characters(char[] ch, int start, int length) throws SAXException { TypeInfoProvider infoProvider = null; synchronized (validatorHandler) { infoProvider = validatorHandler.getTypeInfoProvider(); } if (infoProvider == null) { Assert.fail("Can't obtain TypeInfo object."); } try { infoProvider.getElementTypeInfo(); Assert.fail("IllegalStateException was not thrown."); } catch (IllegalStateException e) { // as expected System.out.println("OK"); } } }; validatorHandler.setContentHandler(handler); parse(xmlReader, XML); }
@Test public void test1() throws SAXException { try { ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler(); validatorHandler.getProperty("unknown1234"); Assert.fail("SAXNotRecognizedException was not thrown."); } catch (SAXNotRecognizedException e) { } }
@Test public void testErrorHandler() { ValidatorHandler validatorHandler = getValidatorHandler(); assertNull(validatorHandler.getErrorHandler(), "When ValidatorHandler is created, initially ErrorHandler should not be set."); ErrorHandler handler = new MyErrorHandler(); validatorHandler.setErrorHandler(handler); assertSame(validatorHandler.getErrorHandler(), handler); }
@Test(expectedExceptions = NullPointerException.class) public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { ValidatorHandler validatorHandler = getValidatorHandler(); assertNotNull(validatorHandler); validatorHandler.getProperty(null); }
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."); }
@Test(expectedExceptions = NullPointerException.class) public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { ValidatorHandler validatorHandler = getValidatorHandler(); assertNotNull(validatorHandler); validatorHandler.getFeature(null); }
@Test public void testContentHandler() { ValidatorHandler validatorHandler = getValidatorHandler(); assertNull(validatorHandler.getContentHandler(), "When ValidatorHandler is created, initially ContentHandler should not be set."); ContentHandler handler = new DefaultHandler(); validatorHandler.setContentHandler(handler); assertSame(validatorHandler.getContentHandler(), handler); validatorHandler.setContentHandler(null); assertNull(validatorHandler.getContentHandler()); }
public ValidatorHandler newValidator() { if (schema==null) { synchronized (this) { if (schema == null) { ResourceResolver resourceResolver = null; try (InputStream is = clazz.getResourceAsStream(resourceName)) { StreamSource source = new StreamSource(is); source.setSystemId(resourceName); // do not disable secure processing - these are well-known schemas SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false); SchemaFactory schemaFactory = allowExternalAccess(sf, "file", false); if (createResolver) { resourceResolver = new ResourceResolver(clazz); schemaFactory.setResourceResolver(resourceResolver); } schema = schemaFactory.newSchema(source); } catch (IOException | SAXException e) { throw new InternalError(e); } finally { if (resourceResolver != null) resourceResolver.closeStreams(); } } } } return schema.newValidatorHandler(); }