public String getNamespaceOfNode(Node n)
{
String namespaceOfPrefix;
boolean hasProcessedNS;
NSInfo nsInfo;
short ntype = n.getNodeType();
if (Node.ATTRIBUTE_NODE != ntype)
{
Object nsObj = m_NSInfos.get(n); // return value
nsInfo = (nsObj == null) ? null : (NSInfo) nsObj;
hasProcessedNS = (nsInfo == null) ? false : nsInfo.m_hasProcessedNS;
}
else
{
hasProcessedNS = false;
nsInfo = null;
}
if (hasProcessedNS)
{
namespaceOfPrefix = nsInfo.m_namespace;
}
else
{
namespaceOfPrefix = null;
String nodeName = n.getNodeName();
int indexOfNSSep = nodeName.indexOf(':');
String prefix;
if (Node.ATTRIBUTE_NODE == ntype)
{
if (indexOfNSSep > 0)
{
prefix = nodeName.substring(0, indexOfNSSep);
}
else
{
// Attributes don't use the default namespace, so if
// there isn't a prefix, we're done.
return namespaceOfPrefix;
}
}
else
{
prefix = (indexOfNSSep >= 0)
? nodeName.substring(0, indexOfNSSep) : "";
}
boolean ancestorsHaveXMLNS = false;
boolean nHasXMLNS = false;
if (prefix.equals("xml"))
{
namespaceOfPrefix = QName.S_XMLNAMESPACEURI;
}
else
{
int parentType;
Node parent = n;
while ((null != parent) && (null == namespaceOfPrefix))
{
if ((null != nsInfo)
&& (nsInfo.m_ancestorHasXMLNSAttrs
== nsInfo.ANCESTORNOXMLNS))
{
break;
}
parentType = parent.getNodeType();
if ((null == nsInfo) || nsInfo.m_hasXMLNSAttrs)
{
boolean elementHasXMLNS = false;
if (parentType == Node.ELEMENT_NODE)
{
NamedNodeMap nnm = parent.getAttributes();
for (int i = 0; i < nnm.getLength(); i++)
{
Node attr = nnm.item(i);
String aname = attr.getNodeName();
if (aname.charAt(0) == 'x')
{
boolean isPrefix = aname.startsWith("xmlns:");
if (aname.equals("xmlns") || isPrefix)
{
if (n == parent)
nHasXMLNS = true;
elementHasXMLNS = true;
ancestorsHaveXMLNS = true;
String p = isPrefix ? aname.substring(6) : "";
if (p.equals(prefix))
{
namespaceOfPrefix = attr.getNodeValue();
break;
}
}
}
}
}
if ((Node.ATTRIBUTE_NODE != parentType) && (null == nsInfo)
&& (n != parent))
{
nsInfo = elementHasXMLNS
? m_NSInfoUnProcWithXMLNS : m_NSInfoUnProcWithoutXMLNS;
m_NSInfos.put(parent, nsInfo);
}
}
if (Node.ATTRIBUTE_NODE == parentType)
{
parent = getParentOfNode(parent);
}
else
{
m_candidateNoAncestorXMLNS.addElement(parent);
m_candidateNoAncestorXMLNS.addElement(nsInfo);
parent = parent.getParentNode();
}
if (null != parent)
{
Object nsObj = m_NSInfos.get(parent); // return value
nsInfo = (nsObj == null) ? null : (NSInfo) nsObj;
}
}
int nCandidates = m_candidateNoAncestorXMLNS.size();
if (nCandidates > 0)
{
if ((false == ancestorsHaveXMLNS) && (null == parent))
{
for (int i = 0; i < nCandidates; i += 2)
{
Object candidateInfo = m_candidateNoAncestorXMLNS.elementAt(i
+ 1);
if (candidateInfo == m_NSInfoUnProcWithoutXMLNS)
{
m_NSInfos.put(m_candidateNoAncestorXMLNS.elementAt(i),
m_NSInfoUnProcNoAncestorXMLNS);
}
else if (candidateInfo == m_NSInfoNullWithoutXMLNS)
{
m_NSInfos.put(m_candidateNoAncestorXMLNS.elementAt(i),
m_NSInfoNullNoAncestorXMLNS);
}
}
}
m_candidateNoAncestorXMLNS.removeAllElements();
}
}
if (Node.ATTRIBUTE_NODE != ntype)
{
if (null == namespaceOfPrefix)
{
if (ancestorsHaveXMLNS)
{
if (nHasXMLNS)
m_NSInfos.put(n, m_NSInfoNullWithXMLNS);
else
m_NSInfos.put(n, m_NSInfoNullWithoutXMLNS);
}
else
{
m_NSInfos.put(n, m_NSInfoNullNoAncestorXMLNS);
}
}
else
{
m_NSInfos.put(n, new NSInfo(namespaceOfPrefix, nHasXMLNS));
}
}
}
return namespaceOfPrefix;