View Javadoc

1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    * 
4    * This software is open source. 
5    * See the bottom of this file for the licence.
6    * 
7    * $Id: AbstractDocument.java,v 1.29 2004/07/11 10:49:37 maartenc Exp $
8    */
9   
10  package org.dom4j.tree;
11  
12  import java.io.ByteArrayOutputStream;
13  import java.io.IOException;
14  import java.io.Writer;
15  import java.util.Iterator;
16  import java.util.List;
17  import java.util.Map;
18  
19  import org.dom4j.Comment;
20  import org.dom4j.Document;
21  import org.dom4j.DocumentType;
22  import org.dom4j.Element;
23  import org.dom4j.IllegalAddException;
24  import org.dom4j.Node;
25  import org.dom4j.ProcessingInstruction;
26  import org.dom4j.QName;
27  import org.dom4j.Text;
28  import org.dom4j.Visitor;
29  import org.dom4j.io.XMLWriter;
30  
31  /*** <p><code>AbstractDocument</code> is an abstract base class for 
32    * tree implementors to use for implementation inheritence.</p>
33    *
34    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
35    * @version $Revision: 1.29 $
36    */
37  public abstract class AbstractDocument extends AbstractBranch implements Document {
38      
39      public AbstractDocument() { 
40      }
41   
42      public short getNodeType() {
43          return DOCUMENT_NODE;
44      }
45  
46      public String getPath(Element context) {
47          return "/";
48      }
49      
50      public String getUniquePath(Element context) {
51          return "/";
52      }
53      
54      public Document getDocument() {
55          return this;
56      }
57      
58      public String getXMLEncoding() {
59          return null;
60      }
61  
62      public String getStringValue() {
63          Element root = getRootElement();
64          return ( root != null ) ? root.getStringValue() : "";
65      }
66      
67      public String asXML() {
68          try {
69              ByteArrayOutputStream out = new ByteArrayOutputStream();
70              XMLWriter writer = new XMLWriter(out, outputFormat);
71              writer.write(this);
72              return out.toString();
73          } 
74          catch (IOException e) {
75              throw new RuntimeException("IOException while generating textual representation: " + e.getMessage());
76          }
77      }
78  
79      public void write(Writer out) throws IOException {
80          XMLWriter writer = new XMLWriter( out, outputFormat );
81          writer.write(this);
82      }
83          
84      /*** <p><code>accept</code> method is the <code>Visitor Pattern</code> method.
85        * </p>
86        *
87        * @param visitor <code>Visitor</code> is the visitor.
88        */
89      public void accept(Visitor visitor) {
90          visitor.visit(this);
91  
92          DocumentType docType = getDocType();
93          if ( docType != null ) {
94              visitor.visit( docType );
95          }
96          
97          // visit content
98          List content = content();
99          if (content != null) {
100             for ( Iterator iter = content.iterator(); iter.hasNext(); ) {
101                 Object object = iter.next();
102                 if (object instanceof String) {
103                     Text text = getDocumentFactory().createText((String) object);
104                     visitor.visit(text);
105                 } 
106                 else {
107                     Node node = (Node) object;
108                     node.accept(visitor);
109                 }
110             }            
111         }
112     }
113     
114     public String toString() {
115         return super.toString() + " [Document: name " + getName() + "]";
116     }
117     
118     public void normalize() {
119         Element element = getRootElement();
120         if ( element != null ) {
121             element.normalize();
122         }
123     }
124     
125     public Document addComment(String comment) {
126         Comment node = getDocumentFactory().createComment( comment );
127         add( node );
128         return this;
129     }
130        
131     public Document addProcessingInstruction(String target, String data) {
132         ProcessingInstruction node = getDocumentFactory().createProcessingInstruction( target, data );
133         add( node );
134         return this;
135     }
136     
137     public Document addProcessingInstruction(String target, Map data) {
138         ProcessingInstruction node = getDocumentFactory().createProcessingInstruction( target, data );
139         add( node );
140         return this;
141     }
142     
143     public Element addElement(String name) {
144         Element element = getDocumentFactory().createElement(name);
145         add(element);
146         return element;
147     }
148     
149     public Element addElement(String qualifiedName, String namespaceURI) {
150         Element element = getDocumentFactory().createElement(qualifiedName, namespaceURI);
151         add(element);
152         return element;
153     }
154     
155     public Element addElement(QName qName) {
156         Element element = getDocumentFactory().createElement(qName);
157         add(element);
158         return element;
159     }
160 
161     public void setRootElement(Element rootElement) {
162         clearContent();
163         if ( rootElement != null ) {
164             super.add(rootElement);
165             rootElementAdded(rootElement);
166         }
167     }
168 
169     public void add(Element element) {
170         checkAddElementAllowed(element);
171         super.add(element);
172         rootElementAdded(element);
173     }
174     
175     public boolean remove(Element element) {
176         boolean answer = super.remove(element);        
177         Element root = getRootElement();
178         if ( root != null && answer ) {
179             setRootElement(null);
180         }
181         element.setDocument(null);
182         return answer;
183     }
184         
185     public Node asXPathResult(Element parent) {
186         return this;
187     }
188     
189     protected void childAdded(Node node) {
190         if (node != null ) {
191             node.setDocument(this);
192         }
193     }
194     
195     protected void childRemoved(Node node) {
196         if ( node != null ) {
197             node.setDocument(null);
198         }
199     }     
200 
201     protected void checkAddElementAllowed(Element element) {
202         Element root = getRootElement();
203         if ( root != null ) {
204             throw new IllegalAddException(  
205                 this, 
206                 element, 
207                 "Cannot add another element to this Document as it already has "
208                 + " a root element of: " + root.getQualifiedName()
209             );
210         }
211     }
212 
213     /*** Called to set the root element variable */
214     protected abstract void rootElementAdded(Element rootElement);
215     
216 }
217 
218 
219 
220 
221 /*
222  * Redistribution and use of this software and associated documentation
223  * ("Software"), with or without modification, are permitted provided
224  * that the following conditions are met:
225  *
226  * 1. Redistributions of source code must retain copyright
227  *    statements and notices.  Redistributions must also contain a
228  *    copy of this document.
229  *
230  * 2. Redistributions in binary form must reproduce the
231  *    above copyright notice, this list of conditions and the
232  *    following disclaimer in the documentation and/or other
233  *    materials provided with the distribution.
234  *
235  * 3. The name "DOM4J" must not be used to endorse or promote
236  *    products derived from this Software without prior written
237  *    permission of MetaStuff, Ltd.  For written permission,
238  *    please contact dom4j-info@metastuff.com.
239  *
240  * 4. Products derived from this Software may not be called "DOM4J"
241  *    nor may "DOM4J" appear in their names without prior written
242  *    permission of MetaStuff, Ltd. DOM4J is a registered
243  *    trademark of MetaStuff, Ltd.
244  *
245  * 5. Due credit should be given to the DOM4J Project - 
246  *    http://www.dom4j.org
247  *
248  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
249  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
250  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
251  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
252  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
253  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
254  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
255  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
256  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
257  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
258  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
259  * OF THE POSSIBILITY OF SUCH DAMAGE.
260  *
261  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
262  *
263  * $Id: AbstractDocument.java,v 1.29 2004/07/11 10:49:37 maartenc Exp $
264  */