Source for gnu.java.beans.encoder.StAXWriter

   1: /* StAXWriter.java
   2:  Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4:  This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package gnu.java.beans.encoder;
  40: 
  41: import java.io.OutputStream;
  42: 
  43: import javax.xml.stream.XMLOutputFactory;
  44: import javax.xml.stream.XMLStreamException;
  45: import javax.xml.stream.XMLStreamWriter;
  46: 
  47: /** A {@link Writer} implementation based on the StAX API.
  48:  *
  49:  * @author Robert Schuster (robertschuster@fsfe.org)
  50:  *
  51:  */
  52: public class StAXWriter implements Writer
  53: {
  54:   XMLStreamWriter writer;
  55: 
  56:   int indent = 0;
  57: 
  58:   public StAXWriter(OutputStream os)
  59:   {
  60:     try
  61:       {
  62:         XMLOutputFactory factory = XMLOutputFactory.newInstance();
  63:         writer = factory.createXMLStreamWriter(os);
  64:       }
  65:     catch (XMLStreamException se)
  66:       {
  67:         throw (InternalError)
  68:           new InternalError(
  69:           "Could not instantiate a streaming XML writer.")
  70:           .initCause(se);
  71:       }
  72: 
  73:   }
  74: 
  75:   public void flush()
  76:   {
  77:     if (writer != null)
  78:       {
  79:         try
  80:           {
  81:             writer.flush();
  82:           }
  83:         catch (XMLStreamException xse)
  84:           {
  85:             // TODO: find out
  86:           }
  87:       }
  88: 
  89:   }
  90: 
  91:   public void close()
  92:   {
  93:     if (writer != null)
  94:       {
  95:         try
  96:           {
  97:             writer.close();
  98:           }
  99:         catch (XMLStreamException xse)
 100:           {
 101:             // TODO: find out
 102:           }
 103:         writer = null;
 104:       }
 105: 
 106:   }
 107: 
 108:   public void writePreamble()
 109:   {
 110:     try
 111:       {
 112:         writer.writeStartDocument("UTF-8", "1.0");
 113:       }
 114:     catch (XMLStreamException xmlse)
 115:       {
 116: 
 117:       }
 118:   }
 119: 
 120:   public void writeEnd(boolean wasEmpty)
 121:   {
 122:     try
 123:       {
 124:         indent -= 2;
 125: 
 126:         if (wasEmpty)
 127:           return;
 128: 
 129:         for (int i = 0; i < indent; i++)
 130:           writer.writeCharacters(" ");
 131: 
 132:         writer.writeEndElement();
 133: 
 134:         writer.writeCharacters("\n");
 135:       }
 136:     catch (XMLStreamException xmlse)
 137:       {
 138: 
 139:       }
 140:   }
 141: 
 142:   public void writeEndNoChildren()
 143:   {
 144:     try
 145:       {
 146:         writer.writeEndElement();
 147:         writer.writeCharacters("\n");
 148:       }
 149:     catch (XMLStreamException xmlse)
 150:       {
 151: 
 152:       }
 153:   }
 154: 
 155:   public void write(String tagName, boolean empty)
 156:   {
 157:     write(tagName, null, null, null, empty);
 158:   }
 159: 
 160:   public void write(String tagName, String value)
 161:   {
 162:     write(tagName, value, null, null, value == null);
 163:   }
 164: 
 165:   public void writeNoChildren(String tagName, String value)
 166:   {
 167:     try
 168:       {
 169:         for (int i = 0; i < indent; i++)
 170:           writer.writeCharacters(" ");
 171: 
 172:         writer.writeStartElement(tagName);
 173: 
 174:         writer.writeCharacters(value);
 175:       }
 176:     catch (XMLStreamException xmlse)
 177:       {
 178: 
 179:       }
 180:   }
 181: 
 182:   public void write(String tagName, String attributeName,
 183:                     String attributeValue, boolean empty)
 184:   {
 185:     write(tagName, null, new String[] { attributeName },
 186:           new String[] { attributeValue }, empty);
 187:   }
 188: 
 189:   public void write(String tagName, String value, String[] attributeNames,
 190:                     String[] attributeValues, boolean empty)
 191:   {
 192:     try
 193:       {
 194:         for (int i = 0; i < indent; i++)
 195: 
 196:           writer.writeCharacters(" ");
 197: 
 198:         if (empty)
 199:           writer.writeEmptyElement(tagName);
 200:         else
 201:           writer.writeStartElement(tagName);
 202: 
 203:         if (attributeNames != null)
 204:           for (int i = 0; i < attributeNames.length; i++)
 205:             writer.writeAttribute(attributeNames[i], attributeValues[i]);
 206: 
 207:         writer.writeCharacters("\n");
 208: 
 209:         indent += 2;
 210: 
 211:         if (value != null)
 212:           {
 213:             for (int i = 0; i < indent; i++)
 214:               writer.writeCharacters(" ");
 215: 
 216:             writer.writeCharacters(value);
 217: 
 218:             writer.writeCharacters("\n");
 219:           }
 220:       }
 221:     catch (XMLStreamException xmlse)
 222:       {
 223: 
 224:       }
 225:   }
 226: 
 227:   public void write(String tagName, String[] attributeNames,
 228:                     String[] attributeValues, boolean empty)
 229:   {
 230:     write(tagName, null, attributeNames, attributeValues, empty);
 231:   }
 232: 
 233: }