/** * Load schema from the given resource. * * @param resources the resources to load from * @param schemaLanguage the language of the schema. Can be {@code XMLConstants.W3C_XML_SCHEMA_NS_URI} or * {@code XMLConstants.RELAXNG_NS_URI}. * @throws IOException if loading failed * @throws SAXException if loading failed * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI * @see javax.xml.XMLConstants#RELAXNG_NS_URI */ public static Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException { Assert.notEmpty(resources, "No resources given"); Assert.hasLength(schemaLanguage, "No schema language provided"); Source[] schemaSources = new Source[resources.length]; XMLReader xmlReader = XMLReaderFactoryUtils.createXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); for (int i = 0; i < resources.length; i++) { Assert.notNull(resources[i], "Resource is null"); Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist"); schemaSources[i] = new ResourceSource(xmlReader, resources[i]); } SchemaFactory schemaFactory = SchemaFactoryUtils.newInstance(schemaLanguage); return schemaFactory.newSchema(schemaSources); }
/** * Load schema from the given resource. * * @param resources the resources to load from * @param schemaLanguage the language of the schema. Can be {@code XMLConstants.W3C_XML_SCHEMA_NS_URI} or * {@code XMLConstants.RELAXNG_NS_URI}. * @throws IOException if loading failed * @throws SAXException if loading failed * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI * @see javax.xml.XMLConstants#RELAXNG_NS_URI */ public static Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException { Assert.notEmpty(resources, "No resources given"); Assert.hasLength(schemaLanguage, "No schema language provided"); Source[] schemaSources = new Source[resources.length]; XMLReader xmlReader = XMLReaderFactoryUtils.createXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); for (int i = 0; i < resources.length; i++) { Assert.notNull(resources[i], "Resource is null"); Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist"); schemaSources[i] = new ResourceSource(xmlReader, resources[i]); } SchemaFactory schemaFactory = SchemaFactoryUtils.newInstance(schemaLanguage); return schemaFactory.newSchema(schemaSources); }