nu.xom.samples
Class IDFilter

java.lang.Object
  extended by nu.xom.NodeFactory
      extended by nu.xom.samples.IDFilter

public class IDFilter
extends nu.xom.NodeFactory

Demonstrates a custom NodeFactory that adds ID attributes to all elements that don't already have one. This is inspired by Example 8-12 in Processing XML with Java. In brief, it demonstrates that major modifications may have to take place in endElement but can still be effectively streamed.

Version:
1.0
Author:
Elliotte Rusty Harold

Constructor Summary
IDFilter()
           
 
Method Summary
 nu.xom.Nodes finishMakingElement(nu.xom.Element element)
           Signals the end of an element.
 nu.xom.Document startMakingDocument()
           Creates a new Document object.
 
Methods inherited from class nu.xom.NodeFactory
finishMakingDocument, makeAttribute, makeComment, makeDocType, makeProcessingInstruction, makeRootElement, makeText, startMakingElement
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IDFilter

public IDFilter()
Method Detail

startMakingDocument

public nu.xom.Document startMakingDocument()
Description copied from class: nu.xom.NodeFactory

Creates a new Document object. The root element of this document is initially set to <root xmlns=http://www.xom.nu/fakeRoot""/>. This is only temporary. As soon as the real root element's start-tag is read, this element is replaced by the real root. This fake root should never be exposed.

The builder calls this method at the beginning of each document, before it calls any other method in this class. Thus this is a useful place to perform per-document initialization tasks.

Subclasses may change the root element, content, or other characteristics of the document returned. However, this method must not return null or the builder will throw a ParsingException.

Overrides:
startMakingDocument in class nu.xom.NodeFactory
Returns:
the newly created Document

finishMakingElement

public nu.xom.Nodes finishMakingElement(nu.xom.Element element)
Description copied from class: nu.xom.NodeFactory

Signals the end of an element. This method should return the Nodes to be added to the tree. They need not contain the Element that was passed to this method, though most often they will. By default the Nodes returned contain only the built element. However, subclasses may return a list containing any number of nodes, all of which will be added to the tree at the current position in the order given by the list (subject to the usual well-formedness constraints, of course. For instance, the list should not contain a DocType object unless the element is the root element, and the document does not already have a DocType). All of the nodes returned must be parentless. If this method returns an empty list, then the element (including all its contents) is not included in the finished document.

To process an element at a time, override this method in a subclass so that it functions as a callback. When you're done processing the Element, return an empty list so that it will be removed from the tree and garbage collected. Be careful not to return an empty list for the root element though. That is, when the element passed to this method is the root element, the list returned must contain exactly one Element object. The simplest way to check this is testing if element.getParent() instanceof Document.

Do not detach element or any of its ancestors while inside this method. Doing so can royally muck up the build.

Overrides:
finishMakingElement in class nu.xom.NodeFactory
Parameters:
element - the finished Element
Returns:
the nodes to be added to the tree


Copyright 2002-2005 Elliotte Rusty Harold
elharo@metalab.unc.edu