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"));
... Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new File("Item.xsd")); unmarshaller.setSchema(schema); //register item.xsd shcema for validation ...
/** * <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)); }
SchemaFactory factory = SchemaFactory .newInstance("http://www.w3.org/2001/XMLSchema"); File file = new File("test.xml"); Schema schema = factory.newSchema(file); JAXBContext context = JAXBContext.newInstance(ACCESREFUSE.class); Unmarshaller u = context.createUnmarshaller(); u.setSchema(schema); Object anObject = u.unmarshal(new StreamSource(new StringReader( getMessage())), ACCESREFUSE.class);
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 Schema createSchema() throws SAXException { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = null; if (classloader != null) { schema = factory.newSchema(classloader.getResource(BPMN_XSD)); } if (schema == null) { schema = factory.newSchema(BpmnXMLConverter.class.getClassLoader().getResource(BPMN_XSD)); } if (schema == null) { throw new XMLException("BPMN XSD could not be found"); } return schema; }
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; }
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL schemaURL = // The URL to your XML Schema; Schema schema = sf.newSchema(schemaURL); Validator validator = schema.newValidator(); DOMSource source = new DOMSource(xmlDOM); validator.validate(source);
@Test public void validatorProperties() throws Exception { SetPropertyAnswer setPropertyAnswer = new SetPropertyAnswer(schemaFactory.newSchema().newValidator()); doAnswer(setPropertyAnswer).when(validatorWrapper).setProperty(anyString(), anyObject()); defaultXMLSecureFactories.configureValidator(validatorWrapper); assertThat(setPropertyAnswer.exception, is(nullValue())); for (String property : VALIDATOR_PROPERTIES) { verify(validatorWrapper).setProperty(property, ""); } }
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; } }
SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("myschema.xsd")); unmarshaller.setSchema(schema);
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source schemaFile = new StreamSource(new File(namespace + ".xsd")); Schema schema = factory.newSchema(schemaFile); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware( true ); dbf.setValidating(true); dbf.setSchema(schema);
public ClusterMapper() { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source source = new StreamSource(ClusterMapper.class.getResourceAsStream("cluster.xsd")); this.schema = factory.newSchema(source); } catch(SAXException e) { throw new MappingException(e); } }
public FlowParser() throws SAXException { schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); flowSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(FLOW_XSD)); }
/** * <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())); }
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()); }
/** * Validate the document against a given schema. Error will be written to {@link java.lang.System#err} * * @param xsdFilename schema to validate against. * @param in document to be validated. * @param options to be applied during parsing. * @throws Exception if an error occurs when parsing the document or schema. */ public static void validate(final String xsdFilename, final InputStream in, final ParserOptions options) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setSchema(schemaFactory.newSchema(new File(xsdFilename))); factory.setNamespaceAware(true); if (options.xIncludeAware()) { factory.setXIncludeAware(true); factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false); } factory.newDocumentBuilder().parse(in); }
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()); } }
@Override public void initialize(UserGroupProviderInitializationContext initializationContext) throws AuthorizerCreationException { try { final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); tenantsSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(TENANTS_XSD)); usersSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(USERS_XSD)); } catch (Exception e) { throw new AuthorizerCreationException(e); } }
public XmlSchemaValidator(MockServerLogger mockServerLogger, String schema) { this.mockServerLogger = mockServerLogger; try { if (schema.trim().endsWith(">") || Strings.isNullOrEmpty(schema)) { this.schema = schemaFactory.newSchema(new StreamSource(new StringReader(schema))); } else if (schema.trim().endsWith(".xsd")) { this.schema = schemaFactory.newSchema(new StreamSource(FileReader.openReaderToFileFromClassPathOrPath(schema))); } else { throw new IllegalArgumentException("Schema must either be a path reference to a *.xsd file or an xml string"); } } catch (SAXException e) { throw new IllegalArgumentException("Schema is not valid", e); } catch (FileNotFoundException e) { throw new IllegalArgumentException("Schema file not found", e); } }