@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; } }
/** Register a namespace prefix */ private java.lang.String registerPrefix( javax.xml.stream.XMLStreamWriter xmlWriter, java.lang.String namespace) throws javax.xml.stream.XMLStreamException { java.lang.String prefix = xmlWriter.getPrefix(namespace); if (prefix == null) { prefix = generatePrefix(namespace); javax.xml.namespace.NamespaceContext nsContext = xmlWriter.getNamespaceContext(); while (true) { java.lang.String uri = nsContext.getNamespaceURI(prefix); if ((uri == null) || (uri.length() == 0)) { break; } prefix = org.apache.axis2.databinding.utils.BeanUtil.getUniquePrefix(); } xmlWriter.writeNamespace(prefix, namespace); xmlWriter.setPrefix(prefix, namespace); } return prefix; }
@Override public void variableReference(String prefix, String variableName) throws SAXPathException { String uri = prefix.length() == 0 ? "" : nsContext.getNamespaceURI(prefix); if (uri == null) throw new SAXPathException("undeclared prefix: " + prefix); if (variableResolver == null) throw new SAXPathException("VariableResolver is required"); push(new Variable(variableResolver, new javax.xml.namespace.QName(uri, variableName))); }
public String getURI(String prefix) { if (fNamespaceContext != null) { String uri = fNamespaceContext.getNamespaceURI(prefix); if (uri != null && !XMLConstants.NULL_NS_URI.equals(uri)) { return (fSymbolTable != null) ? fSymbolTable.addSymbol(uri) : uri.intern(); } } return null; }
/** * 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(); }
@Override public void startFunction(String prefix, String name) throws SAXPathException { String uri = prefix.length() == 0 ? "" : nsContext.getNamespaceURI(prefix); if (uri == null) throw new SAXPathException("undeclared prefix: " + prefix); if (uri.length() > 0 && functionResolver == null) throw new SAXPathException("FunctionResolver is required"); pushFrame(); push(new javax.xml.namespace.QName(uri, name)); }
/** * Get the namespace URI corresponding to a given prefix. Return null if the prefix is not in * scope. This method first searches any namespaces declared using {@link * #declareNamespace(String, String)}, and then searches any namespace context supplied using * {@link #setNamespaceContext(javax.xml.namespace.NamespaceContext)}. * * @param prefix the namespace prefix * @param useDefault true if the default namespace is to be used when the prefix is "" * @return the uri for the namespace, or null if the prefix is not in scope. Return "" if the * prefix maps to the null namespace. */ public String getURIForPrefix(String prefix, boolean useDefault) { if (prefix.equals("") && !useDefault) { return ""; } else { String uri = (String) namespaces.get(prefix); if (uri == null) { return namespaceContext.getNamespaceURI(prefix); } else { return uri; } } }
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; }
/** @return null if fails to convert. */ public static QName _parseQName(CharSequence text, NamespaceContext nsc) { int length = text.length(); // trim whitespace int start = 0; while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) start++; int end = length; while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) end--; if (end == start) throw new IllegalArgumentException("input is empty"); String uri; String localPart; String prefix; // search ':' int idx = start + 1; // no point in searching the first char. that's not valid. while (idx < end && text.charAt(idx) != ':') idx++; if (idx == end) { uri = nsc.getNamespaceURI(""); localPart = text.subSequence(start, end).toString(); prefix = ""; } else { // Prefix exists, check everything prefix = text.subSequence(start, idx).toString(); localPart = text.subSequence(idx + 1, end).toString(); uri = nsc.getNamespaceURI(prefix); // uri can never be null according to javadoc, // but some users reported that there are implementations that return null. if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken. // error: unbound prefix throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace"); } return new QName(uri, localPart, prefix); }
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; }
@Override public void startNameStep(int axis, String prefix, String localName) throws SAXPathException { Constraint constraint; boolean star = localName.equals("*"); if (star && prefix.length() == 0) constraint = Star.INSTANCE; else { String uri = !allowDefaultPrefixMapping && prefix.length() == 0 ? "" : nsContext.getNamespaceURI(prefix); if (uri == null) throw new SAXPathException("undeclared prefix: " + prefix); if (star) constraint = namespaceURIStub.get(uri); else { if ("*".equals(uri)) constraint = localNameStub.get(localName); else constraint = qnameStub.get(uri, localName); } } startStep(axis, constraint); }
public String getPrefix(String uri) { Iterator<String> iter = context.getPrefixes(uri); if (iter.hasNext()) return iter.next(); return null; }
public ArrayTypeInfo( NamespaceContext namespaceContext, String arrayTypeValue, String offsetString) { if (arrayTypeValue == null) { throw new NullPointerException("arrayTypeValue is null"); } // arrayTypeValue = atype , asize ; // atype = QName , [ rank ] ; // rank = "[" , { "," } , "]" ; // asize = "[" , length , { "," length} , "]" ; // length = DIGIT , { DIGIT } ; // // x:foo[,,,][1,2,3,4] StringTokenizer tokenizer = new StringTokenizer(arrayTypeValue, "[],:", true); List<String> tokens = CastUtils.cast(Collections.list(tokenizer)); // ArrayType QName if (tokens.size() < 3) { throw new DatabindingException("Invalid ArrayType value " + arrayTypeValue); } if (tokens.get(1).equals(":")) { typeName = new QName(namespaceContext.getNamespaceURI(tokens.get(0)), tokens.get(2), tokens.get(0)); tokens = tokens.subList(3, tokens.size()); } else { typeName = new QName("", tokens.get(0)); tokens = tokens.subList(1, tokens.size()); } if (!tokens.get(0).equals("[")) { throw new DatabindingException("Invalid ArrayType value " + arrayTypeValue); } // Rank: [,,,,] boolean hasRank = tokens.subList(1, tokens.size()).contains("["); if (hasRank) { // there are atleast [] there is one rank ranks = 1; for (String token : tokens.subList(1, tokens.size())) { if ("]".equals(token)) { if (tokens.size() < ranks + 1) { throw new DatabindingException("Invalid ArrayType value " + arrayTypeValue); } tokens = tokens.subList(ranks + 1, tokens.size()); break; } else if (",".equals(token)) { ranks++; } else { throw new DatabindingException("Invalid ArrayType value " + arrayTypeValue); } } } // Dimensions [1,2,3,4] for (int i = 1; i < tokens.size(); i = i + 2) { String dimension = tokens.get(i); if ("]".equals(dimension)) { if (i + 1 != tokens.size()) { throw new DatabindingException("Invalid ArrayType value " + arrayTypeValue); } break; } int value; try { value = Integer.parseInt(dimension); } catch (NumberFormatException e) { throw new DatabindingException("Invalid ArrayType value " + arrayTypeValue); } if (value < 1) { throw new DatabindingException("Invalid ArrayType value " + arrayTypeValue); } dimensions.add(value); // verify next token is a ',' or ']' String next = tokens.get(i + 1); if (!",".equals(next) && !"]".equals(next)) { throw new DatabindingException("Invalid ArrayType value " + arrayTypeValue); } } if (dimensions.isEmpty()) { throw new DatabindingException("Invalid ArrayType value " + arrayTypeValue); } if (offsetString != null) { // offset = "[" , length , "]" ; tokens = CastUtils.cast(Collections.list(new StringTokenizer(offsetString, "[]", true))); if (tokens.size() != 3 || !"[".equals(tokens.get(0)) || !"]".equals(tokens.get(2))) { throw new DatabindingException("Invalid Array offset value " + offsetString); } try { offset = Integer.parseInt(tokens.get(1)); } catch (NumberFormatException e) { throw new DatabindingException("Invalid Array offset value " + offsetString); } } }
public String getNamespaceForPrefix(String prefix) { return namespaceContext.getNamespaceURI(prefix); }