[jdom-interest] Reusable JDOM Elements

Norrman Per per.norrman at canovia.se
Tue Nov 12 00:43:40 PST 2002


Hi again,

had to test this (my kids have the flu, anyways).

------------------------------------------------------
import org.jdom.*;
import org.jdom.output.*;
import java.util.*;

public class FakeTree
{
    
    public static void main(String[] args) throws Exception {
        XMLOutputter out = new XMLOutputter("   ", true);
        // The cached sub-tree
        Element e = new Element("static");
        e.addContent(new Element("sub"));
        e.addContent(new Element("sub"));

        // The 'dynamic' document
        Element root = new Element("dynamic");
        Document doc = new Document(root);
        root.addContent(new Diverter("static-holder", e));
        root.addContent(new Element("foo").addContent(new Element("bar")));
        
        out.output(doc, System.out);
    }
    
}

class Diverter extends Element {
    Element e;
    
    public Diverter(String name, Element e) {
        super(name);
        this.e = e;
    }
    
    public List getContent() {
        List l = new ArrayList(1);
        l.add(e);
        return l;
    }
}

-- Output --

<dynamic>
   <static-holder>
      <static>
         <sub />
         <sub />
      </static>
   </static-holder>
   <foo>
      <bar />
   </foo>
</dynamic>


-----Original Message-----
From: Norrman Per
To: 'Hallvard Trætteberg '; 'jdom-interest at jdom.org '
Sent: 2002-11-12 09:23
Subject: RE: [jdom-interest] Reusable JDOM Elements

Hmm,

interesting. It looks like XMLOuputter is purley
recursive. If you subclass the elements that will
be the parent to the cached sub-trees, I guess you
could 'divert' XMLOutputter down the cached sub-tree
by overriding the getContent() method.

/pmn



-----Original Message-----
From: Hallvard Trætteberg
To: jdom-interest at jdom.org
Sent: 2002-11-12 08:36
Subject: RE: [jdom-interest] Reusable JDOM Elements

Concerning caching sub-trees:

in a potentially busy server environment, I preconstruct and cache
potentially many JDOM subtrees. Comes usage time, the subtree gets
cloned and attached to a real Document, which is subsequently output to
XML and sent out. 
 
It appears wasteful to have to clone the Elements, even if they are not
going to be changed, for the sole reason that they maintain a reference
to their parent.  It seems also to be slightly convoluted to manage
recycling of such subtrees. 
 
Is there a good pattern or solution out there (JDOM or closely related)
which allows me to build Document trees, which are only top-to-bottom
connected or which allows to reuse the constant subtree(s) easily? 

The main problem with parentless JDOM elements is traversal, i.e. which
parent should you navigate to when going up in a tree. However,
sometimes your algorithm won't traverse upwards, which might be the case
for outputting XML into a stream. This depends on how XMLOutputter
works. One way is to make the traversing/visiting function map from an
element to an alternative one, and proceed with the alternative Element,
something like:
- make a hashtable which maps from Element to Element
- build your cache of Element trees
- add leaf Element nodes to your document as placeholders for the trees
- put placeholder, tree pairs in the hashtable
- subclass XMLOutputter (if possible) and before outputting a child,
include something like
if (treemap.haskey(child)) child = treemap.get(child)
 
I'm not sure XMLOutputter is easy to subclass like this, but the trick
at least has worked for me in other cases. I've even included a map for
the other direction, so I can traverse the other way, from sub-tree to
parent.
 
Hallvard
 
 
 
 
###########################################

This message has been scanned by F-Secure Anti-Virus for Microsoft
Exchange.
For more information, connect to http://www.F-Secure.com/
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@you
rhost.com
###########################################

This message has been scanned by F-Secure Anti-Virus for Microsoft Exchange.
For more information, connect to http://www.F-Secure.com/



More information about the jdom-interest mailing list