private void validate(WSDLModel model, Source saxSource, XsdBasedValidator.Handler handler, LSResourceResolver resolver) { try { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); if (resolver != null) { sf.setResourceResolver(resolver); } sf.setErrorHandler(handler); if (saxSource == null) { return; } sf.newSchema(saxSource); } catch(SAXException sax) { //already processed by handler } catch(Exception ex) { handler.logValidationErrors(Validator.ResultType.ERROR, ex.getMessage()); } }
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { LSInput input = null; Iterator<ValidatorSchemaFactory> it = mExtSchemaFactories.iterator(); while(it.hasNext()) { ValidatorSchemaFactory fac = it.next(); LSResourceResolver resolver = fac.getLSResourceResolver(); if(resolver != null) { input = resolver.resolveResource(type, namespaceURI, publicId, systemId, baseURI); if(input != null) { break; } } } return input; }
/** * Subclasses can use this to get a compiled schema object. * @param schemas Input stream of schemas. * @param lsResourceResolver resolver can be supplied optionally. Otherwise pass null. * @return Compiled Schema object. */ protected Schema getCompiledSchema(InputStream[] schemas, LSResourceResolver lsResourceResolver) { Schema schema = null; // Convert InputStream[] to StreamSource[] StreamSource[] schemaStreamSources = new StreamSource[schemas.length]; for(int index1=0 ; index1<schemas.length ; index1++) schemaStreamSources[index1] = new StreamSource(schemas[index1]); // Create a compiled Schema object. SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setResourceResolver(lsResourceResolver); try { schema = schemaFactory.newSchema(schemaStreamSources); } catch(SAXException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getCompiledSchema", ex); } return schema; }
protected Schema getCompiledSchema(Source[] schemas, LSResourceResolver lsResourceResolver, ErrorHandler errorHandler) { Schema schema = null; // Create a compiled Schema object. SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setResourceResolver(lsResourceResolver); schemaFactory.setErrorHandler(errorHandler); try { schema = schemaFactory.newSchema(schemas); } catch(SAXException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getCompiledSchema", ex); } return schema; }
public void testValidation(boolean setUseCatalog, boolean useCatalog, String catalog, String xsd, LSResourceResolver resolver) throws Exception { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // use resolver or catalog if resolver = null if (resolver != null) { factory.setResourceResolver(resolver); } if (setUseCatalog) { factory.setFeature(XMLConstants.USE_CATALOG, useCatalog); } factory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog); Schema schema = factory.newSchema(new StreamSource(new StringReader(xsd))); success("XMLSchema.dtd and datatypes.dtd are resolved."); }
/** * Creates the schema. * * @param xsdInputStream the xsd input stream * @param resourceResolver the resource resolver * @return the schema * @throws XmlSchemaFailureException the xml schema failure exception */ private Schema createSchema(InputStream xsdInputStream, LSResourceResolver resourceResolver) throws XmlSchemaFailureException { final SchemaFactory factory = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Source schemaSource = new StreamSource(xsdInputStream); if (resourceResolver != null) { factory.setResourceResolver(resourceResolver); } Schema schema; try { schema = factory.newSchema(schemaSource); } catch (final SAXException e) { logger.error(e.getMessage()); throw new XmlSchemaFailureException(e); } return schema; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { // we have to do it here, because we need the context created first CatalogManager.getStaticManager().setIgnoreMissingProperties(true); CatalogResolver catalogResolver = new CatalogResolver(true); URL catalogUrl = ResourceHelper.resolveMandatoryResourceAsUrl(context.getClassResolver(), "org/apache/camel/component/validator/catalog.cat"); catalogResolver.getCatalog().parseCatalog(catalogUrl); LSResourceResolver resourceResolver = new CatalogLSResourceResolver(catalogResolver); JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry(); registry.bind("resourceResolver", resourceResolver); return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .doTry() .to("validator:org/apache/camel/component/validator/report.xsd?resourceResolver=#resourceResolver") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .doFinally() .to("mock:finally") .end(); } }; }
@Test public void testInstanceResourceResolver() throws SAXException, IOException { SchemaFactory f = factory(); Validator v = f.newSchema(charStreamSource(element("doc", element("inner")))).newValidator(); Assert.assertNull(v.getResourceResolver()); LSResourceResolver rr = new LSResourceResolver() { public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { // In Java 5 Xerces absolutized the systemId relative to the current directory int slashIndex = systemId.lastIndexOf('/'); if (slashIndex >= 0) systemId = systemId.substring(slashIndex + 1); Assert.assertEquals(systemId, "e.xml"); Assert.assertEquals(type, "http://www.w3.org/TR/REC-xml"); LSInput in = new LSInputImpl(); in.setStringData("<inner/>"); return in; } }; v.setResourceResolver(rr); Assert.assertSame(v.getResourceResolver(), rr); v.validate(charStreamSource("<!DOCTYPE doc [ <!ENTITY e SYSTEM 'e.xml'> ]><doc>&e;</doc>")); }
@Test public void testSchemaResourceResolver() throws SAXException, IOException { SchemaFactory f = factory(); Assert.assertNull(f.getResourceResolver()); LSResourceResolver rr = new LSResourceResolver() { public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { Assert.assertEquals(systemId, "myschema"); Assert.assertEquals(type, getLSType()); Assert.assertNull(baseURI); Assert.assertNull(namespaceURI); Assert.assertNull(publicId); LSInput in = new LSInputImpl(); in.setStringData(createSchema("doc")); return in; } }; f.setResourceResolver(rr); Assert.assertSame(f.getResourceResolver(), rr); Validator v = f.newSchema(charStreamSource(externalRef("myschema"))).newValidator(); v.validate(charStreamSource("<doc/>")); }
public XSParser(LSResourceResolver entityResolver, DOMErrorHandler errorHandler){ System.setProperty(DOMImplementationRegistry.PROPERTY, DOMXSImplementationSourceImpl.class.getName()); DOMImplementationRegistry registry; try{ registry = DOMImplementationRegistry.newInstance(); }catch(Exception ex){ throw new ImpossibleException(ex); } XSImplementationImpl xsImpl = (XSImplementationImpl)registry.getDOMImplementation("XS-Loader"); xsLoader = xsImpl.createXSLoader(null); DOMConfiguration config = xsLoader.getConfig(); config.setParameter(Constants.DOM_VALIDATE, Boolean.TRUE); if(entityResolver!=null) config.setParameter(Constants.DOM_RESOURCE_RESOLVER, entityResolver); if(errorHandler!=null) config.setParameter(Constants.DOM_ERROR_HANDLER, errorHandler); }
/** * Validates *.faces-config.xml file * * @param xml * @return */ private boolean isValidFacesConfig(InputStream xml) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setResourceResolver((LSResourceResolver) DbfFactory.FACES_ENTITY_RESOLVER); InputStream xsd = this.getClass().getResourceAsStream(FACES_2_2_XSD); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml)); return true; } catch (Exception ex) { return false; } }
public static LSResourceResolver getLSResourceResolver() throws Exception { logger.info(""); LSResourceResolver resourceResolver = new LSResourceResolver() { public LSInput resolveResource(String type, String namespaceUri, String publicId, String systemId, String baseUri) { try { final InputStream in = GXmlEntity.class.getResource( "/" + systemId).openStream(); LSInputAdapter adapter = new LSInputAdapter(in); return adapter; } catch (Exception exception) { exception.printStackTrace(); return null; } } }; return resourceResolver; }
private static LSResourceResolver getLSResourceResolver() throws Exception { logger.info(""); LSResourceResolver resourceResolver = new LSResourceResolver() { public LSInput resolveResource(String type, String namespaceUri, String publicId, String systemId, String baseUri) { try { final InputStream in = GXmlEntity.class.getResource( "/" + systemId).openStream(); LSInputAdapter adapter = new LSInputAdapter(in); return adapter; } catch (Exception exception) { logger.error(GStringUtils.stackTraceToString(exception)); return null; } } }; return resourceResolver; }
@Test public void test1() throws Exception { String xsd1 = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" + " xmlns:test='jaxp13_test1'\n" + " targetNamespace='jaxp13_test1'\n" + " elementFormDefault='qualified'>\n" + " <import namespace='jaxp13_test2'/>\n" + " <element name='test'/>\n" + " <element name='child1'/>\n" + "</schema>\n"; final NullPointerException EUREKA = new NullPointerException("NewSchema015"); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); StringReader reader = new StringReader(xsd1); StreamSource source = new StreamSource(reader); LSResourceResolver resolver = new LSResourceResolver() { public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { LSInput input; if (namespaceURI != null && namespaceURI.endsWith("jaxp13_test2")) { throw EUREKA; } else { input = null; } return input; } }; schemaFactory.setResourceResolver(resolver); try { schemaFactory.newSchema(new Source[] { source }); Assert.fail("NullPointerException was not thrown."); } catch (RuntimeException e) { if (e != EUREKA) throw e; } }
@Test(dataProvider = "data_SchemaA") public void testValidationA(boolean setUseCatalog, boolean useCatalog, String catalog, String xsd, LSResourceResolver resolver) throws Exception { testValidation(setUseCatalog, useCatalog, catalog, xsd, resolver) ; }
@Test(dataProvider = "data_ValidatorA") public void testValidatorA(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog, Source source, LSResourceResolver resolver1, LSResourceResolver resolver2, String catalog1, String catalog2) throws Exception { testValidator(setUseCatalog1, setUseCatalog2, useCatalog, source, resolver1, resolver2, catalog1, catalog2); }
@Test(dataProvider = "data_ValidatorC", expectedExceptions = {SAXException.class, IOException.class}) public void testValidatorC(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog, Source source, LSResourceResolver resolver1, LSResourceResolver resolver2, String catalog1, String catalog2) throws Exception { testValidator(setUseCatalog1, setUseCatalog2, useCatalog, source, resolver1, resolver2, catalog1, catalog2); }
@Test(dataProvider = "data_ValidatorC", expectedExceptions = SAXException.class) public void testValidatorC(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog, Source source, LSResourceResolver resolver1, LSResourceResolver resolver2, String catalog1, String catalog2) throws Exception { testValidator(setUseCatalog1, setUseCatalog2, useCatalog, source, resolver1, resolver2, catalog1, catalog2); }
@DataProvider(name = "data_SchemaA") public Object[][] getDataSchema() { String[] systemIds = {"pathto/XMLSchema.dtd", "datatypes.dtd"}; XmlInput[] returnValues = {new XmlInput(null, dtd_xmlSchema, null), new XmlInput(null, dtd_datatypes, null)}; LSResourceResolver resolver = new SourceResolver(null, systemIds, returnValues); String[] systemIds1 = {"xml.xsd"}; XmlInput[] returnValues1 = {new XmlInput(null, xsd_xml, null)}; LSResourceResolver resolverImport = new SourceResolver(null, systemIds1, returnValues1); String[] systemIds2 = {"XSDInclude_person.xsd", "XSDInclude_product.xsd"}; XmlInput[] returnValues2 = {new XmlInput(null, xsd_include_person, null), new XmlInput(null, xsd_include_product, null)}; LSResourceResolver resolverInclude = new SourceResolver(null, systemIds2, returnValues2); return new Object[][]{ // for resolving DTD in xsd {false, true, xml_catalog, xsd_xmlSchema, null}, {false, true, xml_bogus_catalog, xsd_xmlSchema, resolver}, {true, true, xml_bogus_catalog, xsd_xmlSchema, resolver}, // for resolving xsd import {false, true, xml_catalog, xsd_xmlSchema_import, null}, {false, true, xml_bogus_catalog, xsd_xmlSchema_import, resolverImport}, {true, true, xml_bogus_catalog, xsd_xmlSchema_import, resolverImport}, // for resolving xsd include {false, true, xml_catalog, xsd_include_company, null}, {false, true, xml_bogus_catalog, xsd_include_company, resolverInclude}, {true, true, xml_bogus_catalog, xsd_include_company, resolverInclude} }; }
@DataProvider(name = "data_ValidatorA") public Object[][] getDataValidator() { DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, false, true, xml_catalog); SAXSource ss = new SAXSource(new InputSource(xml_val_test)); ss.setSystemId(xml_val_test_id); StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog); StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog); StreamSource source = new StreamSource(new File(xml_val_test)); String[] systemIds = {"system.dtd", "val_test.xsd"}; XmlInput[] returnValues = {new XmlInput(null, dtd_system, null), new XmlInput(null, xsd_val_test, null)}; LSResourceResolver resolver = new SourceResolver(null, systemIds, returnValues); StAXSource stax2 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog); return new Object[][]{ // use catalog {false, false, true, ds, null, null, xml_catalog, null}, {false, false, true, ds, null, null, null, xml_catalog}, {false, false, true, ss, null, null, xml_catalog, null}, {false, false, true, ss, null, null, null, xml_catalog}, {false, false, true, stax, null, null, xml_catalog, null}, {false, false, true, stax1, null, null, null, xml_catalog}, {false, false, true, source, null, null, xml_catalog, null}, {false, false, true, source, null, null, null, xml_catalog}, // use resolver {false, false, true, ds, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog}, {false, false, true, ss, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog}, {false, false, true, stax2, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog}, {false, false, true, source, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog} }; }
/** * Verifies Catalog Support for the Validator. * @param setUseCatalog1 a flag to indicate whether USE_CATALOG shall be set * on the factory. * @param setUseCatalog2 a flag to indicate whether USE_CATALOG shall be set * on the Validator. * @param source the XML source * @param resolver1 a resolver to be set on the factory if specified * @param resolver2 a resolver to be set on the Validator if specified * @param catalog1 a catalog to be set on the factory if specified * @param catalog2 a catalog to be set on the Validator if specified */ public void testValidator(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog, Source source, LSResourceResolver resolver1, LSResourceResolver resolver2, String catalog1, String catalog2) throws Exception { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); if (setUseCatalog1) { schemaFactory.setFeature(XMLConstants.USE_CATALOG, useCatalog); } if (catalog1 != null) { schemaFactory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog1); } if (resolver1 != null) { schemaFactory.setResourceResolver(resolver1); } Schema schema = schemaFactory.newSchema(); Validator validator = schema.newValidator(); if (setUseCatalog2) { validator.setFeature(XMLConstants.USE_CATALOG, useCatalog); } if (catalog2 != null) { validator.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog2); } if (resolver2 != null) { validator.setResourceResolver(resolver2); } validator.validate(source); }
@Test(dataProvider = "data_ValidatorC") public void testValidatorA(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog, Source source, LSResourceResolver resolver1, LSResourceResolver resolver2, String catalog1, String catalog2) throws Exception { testValidator(setUseCatalog1, setUseCatalog2, useCatalog, source, resolver1, resolver2, catalog1, catalog2); }
@DataProvider(name = "data_ValidatorC") public Object[][] getDataValidator() { DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, false, true, null); SAXSource ss = new SAXSource(new InputSource(xml_val_test)); ss.setSystemId(xml_val_test_id); StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog); StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog); StreamSource source = new StreamSource(new File(xml_val_test)); String[] systemIds = {"system.dtd", "val_test.xsd"}; XmlInput[] returnValues = {new XmlInput(null, dtd_system, null), new XmlInput(null, xsd_val_test, null)}; LSResourceResolver resolver = new SourceResolver(null, systemIds, returnValues); StAXSource stax2 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog); return new Object[][]{ // use catalog {false, false, true, ds, null, null, null, null}, {false, false, true, ds, null, null, null, null}, {false, false, true, ss, null, null, null, null}, {false, false, true, ss, null, null, null, null}, {false, false, true, stax, null, null, null, null}, {false, false, true, stax1, null, null, null, null}, {false, false, true, source, null, null, null, null}, {false, false, true, source, null, null, null, null}, // use resolver {false, false, true, ds, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog}, {false, false, true, ss, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog}, {false, false, true, stax2, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog}, {false, false, true, source, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog} }; }
/** * Validate given xml file again specified xsd resource * @param xmlFilePath xml file path * @param xsd_resource_path xsd resource * @throws SAXException if xmlFile is not followed xsd * @throws IOException if file path is invalid */ public void validateXmlAgainsXsdResource(String xmlFilePath, String xsd_resource_path) throws SAXException, IOException { Source xmlFile = new StreamSource(new File(xmlFilePath)); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); LSResourceResolver resourceResolver = (LSResourceResolver) ImfResourceResolver.getInstance(); schemaFactory.setResourceResolver(resourceResolver); InputStream xsd_source = loadXsdResource(xsd_resource_path); if (xsd_source != null) { Schema schema = schemaFactory.newSchema(new StreamSource(xsd_source)); Validator validator = schema.newValidator(); validator.validate(xmlFile); } else { throw new IOException(xsd_resource_path + " does not exists"); } }
/** * Instantiates a new xml validation. * * @param xsdInputStream the xsd input stream * @param schemaBasePath the schema base path */ public XmlValidation(InputStream xsdInputStream, String schemaBasePath) { LSResourceResolver resourceResolver = null; Assert.hasText(schemaBasePath, "'schemaBasePath' must not be empty."); Assert.notNull(xsdInputStream, "'xsdInputStream' must not be null."); this.schemaBasePath = schemaBasePath; resourceResolver = new ResourceResolver(this.schemaBasePath); try { this.schema = createSchema(xsdInputStream, resourceResolver); this.validator = createValidator(this.schema); } catch (final XmlSchemaFailureException e) { logger.error("XmlValidation initialization is failed: the schema cannot be loaded."); } }
/** * @see org.w3c.dom.ls.LSResourceResolver#resolveResource(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { for (LSResourceResolver resolver: resolvers) { LSInput input = resolver.resolveResource(type, namespaceURI, publicId, systemId, baseURI); if (input != null) { return input; } } return null; }
public SchemaCache (@Nonnull final String sSchemaTypeName, @Nonnull final SchemaFactory aSchemaFactory, @Nullable final ErrorHandler aErrorHandler, @Nullable final LSResourceResolver aResourceResolver) { super (aKey -> createSchema (aSchemaFactory, sSchemaTypeName, aKey), 500, SchemaCache.class.getName () + "$" + sSchemaTypeName); ValueEnforcer.notNull (sSchemaTypeName, "SchemaTypeName"); ValueEnforcer.notNull (aSchemaFactory, "SchemaFactory"); m_sSchemaTypeName = sSchemaTypeName; m_aSchemaFactory = aSchemaFactory; m_aSchemaFactory.setErrorHandler (aErrorHandler); m_aSchemaFactory.setResourceResolver (aResourceResolver); }
@Test public void testOSGIBundle () throws BundleException { LSInput aRes; // Initializing Apache Felix as OSGI container is required to get the // "bundle" URL protocol installed correctly // Otherwise the first call would end up as a "file" resource ;-) final Framework aOSGI = new FrameworkFactory ().newFramework (new HashMap <String, String> ()); aOSGI.start (); try { // Bundle 0 is the org.apache.felix.framework bundle final Bundle b = aOSGI.getBundleContext ().getBundle (0); assertNotNull (b); assertEquals (b.getState (), Bundle.ACTIVE); // No leading slash is important as the ClassLoader is used! assertNotNull (b.getResource ("org/apache/felix/framework/util/Mutex.class")); final LSResourceResolver aRR = new SimpleLSResourceResolver (); // No class loader aRes = aRR.resolveResource (XMLConstants.W3C_XML_SCHEMA_NS_URI, null, null, "../Felix.class", "bundle://0.0:1/org/apache/felix/framework/util/Mutex.class"); assertTrue (aRes instanceof ResourceLSInput); final IHasInputStream aISP = ((ResourceLSInput) aRes).getInputStreamProvider (); assertTrue (aISP instanceof URLResource); // Path maybe a "jar:file:" resource assertTrue (((URLResource) aISP).getPath ().endsWith ("org/apache/felix/framework/Felix.class")); } finally { aOSGI.stop (); } }
private Schema newRelaxNgSchema(InputSource source) throws SAXException { CompactSyntaxSchemaFactory factory = new CompactSyntaxSchemaFactory(); factory.setResourceResolver(new LSResourceResolver() { @Override public LSInput resolveResource(String type, String nsUri, String publicId, String systemId, String baseUri) { return new ClassLoaderInput(systemId, baseUri, publicId); } }); return factory.newSchema(new SAXSource(source)); }
public void readSchema() throws Exception { logger.info(""); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); LSResourceResolver resourceResolver = GXmlUtils.getLSResourceResolver(); schemaFactory.setResourceResolver(resourceResolver); String schemaFile = getSchemaFile(); InputStream in = getClass().getResourceAsStream(schemaFile); Source schemaSource = new StreamSource(in); schema = schemaFactory.newSchema(schemaSource); in.close(); }
private static void validate(Element docElement, String schemaFile) throws Exception { SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); LSResourceResolver resourceResolver = GXmlUtils.getLSResourceResolver(); sf.setResourceResolver(resourceResolver); InputStream in = GXmlUtils.class.getResourceAsStream(schemaFile); Source schemaSource = new StreamSource(in); Schema schema = sf.newSchema(schemaSource); in.close(); DOMSource source = new DOMSource(docElement); Validator validator = schema.newValidator(); validator.validate(source); }
/** * Returns the {@link Schema} associated with this validator. This ia an XSD schema containing knowledge about the * schema source as returned by {@link #getSchemaSources(List)} * @throws ConfigurationException */ protected synchronized Schema getSchemaObject(String schemasId, List<nl.nn.adapterframework.validation.Schema> schemas) throws ConfigurationException { Schema schema = javaxSchemas.get(schemasId); if (schema == null) { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setResourceResolver(new LSResourceResolver() { public LSInput resolveResource(String s, String s1, String s2, String s3, String s4) { return null; } }); try { Collection<Source> sources = getSchemaSources(schemas); schema = factory.newSchema(sources.toArray(new Source[sources.size()])); javaxSchemas.put(schemasId, schema); } catch (Exception e) { throw new ConfigurationException("cannot read schema's [" + schemasId + "]", e); } } return schema; }
/** * Creates a new validator. * @param resolver The resolver for resources * @return A new validator */ private static Validator newValidator(final LSResourceResolver resolver) { try { final Validator validator = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema() .newValidator(); validator.setResourceResolver(resolver); return validator; } catch (final SAXException ex) { throw new IllegalStateException(ex); } }
public static LSResourceResolver getDefault(){ return new LSResourceResolverImpl(); }