View Javadoc

1   /*
2    * Copyright (c) 2003, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of XmlWriter nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package com.generationjava.io.xml;
33  
34  import java.io.IOException;
35  import java.io.Writer;
36  
37  /***
38   * Superclass for any XmlWriter which will wrap another XmlWriter. 
39   * It passes all calls on to the underlying XmlWriter, and is 
40   * expected to be used by all filtering XmlWriters. 
41   *
42   * Possibly this class should be abstract.
43   */
44  public class DelegatingXmlWriter implements XmlWriter {
45  
46      private XmlWriter xmlWriter;
47  
48      public DelegatingXmlWriter(XmlWriter xmlWriter) {
49          this.xmlWriter = xmlWriter;
50      }
51  
52      /***
53       * Makes it easy to output the xml version if such a thing is desired.
54       * While it usually is desired, it is less surprising not to handle it.
55       */
56      public XmlWriter writeXmlVersion() throws IOException {
57          this.xmlWriter.writeXmlVersion();
58          return this;
59      }
60  
61      /*** @see DelegatingXmlWriter.writeXmlVersion(String, String, String) */
62      public XmlWriter writeXmlVersion(String version, String encoding) throws IOException {
63          this.xmlWriter.writeXmlVersion(version, encoding);
64          return this;
65      }
66  
67      /***
68       * Output the version, encoding and standalone nature of an xml file.
69       */
70      public XmlWriter writeXmlVersion(String version, String encoding, String standalone) throws IOException {
71          this.xmlWriter.writeXmlVersion(version, encoding, standalone);
72          return this;
73      }
74  
75      /***
76       * A helper method. It writes out an entity which contains only text.
77       *
78       * @param name String name of tag
79       * @param text String of text to go inside the tag
80       */
81      public XmlWriter writeEntityWithText(String name, Object text) throws
82      IOException {
83          this.xmlWriter.writeEntityWithText(name, text);
84          return this;
85      }
86  
87      /***
88       * A helper method. It writes out empty entities.
89       *
90       * @param name String name of tag
91       */
92      public XmlWriter writeEmptyEntity(String name) throws IOException {
93          this.xmlWriter.writeEmptyEntity(name);
94          return this;
95      }
96  
97      /***
98       * Begin to write out an entity. Unlike the helper tags, this tag 
99       * will need to be ended with the endEntity method.
100      *
101      * @param name String name of tag
102      */
103     public XmlWriter writeEntity(String name) throws IOException {
104         this.xmlWriter.writeEntity(name);
105         return this;
106     }
107 
108     /***
109      * Write an attribute out for the current entity. 
110      * Any xml characters in the value are escaped.
111      * Currently it does not actually throw the exception, but 
112      * the api is set that way for future changes.
113      *
114      * @param String name of attribute.
115      * @param Object value of attribute.
116      */
117     public XmlWriter writeAttribute(String attr, Object value) throws IOException {
118         this.xmlWriter.writeAttribute(attr, value);
119         return this;
120     }
121 
122     /***
123      * End the current entity. This will throw an exception 
124      * if it is called when there is not a currently open 
125      * entity.
126      */
127     public XmlWriter endEntity() throws IOException {
128         this.xmlWriter.endEntity();
129         return this;
130     }
131 
132     /***
133      * Close this writer. It does not close the underlying 
134      * writer, but does throw an exception if there are 
135      * as yet unclosed tags.
136      */
137     public void close() throws IOException {
138         this.xmlWriter.close();
139     }
140 
141     /***
142      * Output body text. Any xml characters are escaped. 
143      */
144     public XmlWriter writeText(Object text) throws IOException {
145         this.xmlWriter.writeText(text);
146         return this;
147     }
148 
149     /***
150      * Write out a chunk of CDATA. This helper method surrounds the 
151      * passed in data with the CDATA tag.
152      *
153      * @param String of CDATA text.
154      */
155     public XmlWriter writeCData(String cdata) throws IOException {
156         this.xmlWriter.writeCData(cdata);
157         return this;
158     }
159 
160     /***
161      * Write out a chunk of comment. This helper method surrounds the 
162      * passed in data with the xml comment tag.
163      *
164      * @param String of text to comment.
165      */
166     public XmlWriter writeComment(String comment) throws IOException {
167         this.xmlWriter.writeComment(comment);
168         return this;
169     }
170 
171     public Writer getWriter() {
172         return this.xmlWriter.getWriter();
173     }
174 
175 }