/** * Is there an import for a particular namespace in a schema? * * @param schema * @param namespaceUri */ public static boolean schemaImportsNamespace(XmlSchema schema, String namespaceUri) { List<XmlSchemaExternal> externals = schema.getExternals(); for (XmlSchemaExternal what : externals) { if (what instanceof XmlSchemaImport) { XmlSchemaImport imp = (XmlSchemaImport) what; // already there. if (namespaceUri.equals(imp.getNamespace())) { return true; } } } return false; }
/** * Assist in managing the required <import namespace='uri'> for imports of peer schemas. * * @param schema * @param namespaceUri */ public static void addImportIfNeeded(XmlSchema schema, String namespaceUri) { // no need to import nothing or the XSD schema, or the schema we are fixing. if ("".equals(namespaceUri) || Constants.URI_2001_SCHEMA_XSD.equals(namespaceUri) || schema.getTargetNamespace().equals(namespaceUri)) { return; } List<XmlSchemaExternal> externals = schema.getExternals(); for (XmlSchemaExternal what : externals) { if (what instanceof XmlSchemaImport) { XmlSchemaImport imp = (XmlSchemaImport) what; // already there. if (namespaceUri.equals(imp.getNamespace())) { return; } } } XmlSchemaImport imp = new XmlSchemaImport(schema); imp.setNamespace(namespaceUri); }