/** 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; } } }
/** @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); }
@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 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); }