001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xml;
030:
031: import com.caucho.util.CharBuffer;
032:
033: import org.w3c.dom.Document;
034: import org.w3c.dom.DocumentFragment;
035: import org.w3c.dom.Node;
036:
037: import java.io.IOException;
038:
039: public class QDocumentFragment extends QNode implements
040: DocumentFragment {
041: protected Document _masterDoc;
042:
043: QDocumentFragment() {
044: }
045:
046: protected QDocumentFragment(QDocument owner) {
047: super (owner);
048: }
049:
050: public short getNodeType() {
051: return DOCUMENT_FRAGMENT_NODE;
052: }
053:
054: public Document getMasterDoc() {
055: return _masterDoc;
056: }
057:
058: public String getNodeName() {
059: return "#document";
060: }
061:
062: /**
063: * Clones the fragment into the new document.
064: *
065: * @param doc the new document
066: * @param deep if true, return a recursive copy.
067: *
068: * @return the imported node.
069: */
070: Node importNode(QDocument owner, boolean deep) {
071: QDocumentFragment frag = new QDocumentFragment();
072: frag._owner = owner;
073:
074: if (!deep)
075: return frag;
076:
077: for (Node node = getFirstChild(); node != null; node = node
078: .getNextSibling()) {
079: frag.appendChild(node.cloneNode(true));
080: }
081:
082: return frag;
083: }
084:
085: public String getTextValue() {
086: CharBuffer cb = new CharBuffer();
087:
088: for (QAbstractNode node = _firstChild; node != null; node = node._next) {
089: cb.append(node.getTextValue());
090: }
091:
092: return cb.toString();
093: }
094:
095: void print(XmlPrinter out) throws IOException {
096: out.print("<#fragment>");
097: for (QAbstractNode node = _firstChild; node != null; node = node._next) {
098: node.print(out);
099: }
100: out.print("</#fragment>");
101: }
102:
103: private Object writeReplace() {
104: return new SerializedXml(this );
105: }
106:
107: public String toString() {
108: if (_firstChild == null)
109: return "DocumentFragment[]";
110: else if (_firstChild == _lastChild)
111: return "DocumentFragment[" + _firstChild.getNodeName()
112: + "]";
113: else
114: return ("DocumentFragment[" + _firstChild.getNodeName()
115: + " " + _lastChild.getNodeName() + "]");
116: }
117: }
|