/**
* Set up <schema> namespaces appropriately and append that attr
* into specified element
*/
private Element setupNamespaces(Document schemaDocs, XmlSchema schemaObj) {
NamespacePrefixList ctx = schemaObj.getNamespaceContext();
schemaObj.schema_ns_prefix = xsdPrefix = ctx == null ? null : ctx.getPrefix(xsdNamespace);
if(xsdPrefix == null) {
//find a prefix to use
xsdPrefix = "";
if (ctx != null && ctx.getNamespaceURI(xsdPrefix) != null) {
xsdPrefix = "xsd";
}
int count = 0;
while (ctx != null && ctx.getNamespaceURI(xsdPrefix) != null) {
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";