Search in sources :

Example 1 with NamedNodeMap

use of org.loboevolution.html.node.NamedNodeMap in project LoboEvolution by LoboEvolution.

the class XPathNSResolverImpl method lookupNamespaceURI.

/**
 * {@inheritDoc}
 */
@Override
public String lookupNamespaceURI(String prefix) {
    String namespace = null;
    if (prefix.equals("xml")) {
        namespace = Constants.S_XMLNAMESPACEURI;
    } else {
        NodeType type;
        while ((null != parent) && (null == namespace) && (((type = parent.getNodeType()) == NodeType.ELEMENT_NODE) || ((type = parent.getNodeType()) == NodeType.DOCUMENT_NODE) || (type == NodeType.ENTITY_REFERENCE_NODE))) {
            if (type == NodeType.DOCUMENT_NODE) {
                Document document = (Document) parent;
                Element docelm = document.getDocumentElement();
                if (docelm != null && docelm.getNodeName().indexOf(prefix.toUpperCase() + ":") == 0) {
                    return docelm.getNamespaceURI();
                }
            }
            if (type == NodeType.ELEMENT_NODE) {
                if (parent.getNodeName().indexOf(prefix.toUpperCase() + ":") == 0) {
                    return parent.getNamespaceURI();
                }
                NamedNodeMap nnm = ((Element) parent).getAttributes();
                for (int i = 0; i < nnm.getLength(); i++) {
                    Node attr = nnm.item(i);
                    String aname = attr.getNodeName();
                    boolean isPrefix = aname.startsWith("xmlns:");
                    if (isPrefix || aname.equals("xmlns")) {
                        int index = aname.indexOf(':');
                        String p = isPrefix ? aname.substring(index + 1) : "";
                        if (p.equals(prefix)) {
                            namespace = attr.getNodeValue();
                            break;
                        }
                    }
                }
            }
            parent = parent.getParentNode();
        }
    }
    return namespace;
}
Also used : NamedNodeMap(org.loboevolution.html.node.NamedNodeMap) NodeType(org.loboevolution.type.NodeType) Element(org.loboevolution.html.node.Element) Node(org.loboevolution.html.node.Node) Document(org.loboevolution.html.node.Document)

Aggregations

Document (org.loboevolution.html.node.Document)1 Element (org.loboevolution.html.node.Element)1 NamedNodeMap (org.loboevolution.html.node.NamedNodeMap)1 Node (org.loboevolution.html.node.Node)1 NodeType (org.loboevolution.type.NodeType)1