Merge from mainline
[official-gcc.git] / libjava / classpath / gnu / java / beans / encoder / StAXWriter.java
blobfdb5f7d4571b43d03e02bdc8e71f0b3f99d58c71
1 /* StAXWriter.java
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.beans.encoder;
41 import java.io.OutputStream;
43 import javax.xml.stream.XMLOutputFactory;
44 import javax.xml.stream.XMLStreamException;
45 import javax.xml.stream.XMLStreamWriter;
47 /** A {@link Writer} implementation based on the StAX API.
49 * @author Robert Schuster (robertschuster@fsfe.org)
52 public class StAXWriter implements Writer
54 XMLStreamWriter writer;
56 int indent = 0;
58 public StAXWriter(OutputStream os)
60 try
62 XMLOutputFactory factory = XMLOutputFactory.newInstance();
63 writer = factory.createXMLStreamWriter(os);
65 catch (XMLStreamException se)
67 throw (InternalError)
68 new InternalError(
69 "Could not instantiate a streaming XML writer.")
70 .initCause(se);
75 public void flush()
77 if (writer != null)
79 try
81 writer.flush();
83 catch (XMLStreamException xse)
85 // TODO: find out
91 public void close()
93 if (writer != null)
95 try
97 writer.close();
99 catch (XMLStreamException xse)
101 // TODO: find out
103 writer = null;
108 public void writePreamble()
112 writer.writeStartDocument("UTF-8", "1.0");
114 catch (XMLStreamException xmlse)
120 public void writeEnd(boolean wasEmpty)
124 indent -= 2;
126 if (wasEmpty)
127 return;
129 for (int i = 0; i < indent; i++)
130 writer.writeCharacters(" ");
132 writer.writeEndElement();
134 writer.writeCharacters("\n");
136 catch (XMLStreamException xmlse)
142 public void writeEndNoChildren()
146 writer.writeEndElement();
147 writer.writeCharacters("\n");
149 catch (XMLStreamException xmlse)
155 public void write(String tagName, boolean empty)
157 write(tagName, null, null, null, empty);
160 public void write(String tagName, String value)
162 write(tagName, value, null, null, value == null);
165 public void writeNoChildren(String tagName, String value)
169 for (int i = 0; i < indent; i++)
170 writer.writeCharacters(" ");
172 writer.writeStartElement(tagName);
174 writer.writeCharacters(value);
176 catch (XMLStreamException xmlse)
182 public void write(String tagName, String attributeName,
183 String attributeValue, boolean empty)
185 write(tagName, null, new String[] { attributeName },
186 new String[] { attributeValue }, empty);
189 public void write(String tagName, String value, String[] attributeNames,
190 String[] attributeValues, boolean empty)
194 for (int i = 0; i < indent; i++)
196 writer.writeCharacters(" ");
198 if (empty)
199 writer.writeEmptyElement(tagName);
200 else
201 writer.writeStartElement(tagName);
203 if (attributeNames != null)
204 for (int i = 0; i < attributeNames.length; i++)
205 writer.writeAttribute(attributeNames[i], attributeValues[i]);
207 writer.writeCharacters("\n");
209 indent += 2;
211 if (value != null)
213 for (int i = 0; i < indent; i++)
214 writer.writeCharacters(" ");
216 writer.writeCharacters(value);
218 writer.writeCharacters("\n");
221 catch (XMLStreamException xmlse)
227 public void write(String tagName, String[] attributeNames,
228 String[] attributeValues, boolean empty)
230 write(tagName, null, attributeNames, attributeValues, empty);