KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > dom > DocumentFragmentImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.dom;
18
19 import org.w3c.dom.DocumentFragment JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.Text JavaDoc;
22
23 /**
24  * DocumentFragment is a "lightweight" or "minimal" Document
25  * object. It is very common to want to be able to extract a portion
26  * of a document's tree or to create a new fragment of a
27  * document. Imagine implementing a user command like cut or
28  * rearranging a document by moving fragments around. It is desirable
29  * to have an object which can hold such fragments and it is quite
30  * natural to use a Node for this purpose. While it is true that a
31  * Document object could fulfil this role, a Document object can
32  * potentially be a heavyweight object, depending on the underlying
33  * implementation... and in DOM Level 1, nodes aren't allowed to cross
34  * Document boundaries anyway. What is really needed for this is a
35  * very lightweight object. DocumentFragment is such an object.
36  * <P>
37  * Furthermore, various operations -- such as inserting nodes as
38  * children of another Node -- may take DocumentFragment objects as
39  * arguments; this results in all the child nodes of the
40  * DocumentFragment being moved to the child list of this node.
41  * <P>
42  * The children of a DocumentFragment node are zero or more nodes
43  * representing the tops of any sub-trees defining the structure of
44  * the document. DocumentFragment do not need to be well-formed XML
45  * documents (although they do need to follow the rules imposed upon
46  * well-formed XML parsed entities, which can have multiple top
47  * nodes). For example, a DocumentFragment might have only one child
48  * and that child node could be a Text node. Such a structure model
49  * represents neither an HTML document nor a well-formed XML document.
50  * <P>
51  * When a DocumentFragment is inserted into a Document (or indeed any
52  * other Node that may take children) the children of the
53  * DocumentFragment and not the DocumentFragment itself are inserted
54  * into the Node. This makes the DocumentFragment very useful when the
55  * user wishes to create nodes that are siblings; the DocumentFragment
56  * acts as the parent of these nodes so that the user can use the
57  * standard methods from the Node interface, such as insertBefore()
58  * and appendChild().
59  *
60  * @xerces.internal
61  *
62  * @version $Id: DocumentFragmentImpl.java,v 1.14 2004/10/05 17:12:51 mrglavas Exp $
63  * @since PR-DOM-Level-1-19980818.
64  */

65 public class DocumentFragmentImpl
66     extends ParentNode
67     implements DocumentFragment JavaDoc {
68
69     //
70
// Constants
71
//
72

73     /** Serialization version. */
74     static final long serialVersionUID = -7596449967279236746L;
75     
76     //
77
// Constructors
78
//
79

80     /** Factory constructor. */
81     public DocumentFragmentImpl(CoreDocumentImpl ownerDoc) {
82         super(ownerDoc);
83     }
84   
85     /** Constructor for serialization. */
86     public DocumentFragmentImpl() {}
87
88     //
89
// Node methods
90
//
91

92     /**
93      * A short integer indicating what type of node this is. The named
94      * constants for this value are defined in the org.w3c.dom.Node interface.
95      */

96     public short getNodeType() {
97         return Node.DOCUMENT_FRAGMENT_NODE;
98     }
99
100     /** Returns the node name. */
101     public String JavaDoc getNodeName() {
102         return "#document-fragment";
103     }
104     
105     /**
106      * Override default behavior to call normalize() on this Node's
107      * children. It is up to implementors or Node to override normalize()
108      * to take action.
109      */

110     public void normalize() {
111         // No need to normalize if already normalized.
112
if (isNormalized()) {
113             return;
114         }
115         if (needsSyncChildren()) {
116             synchronizeChildren();
117         }
118         ChildNode kid, next;
119
120         for (kid = firstChild; kid != null; kid = next) {
121             next = kid.nextSibling;
122
123             // If kid is a text node, we need to check for one of two
124
// conditions:
125
// 1) There is an adjacent text node
126
// 2) There is no adjacent text node, but kid is
127
// an empty text node.
128
if ( kid.getNodeType() == Node.TEXT_NODE )
129             {
130                 // If an adjacent text node, merge it with kid
131
if ( next!=null && next.getNodeType() == Node.TEXT_NODE )
132                 {
133                     ((Text JavaDoc)kid).appendData(next.getNodeValue());
134                     removeChild( next );
135                     next = kid; // Don't advance; there might be another.
136
}
137                 else
138                 {
139                     // If kid is empty, remove it
140
if ( kid.getNodeValue() == null || kid.getNodeValue().length() == 0 ) {
141                         removeChild( kid );
142                     }
143                 }
144             }
145
146             kid.normalize();
147         }
148
149         isNormalized(true);
150     }
151
152 } // class DocumentFragmentImpl
153
Popular Tags