[jdom-interest] Protected methods to make private

Laurent Bihanic laurent.bihanic at atosorigin.com
Mon Jun 2 09:40:38 PDT 2003


Jason Hunter wrote:
> SAXHandler:
>   flushCharacters(String)
> 
> Brad, I seem to recall you had a performance trick involving this 
> method?  What's the point of keeping it?

This method was introduced to allow implementing handlers that strip the white 
spaces from the text content such as:

protected void flushCharacters(String data) throws SAXException {
     super.flushCharacters(data.trim());
}

> SAXHandler:
>   currentElement
>   getCurrentElement()
> 
> I see org.jdom.contrib.input.scanner.ElementScanner.ElementBuilder uses 
> currentElement.  But must they both be exposed?  Should 
> getCurrentElement() be public perhaps?

getCurrentElement can be made public.
Yet ElementScanner.ElementBuilder needs to override it because it builds 
rootlees subtrees and thus has to remove the consistency checks SAXHandler's 
getCurrentElement does. If currentElement is made private, 
ElementScanner.ElementBuilder.getCurrentElement() will look like:

       public Element getCurrentElement() throws SAXException {
          try {
             return super.getCurrentElement();
          }
          catch (SAXException e) {
             return null;
          }
       }


> SAXHandler:
>   atRoot
> 
> I see JDOMResult.FragmentHandler uses this.  That's an odd property to 
> expose.  Could this be done another way?  Laurent?

Sure, this could be removed if SAXHandler provided a protected pushElement() 
method.
As JDOMResult may receive a node list and not a document, it needs to add a 
dummy root element to the being-built document before the parse begins. To do 
that, I simply pasted some code from startElement.

For example, in SAXHandler:

protected void pushElement(Element element) {
     if (atRoot) {
         document.setRootElement(element);
         atRoot = false;
     } else {
         getCurrentElement().addContent(element);
     }
     currentElement = element;
}

Then JDOMResult.FragmentHandler constructor could be changed to:
     public FragmentHandler(JDOMFactory factory) {
       super(factory);

       // Add a dummy root element to the being-built document as XSL
       // transformation can output node lists instead of well-formed
       // documents.
       this.pushElement(dummyRoot);
     }

Laurent




More information about the jdom-interest mailing list