Exemplo n.º 1
0
  public String getNamespacePrefix(String uri) {

    NamespaceContextIterator eachNamespace = getNamespaceContextNodes();
    while (eachNamespace.hasNext()) {
      org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr();
      if (namespaceDecl.getNodeValue().equals(uri)) {
        String candidatePrefix = namespaceDecl.getLocalName();
        if ("xmlns".equals(candidatePrefix)) return "";
        else return candidatePrefix;
      }
    }

    // Find if any of the ancestors' name has this uri
    org.w3c.dom.Node currentAncestor = this;
    while (currentAncestor != null && !(currentAncestor instanceof Document)) {

      if (uri.equals(currentAncestor.getNamespaceURI())) return currentAncestor.getPrefix();
      currentAncestor = currentAncestor.getParentNode();
    }

    return null;
  }
Exemplo n.º 2
0
  public String getNamespaceURI(String prefix) {

    if ("xmlns".equals(prefix)) {
      return XMLNS_URI;
    }

    if ("xml".equals(prefix)) {
      return XML_URI;
    }

    if ("".equals(prefix)) {

      org.w3c.dom.Node currentAncestor = this;
      while (currentAncestor != null && !(currentAncestor instanceof Document)) {

        if (currentAncestor instanceof ElementImpl) {
          QName name = ((ElementImpl) currentAncestor).getElementQName();
          /*
          if (prefix.equals(name.getPrefix())) {
              String uri = name.getNamespaceURI();
              if ("".equals(uri)) {
                  return null;
              }
              else {
                  return uri;
              }
          }*/
          if (((Element) currentAncestor).hasAttributeNS(XMLNS_URI, "xmlns")) {

            String uri = ((Element) currentAncestor).getAttributeNS(XMLNS_URI, "xmlns");
            if ("".equals(uri)) return null;
            else {
              return uri;
            }
          }
        }
        currentAncestor = currentAncestor.getParentNode();
      }

    } else if (prefix != null) {
      // Find if there's an ancester whose name contains this prefix
      org.w3c.dom.Node currentAncestor = this;

      //            String uri = currentAncestor.lookupNamespaceURI(prefix);
      //            return uri;
      while (currentAncestor != null && !(currentAncestor instanceof Document)) {

        /* if (prefix.equals(currentAncestor.getPrefix())) {
            String uri = currentAncestor.getNamespaceURI();
            // this is because the javadoc says getNamespaceURI() is not a computed value
            // and URI for a non-empty prefix cannot be null
            if (uri != null)
                return uri;
        }*/
        // String uri = currentAncestor.lookupNamespaceURI(prefix);
        // if (uri != null) {
        //    return uri;
        // }

        if (((Element) currentAncestor).hasAttributeNS(XMLNS_URI, prefix)) {
          return ((Element) currentAncestor).getAttributeNS(XMLNS_URI, prefix);
        }

        currentAncestor = currentAncestor.getParentNode();
      }
    }

    return null;
  }