/** * This will invoke the callbacks for the content of an element. * * @param node a <code>Content</code> node. * @param namespaces <code>List</code> stack of Namespaces in scope. */ private void elementContent(Content node, NamespaceStack stack) throws JDOMException { // update locator if (locator != null) { locator.setNode(node); } if (node instanceof Element) { element((Element) node, stack); } else if (node instanceof CDATA) { cdata(((CDATA) node).getText()); } else if (node instanceof Text) { // contentHandler.characters() characters(((Text) node).getText()); } else if (node instanceof ProcessingInstruction) { // contentHandler.processingInstruction() processingInstruction((ProcessingInstruction) node); } else if (node instanceof Comment) { // lexicalHandler.comment() comment(((Comment) node).getText()); } else if (node instanceof EntityRef) { // contentHandler.skippedEntity() entityRef((EntityRef) node); } 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: " + node)); } }
/** * This will recursively invoke all of the callbacks for a particular element. * * @param element <code>Element</code> used in callbacks. * @param namespaces <code>List</code> stack of Namespaces in scope. */ private void element(Element element, NamespaceStack stack) throws JDOMException { AttributesImpl nsAtts = null; // The namespaces as xmlns attributes // contentHandler.startPrefixMapping() stack.push(element); try { for (Namespace ns : stack.addedForward()) { startPrefixMapping(ns); nsAtts = this.addNsAttribute(nsAtts, ns); } // contentHandler.startElement() startElement(element, nsAtts); // handle content in the element elementContent(element.getContent(), stack); // update locator if (locator != null) { locator.setNode(element); } // contentHandler.endElement() endElement(element); } finally { stack.pop(); } // contentHandler.endPrefixMapping() // de-map in reverse order to the mapping. for (Namespace ns : stack.addedReverse()) { endPrefixMapping(ns); } }
/** * This method tells you the line of the XML file being parsed. For an in-memory document, it's * meaningless. The location is only valid for the current parsing lifecycle, but the document has * already been parsed. Therefore, it returns -1 for both line and column numbers. * * @param document JDOM <code>Document</code>. */ private void documentLocator(Document document) { locator = new JDOMLocator(); String publicID = null; String systemID = null; if (document != null) { DocType docType = document.getDocType(); if (docType != null) { publicID = docType.getPublicID(); systemID = docType.getSystemID(); } } locator.setPublicId(publicID); locator.setSystemId(systemID); locator.setLineNumber(-1); locator.setColumnNumber(-1); contentHandler.setDocumentLocator(locator); }
/** * This will output the <code>JDOM Document</code>, firing off the SAX events that have been * registered. * * @param document <code>JDOM Document</code> to output. * @throws JDOMException if any error occurred. */ public void output(Document document) throws JDOMException { if (document == null) { return; } // contentHandler.setDocumentLocator() documentLocator(document); // contentHandler.startDocument() startDocument(); // Fire DTD events if (this.reportDtdEvents) { dtdEvents(document); } // Handle root element, as well as any root level // processing instructions and comments Iterator i = document.getContent().iterator(); while (i.hasNext()) { Object obj = i.next(); // update locator locator.setNode(obj); if (obj instanceof Element) { // process root element and its content element(document.getRootElement(), new NamespaceStack()); } else if (obj instanceof ProcessingInstruction) { // contentHandler.processingInstruction() processingInstruction((ProcessingInstruction) obj); } else if (obj instanceof Comment) { // lexicalHandler.comment() comment(((Comment) obj).getText()); } } // contentHandler.endDocument() endDocument(); }
/** * This will recursively invoke all of the callbacks for a particular element. * * @param element <code>Element</code> used in callbacks. * @param namespaces <code>List</code> stack of Namespaces in scope. */ private void element(Element element, NamespaceStack namespaces) throws JDOMException { // used to check endPrefixMapping int previouslyDeclaredNamespaces = namespaces.size(); // contentHandler.startPrefixMapping() Attributes nsAtts = startPrefixMapping(element, namespaces); // contentHandler.startElement() startElement(element, nsAtts); // handle content in the element elementContent(element.getContent(), namespaces); // update locator locator.setNode(element); // contentHandler.endElement() endElement(element); // contentHandler.endPrefixMapping() endPrefixMapping(namespaces, previouslyDeclaredNamespaces); }