Source for gnu.xml.stream.XMLEventWriterImpl

   1: /* XMLEventWriterImpl.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: package gnu.xml.stream;
  39: 
  40: import javax.xml.namespace.NamespaceContext;
  41: import javax.xml.namespace.QName;
  42: import javax.xml.stream.XMLEventReader;
  43: import javax.xml.stream.XMLEventWriter;
  44: import javax.xml.stream.XMLStreamConstants;
  45: import javax.xml.stream.XMLStreamException;
  46: import javax.xml.stream.XMLStreamWriter;
  47: import javax.xml.stream.events.Attribute;
  48: import javax.xml.stream.events.Characters;
  49: import javax.xml.stream.events.Comment;
  50: import javax.xml.stream.events.DTD;
  51: import javax.xml.stream.events.Namespace;
  52: import javax.xml.stream.events.ProcessingInstruction;
  53: import javax.xml.stream.events.StartDocument;
  54: import javax.xml.stream.events.StartElement;
  55: import javax.xml.stream.events.XMLEvent;
  56: 
  57: /**
  58:  * Writer to write events to an underlying XML stream writer.
  59:  *
  60:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  61:  */
  62: public class XMLEventWriterImpl
  63:   implements XMLEventWriter
  64: {
  65: 
  66:   protected final XMLStreamWriter writer;
  67: 
  68:   protected XMLEventWriterImpl(XMLStreamWriter writer)
  69:   {
  70:     this.writer = writer;
  71:   }
  72: 
  73:   public void flush()
  74:     throws XMLStreamException
  75:   {
  76:     writer.flush();
  77:   }
  78: 
  79:   public void close()
  80:     throws XMLStreamException
  81:   {
  82:     writer.close();
  83:   }
  84: 
  85:   public void add(XMLEvent event)
  86:     throws XMLStreamException
  87:   {
  88:     QName name;
  89:     String uri;
  90:     switch (event.getEventType())
  91:       {
  92:       case XMLStreamConstants.START_ELEMENT:
  93:         StartElement startElement = event.asStartElement();
  94:         name = startElement.getName();
  95:         uri = name.getNamespaceURI();
  96:         if (uri != null && !"".equals(uri))
  97:           writer.writeStartElement(name.getPrefix(), name.getLocalPart(), uri);
  98:         else
  99:           writer.writeStartElement(name.getLocalPart());
 100:         break;
 101:       case XMLStreamConstants.END_ELEMENT:
 102:         writer.writeEndElement();
 103:         break;
 104:       case XMLStreamConstants.ATTRIBUTE:
 105:         Attribute attribute = (Attribute) event;
 106:         name = attribute.getName();
 107:         uri = name.getNamespaceURI();
 108:         if (uri != null && !"".equals(uri))
 109:           writer.writeAttribute(name.getPrefix(), uri, name.getLocalPart(),
 110:                                 attribute.getValue());
 111:         else
 112:           writer.writeAttribute(name.getLocalPart(), attribute.getValue());
 113:         break;
 114:       case XMLStreamConstants.NAMESPACE:
 115:         Namespace namespace = (Namespace) event;
 116:         uri = namespace.getNamespaceURI();
 117:         writer.writeNamespace(namespace.getPrefix(), uri);
 118:         break;
 119:       case XMLStreamConstants.PROCESSING_INSTRUCTION:
 120:         ProcessingInstruction pi = (ProcessingInstruction) event;
 121:         String data = pi.getData();
 122:         if (data == null)
 123:           writer.writeProcessingInstruction(pi.getTarget());
 124:         else
 125:           writer.writeProcessingInstruction(pi.getTarget(), data);
 126:         break;
 127:       case XMLStreamConstants.COMMENT:
 128:         Comment comment = (Comment) event;
 129:         writer.writeComment(comment.getText());
 130:         break;
 131:       case XMLStreamConstants.START_DOCUMENT:
 132:         StartDocument startDocument = (StartDocument) event;
 133:         writer.writeStartDocument(startDocument.getVersion());
 134:         break;
 135:       case XMLStreamConstants.END_DOCUMENT:
 136:         writer.writeEndDocument();
 137:         break;
 138:       case XMLStreamConstants.DTD:
 139:         DTD dtd = (DTD) event;
 140:         writer.writeDTD(dtd.getDocumentTypeDeclaration());
 141:         break;
 142:       case XMLStreamConstants.CHARACTERS:
 143:       case XMLStreamConstants.SPACE:
 144:         Characters characters = event.asCharacters();
 145:         writer.writeCharacters(characters.getData());
 146:         break;
 147:       case XMLStreamConstants.CDATA:
 148:         Characters cdata = event.asCharacters();
 149:         writer.writeCData(cdata.getData());
 150:         break;
 151:       }
 152:   }
 153: 
 154:   public void add(XMLEventReader reader)
 155:     throws XMLStreamException
 156:   {
 157:     while (reader.hasNext())
 158:       add(reader.nextEvent());
 159:   }
 160: 
 161:   public String getPrefix(String uri)
 162:     throws XMLStreamException
 163:   {
 164:     return writer.getPrefix(uri);
 165:   }
 166: 
 167:   public void setPrefix(String prefix, String uri)
 168:     throws XMLStreamException
 169:   {
 170:     writer.setPrefix(prefix, uri);
 171:   }
 172: 
 173:   public void setDefaultNamespace(String uri)
 174:     throws XMLStreamException
 175:   {
 176:     writer.setDefaultNamespace(uri);
 177:   }
 178: 
 179:   public void setNamespaceContext(NamespaceContext context)
 180:     throws XMLStreamException
 181:   {
 182:     writer.setNamespaceContext(context);
 183:   }
 184: 
 185:   public NamespaceContext getNamespaceContext()
 186:   {
 187:     return writer.getNamespaceContext();
 188:   }
 189: 
 190: }