KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > QDocumentFragment


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml;
30
31 import com.caucho.util.CharBuffer;
32
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.DocumentFragment JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36
37 import java.io.IOException JavaDoc;
38
39 public class QDocumentFragment extends QNode implements DocumentFragment JavaDoc {
40   protected Document JavaDoc _masterDoc;
41
42   QDocumentFragment()
43   {
44   }
45
46   protected QDocumentFragment(QDocument owner)
47   {
48     super(owner);
49   }
50
51   public short getNodeType() { return DOCUMENT_FRAGMENT_NODE; }
52
53   public Document JavaDoc getMasterDoc()
54   {
55     return _masterDoc;
56   }
57
58   public String JavaDoc getNodeName() { return "#document"; }
59
60   /**
61    * Clones the fragment into the new document.
62    *
63    * @param doc the new document
64    * @param deep if true, return a recursive copy.
65    *
66    * @return the imported node.
67    */

68   Node JavaDoc importNode(QDocument owner, boolean deep)
69   {
70     QDocumentFragment frag = new QDocumentFragment();
71     frag._owner = owner;
72
73     if (! deep)
74       return frag;
75
76     for (Node JavaDoc node = getFirstChild();
77      node != null;
78      node = node.getNextSibling()) {
79       frag.appendChild(node.cloneNode(true));
80     }
81
82     return frag;
83   }
84
85   public String JavaDoc getTextValue()
86   {
87     CharBuffer cb = new CharBuffer();
88
89     for (QAbstractNode node = _firstChild; node != null; node = node._next) {
90       cb.append(node.getTextValue());
91     }
92
93     return cb.toString();
94   }
95
96   void print(XmlPrinter out) throws IOException JavaDoc
97   {
98     out.print("<#fragment>");
99     for (QAbstractNode node = _firstChild; node != null; node = node._next) {
100       node.print(out);
101     }
102     out.print("</#fragment>");
103   }
104
105   private Object JavaDoc writeReplace()
106   {
107     return new SerializedXml(this);
108   }
109
110   public String JavaDoc toString()
111   {
112     if (_firstChild == null)
113       return "DocumentFragment[]";
114     else if (_firstChild == _lastChild)
115       return "DocumentFragment[" + _firstChild.getNodeName() + "]";
116     else
117       return ("DocumentFragment[" + _firstChild.getNodeName() +
118               " " + _lastChild.getNodeName() + "]");
119   }
120 }
121
Popular Tags