Esempio n. 1
0
 void lookupPrefix(org.w3c.dom.Node node) {
   if (node == null) throw new IllegalArgumentException("node must not be null");
   String prefix = node.lookupPrefix(namespace.getUri());
   if (prefix == null) {
     //    check to see if we match the default namespace
     String defaultNamespace = node.lookupNamespaceURI(null);
     if (defaultNamespace == null) defaultNamespace = "";
     String nodeNamespace = namespace.getUri();
     if (nodeNamespace.equals(defaultNamespace)) {
       prefix = "";
     }
   }
   int i = 0;
   while (prefix == null) {
     String generatedPrefix = "e4x_" + i++;
     String generatedUri = node.lookupNamespaceURI(generatedPrefix);
     if (generatedUri == null) {
       prefix = generatedPrefix;
       org.w3c.dom.Node top = node;
       while (top.getParentNode() != null
           && top.getParentNode() instanceof org.w3c.dom.Element) {
         top = top.getParentNode();
       }
       ((org.w3c.dom.Element) top)
           .setAttributeNS(
               "http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace.getUri());
     }
   }
   namespace.setPrefix(prefix);
 }
Esempio n. 2
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;
  }
Esempio n. 3
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;
  }