<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Message</TITLE>
<META http-equiv=Content-Type content="text/html; charset=Windows-1252">
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=357281207-12112002>Concerning caching sub-trees:</SPAN></FONT></DIV>
<BLOCKQUOTE dir=ltr
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT size=2>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.<SPAN
class=357281207-12112002><FONT face=Arial
color=#0000ff> </FONT></SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT size=2><SPAN
class=357281207-12112002></SPAN></FONT> </DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT size=2>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.<SPAN class=357281207-12112002><FONT face=Arial
color=#0000ff> </FONT></SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT size=2><SPAN
class=357281207-12112002></SPAN></FONT> </DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT size=2>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?<SPAN
class=357281207-12112002><FONT face=Arial
color=#0000ff> </FONT></SPAN></FONT></DIV></BLOCKQUOTE>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002>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:</SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002>- make a hashtable which
maps from Element to Element</SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002>- build your cache of
Element trees</SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002>- add leaf Element nodes to
your document as placeholders for the trees</SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002>- put placeholder, tree
pairs in the hashtable</SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002>- subclass XMLOutputter (if
possible) and before outputting a child, include something
like</SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002>if (treemap.haskey(child))
child = treemap.get(child)</SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002></SPAN></FONT> </DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002>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.</SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002></SPAN></FONT> </DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002>Hallvard</SPAN></FONT></DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002></SPAN></FONT> </DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002></SPAN></FONT> </DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN class=357281207-12112002></SPAN></FONT> </DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Arial
color=#0000ff size=2><SPAN
class=357281207-12112002></SPAN></FONT> </DIV></BODY></HTML>