// accumulate namespace declarations from this element
ArrayList nss = null;
NamedNodeMap attrs = element.getAttributes();
int size = attrs.getLength();
for (int i = 0; i < size; i++) {
Attr attr = (Attr)attrs.item(i);
if (XMLNS_NAMESPACE.equals(attr.getNamespaceURI())) {
// found namespace declaration, convert to simple prefix
String declpref = attr.getLocalName();
if ("xmlns".equals(declpref)) {
declpref = null;
}
String decluri = attr.getValue();
if (findNamespaceIndex(declpref, decluri) < 0) {
if (nss == null) {
nss = new ArrayList();
}
addNamespace(declpref, decluri, nss);
}
}
}
// check that namespace used by element name is defined
String prefix = element.getPrefix();
String uri = element.getNamespaceURI();
int nsi = findNamespaceIndex(prefix, uri);
if (nsi < 0) {
if (nss == null) {
nss = new ArrayList();
}
addNamespaceUnique(prefix, uri, nss);
}
// check that every namespace used by an attribute is defined
for (int i = 0; i < size; i++) {
Attr attr = (Attr)attrs.item(i);
if (!XMLNS_NAMESPACE.equals(attr.getNamespaceURI())) {
// found normal attribute, check namespace and prefix
String attruri = attr.getNamespaceURI();
if (attruri != null) {
String attrpref = attr.getPrefix();
if (findNamespaceIndex(attrpref, attruri) < 0) {
if (nss == null) {
nss = new ArrayList();
}
addNamespaceUnique(attrpref, attruri, nss);
}
}
}
}
// check for default namespace setting
int defind = -1;
String defuri = null;
if (nss != null) {
for (int i = 0; i < nss.size(); i += 2) {
if ("".equals(nss.get(i))) {
defind = i / 2;
defuri = (String)nss.get(i+1);
}
}
}
// check for namespace declarations required
String[] uris = null;
String name = element.getLocalName();
if (name == null) {
name = element.getTagName();
}
if (nss == null) {
m_xmlWriter.startTagOpen(nsi, name);
} else {
int base = getNextNamespaceIndex();
if (defind >= 0) {
m_defaultNamespaceIndex = base + defind;
m_defaultNamespaceURI = defuri;
}
int length = nss.size() / 2;
uris = new String[length];
int[] nums = new int[length];
String[] prefs = new String[length];
for (int i = 0; i < length; i++) {
prefs[i] = (String)nss.get(i*2);
uris[i] = (String)nss.get(i*2+1);
nums[i] = base + i;
if (nsi < 0 && uri.equals(uris[i])) {
if ((prefix == null && prefs[i] == "") ||
(prefix != null && prefix.equals(prefs[i]))) {
nsi = base + i;
}
}
}
m_xmlWriter.pushExtensionNamespaces(uris);
m_xmlWriter.startTagNamespaces(nsi, name, nums, prefs);
if (defind >= 0) {
m_defaultNamespaceIndex = defind;
m_defaultNamespaceURI = defuri;
}
}
// add attributes if present
for (int i = 0; i < size; i++) {
Attr attr = (Attr)attrs.item(i);
if (!XMLNS_NAMESPACE.equals(attr.getNamespaceURI())) {
int index = 0;
String apref = attr.getPrefix();
if (apref != null) {
index = findNamespaceIndex(apref, attr.getNamespaceURI());
}
String aname = attr.getLocalName();
if (aname == null) {
aname = attr.getName();
}
m_xmlWriter.addAttribute(index, aname, attr.getValue());
}
}
// check for content present
NodeList nodes = element.getChildNodes();