/**
* Set up <schema> namespaces appropriately and append that attr
* into specified element
*/
private Element setupNamespaces(Document schemaDocs, XmlSchema schemaObj) {
NamespacePrefixList ctx = schemaObj.getNamespaceContext();
if (ctx == null) {
schemaObj.schema_ns_prefix = null;
xsdPrefix = null;
} else {
schemaObj.schema_ns_prefix = ctx.getPrefix(xsdNamespace);
xsdPrefix = schemaObj.schema_ns_prefix;
}
if(xsdPrefix == null) {
//find a prefix to use
xsdPrefix = XMLConstants.DEFAULT_NS_PREFIX;
// Note: NULL_NS_URI is *not* the same as the null reference!
// Java 1.4 hasn't got NULL_NS_URI, it's just "".
if (ctx != null && !"".equals(ctx.getNamespaceURI(xsdPrefix))) {
xsdPrefix = "xsd";
}
int count = 0;
while (ctx != null && !"".equals(ctx.getNamespaceURI(xsdPrefix))) {
xsdPrefix = "xsd" + ++count;
}
schemaObj.schema_ns_prefix = xsdPrefix;
}
Element schemaEl = createNewElement(schemaDocs, "schema",
schemaObj.schema_ns_prefix, XmlSchema.SCHEMA_NS);
if (ctx != null) {
String[] prefixes = ctx.getDeclaredPrefixes();
for (int i = 0; i < prefixes.length; i++) {
String prefix = prefixes[i];
String uri = ctx.getNamespaceURI(prefix);
if (uri != null && prefix != null) {
if ("".equals(prefix) || !schema_ns.containsKey(uri)) {
schema_ns.put(uri, prefix);
}
prefix = (prefix.length() > 0) ? "xmlns:" + prefix : "xmlns";