KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > lazydom > LazyDocument


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: LazyDocument.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.lazydom;
25
26 import org.enhydra.apache.xerces.dom.DocumentImpl;
27 import org.enhydra.xml.io.OutputOptions;
28 import org.enhydra.xml.io.PreFormattedTextDocument;
29 import org.enhydra.xml.xmlc.XMLObject;
30 import org.enhydra.xml.xmlc.XMLObjectLink;
31 import org.w3c.dom.Attr JavaDoc;
32 import org.w3c.dom.CDATASection JavaDoc;
33 import org.w3c.dom.Comment JavaDoc;
34 import org.w3c.dom.DOMException JavaDoc;
35 import org.w3c.dom.DOMImplementation JavaDoc;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.DocumentType JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.Entity JavaDoc;
40 import org.w3c.dom.EntityReference JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43 import org.w3c.dom.Notation JavaDoc;
44 import org.w3c.dom.ProcessingInstruction JavaDoc;
45 import org.w3c.dom.Text JavaDoc;
46
47 // FIXME: not sure the Lazy* constructors with TemplateNodes are actually
48
// needed, as we go thorugh the factory methods.
49
//FIXME: If no childred/attributes/etc exist, initialize expanded flag to true.
50
// FIXME: need serializable support.
51
//FIXME: Need clone support.
52
//FIXME: are createXXX(nodeId) really needed.
53

54 /**
55  * A DOM Document that supports lazy instantiation of a template DOM. Nodes
56  * in the instance DOM are created as accessed. This can be either by
57  * traversing the tree or by direct access to a node by id number.
58  * Instantiation of nodes in the middle of the virtual tree is support. Thus
59  * a node can exist without a parent being expanded. This is used by XMLC,
60  * were the dynamic nodes tend to be towards the leaves of the tree.
61  * <p>
62  * Instances contain a reference to a DOM that is a shared template for the
63  * document. Each node in the template is assigned an integer node id that be
64  * used to index tables to directly look up the template of a node created
65  * from the template.
66  * <p>
67  * This DOM also supports associating pre-formatted text with some nodes, which
68  * is used to avoid exprensive string scanning operations during the output
69  * of unmodified nodes.
70  * <p>
71  * When a child of a node is requested, all direct children are expanded.
72  * This eliminates a lot of difficult book keep. Attributes are treated
73  * as a separate set from children, only instantiated when an atttribute
74  * is accessed.
75  *
76  * Expansion of nodes accesed from an existing node works as follows:
77  * <ul>
78  * <li> Lazy nodes have internal flag to indicate if the particular type
79  * of expansion has occured (parent, child, or attribute).
80  * <li> When an access is made, check the flag to see if the expansion has
81  * occured.
82  * <li> If expansion has not occured, call the appropriate LazyDocument
83  * doExpand method, which is synchronized. This method again check
84  * the flag on the node and expands if necessary. Some expansion
85  * are only used by one node class and maybe implemented there,
86  * but still synchronize on the document.
87  * <li> To simplify the book keeping, all children of a node will be expanded
88  * when one is expanded.
89  * <li> When the parent of a node is requested, the parent and all of the other
90  * children will be expanded and linked.
91  * <li> Nodes created using create methods (without node ids), are always
92  * marked as expanded, as they always go through standard DOM methods to
93  * be inserted into the tree.
94  * </ul>
95  *
96  * This coarse-grained locking approach minimizes the number of locks * on the
97  * assumption that collisions do not occur frequently
98  * <p>
99  * To created an extended DOM, one must override both the factory methods that
100  * take strings and those that take node ids.
101  */

102 public class LazyDocument extends DocumentImpl
103     implements LazyParent, PreFormattedTextDocument, XMLObjectLink {
104
105     /**
106      * Constructor.
107      * @param documentType Document type to associate with this document,
108      * or null if no doctype or should be obtained from template.
109      * @param templateDOM Template DOM, with each node cotaining a node id.
110      * Maybe null if no associated template.
111      */

112     public LazyDocument(DocumentType JavaDoc documentType,
113                         TemplateDOM templateDOM) {
114         super(null); // can't set doctype here (see below)!
115
if (templateDOM != null) {
116             fTemplateDOM = templateDOM;
117             fExpandedNodes = new LazyNode[fTemplateDOM.getMaxNodeId()+1];
118             fExpandedNodes[DOCUMENT_NODE_ID] = this;
119             fTemplateNode = (LazyDocument)fTemplateDOM.getNode(DOCUMENT_NODE_ID);
120             fNodeId = fTemplateNode.getNodeId();
121             if (documentType != null) {
122                 throw new LazyDOMException(LazyDOMException.NOT_SUPPORTED_ERR,
123                                            "can't specify both DocumentType and TemplateDOM");
124             }
125         } else {
126             // No template, mark as expanded.
127
fDocTypeExpanded = true;
128             fChildrenExpanded = true;
129             fIsTemplateNode = true; // No template, so this must be a template.
130

131             // Have to wait until here to set up DocumentType. Can't pass it
132
// to the constructor, as it will call appendChild before the
133
// above template initialization has taken place. So now we do
134
// what the constructor would have done.
135
if (documentType != null) {
136                 try {
137                     ((LazyDocumentType)documentType).setOwnerDocument(this);
138                 } catch (ClassCastException JavaDoc e) {
139                     throw new LazyDOMException(LazyDOMException.WRONG_DOCUMENT_ERR,
140                                                "DOM005 Wrong document");
141                 }
142                 appendChild(documentType);
143             }
144         }
145     }
146
147     /**
148      * Constructor with no argument, for LazyHTMLDocument.
149      */

150     public LazyDocument() {
151         this(null, null);
152     }
153
154     /**
155      * @see Document#getDocumentElement
156      */

157     public Element JavaDoc getDocumentElement() {
158         if (!fChildrenExpanded) {
159             expandChildren();
160         }
161         return super.getDocumentElement();
162     }
163
164     /**
165      * @see Document#getImplementation
166      */

167     public DOMImplementation JavaDoc getImplementation() {
168         return LazyDOMImplementation.getDOMImplementation();
169     }
170
171     //-------------------------------------------------------------------------
172
// Document-wide expansion support (FIXME: move to a different class???)
173
//-------------------------------------------------------------------------
174

175     /**
176      * Template document DOM. This is shared by all instances and must not be
177      * modified.
178      */

179     private TemplateDOM fTemplateDOM;
180
181     /**
182      * Table of LazyNodes that have already been expanded, indexed
183      * by nodeId. Entry is null if the node has not been expanded.
184      */

185     private LazyNode[] fExpandedNodes;
186
187     /**
188      * Detection of recursive calls of one of the expansion routines.
189      */

190     private boolean fExpanding = false;
191
192     /**
193      * Flag that an expansion is in progress, which is used to detect
194      * recursion. MUST be synchronized on document.
195      */

196     protected final void enterExpansion() {
197         if (fExpanding) {
198             throw new LazyDOMError("recursive call doing lazy DOM expansion");
199         }
200         fExpanding = true;
201     }
202
203     /**
204      * Flag that an expansion is complete.
205      * MUST be synchronized on document.
206      */

207     protected final void leaveExpansion() {
208         fExpanding = false;
209     }
210
211     /**
212      * Create a new node from a template, placing it in the expanded table.
213      * Uses factory methods so that derived document can override node type.
214      * MUST be synchronized on document.
215      */

216     private LazyNode createLazyNode(int nodeId) {
217         LazyNode template = fTemplateDOM.getNode(nodeId);
218         switch (template.getNodeType()) {
219         case ELEMENT_NODE:
220             return createElement(nodeId);
221         case ATTRIBUTE_NODE:
222             return createAttribute(nodeId);
223         case TEXT_NODE:
224             return createTextNode(nodeId);
225         case CDATA_SECTION_NODE:
226             return createCDATASection(nodeId);
227         case ENTITY_REFERENCE_NODE:
228             return createEntityReference(nodeId);
229         case ENTITY_NODE:
230             return createEntity(nodeId);
231         case PROCESSING_INSTRUCTION_NODE:
232             return createProcessingInstruction(nodeId);
233         case COMMENT_NODE:
234             return createComment(nodeId);
235         case NOTATION_NODE:
236             return createNotation(nodeId);
237         case DOCUMENT_TYPE_NODE:
238             return createDocumentType(nodeId);
239         case DOCUMENT_NODE:
240         case DOCUMENT_FRAGMENT_NODE:
241             throw new LazyDOMError(nodeId, "Can't create node from template of type "
242                                    + template.getNodeType());
243         default:
244             throw new LazyDOMError(nodeId, "Invalid node type "
245                                    + template.getNodeType());
246         }
247     }
248
249     /**
250      * Get or create a lazy node, given its id. Expanding if it doesn't
251      * exist. All node creation must go through here so that a derived
252      * document create factory is used.
253      */

254     public final LazyNode getNodeById(int nodeId) {
255         if (fExpandedNodes[nodeId] == null) {
256             synchronized(this) {
257                 if (fExpandedNodes[nodeId] == null) {
258                     fExpandedNodes[nodeId] = createLazyNode(nodeId);
259                 }
260             }
261         }
262         return fExpandedNodes[nodeId];
263     }
264
265     /**
266      * Get or create a node given, the template node.
267      * @see #getNodeById
268      */

269     public final LazyNode getNodeFromTemplate(LazyNode template) {
270         return getNodeById(template.getNodeId());
271     }
272
273     /**
274      * Method to expand children of a node once synchronized and fExpanding is
275      * set.
276      * MUST be synchronized on document.
277      */

278     private void expandNodeChildren(LazyParent node) {
279         if (!node.areChildrenExpanded()) {
280             LazyNode template = node.getTemplateNode();
281             if (template == null) {
282                 throw new LazyDOMError("bug: expandNodeChildren null template on: "
283                                        + node.getClass()); // FIXME: should be assert.
284
}
285             for (LazyNode templateChild = (LazyNode)template.getFirstChild();
286                  templateChild != null;
287                  templateChild = (LazyNode)templateChild.getNextSibling()) {
288                 // Ignore doctype if its a child
289
if (!(templateChild instanceof DocumentType JavaDoc)) {
290                     LazyNode child = getNodeFromTemplate(templateChild);
291                     node.appendChildWhileExpanding(child);
292                     if (child instanceof LazyParent) {
293                         // Tell child parent is expanded
294
((LazyParent)child).setParentExpanded();
295                     }
296                 }
297             }
298             node.setChildrenExpanded();
299         }
300     }
301
302     /**
303      * Do work of expanding the parent of a node, if it is not already
304      * expanded. This also expands the children, as this keeps the expansion
305      * bookkeeping simple and getting a parent not a common operation except to
306      * access a sibling or add a new sibling
307      * Only used internally to this package.
308      */

309     protected synchronized void doExpandParent(LazyParent node) {
310         // Check again for it being expanded now that we are synchronized
311
// getParent used during insertion of new node, so allow for recursion
312
// without actually expanding.
313
if (!node.isParentExpanded() && !fExpanding) {
314             enterExpansion();
315             try {
316                 LazyParent parentTemplate = (LazyParent)node.getTemplateNode().getParentNode();
317                 if (parentTemplate != null) {
318                     expandNodeChildren((LazyParent)getNodeFromTemplate(parentTemplate));
319                 }
320                 node.setParentExpanded();
321             } finally {
322                 leaveExpansion();
323             }
324         }
325     }
326
327     /**
328      * Do work of expanding the children of a node, if they are not already
329      * expanded. Only lazy parent's have a flag for siblings, as data nodes
330      * can never be accessed without going through the parent and expanding
331      * the children.
332      * Only used internally to this package.
333      */

334     protected synchronized void doExpandChildren(LazyParent node) {
335         enterExpansion();
336         try {
337             expandNodeChildren(node);
338         } finally {
339             leaveExpansion();
340         }
341     }
342
343     /**
344      * Get a pointer to a node if its been expanded, otherwise
345      * return null.
346      */

347     public final LazyNode getExpandedNode(int nodeId) {
348         return fExpandedNodes[nodeId];
349     }
350
351     /**
352      * Get a template node, given a node id.
353      */

354     public final LazyNode getTemplateNode(int nodeId) {
355         return fTemplateDOM.getNode(nodeId);
356     }
357
358     //-------------------------------------------------------------------------
359
// LazyDocument specific
360
//-------------------------------------------------------------------------
361

362     /**
363      * Has the DocumentType been expanded?
364      */

365     private boolean fDocTypeExpanded = false;
366
367     /**
368      * Detection of recursive calls of doctype expansion. A seperate
369      * flag, since doctype maybe expanded while expanding attributes.
370      */

371     private boolean fExpandingDocType = false;
372
373     /**
374      * Template for this Document.
375      */

376     private LazyDocument fTemplateNode = null;
377
378     /**
379      * Has the DocumentType been expanded?
380      */

381     public boolean isDocTypeExpanded() {
382         return fDocTypeExpanded;
383     }
384
385     /**
386      * Get the template for this node.
387      * @see LazyNode#getTemplateNode
388      */

389     public LazyDocument getTemplateDocument() {
390         return fTemplateNode;
391     }
392
393     /**
394      * @see Node#cloneNode
395      */

396     public Node JavaDoc cloneNode(boolean deep) {
397         // N.B. This closely follows the code in Xerces
398
// DocumentImpl.cloneNode(), may have to track changes.
399

400         if (deep) {
401             // If children are copied, we must expand now.
402
if (!fDocTypeExpanded) {
403                 doExpandDocType();
404             }
405             if (!fChildrenExpanded) {
406                 expandChildren();
407             }
408         }
409
410         /**
411          * This does a clone(), must clean up all fields and make
412          * fully expanded.
413          * FIXME: Should support cloning in unexpanded state.
414          */

415         LazyDocument newdoc = new LazyDocument();
416         newdoc.fTemplateDOM = null;
417         newdoc.fExpandedNodes = null;
418         newdoc.fDocTypeExpanded = true;
419         newdoc.fChildrenExpanded = true;
420
421         cloneNode(newdoc, deep);
422         newdoc.mutationEvents = mutationEvents;
423         return newdoc;
424     }
425
426     //-------------------------------------------------------------------------
427
// LazyNode support
428
//-------------------------------------------------------------------------
429

430     /*
431      * Node id for this element.
432      */

433     private int fNodeId = NULL_NODE_ID;
434
435     /**
436      * Is this a template node?
437      */

438     private boolean fIsTemplateNode;
439
440     /*
441      * @see LazyNode#makeTemplateNode
442      */

443     public void makeTemplateNode(int nodeId) {
444         fNodeId = nodeId;
445         fIsTemplateNode = true;
446     }
447
448     /**
449      * @see LazyNode#getNodeId
450      */

451     public int getNodeId() {
452         return fNodeId;
453     }
454
455     /**
456      * @see LazyNode#isTemplateNode
457      */

458     public boolean isTemplateNode() {
459         return fIsTemplateNode;
460     }
461
462     /**
463      * @see LazyNode#getTemplateNode
464      */

465     public LazyNode getTemplateNode() {
466         return fTemplateNode;
467     }
468
469     /**
470      * @see LazyNode#templateClone
471      */

472     public LazyNode templateClone(Document ownerDocument) {
473         throw new LazyDOMError("templateClone not support on LazyDocument node");
474     }
475
476     /**
477      * Set the node value, invalidating the id. All node data is modified
478      * by this routine.
479      * @see org.w3c.dom.Node#setNodeValue
480      */

481     public void setNodeValue(String JavaDoc value) {
482         fNodeId = NULL_NODE_ID;
483         super.setNodeValue(value);
484     }
485
486     //-------------------------------------------------------------------------
487
// LazyParent support
488
//-------------------------------------------------------------------------
489

490     /**
491      * Children expanded flag.
492      * NB: code is special, since there is node parent.
493      */

494     private boolean fChildrenExpanded = false;
495
496     /**
497      * @see LazyParent#isParentExpanded()
498      */

499     public boolean isParentExpanded() {
500         return true;
501     }
502
503     /**
504      * @see LazyParent#setParentExpanded
505      */

506     public void setParentExpanded() {
507         throw new LazyDOMError("setParentExpanded invalid on document");
508     }
509
510     /**
511      * @see LazyParent#setParentWhileExpanding
512      */

513     public void setParentWhileExpanding(Node JavaDoc parent) {
514         throw new LazyDOMError("setParentWhileExpanding invalid on document");
515     }
516
517     /**
518      * @see LazyParent#areChildrenExpanded()
519      */

520     public boolean areChildrenExpanded() {
521         return fChildrenExpanded;
522     }
523
524     /**
525      * @see LazyParent#setChildrenExpanded
526      */

527     public void setChildrenExpanded() {
528         fChildrenExpanded = true;
529     }
530
531     /**
532      * @see LazyParent#appendChildWhileExpanding
533      */

534     public void appendChildWhileExpanding(Node JavaDoc child) {
535         super.insertBefore(child, null);
536     }
537
538     /**
539      * Expand the parent of this element, if it is not already expanded.
540      */

541     private void expandParent() {
542         doExpandParent(this);
543     }
544
545     /**
546      * Expand the children of this element, if they are not already expanded.
547      */

548     private void expandChildren() {
549         doExpandChildren(this);
550     }
551
552     /**
553      * @see org.w3c.dom.Node#getChildNodes
554      */

555     public NodeList JavaDoc getChildNodes() {
556         if (!fChildrenExpanded) {
557             expandChildren();
558         }
559         return super.getChildNodes();
560     }
561
562     /**
563      * @see org.w3c.dom.Node#getFirstChild
564      */

565     public Node JavaDoc getFirstChild() {
566         if (!fChildrenExpanded) {
567             expandChildren();
568         }
569         return super.getFirstChild();
570     }
571
572     /**
573      * @see org.w3c.dom.Node#getLastChild
574      */

575     public Node JavaDoc getLastChild() {
576         if (!fChildrenExpanded) {
577             expandChildren();
578         }
579         return super.getLastChild();
580     }
581
582     /**
583      * @see org.w3c.dom.Node#insertBefore
584      */

585     public Node JavaDoc insertBefore(Node JavaDoc newChild,
586                              Node JavaDoc refChild)
587         throws DOMException JavaDoc {
588         if (!fChildrenExpanded) {
589             expandChildren();
590         }
591         return super.insertBefore(newChild, refChild);
592     }
593
594     /**
595      * @see org.w3c.dom.Node#replaceChild
596      */

597     public Node JavaDoc replaceChild(Node JavaDoc newChild,
598                              Node JavaDoc oldChild)
599         throws DOMException JavaDoc {
600         if (!fChildrenExpanded) {
601             expandChildren();
602         }
603         return super.replaceChild(newChild, oldChild);
604     }
605
606     /**
607      * @see org.w3c.dom.Node#removeChild
608      */

609     public Node JavaDoc removeChild(Node JavaDoc oldChild)
610         throws DOMException JavaDoc {
611         if (!fChildrenExpanded) {
612             expandChildren();
613         }
614         return super.removeChild(oldChild);
615     }
616
617     /**
618      * @see org.w3c.dom.Node#appendChild
619      */

620     public Node JavaDoc appendChild(Node JavaDoc newChild)
621         throws DOMException JavaDoc {
622         if (!fChildrenExpanded) {
623             expandChildren();
624         }
625         return super.appendChild(newChild);
626     }
627
628     /**
629      * @see org.w3c.dom.Node#hasChildNodes
630      */

631     public boolean hasChildNodes() {
632         if (!fChildrenExpanded) {
633             return fTemplateNode.hasChildNodes();
634         } else {
635             return super.hasChildNodes();
636         }
637     }
638
639     /**
640      * @see org.w3c.dom.Node#normalize
641      */

642     public void normalize() {
643         if (!fChildrenExpanded) {
644             expandChildren();
645         }
646         super.normalize();
647     }
648
649     //-------------------------------------------------------------------------
650
// DocumentType support
651
//-------------------------------------------------------------------------
652

653     /**
654      * Expand the document type if needed.
655      */

656     private synchronized void doExpandDocType() {
657         if (!fDocTypeExpanded) {
658             if (fExpandingDocType) {
659                 throw new LazyDOMError("recursive call expanding Lazy DOM DocumentType");
660             }
661             fExpandingDocType = true;
662             try {
663                 LazyDocumentType template = (LazyDocumentType)fTemplateNode.getDoctype();
664                 if (template != null) {
665                     docType = (LazyDocumentType)getNodeFromTemplate(template);
666                 }
667                 fDocTypeExpanded = true;
668             } finally {
669                 fExpandingDocType = false;
670             }
671         }
672     }
673
674     /**
675      * @see Document#getDoctype
676      */

677     public DocumentType JavaDoc getDoctype() {
678         if (!fDocTypeExpanded) {
679             doExpandDocType();
680         }
681         return super.getDoctype();
682     }
683
684     //-------------------------------------------------------------------------
685
// Implemention of node org.w3c.dom.Document factories
686
//-------------------------------------------------------------------------
687

688     /**
689      * @see org.w3c.dom.Document#createElement
690      */

691     public Element JavaDoc createElement(String JavaDoc tagName) throws DOMException JavaDoc {
692         return new LazyElementNoNS(this, null, tagName);
693     }
694
695     /**
696      * @see org.w3c.dom.Document#createTextNode
697      */

698     public Text JavaDoc createTextNode(String JavaDoc data) {
699         return new LazyText(this, null, data);
700     }
701
702     /**
703      * @see org.w3c.dom.Document#createComment
704      */

705     public Comment JavaDoc createComment(String JavaDoc data) {
706         return new LazyComment(this, null, data);
707     }
708
709     /**
710      * @see org.w3c.dom.Document#createCDATASection
711      */

712     public CDATASection JavaDoc createCDATASection(String JavaDoc data) throws DOMException JavaDoc {
713         return new LazyCDATASection(this, null, data);
714     }
715
716     /**
717      * @see org.w3c.dom.Document#createProcessingInstruction
718      */

719     public ProcessingInstruction JavaDoc createProcessingInstruction(String JavaDoc target,
720                                                              String JavaDoc data) {
721         return new LazyProcessingInstruction(this, null, target, data);
722     }
723
724     /**
725      * @see org.w3c.dom.Document#createAttribute
726      */

727     public Attr JavaDoc createAttribute(String JavaDoc name) throws DOMException JavaDoc {
728         return new LazyAttrNoNS(this, null, name);
729     }
730
731     /**
732      * See org.w3c.dom.Document#createNotation
733      */

734     public Notation JavaDoc createNotation(String JavaDoc name) throws DOMException JavaDoc {
735         return new LazyNotation(this, null, name, null, null);
736     }
737
738     /**
739      * @see org.w3c.dom.Document#createEntityReference
740      */

741     public EntityReference JavaDoc createEntityReference(String JavaDoc name) throws DOMException JavaDoc {
742         return new LazyEntityReference(this, null, name);
743     }
744
745     /**
746      * @see org.w3c.dom.Document#createElementNS
747      */

748     public Element JavaDoc createElementNS(String JavaDoc namespaceURI,
749                                    String JavaDoc qualifiedName) throws DOMException JavaDoc {
750         return new LazyElementNS(this, null, namespaceURI, qualifiedName);
751     }
752
753     /**
754      * @see org.w3c.dom.Document#createAttributeNS
755      */

756     public Attr JavaDoc createAttributeNS(String JavaDoc namespaceURI,
757                                   String JavaDoc qualifiedName) throws DOMException JavaDoc {
758         return new LazyAttrNS(this, null, namespaceURI, qualifiedName);
759     }
760
761     /**
762      * Create a new DocumentType object (Non-DOM).
763      * @see DocumentImpl#createDocumentType
764      */

765     public DocumentType JavaDoc createDocumentType(String JavaDoc qualifiedName,
766                                            String JavaDoc publicID,
767                                            String JavaDoc systemID,
768                                            String JavaDoc internalSubset) throws DOMException JavaDoc {
769         return new LazyDocumentType(this, null, qualifiedName, publicID, systemID, internalSubset);
770     }
771
772     /**
773      * Create a new DocumentType object (Non-DOM). This overrides the method
774      * in the Xerces DOM used by importNode().
775      * @see DocumentImpl#createDocumentType
776      */

777     public DocumentType JavaDoc createDocumentType(String JavaDoc qualifiedName,
778                                            String JavaDoc publicID,
779                                            String JavaDoc systemID) throws DOMException JavaDoc {
780
781         return new LazyDocumentType(this, null, qualifiedName, publicID, systemID, null);
782
783     }
784     /**
785      * Create a new Entity object (Non-DOM).
786      * @see DocumentImpl#createEntity
787      */

788     public Entity JavaDoc createEntity(String JavaDoc name) throws DOMException JavaDoc {
789         return new LazyEntity(this, null, name, null, null, null);
790     }
791
792     //-------------------------------------------------------------------------
793
// Node id-based node creation methods.
794
//-------------------------------------------------------------------------
795

796     /**
797      * Create a element from a template given its id.
798      * @see org.w3c.dom.Document#createElement
799      */

800     public LazyElement createElement(int nodeId) throws DOMException JavaDoc {
801         Node JavaDoc template = (LazyElement)fTemplateDOM.getNode(nodeId);
802         if (template instanceof LazyElementNS) {
803             return new LazyElementNS(this, (LazyElementNS)template, null, null);
804         } else {
805             return new LazyElementNoNS(this, (LazyElementNoNS)template, null);
806         }
807     }
808
809     /**
810      * Create a text node from a template given its id.
811      * @see org.w3c.dom.Document#createTextNode
812      */

813     public LazyText createTextNode(int nodeId) {
814         return new LazyText(this, (LazyText)fTemplateDOM.getNode(nodeId), null);
815     }
816
817     /**
818      * Create a comment from a template given its id.
819      * @see org.w3c.dom.Document#createComment
820      */

821     public LazyComment createComment(int nodeId) {
822         return new LazyComment(this, (LazyComment)fTemplateDOM.getNode(nodeId), null);
823     }
824
825     /**
826      * Create a CDATASection from a template given its id.
827      * @see org.w3c.dom.Document#createCDATASection
828      */

829     public LazyCDATASection createCDATASection(int nodeId) throws DOMException JavaDoc {
830         return new LazyCDATASection(this, (LazyCDATASection)fTemplateDOM.getNode(nodeId), null);
831     }
832
833     /**
834      * Create a process instruction node from a template given its id.
835      * @see org.w3c.dom.Document#createProcessingInstruction
836      */

837     public LazyProcessingInstruction createProcessingInstruction(int nodeId) {
838         return new LazyProcessingInstruction(this, (LazyProcessingInstruction)fTemplateDOM.getNode(nodeId), null, null);
839     }
840
841     /**
842      * Create an attribute from a template given its id.
843      * @see org.w3c.dom.Document#createAttribute
844      */

845     public LazyAttr createAttribute(int nodeId) throws DOMException JavaDoc {
846         Node JavaDoc template = fTemplateDOM.getNode(nodeId);
847         if (template instanceof LazyAttrNS) {
848             return new LazyAttrNS(this, (LazyAttrNS)template, null, null);
849         } else {
850             return new LazyAttrNoNS(this, (LazyAttrNoNS)fTemplateDOM.getNode(nodeId), null);
851         }
852     }
853
854     /**
855      * Create a notation node from a template given its id.
856      * See org.w3c.dom.Document#createNotation
857      */

858     public LazyNotation createNotation(int nodeId) throws DOMException JavaDoc {
859         return new LazyNotation(this, (LazyNotation)fTemplateDOM.getNode(nodeId),
860                                 null, null, null);
861     }
862
863     /**
864      * Create a entity reference from a template given its id.
865      * @see org.w3c.dom.Document#createEntityReference
866      */

867     public LazyEntityReference createEntityReference(int nodeId) throws DOMException JavaDoc {
868         return new LazyEntityReference(this, (LazyEntityReference)fTemplateDOM.getNode(nodeId), null);
869     }
870
871     /**
872      * Create a new DocumentType object (Non-DOM).
873      * @see DocumentImpl#createDocumentType
874      */

875     public LazyDocumentType createDocumentType(int nodeId) throws DOMException JavaDoc {
876         // FIXME: This isn't all that useful, as one can't set doctype after creating
877
// the document.
878
return new LazyDocumentType(this, (LazyDocumentType)fTemplateDOM.getNode(nodeId),
879                                     null, null, null, null);
880     }
881
882     /**
883      * Create a new Entity object (Non-DOM).
884      * @see DocumentImpl#createEntity
885      */

886     public LazyEntity createEntity(int nodeId) throws DOMException JavaDoc {
887         return new LazyEntity(this, (LazyEntity)fTemplateDOM.getNode(nodeId),
888                               null, null, null, null);
889     }
890
891     //-------------------------------------------------------------------------
892
// Template node creation methods.
893
//-------------------------------------------------------------------------
894

895     /**
896      * Create a template element. This will call the derived document create
897      * method.
898      *
899      * @see #createElement(String)
900      * @see org.w3c.dom.Document#createElement
901      */

902     public LazyElement createTemplateElement(String JavaDoc tagName,
903                                              int nodeId,
904                                              String JavaDoc preFormattedText) throws DOMException JavaDoc {
905         LazyElement element = (LazyElement)createElement(tagName);
906         element.makeTemplateNode(nodeId, preFormattedText);
907         return element;
908     }
909
910     /**
911      * Create a template text node. This will call the derived document
912      * create method.
913      *
914      * @see LazyDocument#createTextNode(String)
915      * @see org.w3c.dom.Document#createTextNode
916      */

917     public LazyText createTemplateTextNode(String JavaDoc data,
918                                            int nodeId,
919                                            String JavaDoc preFormattedText) {
920         LazyText text = (LazyText)createTextNode(data);
921         text.makeTemplateNode(nodeId, preFormattedText);
922         return text;
923     }
924
925     /**
926      * Create a template comment. This will call the derived document create
927      * method.
928      *
929      * @see #createComment(String)
930      * @see org.w3c.dom.Document#createComment
931      */

932     public LazyComment createTemplateComment(String JavaDoc data,
933                                              int nodeId) {
934         LazyComment comment = (LazyComment)createComment(data);
935         comment.makeTemplateNode(nodeId);
936         return comment;
937     }
938
939     /**
940      * Create a template CDATASection. This will call the derived document
941      * create method.
942      *
943      * @see #createCDATASection(String)
944      * @see org.w3c.dom.Document#createCDATASection
945      */

946     public LazyCDATASection createTemplateCDATASection(String JavaDoc data,
947                                                        int nodeId) throws DOMException JavaDoc {
948         LazyCDATASection cdataSection = (LazyCDATASection)createCDATASection(data);
949         cdataSection.makeTemplateNode(nodeId);
950         return cdataSection;
951     }
952
953     /**
954      * Create a template ProcessingInstruction. This will call the derived
955      * document create method.
956      *
957      * @see #createProcessingInstruction(String,String)
958      * @see org.w3c.dom.Document#createProcessingInstruction
959      */

960     public LazyProcessingInstruction createTemplateProcessingInstruction(String JavaDoc target,
961                                                                          String JavaDoc data,
962                                                                          int nodeId) {
963         LazyProcessingInstruction pi = (LazyProcessingInstruction)createProcessingInstruction(target, data);
964         pi.makeTemplateNode(nodeId);
965         return pi;
966     }
967
968     /**
969      * Create a template Attr. This will call the derived document create
970      * method.
971      *
972      * @see LazyDocument#createAttribute(String)
973      * @see org.w3c.dom.Document#createAttribute
974      */

975     public LazyAttr createTemplateAttribute(String JavaDoc name,
976                                             int nodeId) throws DOMException JavaDoc {
977         LazyAttr attr = (LazyAttr)createAttribute(name);
978         attr.makeTemplateNode(nodeId);
979         return attr;
980     }
981
982     /**
983      * Create a template Attr. This will call the derived document create
984      * method.
985      *
986      * @see LazyDocument#createAttributeNS(String,String)
987      * @see org.w3c.dom.Document#createAttributeNS
988      */

989     public LazyAttr createTemplateAttributeNS(String JavaDoc namespaceURI,
990                                               String JavaDoc qualifiedName,
991                                               int nodeId) throws DOMException JavaDoc {
992         LazyAttr attr = (LazyAttr)createAttributeNS(namespaceURI, qualifiedName);
993         attr.makeTemplateNode(nodeId);
994         return attr;
995     }
996
997     /**
998      * Create a template Notation. This will call the derived document create
999      * method.
1000     *
1001     * See org.w3c.dom.Document#createNotation
1002     * @see #createNotation(String)
1003     */

1004    public LazyNotation createTemplateNotation(String JavaDoc name,
1005                                               int nodeId) throws DOMException JavaDoc {
1006        LazyNotation notation = (LazyNotation) createNotation(name);
1007        notation.makeTemplateNode(nodeId);
1008        return notation;
1009    }
1010
1011    /**
1012     * Create a template EntityReference. This will call the derived document
1013     * create method.
1014     *
1015     * @see #createEntityReference(String)
1016     * @see org.w3c.dom.Document#createEntityReference
1017     */

1018    public LazyEntityReference createTemplateEntityReference(String JavaDoc name,
1019                                                             int nodeId) throws DOMException JavaDoc {
1020        LazyEntityReference entityRef = (LazyEntityReference)createEntityReference(name);
1021        entityRef.makeTemplateNode(nodeId);
1022        return entityRef;
1023    }
1024
1025    /**
1026     * Create a template Element with namespace. This will call the derived
1027     * document create method.
1028     *
1029     * @see #createElementNS(String,String)
1030     * @see org.w3c.dom.Document#createElementNS
1031     */

1032    public LazyElement createTemplateElementNS(String JavaDoc namespaceURI,
1033                                               String JavaDoc qualifiedName,
1034                                               int nodeId,
1035                                               String JavaDoc preFormattedText) throws DOMException JavaDoc {
1036        LazyElement element = (LazyElement)createElementNS(namespaceURI,
1037                                                           qualifiedName);
1038        element.makeTemplateNode(nodeId, preFormattedText);
1039        return element;
1040    }
1041
1042    /**
1043     * Create a template Attr with namespace. This will call the derived
1044     * document create method.
1045     *
1046     * @see #createAttributeNS(String,String)
1047     * @see org.w3c.dom.Document#createAttributeNS
1048     */

1049    public LazyAttr createAttributeNS(String JavaDoc namespaceURI,
1050                                      String JavaDoc qualifiedName,
1051                                      int nodeId) throws DOMException JavaDoc {
1052        LazyAttr attr = (LazyAttr)createAttributeNS(namespaceURI, qualifiedName);
1053        attr.makeTemplateNode(nodeId);
1054        return attr;
1055    }
1056
1057    /**
1058     * Create a template DocumentType.
1059     *
1060     * @see #createDocumentType(String,String,String,String)
1061     * @see DocumentImpl#createDocumentType
1062     */

1063    public LazyDocumentType createTemplateDocumentType(String JavaDoc qualifiedName,
1064                                                       String JavaDoc publicID,
1065                                                       String JavaDoc systemID,
1066                                                       String JavaDoc internalSubset,
1067                                                       int nodeId) throws DOMException JavaDoc {
1068        LazyDocumentType docType = new LazyDocumentType(this, null, qualifiedName, publicID,
1069                                                        systemID, internalSubset);
1070        docType.makeTemplateNode(nodeId);
1071        return docType;
1072    }
1073
1074    /**
1075     * Create a template Entity.
1076     *
1077     * @see #createEntity(String)
1078     * @see DocumentImpl#createEntity
1079     */

1080    public LazyEntity createTemplateEntity(String JavaDoc name,
1081                                           String JavaDoc publicId,
1082                                           String JavaDoc systemId,
1083                                           String JavaDoc notationName,
1084                                           int nodeId) throws DOMException JavaDoc {
1085        LazyEntity entity = new LazyEntity(this, null, name, publicId,
1086                                           systemId, notationName);
1087        entity.makeTemplateNode(nodeId);
1088        return entity;
1089    }
1090
1091    //-------------------------------------------------------------------------
1092
// PreFormattedTextDocument specific
1093
//-------------------------------------------------------------------------
1094

1095    /**
1096     * OutputOptions used for creating the preformatted text.
1097     */

1098    private OutputOptions fPreFormatOutputOptions;
1099
1100    /**
1101     * Set the output options that were used to preformat this document.
1102     * @param outputOptions The output options; should be read-only.
1103     */

1104    public void setPreFormatOutputOptions(OutputOptions outputOptions) {
1105        fPreFormatOutputOptions = outputOptions;
1106    }
1107
1108    /**
1109     * @see PreFormattedTextDocument#getPreFormatOutputOptions
1110     */

1111    public OutputOptions getPreFormatOutputOptions() {
1112        return fPreFormatOutputOptions;
1113    }
1114
1115    //-------------------------------------------------------------------------
1116
// XMLObjectLink implementation
1117
//-------------------------------------------------------------------------
1118

1119    /**
1120     * Reference to XMLObject containing this Document.
1121     */

1122    private XMLObject fXmlObjectLink;
1123
1124    /**
1125     * @see XMLObjectLink#setXMLObject
1126     */

1127    public void setXMLObject(XMLObject xmlObject) {
1128        fXmlObjectLink = xmlObject;
1129    }
1130
1131    /**
1132     * @see XMLObjectLink#getXMLObject
1133     */

1134    public XMLObject getXMLObject() {
1135        return fXmlObjectLink;
1136    }
1137}
1138
Popular Tags