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 Schema newSchema() throws SAXException { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setResourceResolver(resourceResolver); return factory.newSchema(); }
... 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 static Schema resolveLocalSchema(URL schemaUrl) { try { InputStream schemaStream = schemaUrl.openStream(); try { StreamSource source = new StreamSource(schemaUrl.openStream()); SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI ); return schemaFactory.newSchema(source); } catch ( Exception e ) { throw new XmlInfrastructureException( "Unable to load schema [" + schemaUrl.toExternalForm() + "]", e ); } finally { try { schemaStream.close(); } catch ( IOException e ) { log.debugf( "Problem closing schema stream - %s", e.toString() ); } } } catch ( IOException e ) { throw new XmlInfrastructureException( "Stream error handling schema url [" + schemaUrl.toExternalForm() + "]" ); } } }
public static void validate(final Node node, final String schemaFile) throws Exception { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new URL(findResource(schemaFile).toURI().toASCIIString())); Validator validator = schema.newValidator(); // validate the DOM tree try { validator.validate(new DOMSource(node)); } catch (SAXException e) { ActiveMQClientLogger.LOGGER.errorOnXMLTransformInvalidConf(e); throw new IllegalStateException("Invalid configuration", e); } }
private LoginIdentityProviders loadLoginIdentityProvidersConfiguration() throws Exception { final File loginIdentityProvidersConfigurationFile = properties.getLoginIdentityProviderConfigurationFile(); // load the users from the specified file if (loginIdentityProvidersConfigurationFile.exists()) { try { // find the schema final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema = schemaFactory.newSchema(LoginIdentityProviders.class.getResource(LOGIN_IDENTITY_PROVIDERS_XSD)); // attempt to unmarshal XMLStreamReader xsr = XmlUtils.createSafeReader(new StreamSource(loginIdentityProvidersConfigurationFile)); final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller(); unmarshaller.setSchema(schema); final JAXBElement<LoginIdentityProviders> element = unmarshaller.unmarshal(xsr, LoginIdentityProviders.class); return element.getValue(); } catch (SAXException | JAXBException e) { throw new Exception("Unable to load the login identity provider configuration file at: " + loginIdentityProvidersConfigurationFile.getAbsolutePath()); } } else { throw new Exception("Unable to find the login identity provider configuration file at " + loginIdentityProvidersConfigurationFile.getAbsolutePath()); } }
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Source schemaSource = new StreamSource(new URL("http://maven.apache.org/xsd/maven-4.0.0.xsd").openStream()); final Schema schema = schemaFactory.newSchema(schemaSource); schema.newValidator().validate(new DOMSource(document.getFirstChild()));
public static Schema createSchema() throws SAXException { final SchemaFactory schemaFactory = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final URL schemaResource = WpsUtils.class.getClassLoader().getResource("wps/1.0.0/wpsAll.xsd"); //$NON-NLS-1$ final Schema schema = schemaFactory.newSchema(new StreamSource(schemaResource.toString())); return schema; }
/** * <p>Parses the specified <code>URL</code> as a schema and returns it as a <code>Schema</code>.</p> * * <p>This is a convenience method for {@link #newSchema(Source schema)}.</p> * * @param schema <code>URL</code> 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(URL schema) throws SAXException { return newSchema(new StreamSource(schema.toExternalForm())); }
/** * Validate the XML content against the XSD. * * The whole XML content must be valid. */ static void validateXML(String xmlContent, String xsdPath, Properties resolvedProperties) throws Exception { String resolvedXml = resolveAllExpressions(xmlContent, resolvedProperties); InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(xsdPath); final Source source = new StreamSource(stream); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setErrorHandler(ERROR_HANDLER); schemaFactory.setResourceResolver(new JBossEntityResolver()); Schema schema = schemaFactory.newSchema(source); javax.xml.validation.Validator validator = schema.newValidator(); validator.setErrorHandler(ERROR_HANDLER); validator.setFeature("http://apache.org/xml/features/validation/schema", true); validator.validate(new StreamSource(new StringReader(resolvedXml))); }
public StoreDefinitionsMapper() { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source source = new StreamSource(StoreDefinitionsMapper.class.getResourceAsStream("stores.xsd")); this.schema = factory.newSchema(source); } catch(SAXException e) { throw new MappingException(e); } }
protected void validateSchema(final ValidationMethod method, final Resolver resolver, final byte[] schemaData, byte[] xmlData) throws SAXException, IOException { final SchemaFactory schemaFactory = SchemaFactory.newInstance(method.schemaLanguage); schemaFactory.setResourceResolver(resolver); final Schema schema = schemaFactory.newSchema(new StreamSource(new ByteArrayInputStream(schemaData))); final Validator validator = schema.newValidator(); validator.setErrorHandler(new ErrorHandler()); validator.setResourceResolver(resolver); validator.validate(new StreamSource(new ByteArrayInputStream(xmlData))); }
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL schemaUrl = getClass().getResource(schemaSource); String catalog = getClass().getResource("/catalog.xml").getFile(); XMLCatalogResolver resolver = new XMLCatalogResolver(new String[] { catalog }); factory.setResourceResolver(resolver);
/** * <p>Parses the specified <code>File</code> as a schema and returns it as a <code>Schema</code>.</p> * * <p>This is a convenience method for {@link #newSchema(Source schema)}.</p> * * @param schema File 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(File schema) throws SAXException { return newSchema(new StreamSource(schema)); }
public static List validate( Source xml, URL schemaURL, boolean skipTargetNamespaceException, EntityResolver entityResolver) { try { Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(schemaURL); Validator v = schema.newValidator(); if (entityResolver != null) { v.setResourceResolver( new EntityResolverToLSResourceResolver( v.getResourceResolver(), entityResolver)); } Handler handler = new Handler(skipTargetNamespaceException, entityResolver); v.setErrorHandler(handler); v.validate(xml); return handler.errors; } catch (SAXException e) { return exception(e); } catch (IOException e) { return exception(e); } }
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; }
JAXBContext jc = JAXBContext.newInstance(Customer.class); Umarshaller unmarshaller = jc.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL schemaURL = // The URL to your XML Schema; Schema schema = sf.newSchema(schemaURL); unmarshaller.setSchema(schema); Customer customer = (Customer) unmarshaller.unmarshal(xml);
private Schema parseSchema(ErrorHandler h) throws SAXException { SchemaFactory fac = factory != null ? factory : SchemaFactory.newInstance(schemaLanguage); fac.setErrorHandler(h); try { return fac.newSchema((Source[]) sources.toArray(new Source[sources.size()])); } finally { fac.setErrorHandler(null); } }
public void parseAndValidate(File xmlFile, ContentHandler handler) { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setResourceResolver(new MySchemaResolver()); Schema schema = schemaFactory.newSchema(); Validator v = schema.newValidator(); v.setResourceResolver(schemaFactory.getResourceResolver()); InputSource is = new InputSource(new FileInputStream(xmlFile)); v.validate(new SAXSource(is), new SAXResult(handler)); }
private Schema loadSchema(String theVersion, String theSchemaName) { String key = theVersion + "-" + theSchemaName; synchronized (myKeyToSchema) { Schema schema = myKeyToSchema.get(key); if (schema != null) { return schema; } Source baseSource = loadXml(null, theSchemaName); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setResourceResolver(new MyResourceResolver()); try { try { /* * See https://github.com/jamesagnew/hapi-fhir/issues/339 * https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Processing */ schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); }catch (SAXNotRecognizedException snex){ ourLog.warn("Jaxp 1.5 Support not found.",snex); } schema = schemaFactory.newSchema(new Source[] { baseSource }); } catch (SAXException e) { throw new ConfigurationException("Could not load/parse schema file: " + theSchemaName, e); } myKeyToSchema.put(key, schema); return schema; } }