/** * Loops through the SchemaDocuments in the Schema and returns null or the first which has a * matching systemID to the given systemID; * * @throws IllegalArgumentException if Schema is null or contains no SchemaDocuments * @param schema the Schema to search * @param systemId the systemID to look for * @return the matching SchemaDocuemnt or null */ public static XMLSchemaDocument getSchemaDocumentFromSchema(XMLSchema schema, String systemId) { if (schema == null) { throw new IllegalArgumentException("Schema must be non null."); } if (schema.getRootDocument().getSystemID().equals(systemId)) { return schema.getRootDocument(); } else { for (XMLSchemaDocument sd : schema.getAdditionalDocuments()) { if (sd.getSystemID().equals(systemId)) { return sd; } } } return null; }
/** * Constructs a Schema using the given namespace and populates it with SchemaDocuments with the * given files' contents. Note: this does not check that the files actually represent valid XML * Schemas, nor does it check that the are all of the same specified namespace. * * @param namespace the namespace to assign to the schema * @param schemaFiles the files to create the schema with * @return the constructed Schema * @throws java.io.FileNotFoundException if a file is not valid * @throws java.io.IOException if a file is not valid */ public static XMLSchema createSchema(URI namespace, List<File> schemaFiles) throws FileNotFoundException, IOException { if (schemaFiles == null || schemaFiles.size() == 0) { throw new IllegalArgumentException("schemaFiles must be a valid array of files."); } XMLSchemaDocument root = createSchemaDocument(schemaFiles.get(0)); Set<XMLSchemaDocument> docs = new HashSet<XMLSchemaDocument>(schemaFiles.size() - 1); for (int i = 1; i < schemaFiles.size(); i++) { docs.add(createSchemaDocument(schemaFiles.get(i))); } XMLSchema schema = new XMLSchema(); schema.setTargetNamespace(namespace); schema.setRootDocument(root); schema.getAdditionalDocuments().addAll(docs); return schema; }