List of usage examples for org.dom4j Namespace NO_NAMESPACE
Namespace NO_NAMESPACE
To view the source code for org.dom4j Namespace NO_NAMESPACE.
Click Source Link
From source file:nl.tue.gale.ae.processor.XMLProcessor.java
License:Open Source License
public void traverse(Element element, Resource resource) throws ProcessorException { if (element == null) return;// w ww. j a va 2 s .c o m Namespace ns = element.getNamespace(); String tag = null; if (ns != Namespace.NO_NAMESPACE) { tag = "{" + ns.getURI() + "}" + element.getName(); if (!moduleTable.containsKey(tag)) tag = null; } if (tag == null) tag = element.getName(); if (moduleTable.containsKey(tag)) { Module mod = moduleTable.get(tag); if (mod.getMimeToHandle().contains(GaleContext.mime(resource))) { try { element = moduleTable.get(tag).traverse(element, resource); } catch (Exception e) { e.printStackTrace(); element = (Element) GaleUtil.replaceNode(element, GaleUtil.createErrorElement("[" + e.getMessage() + "]")); } } } traverseChildren(element, resource); if ("text/xhtml".equals(GaleContext.mime(resource))) if (element != null) if ("http://www.w3.org/1999/xhtml".equals(element.getNamespaceURI())) element.setQName(DocumentFactory.getInstance().createQName(element.getName(), "", "http://www.w3.org/1999/xhtml")); }
From source file:org.apache.archiva.xml.XMLReader.java
License:Apache License
/** * Remove namespaces from element recursively. */// w w w . j a v a 2 s . c o m @SuppressWarnings("unchecked") public void removeNamespaces(Element elem) { elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName())); Node n; Iterator<Node> it = elem.elementIterator(); while (it.hasNext()) { n = it.next(); switch (n.getNodeType()) { case Node.ATTRIBUTE_NODE: ((Attribute) n).setNamespace(Namespace.NO_NAMESPACE); break; case Node.ELEMENT_NODE: removeNamespaces((Element) n); break; } } }
From source file:org.danann.cernunnos.xml.AppendNodeTask.java
License:Apache License
@SuppressWarnings("unchecked") public void perform(TaskRequest req, TaskResponse res) { // Figure out where to put the content... Branch p = null;/*from w ww . j a va 2 s . co m*/ int index; if (sibling != null) { Node sib = (Node) sibling.evaluate(req, res); p = sib.getParent(); index = p.indexOf(sib) + 1; } else { // Work from the PARENT... p = (Branch) parent.evaluate(req, res); index = p.content().size(); } // Figure out what content to add... List list = null; if (content != null && content.size() > 0) { list = content; } else { list = new LinkedList(); list.add(node.evaluate(req, res)); } // Evaluate phrases & add... for (Object o : list) { Node n = (Node) ((Node) o).clone(); NodeProcessor.evaluatePhrases(n, grammar, req, res); // If the parent is an element, check if we should // carry the parent namespace over to the child... if ((Boolean) apply_namespace.evaluate(req, res) && p.getNodeType() == Node.ELEMENT_NODE && !((Element) p).getNamespace().equals(Namespace.NO_NAMESPACE)) { // We know the parent is an Element w/ a namespace, // is the child (also) an Element w/ none? if (n.getNodeType() == Node.ELEMENT_NODE && ((Element) n).getNamespace().equals(Namespace.NO_NAMESPACE)) { // Yes -- we need to port the namespace forward... Namespace nsp = ((Element) p).getNamespace(); if (log.isTraceEnabled()) { StringBuffer msg = new StringBuffer(); msg.append("Adding the following namespace to <").append(n.getName()).append(">: ") .append(nsp); log.trace(msg.toString()); } NodeProcessor.applyNamespace(nsp, (Element) n); } } // Although they *are* nodes, attributes are not technically // content, and therefore they must have special treatment... if (p.getNodeType() == Node.ELEMENT_NODE && n.getNodeType() == Node.ATTRIBUTE_NODE) { // Add attributes by calling addAttribute on the Element contract... ((Element) p).add((Attribute) n); } else { // Add everything else as 'content'... p.content().add(index++, n); } } }
From source file:org.danann.cernunnos.xml.NodeProcessor.java
License:Apache License
/** * Recursively applies the specified <code>Namespace</code> to the specified * <code>Element</code>, unless the <code>Element</code> (or any child * <code>Element</code>) already specifies a <code>Namespace</code>. * * @param nsp Namespace to apply.//from w ww . j a va 2 s. c o m * @param e XML structure upon which to apply the Namespace. */ public static void applyNamespace(Namespace nsp, Element e) { // Assertions... if (nsp == null) { String msg = "Argument 'nsp' cannot be null."; throw new IllegalArgumentException(msg); } if (e == null) { String msg = "Argument 'e [Element]' cannot be null."; throw new IllegalArgumentException(msg); } if (e.getNamespace().equals(Namespace.NO_NAMESPACE)) { e.setQName(new QName(e.getName(), nsp)); for (Object n : e.elements()) { applyNamespace(nsp, (Element) n); } } }
From source file:org.danann.cernunnos.xml.PrependNodeTask.java
License:Apache License
@SuppressWarnings("unchecked") public void perform(TaskRequest req, TaskResponse res) { // Figure out where to put the content... Branch p = null;/*from w ww . ja va 2s . c om*/ int index; if (sibling != null) { Node sib = (Node) sibling.evaluate(req, res); p = sib.getParent(); index = p.indexOf(sib); } else { // Work from the PARENT... p = (Branch) parent.evaluate(req, res); index = 0; } // Figure out what content to add... List list = null; if (content != null && content.size() > 0) { list = content; } else { list = new LinkedList(); list.add(node.evaluate(req, res)); } // Evaluate phrases & add... for (Object o : list) { Node n = (Node) ((Node) o).clone(); NodeProcessor.evaluatePhrases(n, grammar, req, res); // If the parent is an element, check if we should // carry the parent namespace over to the child... if ((Boolean) apply_namespace.evaluate(req, res) && p.getNodeType() == Node.ELEMENT_NODE && !((Element) p).getNamespace().equals(Namespace.NO_NAMESPACE)) { // We know the parent is an Element w/ a namespace, // is the child (also) an Element w/ none? if (n.getNodeType() == Node.ELEMENT_NODE && ((Element) n).getNamespace().equals(Namespace.NO_NAMESPACE)) { // Yes -- we need to port the namespace forward... Namespace nsp = ((Element) p).getNamespace(); if (log.isTraceEnabled()) { StringBuffer msg = new StringBuffer(); msg.append("Adding the following namespace to <").append(n.getName()).append(">: ") .append(nsp); log.trace(msg.toString()); } NodeProcessor.applyNamespace(nsp, (Element) n); } } // Although they *are* nodes, attributes are not technically // content, and therefore they must have special treatment... if (p.getNodeType() == Node.ELEMENT_NODE && n.getNodeType() == Node.ATTRIBUTE_NODE) { // Add attributes by calling addAttribute on the Element contract... ((Element) p).add((Attribute) n); } else { // Add everything else as 'content'... p.content().add(index++, n); } } }
From source file:org.etudes.component.app.melete.MeleteImportServiceImpl.java
License:Apache License
private void removeNamespaces(Element elem) { elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName())); Node n = null;//from www . ja v a 2s .c o m for (int i = 0; i < elem.content().size(); i++) { n = (Node) elem.content().get(i); if (n.getNodeType() == Node.ATTRIBUTE_NODE) ((Attribute) n).setNamespace(Namespace.NO_NAMESPACE); if (n.getNodeType() == Node.ELEMENT_NODE) removeNamespaces((Element) n); } }
From source file:org.jivesoftware.openfire.plugin.Xep227Exporter.java
License:Apache License
/** * Adding offline messages, if there are some. * //from w w w. ja va 2 s .c o m * @param hostname * host name * @param userElement * DOM element * @param userName * user name */ @SuppressWarnings("unchecked") private void exportOfflineMessages(String hostname, Element userElement, String userName) { Collection<OfflineMessage> offlineMessages = offlineMessagesStore.getMessages(userName, false); if (!offlineMessages.isEmpty()) { Element offlineElement = userElement.addElement(OFFLINE_MESSAGES_ELEMENT_NAME); for (OfflineMessage offMessage : offlineMessages) { Element messageElement = offlineElement.addElement(new QName(MESSAGE_ELEMENT_NAME, JABBER_MSG_NS)); for (Object att : offMessage.getElement().attributes()) { Attribute attribute = (Attribute) att; messageElement.addAttribute(attribute.getQName(), attribute.getValue()); } for (Iterator<Element> iterator = offMessage.getElement().elementIterator(); iterator.hasNext();) { Element element = iterator.next(); messageElement.add(element.createCopy(new QName(element.getName(), (element.getNamespace() == Namespace.NO_NAMESPACE ? JABBER_MSG_NS : element.getNamespace())))); } /** * Adding delay element */ Element delayElement = messageElement.addElement("delay", "urn:xmpp:delay"); delayElement.addAttribute(FROM_NAME, hostname); delayElement.addAttribute("stamp", XMPPDateTimeFormat.format(offMessage.getCreationDate())); delayElement.addText("Offline Storage"); } } }
From source file:org.jivesoftware.openfire.websocket.WebSocketConnection.java
License:Open Source License
@Override public void deliver(Packet packet) throws UnauthorizedException { final String xml; if (Namespace.NO_NAMESPACE.equals(packet.getElement().getNamespace())) { // use string-based operation here to avoid cascading xmlns wonkery StringBuilder packetXml = new StringBuilder(packet.toXML()); packetXml.insert(packetXml.indexOf(" "), " xmlns=\"jabber:client\""); xml = packetXml.toString();/*www. ja v a 2s. c om*/ } else { xml = packet.toXML(); } if (validate()) { deliverRawText(xml); } else { // use fallback delivery mechanism (offline) getPacketDeliverer().deliver(packet); } }
From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JXMLOutputter.java
License:Open Source License
/** * <p>/*from w ww . j av a 2 s . c o m*/ * This will handle printing out an <code>{@link Element}</code>, * its <code>{@link Attribute}</code>s, and its value. * </p> * * @param element <code>Element</code> to output. * @param out <code>Writer</code> to write to. * @param indent <code>int</code> level of indention. * @param namespaces <code>List</code> stack of Namespaces in scope. */ protected void printElement(Element element, Writer out, int indentLevel, TDOM4JNamespaceStack namespaces) throws IOException { List mixedContent = element.elements(); boolean empty = mixedContent.size() == 0; boolean stringOnly = !empty && mixedContent.size() == 1 && mixedContent.get(0) instanceof String; // Print beginning element tag /* maybe the doctype, xml declaration, and processing instructions should only break before and not after; then this check is unnecessary, or maybe the println should only come after and never before. Then the output always ends with a newline */ indent(out, indentLevel); // Print the beginning of the tag plus attributes and any // necessary namespace declarations out.write("<"); out.write(element.getQualifiedName()); int previouslyDeclaredNamespaces = namespaces.size(); 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); if (!ns.getURI().equals(uri)) { // output a new namespace decl namespaces.push(ns); printNamespace(ns, out); } } // Print out additional namespace declarations 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); printNamespace(additional, out); } } } printAttributes(element.attributes(), element, out, namespaces); // handle "" string same as empty if (stringOnly) { String elementText = trimText ? element.getTextTrim() : element.getText(); if (elementText == null || elementText.equals("")) { empty = true; } } if (empty) { // Simply close up if (!expandEmptyElements) { out.write(" />"); } else { out.write("></"); out.write(element.getQualifiedName()); out.write(">"); } maybePrintln(out); } else { // we know it's not null or empty from above out.write(">"); if (stringOnly) { // if string only, print content on same line as tags printElementContent(element, out, indentLevel, namespaces, mixedContent); } else { maybePrintln(out); printElementContent(element, out, indentLevel, namespaces, mixedContent); indent(out, indentLevel); } out.write("</"); out.write(element.getQualifiedName()); out.write(">"); maybePrintln(out); } // remove declared namespaces from stack while (namespaces.size() > previouslyDeclaredNamespaces) { namespaces.pop(); } }
From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JXMLOutputter.java
License:Open Source License
/** * <p>/*from w w w . j a v a 2 s.co 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("\""); } }