示例#1
0
 @Override
 public String convert(NodeInfo source) {
   switch (source.getNodeKind()) {
     case Node.DOCUMENT_NODE:
       return "";
     case Node.TEXT_NODE:
     case Node.CDATA_SECTION_NODE:
       return "text()";
     case Node.COMMENT_NODE:
       return "comment()";
     case Node.PROCESSING_INSTRUCTION_NODE:
       return "processing-instruction('" + source.getDisplayName() + "')";
     case Node.ELEMENT_NODE:
       String prefix = nsContext.getPrefix(source.getURI());
       String name = source.getLocalPart();
       return StringUtil.isEmpty(prefix) ? name : prefix + ':' + name;
     case Node.ATTRIBUTE_NODE:
       if (Namespaces.URI_XMLNS.equals(source.getURI()))
         return "namespace::" + source.getLocalPart();
       prefix = nsContext.getPrefix(source.getURI());
       name = source.getLocalPart();
       return '@' + (StringUtil.isEmpty(prefix) ? name : prefix + ':' + name);
     case NodeType.NAMESPACE:
       return "namespace::" + source.getLocalPart();
     default:
       return null;
   }
 }
示例#2
0
 /**
  * For the moment, this method can only transform very simple XPath, like <tt>/n:foo/b:bar/@a</tt>
  *
  * @param xpath
  * @param ns
  * @return
  */
 public static String normalizeNS(String xpath, NamespaceContext ns) {
   StringTokenizer tokenizer = new StringTokenizer(xpath, "/", true);
   StringBuilder sb = new StringBuilder();
   while (tokenizer.hasMoreTokens()) {
     String token = tokenizer.nextToken();
     if (token.equals("/")) sb.append(token);
     else {
       boolean isAttribute = false;
       if (token.startsWith("@")) {
         token = token.substring(1);
         isAttribute = true;
       }
       int pos = token.indexOf(':');
       if (pos >= 0) {
         String prefix = token.substring(0, pos);
         String localName = token.substring(pos + 1);
         String newPrefix = ns.getPrefix(ns.getNamespaceURI(prefix));
         if (isAttribute && (newPrefix == null || newPrefix.length() == 0)) newPrefix = prefix;
         if (isAttribute) sb.append("@");
         if (newPrefix.length() > 0) sb.append(newPrefix).append(":");
         sb.append(localName);
       } else {
         if (!isAttribute) {
           String localName = token;
           if (XmlUtils.isValidNCName(localName)) {
             String uri = ns.getNamespaceURI("");
             if (uri != null) {
               String newPrefix = ns.getPrefix(uri);
               if (newPrefix.length() > 0) sb.append(newPrefix).append(":");
             }
             sb.append(token);
           } else {
             sb.append(token);
           }
         } else {
           // attribute didn't had a namespace, let it like that
           sb.append("@").append(token);
         }
       }
     }
   }
   return sb.toString();
 }
示例#3
0
 public static String getPath(Stack<QName> stack, NamespaceContext ctx) {
   if (ctx == null) logger.fatal("NamespaceContext is null");
   StringBuilder ret = new StringBuilder();
   for (QName q : stack) {
     if (q == null) throw new IllegalStateException("q is null");
     ret.append("/");
     String prefix = ctx.getPrefix(q.getNamespaceURI());
     if (prefix != null && prefix.length() > 0) ret.append(prefix).append(":");
     ret.append(q.getLocalPart());
   }
   return ret.toString();
 }
 public String getPrefix(String uri) {
   if (fNamespaceContext != null) {
     if (uri == null) {
       uri = XMLConstants.NULL_NS_URI;
     }
     String prefix = fNamespaceContext.getPrefix(uri);
     if (prefix == null) {
       prefix = XMLConstants.DEFAULT_NS_PREFIX;
     }
     return (fSymbolTable != null) ? fSymbolTable.addSymbol(prefix) : prefix.intern();
   }
   return null;
 }
  public static String _printQName(QName val, NamespaceContext nsc) {
    // Double-check
    String qname;
    String prefix = nsc.getPrefix(val.getNamespaceURI());
    String localPart = val.getLocalPart();

    if (prefix == null || prefix.length() == 0) { // be defensive
      qname = localPart;
    } else {
      qname = prefix + ':' + localPart;
    }

    return qname;
  }