Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / xml / stream / XMLOutputFactoryImpl.java
blobc8c651fb1afa85370eae276d0ac22f60c3717bf6
1 /* XMLOutputFactoryImpl.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. */
38 package gnu.xml.stream;
40 import java.io.OutputStream;
41 import java.io.OutputStreamWriter;
42 import java.io.Writer;
43 import java.io.UnsupportedEncodingException;
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;
52 /**
53 * Standard output factory.
55 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
57 public class XMLOutputFactoryImpl
58 extends XMLOutputFactory
61 protected boolean prefixDefaulting = false;
63 public XMLOutputFactoryImpl()
67 public XMLStreamWriter createXMLStreamWriter(Writer stream)
68 throws XMLStreamException
70 // XXX try to determine character encoding of writer?
71 return new XMLStreamWriterImpl(stream, null, prefixDefaulting);
74 public XMLStreamWriter createXMLStreamWriter(OutputStream stream)
75 throws XMLStreamException
77 return createXMLStreamWriter(stream, "UTF-8");
80 public XMLStreamWriter createXMLStreamWriter(OutputStream stream,
81 String encoding)
82 throws XMLStreamException
84 if (encoding == null)
85 encoding = "UTF-8";
86 try
88 Writer writer = new OutputStreamWriter(stream, encoding);
89 return new XMLStreamWriterImpl(writer, encoding, prefixDefaulting);
91 catch (UnsupportedEncodingException e)
93 XMLStreamException e2 = new XMLStreamException(e);
94 e2.initCause(e);
95 throw e2;
99 public XMLStreamWriter createXMLStreamWriter(Result result)
100 throws XMLStreamException
102 if (result instanceof StreamResult)
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);
112 throw new UnsupportedOperationException();
115 public XMLEventWriter createXMLEventWriter(OutputStream stream)
116 throws XMLStreamException
118 XMLStreamWriter writer = createXMLStreamWriter(stream);
119 return new XMLEventWriterImpl(writer);
122 public XMLEventWriter createXMLEventWriter(OutputStream stream,
123 String encoding)
124 throws XMLStreamException
126 XMLStreamWriter writer = createXMLStreamWriter(stream, encoding);
127 return new XMLEventWriterImpl(writer);
130 public XMLEventWriter createXMLEventWriter(Writer stream)
131 throws XMLStreamException
133 XMLStreamWriter writer = createXMLStreamWriter(stream);
134 return new XMLEventWriterImpl(writer);
137 public XMLEventWriter createXMLEventWriter(Result result)
138 throws XMLStreamException
140 if (result instanceof StreamResult)
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);
150 throw new UnsupportedOperationException();
153 public void setProperty(String name, Object value)
154 throws IllegalArgumentException
156 if (IS_REPAIRING_NAMESPACES.equals(name))
157 prefixDefaulting = ((Boolean) value).booleanValue();
158 else
159 throw new IllegalArgumentException(name);
162 public Object getProperty(String name)
163 throws IllegalArgumentException
165 if (IS_REPAIRING_NAMESPACES.equals(name))
166 return new Boolean(prefixDefaulting);
167 throw new IllegalArgumentException(name);
170 public boolean isPropertySupported(String name)
172 if (IS_REPAIRING_NAMESPACES.equals(name))
173 return true;
174 return false;
177 public boolean isPrefixDefaulting()
179 return prefixDefaulting;
182 public void setPrefixDefaulting(boolean value)
184 prefixDefaulting = value;