List of usage examples for org.dom4j Namespace getPrefix
public String getPrefix()
From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JXMLOutputter.java
License:Open Source License
/** * <p>// w ww. ja v a 2 s . c o m * This will handle printing out any needed <code>{@link Namespace}</code> * declarations. * </p> * * @param ns <code>Namespace</code> to print definition of * @param out <code>Writer</code> to write to. */ protected void printNamespace(Namespace ns, Writer out) throws IOException { out.write(" xmlns"); String prefix = ns.getPrefix(); if (!prefix.equals("")) { out.write(":"); out.write(prefix); } out.write("=\""); out.write(ns.getURI()); out.write("\""); }
From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JXMLOutputter.java
License:Open Source License
/** * <p>// w w w. j av a 2 s . c o m * This will handle printing out an <code>{@link Attribute}</code> list. * </p> * * @param attributes <code>List</code> of Attribute objcts * @param out <code>Writer</code> to write to */ protected void printAttributes(List attributes, Element parent, Writer out, TDOM4JNamespaceStack namespaces) throws IOException { // I do not yet handle the case where the same prefix maps to // two different URIs. For attributes on the same element // this is illegal; but as yet we don't throw an exception // if someone tries to do this Set prefixes = new HashSet(); for (int i = 0, size = attributes.size(); i < size; i++) { Attribute attribute = (Attribute) attributes.get(i); Namespace ns = attribute.getNamespace(); if (ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) { String prefix = ns.getPrefix(); String uri = namespaces.getURI(prefix); if (!ns.getURI().equals(uri)) { // output a new namespace decl printNamespace(ns, out); namespaces.push(ns); } } out.write(" "); out.write(attribute.getQualifiedName()); out.write("="); out.write("\""); out.write(escapeAttributeEntities(attribute.getValue())); out.write("\""); } }
From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JXMLOutputter.java
License:Open Source License
/** ** Gets all the declared namespaces starting at the given element and accumulates the detected namespaces ** within the given namespace stack. The given namespace stack is also returned as the result. ** Please note, that this method has been added for the purpose that not always all given namespaces ** are serialized if they are already given for an ancestor. **//*from w w w . j ava 2 s . com*/ private TDOM4JNamespaceStack getParentNamespaces(Element element, TDOM4JNamespaceStack namespaces) { if (element == null) return namespaces; if (element.getParent() != null) namespaces = getParentNamespaces(element.getParent(), namespaces); Namespace ns = element.getNamespace(); // Add namespace decl only if it's not the XML namespace and it's // not the NO_NAMESPACE with the prefix "" not yet mapped // (we do output xmlns="" if the "" prefix was already used and we // need to reclaim it for the NO_NAMESPACE) if (ns != Namespace.XML_NAMESPACE && !(ns == Namespace.NO_NAMESPACE && namespaces.getURI("") == null)) { String prefix = ns.getPrefix(); String uri = namespaces.getURI(prefix); // Put a new namespace declaratation into the namespace stack if (!ns.getURI().equals(uri)) { namespaces.push(ns); } } // Add additional namespace declarations if not given yet List additionalNamespaces = element.additionalNamespaces(); if (additionalNamespaces != null) { for (int i = 0; i < additionalNamespaces.size(); i++) { Namespace additional = (Namespace) additionalNamespaces.get(i); String prefix = additional.getPrefix(); String uri = namespaces.getURI(prefix); if (!additional.getURI().equals(uri)) namespaces.push(additional); } } return namespaces; }
From source file:org.orbeon.oxf.transformer.xupdate.statement.Utils.java
License:Open Source License
/** * Evaluates an XPath expression// ww w . j a v a2 s.c om */ public static Object evaluate(final URIResolver uriResolver, Object context, final VariableContextImpl variableContext, final DocumentContext documentContext, final LocationData locationData, String select, NamespaceContext namespaceContext) { FunctionContext functionContext = new FunctionContext() { public org.jaxen.Function getFunction(final String namespaceURI, final String prefix, final String localName) { // Override document() and doc() if (/*namespaceURI == null &&*/ ("document".equals(localName) || "doc".equals(localName))) { return new org.jaxen.Function() { public Object call(Context jaxenContext, List args) { try { String url = (String) Dom4jUtils.createXPath("string(.)").evaluate(args.get(0)); Document result = documentContext.getDocument(url); if (result == null) { Source source = uriResolver.resolve(url, locationData.getSystemID()); if (!(source instanceof SAXSource)) throw new ValidationException("Unsupported source type", locationData); XMLReader xmlReader = ((SAXSource) source).getXMLReader(); LocationSAXContentHandler contentHandler = new LocationSAXContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource()); result = contentHandler.getDocument(); documentContext.addDocument(url, result); } return result; } catch (Exception e) { throw new ValidationException(e, locationData); } } }; } else if (/*namespaceURI == null &&*/ "get-namespace-uri-for-prefix".equals(localName)) { return new org.jaxen.Function() { public Object call(Context jaxenContext, List args) { String prefix = (String) Dom4jUtils.createXPath("string(.)").evaluate(args.get(0)); Element element = null; if (args.get(1) instanceof List) { List list = (List) args.get(1); if (list.size() == 1) element = (Element) list.get(0); } else if (args.get(1) instanceof Element) { element = (Element) args.get(1); } if (element == null) throw new ValidationException("An element is expected as the second argument " + "in get-namespace-uri-for-prefix()", locationData); return element.getNamespaceForPrefix(prefix); } }; } else if (/*namespaceURI == null &&*/ "distinct-values".equals(localName)) { return new org.jaxen.Function() { public Object call(Context jaxenContext, List args) { List originalList = args.get(0) instanceof List ? (List) args.get(0) : Collections.singletonList(args.get(0)); List resultList = new ArrayList(); XPath stringXPath = Dom4jUtils.createXPath("string(.)"); for (Iterator i = originalList.iterator(); i.hasNext();) { Object item = (Object) i.next(); String itemString = (String) stringXPath.evaluate(item); if (!resultList.contains(itemString)) resultList.add(itemString); } return resultList; } }; } else if (/*namespaceURI == null &&*/ "evaluate".equals(localName)) { return new org.jaxen.Function() { public Object call(Context jaxenContext, List args) { try { if (args.size() != 3) { try { return XPathFunctionContext.getInstance().getFunction(namespaceURI, prefix, localName); } catch (UnresolvableException e) { throw new ValidationException(e, locationData); } } else { String xpathString = (String) Dom4jUtils.createXPath("string(.)") .evaluate(args.get(0)); XPath xpath = Dom4jUtils.createXPath(xpathString); Map namespaceURIs = new HashMap(); List namespaces = (List) args.get(1); for (Iterator i = namespaces.iterator(); i.hasNext();) { org.dom4j.Namespace namespace = (org.dom4j.Namespace) i.next(); namespaceURIs.put(namespace.getPrefix(), namespace.getURI()); } xpath.setNamespaceURIs(namespaceURIs); return xpath.evaluate(args.get(2)); } } catch (InvalidXPathException e) { throw new ValidationException(e, locationData); } } }; } else if (/*namespaceURI == null &&*/ "tokenize".equals(localName)) { return new org.jaxen.Function() { public Object call(Context jaxenContext, List args) { try { String input = (String) Dom4jUtils.createXPath("string(.)").evaluate(args.get(0)); String pattern = (String) Dom4jUtils.createXPath("string(.)").evaluate(args.get(1)); List result = new ArrayList(); while (input.length() != 0) { int position = input.indexOf(pattern); if (position != -1) { result.add(input.substring(0, position)); input = input.substring(position + 1); } else { result.add(input); input = ""; } } return result; } catch (InvalidXPathException e) { throw new ValidationException(e, locationData); } } }; } else if (/*namespaceURI == null &&*/ "string-join".equals(localName)) { return new org.jaxen.Function() { public Object call(Context jaxenContext, List args) { try { List strings = (List) args.get(0); String pattern = (String) Dom4jUtils.createXPath("string(.)").evaluate(args.get(1)); StringBuilder result = new StringBuilder(); boolean isFirst = true; for (Iterator i = strings.iterator(); i.hasNext();) { if (!isFirst) result.append(pattern); else isFirst = false; String item = (String) (String) Dom4jUtils.createXPath("string(.)") .evaluate(i.next()); result.append(item); } return result.toString(); } catch (InvalidXPathException e) { throw new ValidationException(e, locationData); } } }; } else if (/*namespaceURI == null &&*/ "reverse".equals(localName)) { return new org.jaxen.Function() { public Object call(Context jaxenContext, List args) { try { List result = new ArrayList((List) args.get(0)); Collections.reverse(result); return result; } catch (InvalidXPathException e) { throw new ValidationException(e, locationData); } } }; } else { try { // Go through standard XPath functions return XPathFunctionContext.getInstance().getFunction(namespaceURI, prefix, localName); } catch (UnresolvableException e) { // User-defined function try { final Closure closure = findClosure( variableContext.getVariableValue(namespaceURI, prefix, localName)); if (closure == null) throw new ValidationException( "'" + qualifiedName(prefix, localName) + "' is not a function", locationData); return new org.jaxen.Function() { public Object call(Context context, List args) { return closure.execute(args); } }; } catch (UnresolvableException e2) { throw new ValidationException("Cannot invoke function '" + qualifiedName(prefix, localName) + "', no such function", locationData); } } } } private Closure findClosure(Object xpathObject) { if (xpathObject instanceof Closure) { return (Closure) xpathObject; } else if (xpathObject instanceof List) { for (Iterator i = ((List) xpathObject).iterator(); i.hasNext();) { Closure closure = findClosure(i.next()); if (closure != null) return closure; } return null; } else { return null; } } }; try { // Create XPath XPath xpath = Dom4jUtils.createXPath(select); // Set variable, namespace, and function context if (context instanceof Context) { // Create a new context, as Jaxen may modify the current node in the context (is this a bug?) Context currentContext = (Context) context; Context newContext = new Context(new ContextSupport(namespaceContext, functionContext, variableContext, DocumentNavigator.getInstance())); newContext.setNodeSet(currentContext.getNodeSet()); newContext.setSize(currentContext.getSize()); newContext.setPosition(currentContext.getPosition()); context = newContext; } else { xpath.setVariableContext(variableContext); xpath.setNamespaceContext(namespaceContext); xpath.setFunctionContext(functionContext); } // Execute XPath return xpath.evaluate(context); } catch (Exception e) { throw new ValidationException(e, locationData); } }
From source file:org.orbeon.oxf.xml.dom4j.Dom4jUtils.java
License:Open Source License
/** * Return a Map of namespaces in scope on the given element. *///from w ww . j av a 2 s. c o m public static Map<String, String> getNamespaceContext(Element element) { final Map<String, String> namespaces = new HashMap<String, String>(); for (Element currentNode = element; currentNode != null; currentNode = currentNode.getParent()) { final List currentNamespaces = currentNode.declaredNamespaces(); for (Iterator j = currentNamespaces.iterator(); j.hasNext();) { final Namespace namespace = (Namespace) j.next(); if (!namespaces.containsKey(namespace.getPrefix())) { namespaces.put(namespace.getPrefix(), namespace.getURI()); // TODO: Intern namespace strings to save memory; should use NamePool later // namespaces.put(namespace.getPrefix().intern(), namespace.getURI().intern()); } } } // It seems that by default this may not be declared. However, it should be: "The prefix xml is by definition // bound to the namespace name http://www.w3.org/XML/1998/namespace. It MAY, but need not, be declared, and MUST // NOT be bound to any other namespace name. Other prefixes MUST NOT be bound to this namespace name, and it // MUST NOT be declared as the default namespace." namespaces.put(XMLConstants.XML_PREFIX, XMLConstants.XML_URI); return namespaces; }
From source file:org.orbeon.oxf.xml.dom4j.NonLazyUserDataElement.java
License:Open Source License
/** * @return A clone. The clone will have parent == null but will have any necessary namespace * declarations this element's ancestors. *//* ww w. j av a 2 s . c o m*/ public Object clone() { final NonLazyUserDataElement ret = cloneInternal(); org.dom4j.Element anstr = getParent(); done: if (anstr != null) { final NodeComparator nc = new NodeComparator(); final java.util.TreeSet nsSet = new java.util.TreeSet(nc); do { final java.util.List sibs = anstr.content(); for (final java.util.Iterator itr = sibs.iterator(); itr.hasNext();) { final org.dom4j.Node sib = (org.dom4j.Node) itr.next(); if (sib.getNodeType() != org.dom4j.Node.NAMESPACE_NODE) continue; nsSet.add(sib); } anstr = anstr.getParent(); } while (anstr != null); if (nsSet.isEmpty()) break done; for (final java.util.Iterator itr = nsSet.iterator(); itr.hasNext();) { final org.dom4j.Namespace ns = (org.dom4j.Namespace) itr.next(); final String pfx = ns.getPrefix(); if (ret.getNamespaceForPrefix(pfx) != null) continue; ret.add(ns); } } return ret; }
From source file:org.orbeon.oxf.xml.dom4j.NonLazyUserDataElement.java
License:Open Source License
/** * If parent != null checks with ancestors and removes any redundant namespace declarations. *///from w w w . j a va2s .c om public void setParent(final org.dom4j.Element prnt) { super.setParent(prnt); done: if (prnt != null) { final org.dom4j.Namespace myNs = getNamespace(); if (myNs != org.dom4j.Namespace.NO_NAMESPACE) { final String myPfx = myNs.getPrefix(); final org.dom4j.Namespace prntNs = prnt.getNamespaceForPrefix(myPfx); if (myPfx.equals(prntNs)) { final String myNm = myNs.getName(); final org.dom4j.QName newNm = new org.dom4j.QName(myNm); setQName(newNm); } } if (content == null) break done; for (final java.util.Iterator itr = content.iterator(); itr.hasNext();) { final org.dom4j.Node chld = (org.dom4j.Node) itr.next(); if (chld.getNodeType() != org.dom4j.Node.NAMESPACE_NODE) continue; final org.dom4j.Namespace ns = (org.dom4j.Namespace) chld; final String pfx = ns.getPrefix(); final org.dom4j.Namespace prntNs = prnt.getNamespaceForPrefix(pfx); if (ns.equals(prntNs)) itr.remove(); } } }
From source file:org.orbeon.saxon.dom4j.NodeWrapper.java
License:Open Source License
/** * Get all namespace undeclarations and undeclarations defined on this element. * * @param buffer If this is non-null, and the result array fits in this buffer, then the result * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap. * @return An array of integers representing the namespace declarations and undeclarations present on * this element. For a node other than an element, return null. Otherwise, the returned array is a * sequence of namespace codes, whose meaning may be interpreted by reference to the name pool. The * top half word of each namespace code represents the prefix, the bottom half represents the URI. * If the bottom half is zero, then this is a namespace undeclaration rather than a declaration. * The XML namespace is never included in the list. If the supplied array is larger than required, * then the first unused entry will be set to -1. * <p/>//w ww . j ava 2 s . com * <p>For a node other than an element, the method returns null.</p> */ public int[] getDeclaredNamespaces(int[] buffer) { if (node instanceof Element) { final Element elem = (Element) node; final List namespaces = elem.declaredNamespaces(); if (namespaces == null || namespaces.isEmpty()) { return EMPTY_NAMESPACE_LIST; } final int count = namespaces.size(); if (count == 0) { return EMPTY_NAMESPACE_LIST; } else { int[] result = (buffer == null || count > buffer.length ? new int[count] : buffer); NamePool pool = getNamePool(); int n = 0; for (Iterator i = namespaces.iterator(); i.hasNext();) { final Namespace namespace = (Namespace) i.next(); final String prefix = namespace.getPrefix(); final String uri = namespace.getURI(); result[n++] = pool.allocateNamespaceCode(prefix, uri); } if (count < result.length) { result[count] = -1; } return result; } } else { return null; } }
From source file:org.pentaho.di.trans.steps.getxmldata.GetXMLData.java
License:Apache License
public void prepareNSMap(Element l) { @SuppressWarnings("unchecked") List<Namespace> namespacesList = l.declaredNamespaces(); for (Namespace ns : namespacesList) { if (ns.getPrefix().trim().length() == 0) { data.NAMESPACE.put("pre" + data.NSPath.size(), ns.getURI()); String path = ""; Element element = l;// w w w . j ava 2s.c om while (element != null) { if (element.getNamespacePrefix() != null && element.getNamespacePrefix().length() > 0) { path = GetXMLDataMeta.N0DE_SEPARATOR + element.getNamespacePrefix() + ":" + element.getName() + path; } else { path = GetXMLDataMeta.N0DE_SEPARATOR + element.getName() + path; } element = element.getParent(); } data.NSPath.add(path); } else { data.NAMESPACE.put(ns.getPrefix(), ns.getURI()); } } @SuppressWarnings("unchecked") List<Element> elementsList = l.elements(); for (Element e : elementsList) { prepareNSMap(e); } }
From source file:org.talend.camel.designer.ui.wizards.export.RouteJavaScriptOSGIForESBManager.java
License:Open Source License
private static void moveNamespace(Element treeRoot, String oldNspURI, String newNspURI) { Namespace oldNsp = treeRoot.getNamespace(); if (oldNspURI.equals(oldNsp.getURI())) { Namespace newNsp = Namespace.get(oldNsp.getPrefix(), newNspURI); treeRoot.setQName(QName.get(treeRoot.getName(), newNsp)); treeRoot.remove(oldNsp);/*from ww w .j a va2s. c o m*/ } moveNamespaceInChildren(treeRoot, oldNspURI, newNspURI); }