View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002, 2003, 2004
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
35  package org.marre.xml;
36  
37  import java.io.IOException;
38  import java.io.Writer;
39  import java.util.Stack;
40  
41  /***
42   * XmlWriter that creates text xml documents.
43   * 
44   * NOTE! Not completed yet.
45   *  
46   * @author Markus Eriksson
47   * @version $Id: TextXmlWriter.java,v 1.2 2004/12/07 20:03:21 c95men Exp $
48   */
49  public class TextXmlWriter implements XmlWriter
50  {
51      /***
52       * The writer that is used internally to store the xml document.
53       */
54      protected Writer myWriter;
55      
56      /***
57       * Stack of tags. Used by addEndElement to know what tag to add.
58       */
59      protected Stack myTagStack;
60  
61      /***
62       * Used by addElement to insert a \r\n.
63       */
64      protected boolean myCharsAddedBetweenTags = true;
65  
66      /***
67       * Constructor.
68       * 
69       * @param writer The writer to write to.
70       */
71      public TextXmlWriter(Writer writer)
72      {
73          myWriter = writer;
74      }
75  
76      /***
77       * Sets the doctype.
78       * 
79       * @see org.marre.xml.XmlWriter#setDoctype(java.lang.String)
80       */
81      public void setDoctype(String publicID)
82      {
83          /* TODO: Not implemented yet */
84      }
85  
86      /***
87       * Sets the doctype.
88       * 
89       * @see org.marre.xml.XmlWriter#setDoctype(java.lang.String, java.lang.String)
90       */
91      public void setDoctype(String name, String systemURI)
92      {
93          /* TODO: Not implemented yet */
94      }
95  
96      /***
97       * Sets the doctype.
98       * 
99       * @see org.marre.xml.XmlWriter#setDoctype(java.lang.String, java.lang.String, java.lang.String)
100      */
101     public void setDoctype(String name, String publicID, String publicURI)
102     {
103         /* TODO: Not implemented yet */
104     }
105 
106     /***
107      * Adds a start element tag.
108      * 
109      * @see org.marre.xml.XmlWriter#addStartElement(java.lang.String)
110      */
111     public void addStartElement(String tag) throws IOException
112     {
113         if (!myCharsAddedBetweenTags)
114         {
115             myWriter.write("\r\n");
116         }
117         myCharsAddedBetweenTags = false;
118 
119         myWriter.write("<" + tag + ">");
120         myTagStack.push(tag);
121     }
122 
123     /***
124      *  Adds a start element tag.
125      *
126      * @see org.marre.xml.XmlWriter#addStartElement(java.lang.String, org.marre.xml.XmlAttribute[])
127      */
128     public void addStartElement(String tag, XmlAttribute[] attribs) throws IOException
129     {
130         throw new IOException("Not implemented");
131     }
132 
133     /***
134      *  Adds an empty start element tag.
135      *
136      * @see org.marre.xml.XmlWriter#addEmptyElement(java.lang.String)
137      */
138     public void addEmptyElement(String tag) throws IOException
139     {
140         if (!myCharsAddedBetweenTags)
141         {
142             myWriter.write("\r\n");
143         }
144         myCharsAddedBetweenTags = false;
145 
146         myWriter.write("<" + tag + "/>\r\n");
147     }
148 
149     /***
150      * Adds an empty start element tag with attributes.
151      * 
152      * @see org.marre.xml.XmlWriter#addEmptyElement(java.lang.String, org.marre.xml.XmlAttribute[])
153      */
154     public void addEmptyElement(String tag, XmlAttribute[] attribs) throws IOException
155     {
156         throw new IOException("Not implemented");
157     }
158 
159     /***
160      * Adds an end tag.
161      * 
162      * @see org.marre.xml.XmlWriter#addEndElement()
163      */
164     public void addEndElement() throws IOException
165     {
166         String tag = (String) myTagStack.pop();
167         myWriter.write("</" + tag + ">\r\n");
168     }
169 
170     /***
171      * Adds characters to the xml document.
172      * 
173      * @see org.marre.xml.XmlWriter#addCharacters(char[], int, int)
174      */
175     public void addCharacters(char[] ch, int start, int length) throws IOException
176     {
177         myCharsAddedBetweenTags = true;
178         myWriter.write(ch, start, length);
179     }
180 
181     /***
182      * Adds a string to the xml document.
183      * 
184      * @see org.marre.xml.XmlWriter#addCharacters(java.lang.String)
185      */
186     public void addCharacters(String str) throws IOException
187     {
188         myCharsAddedBetweenTags = true;
189         myWriter.write(str);
190     }
191 
192     /***
193      * Flushes the writer.
194      * 
195      * @see org.marre.xml.XmlWriter#flush()
196      */
197     public void flush() throws IOException
198     {
199         myWriter.flush();
200     }
201 }