1 /* XMLOutputFactoryImpl.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 java.io.OutputStream;
41 import java.io.OutputStreamWriter;
42 import java.io.Writer;
43 import java.io.UnsupportedEncodingException;
44 
45 import javax.xml.transform.Result;
46 import javax.xml.transform.stream.StreamResult;
47 import javax.xml.stream.XMLEventWriter;
48 import javax.xml.stream.XMLOutputFactory;
49 import javax.xml.stream.XMLStreamException;
50 import javax.xml.stream.XMLStreamWriter;
51 
52 /**
53  * Standard output factory.
54  *
55  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
56  */
57 public class XMLOutputFactoryImpl
58   extends XMLOutputFactory
59 {
60 
61   protected boolean prefixDefaulting = false;
62 
XMLOutputFactoryImpl()63   public XMLOutputFactoryImpl()
64   {
65   }
66 
createXMLStreamWriter(Writer stream)67   public XMLStreamWriter createXMLStreamWriter(Writer stream)
68     throws XMLStreamException
69   {
70     // XXX try to determine character encoding of writer?
71     return new XMLStreamWriterImpl(stream, null, prefixDefaulting);
72   }
73 
createXMLStreamWriter(OutputStream stream)74   public XMLStreamWriter createXMLStreamWriter(OutputStream stream)
75     throws XMLStreamException
76   {
77     return createXMLStreamWriter(stream, "UTF-8");
78   }
79 
createXMLStreamWriter(OutputStream stream, String encoding)80   public XMLStreamWriter createXMLStreamWriter(OutputStream stream,
81                                                String encoding)
82     throws XMLStreamException
83   {
84     if (encoding == null)
85       encoding = "UTF-8";
86     try
87       {
88         Writer writer = new OutputStreamWriter(stream, encoding);
89         return new XMLStreamWriterImpl(writer, encoding, prefixDefaulting);
90       }
91     catch (UnsupportedEncodingException e)
92       {
93         XMLStreamException e2 = new XMLStreamException(e);
94         e2.initCause(e);
95         throw e2;
96       }
97   }
98 
createXMLStreamWriter(Result result)99   public XMLStreamWriter createXMLStreamWriter(Result result)
100     throws XMLStreamException
101   {
102     if (result instanceof StreamResult)
103       {
104         StreamResult sr = (StreamResult) result;
105         OutputStream out = sr.getOutputStream();
106         if (out != null)
107           return createXMLStreamWriter(out);
108         Writer writer = sr.getWriter();
109         if (writer != null)
110           return createXMLStreamWriter(writer);
111       }
112     throw new UnsupportedOperationException();
113   }
114 
createXMLEventWriter(OutputStream stream)115   public XMLEventWriter createXMLEventWriter(OutputStream stream)
116     throws XMLStreamException
117   {
118     XMLStreamWriter writer = createXMLStreamWriter(stream);
119     return new XMLEventWriterImpl(writer);
120   }
121 
createXMLEventWriter(OutputStream stream, String encoding)122   public XMLEventWriter createXMLEventWriter(OutputStream stream,
123                                              String encoding)
124     throws XMLStreamException
125   {
126     XMLStreamWriter writer = createXMLStreamWriter(stream, encoding);
127     return new XMLEventWriterImpl(writer);
128   }
129 
createXMLEventWriter(Writer stream)130   public XMLEventWriter createXMLEventWriter(Writer stream)
131     throws XMLStreamException
132   {
133     XMLStreamWriter writer = createXMLStreamWriter(stream);
134     return new XMLEventWriterImpl(writer);
135   }
136 
createXMLEventWriter(Result result)137   public XMLEventWriter createXMLEventWriter(Result result)
138     throws XMLStreamException
139   {
140     if (result instanceof StreamResult)
141       {
142         StreamResult sr = (StreamResult) result;
143         OutputStream out = sr.getOutputStream();
144         if (out != null)
145           return createXMLEventWriter(out);
146         Writer writer = sr.getWriter();
147         if (writer != null)
148           return createXMLEventWriter(writer);
149       }
150     throw new UnsupportedOperationException();
151   }
152 
setProperty(String name, Object value)153   public void setProperty(String name, Object value)
154     throws IllegalArgumentException
155   {
156     if (IS_REPAIRING_NAMESPACES.equals(name))
157       prefixDefaulting = ((Boolean) value).booleanValue();
158     else
159       throw new IllegalArgumentException(name);
160   }
161 
getProperty(String name)162   public Object getProperty(String name)
163     throws IllegalArgumentException
164   {
165     if (IS_REPAIRING_NAMESPACES.equals(name))
166       return new Boolean(prefixDefaulting);
167     throw new IllegalArgumentException(name);
168   }
169 
isPropertySupported(String name)170   public boolean isPropertySupported(String name)
171   {
172     if (IS_REPAIRING_NAMESPACES.equals(name))
173       return true;
174     return false;
175   }
176 
isPrefixDefaulting()177   public boolean isPrefixDefaulting()
178   {
179     return prefixDefaulting;
180   }
181 
setPrefixDefaulting(boolean value)182   public void setPrefixDefaulting(boolean value)
183   {
184     prefixDefaulting = value;
185   }
186 
187 }
188