1 /*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5 /*
6 * Copyright 1999-2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package com.sun.org.apache.xerces.internal.dom;
22
23 import org.w3c.dom.DocumentFragment;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.Text;
26
27 /**
28 * DocumentFragment is a "lightweight" or "minimal" Document
29 * object. It is very common to want to be able to extract a portion
30 * of a document's tree or to create a new fragment of a
31 * document. Imagine implementing a user command like cut or
32 * rearranging a document by moving fragments around. It is desirable
33 * to have an object which can hold such fragments and it is quite
34 * natural to use a Node for this purpose. While it is true that a
35 * Document object could fulfil this role, a Document object can
36 * potentially be a heavyweight object, depending on the underlying
37 * implementation... and in DOM Level 1, nodes aren't allowed to cross
38 * Document boundaries anyway. What is really needed for this is a
39 * very lightweight object. DocumentFragment is such an object.
40 * <P>
41 * Furthermore, various operations -- such as inserting nodes as
42 * children of another Node -- may take DocumentFragment objects as
43 * arguments; this results in all the child nodes of the
44 * DocumentFragment being moved to the child list of this node.
45 * <P>
46 * The children of a DocumentFragment node are zero or more nodes
47 * representing the tops of any sub-trees defining the structure of
48 * the document. DocumentFragment do not need to be well-formed XML
49 * documents (although they do need to follow the rules imposed upon
50 * well-formed XML parsed entities, which can have multiple top
51 * nodes). For example, a DocumentFragment might have only one child
52 * and that child node could be a Text node. Such a structure model
53 * represents neither an HTML document nor a well-formed XML document.
54 * <P>
55 * When a DocumentFragment is inserted into a Document (or indeed any
56 * other Node that may take children) the children of the
57 * DocumentFragment and not the DocumentFragment itself are inserted
58 * into the Node. This makes the DocumentFragment very useful when the
59 * user wishes to create nodes that are siblings; the DocumentFragment
60 * acts as the parent of these nodes so that the user can use the
61 * standard methods from the Node interface, such as insertBefore()
62 * and appendChild().
63 *
64 * @xerces.internal
65 *
66 * @since PR-DOM-Level-1-19980818.
67 */
68 public class DocumentFragmentImpl
69 extends ParentNode
70 implements DocumentFragment {
71
72 //
73 // Constants
74 //
75
76 /** Serialization version. */
77 static final long serialVersionUID = -7596449967279236746L;
78
79 //
80 // Constructors
81 //
82
83 /** Factory constructor. */
84 public DocumentFragmentImpl(CoreDocumentImpl ownerDoc) {
85 super(ownerDoc);
86 }
87
88 /** Constructor for serialization. */
89 public DocumentFragmentImpl() {}
90
91 //
92 // Node methods
93 //
94
95 /**
96 * A short integer indicating what type of node this is. The named
97 * constants for this value are defined in the org.w3c.dom.Node interface.
98 */
99 public short getNodeType() {
100 return Node.DOCUMENT_FRAGMENT_NODE;
101 }
102
103 /** Returns the node name. */
104 public String getNodeName() {
105 return "#document-fragment";
106 }
107
108 /**
109 * Override default behavior to call normalize() on this Node's
110 * children. It is up to implementors or Node to override normalize()
111 * to take action.
112 */
113 public void normalize() {
114 // No need to normalize if already normalized.
115 if (isNormalized()) {
116 return;
117 }
118 if (needsSyncChildren()) {
119 synchronizeChildren();
120 }
121 ChildNode kid, next;
122
123 for (kid = firstChild; kid != null; kid = next) {
124 next = kid.nextSibling;
125
126 // If kid is a text node, we need to check for one of two
127 // conditions:
128 // 1) There is an adjacent text node
129 // 2) There is no adjacent text node, but kid is
130 // an empty text node.
131 if ( kid.getNodeType() == Node.TEXT_NODE )
132 {
133 // If an adjacent text node, merge it with kid
134 if ( next!=null && next.getNodeType() == Node.TEXT_NODE )
135 {
136 ((Text)kid).appendData(next.getNodeValue());
137 removeChild( next );
138 next = kid; // Don't advance; there might be another.
139 }
140 else
141 {
142 // If kid is empty, remove it
143 if ( kid.getNodeValue() == null || kid.getNodeValue().length() == 0 ) {
144 removeChild( kid );
145 }
146 }
147 }
148
149 kid.normalize();
150 }
151
152 isNormalized(true);
153 }
154
155 } // class DocumentFragmentImpl