private Validator newValidator(Schema schema) { Validator validator = schema.newValidator(); validator.setResourceResolver(resourceResolver); return validator; }
private static Schema newSchema(Source... schemas) throws SAXException { synchronized (XSD_SCHEMA_FACTORY) { return XSD_SCHEMA_FACTORY.newSchema(schemas); } } private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
private Schema newSchema() throws SAXException { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setResourceResolver(resourceResolver); return factory.newSchema(); }
static boolean validateAgainstXSD(InputStream xml, InputStream xsd) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml)); return true; } catch(Exception ex) { return false; } }
private Validator getValidator(URL xmlSchema) throws SAXException { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(xmlSchema); Validator validator = schema.newValidator(); validator.setErrorHandler(new ErrorHandler()); return validator; }
... Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new File("Item.xsd")); unmarshaller.setSchema(schema); //register item.xsd shcema for validation ...
public void validateModel(XMLStreamReader xmlStreamReader) throws Exception { Schema schema = createSchema(); Validator validator = schema.newValidator(); validator.validate(new StAXSource(xmlStreamReader)); }
private static SchemaFactory buildSchemaFactory() { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); try { schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "all"); schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "all"); } catch (Exception e) { new MockServerLogger(XmlSchemaValidator.class).error("exception configuring schema factory", e); } return schemaFactory; }
/** * Validates the specified input. * * <p> * This is just a convenience method of: * <pre> * validate(source,null); * </pre> * * @see #setErrorHandler(ErrorHandler) */ public void validate(Source source) throws SAXException, IOException { validate(source, null); }
public void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException { this.nsContext = nsContext; validator.setDocumentLocator(locator); validator.startDocument(); next.startDocument(locator,nsContext); }
/** * <p>Constructor that specifies <code>ClassLoader</code> to use * to find <code>SchemaFactory</code>.</p> * * @param loader * to be used to load resource, {@link SchemaFactory}, and * {@link SchemaFactoryLoader} implementations during * the resolution process. * If this parameter is null, the default system class loader * will be used. */ public SchemaFactoryFinder(ClassLoader loader) { this.classLoader = loader; if( debug ) { debugDisplayClassLoader(); } }
public void endDocument() throws SAXException { this.nsContext = null; validator.endDocument(); next.endDocument(); }
SchemaFactory schemaFactory = SchemaFactory .newInstance(XMLConstants.XML_DTD_NS_URI); Schema schema = schemaFactory.newSchema(new File( "xmlValidate.dtd")); Validator validator = schema.newValidator(); validator.validate(new StreamSource("xmlValidate.xml"));
private static Schema newSchema(Source[] schemas) throws SAXException { synchronized (XSD_SCHEMA_FACTORY) { return XSD_SCHEMA_FACTORY.newSchema(schemas); } }
public void validateModel(InputStreamProvider inputStreamProvider) throws Exception { Schema schema = createSchema(); Validator validator = schema.newValidator(); validator.validate(new StreamSource(inputStreamProvider.getInputStream())); }
private void validateInternal(Source xmlSrc, URL xmlSchema) throws SAXException, IOException { Validator validator = getValidator(xmlSchema); validator.validate(xmlSrc); }
import javax.xml.XMLConstants; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.*; ... URL schemaFile = new URL("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"); Source xmlFile = new StreamSource(new File("web.xml")); SchemaFactory schemaFactory = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(schemaFile); Validator validator = schema.newValidator(); try { validator.validate(xmlFile); System.out.println(xmlFile.getSystemId() + " is valid"); } catch (SAXException e) { System.out.println(xmlFile.getSystemId() + " is NOT valid"); System.out.println("Reason: " + e.getLocalizedMessage()); }
/** * <p>Parses the specified source as a schema and returns it as a schema.</p> * * <p>This is a convenience method for {@link #newSchema(Source[] schemas)}.</p> * * @param schema Source that represents a schema. * * @return New <code>Schema</code> from parsing <code>schema</code>. * * @throws SAXException If a SAX error occurs during parsing. * @throws NullPointerException if <tt>schema</tt> is null. */ public Schema newSchema(Source schema) throws SAXException { return newSchema(new Source[]{schema}); }
private void validateXmlSchema(final File file) throws SAXException, IOException { final URL schemaFile = this.getClass().getClassLoader().getResource("Log4j-events.xsd"); final Source xmlFile = new StreamSource(file); final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema = schemaFactory.newSchema(schemaFile); final Validator validator = schema.newValidator(); validator.validate(xmlFile); }
private void validateXmlSchema(final File file) throws SAXException, IOException { final URL schemaFile = this.getClass().getClassLoader().getResource("Log4j-events.xsd"); final Source xmlFile = new StreamSource(file); final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema = schemaFactory.newSchema(schemaFile); final Validator validator = schema.newValidator(); validator.validate(xmlFile); }