/** * This will output a list of JDOM nodes as a fragment of an XML document, firing off the SAX * events that have been registered. * * <p><strong>Warning</strong>: This method does not call the {@link * ContentHandler#setDocumentLocator}, {@link ContentHandler#startDocument} and {@link * ContentHandler#endDocument} callbacks on the {@link #setContentHandler ContentHandler}. The * user shall invoke these methods directly prior/after outputting the document fragments. * * @param nodes <code>List</code> of JDOM nodes to output. * @throws JDOMException if any error occurred. * @see #outputFragment(org.jdom2.Content) */ public void outputFragment(List<? extends Content> nodes) throws JDOMException { if ((nodes == null) || (nodes.size() == 0)) { return; } // Output node list as a document fragment. elementContent(nodes, new NamespaceStack()); }
/** * Same story, need to be able to replace NamespaceContainer instances with Namespace content. * * @param results A list potentially containing NamespaceContainer instances * @return The parameter list with NamespaceContainer instances replaced by the wrapped Namespace * instances. */ @SuppressWarnings({"rawtypes", "unchecked"}) private static final List unWrap(List results) { for (ListIterator it = results.listIterator(); it.hasNext(); ) { Object o = it.next(); Object p = unWrapNS(o); if (o != p) { it.set(p); } } return results; }
/** * This will invoke the callbacks for the content of an element. * * @param content element content as a <code>List</code> of nodes. */ private void elementContent(List<? extends Content> content, NamespaceStack stack) throws JDOMException { for (Iterator<? extends Content> i = content.iterator(); i.hasNext(); ) { Object obj = i.next(); if (obj instanceof Content) { this.elementContent((Content) obj, stack); } else { // Not a valid element child. This could happen with // application-provided lists which may contain non // JDOM objects. handleError(new JDOMException("Invalid element content: " + obj)); } } }
/** * This will output a list of JDOM nodes as a document, firing off the SAX events that have been * registered. * * <p><strong>Warning</strong>: This method may output ill-formed XML documents if the list * contains top-level objects that are not legal at the document level (e.g. Text or CDATA nodes, * multiple Element nodes, etc.). Thus, it should only be used to output document portions towards * ContentHandlers capable of accepting such ill-formed documents (such as XSLT processors). * * @param nodes <code>List</code> of JDOM nodes to output. * @throws JDOMException if any error occurred. * @see #output(org.jdom2.Document) */ public void output(List<? extends Content> nodes) throws JDOMException { if ((nodes == null) || (nodes.size() == 0)) { return; } // contentHandler.setDocumentLocator() documentLocator(null); // contentHandler.startDocument() startDocument(); // Process node list. elementContent(nodes, new NamespaceStack()); // contentHandler.endDocument() endDocument(); }