|
|
Re: Tenplates and subtree plugins - Rev 2 - Better: msg#00022
java.enhydra.xmlc
|
Subject: |
Re: Tenplates and subtree plugins - Rev 2 - Better |
Some code improvements...
1.) Generic org.w3c.dom.Element instead of HTMLElement
2.) A new function that takes the two docs and the tag names as Strings
and does the replacement. This is now much more convenient
"template"
code.
P.S. I am using 2.2 Beta 1 from Freshmeat.net and Tomcat
/** This takes the whatToPutThere element and puts a deep copy
** of it under putItHere. Before the insertions all child nodes
** of putItUnderHere are removed.
** The idea here is to have a template doc with some
** DIV tag with an id. You find the div. Then create another
document
** with the contents for some part of the template, and copy it to
** be under the DIV.<P>
** You should call <tt>replaceNodes</tt> before
setting up multi-row tables
** from a database. This way a single row table sample is copied
instead
** of hundreds of rows.
** <code>
** layoutHTML L = new layoutHTML();
** indexHTML t = new indexHTML();
** // All HTMLElements here are org.w3c.dom.html.HTMLElement
** HTMLElement centerMiddle = L.getElementCenterMiddle();
** HTMLElement indexMainContents = t.getElementMainContents();
** Element N = replaceNodes(centerMiddle, indexMainContents);
** </code>
** @returns The new cloned Element.
**/
public org.w3c.dom.Element replaceNodes(
org.w3c.dom.Element putItUnderHere,
org.w3c.dom.Element whatToPutThere) {
org.w3c.dom.NodeList N = putItUnderHere.getChildNodes();
while(N.getLength() > 0) {
putItUnderHere.removeChild(N.item(0));
}
org.w3c.dom.Element theNewNode =
(org.w3c.dom.Element)putItUnderHere.getOwnerDocument().importNode(whatToPutThere,
true);
putItUnderHere.appendChild(theNewNode);
return theNewNode;
}
/** This takes a subtree from the getItFromHereDoc with the id
** getItFromHereId and puts it in the
** putItHereDoc under neath the HTMLElement with
** id of putItUnderHereId. It clears all nodes out from
** under the destination node before putting the
** cloned subtree in.
** The idea here is to have a template doc with some
** DIV tag with an id. You find the div. Then create another
document
** with the contents for some part of the template, and copy it to
** be under the DIV.<P>
** You should call <tt>replaceNodes</tt> before
setting up multi-row tables
** from a database. This way a single row table sample is copied
instead
** of hundreds of rows.
** <code>
** // The template
** layoutHTML L = new layoutHTML();
** // The center screen contents to be inserted in template.
** indexHTML t = new indexHTML();
** // replace L centerMiddle with t mainContents
** Element N = replaceNodes(L, "centerMiddle", t, "mainContents");
** </code>
** @returns The new cloned node. Or null on no such source or dest
node.
**/
public org.w3c.dom.Element replaceNodes(
org.w3c.dom.Document putItHereDoc,
String putItUnderHereId,
org.w3c.dom.Document getItFromHereDoc,
String getItFromHereId) {
org.w3c.dom.Element from =
getItFromHereDoc.getElementById(getItFromHereId);
if(from == null) {
return null;
}
org.w3c.dom.Element to =
putItHereDoc.getElementById(putItUnderHereId);
if(to == null) {
return null;
}
return replaceNodes(to, from);
}
David Corbin wrote:
On Thursday 21 August 2003 00:54, Richard Keene wrote:
I want to take one document called layout.html and another called body.html
and take the main contents node (a div with id='mainContents') and
remove it from the
instance of bodyHTML.class and replace the div in an instance of
layoutHTML.class
with the removed div.
I have code like....
layoutHTML L = new layoutHTML();
bodyHTML t = new bodyHTML();
// The div I want to replace.
HTMLElement centerMiddle = L.getElementCenterMiddle();
// The div I want to plug into the layoutHTML object.
HTMLElement indexMainContents = t.getElementMainContents();
HTMLElement nE =
(HTMLEelemet.getDocument().importNode(t.getElementMainContents(),true);
cenerMiddle.appendChild(newElement);
bodyMainContents.getParentNode().removeChild(indexMainContents);
centerMiddle.appendChild(t.getElementMainContents());
It compiles and when I run it it says 'wrong document'
It appears that the indexMainContents retains the class id or something
of the
owning document t, and so when L goes to render it'sself to the browser
it recognizes that the nodes are from the wrong doc.
Try someting like those two lines above.
--
Richard Keene
801-647-8932
rkeene@xxxxxxxxxx
|
| |