KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > stream > XMLOutputFactoryImpl


1 /*
2 * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
3 *
4 * This file is part of Resin(R) Open Source
5 *
6 * Each copy or derived work must preserve the copyright notice and this
7 * notice unmodified.
8 *
9 * Resin Open Source is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * Resin Open Source is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17 * of NON-INFRINGEMENT. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Resin Open Source; if not, write to the
22 *
23 * Free Software Foundation, Inc.
24 * 59 Temple Place, Suite 330
25 * Boston, MA 02111-1307 USA
26 *
27 * @author Adam Megacz
28 */

29
30 package com.caucho.xml.stream;
31 import com.caucho.util.L10N;
32 import com.caucho.vfs.Vfs;
33
34 import javax.xml.stream.XMLEventWriter;
35 import javax.xml.stream.XMLOutputFactory;
36 import javax.xml.stream.XMLStreamException;
37 import javax.xml.stream.XMLStreamWriter;
38 import javax.xml.transform.Result JavaDoc;
39 import javax.xml.transform.dom.DOMResult JavaDoc;
40 import javax.xml.transform.stream.StreamResult JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.OutputStream JavaDoc;
43 import java.io.OutputStreamWriter JavaDoc;
44 import java.io.Writer JavaDoc;
45
46 public class XMLOutputFactoryImpl extends XMLOutputFactory {
47   private static final L10N L = new L10N(XMLOutputFactoryImpl.class);
48
49   private boolean _repair = false;
50
51   public XMLOutputFactoryImpl()
52   {
53   }
54
55
56   //
57
// Event writer
58
//
59

60   public XMLEventWriter createXMLEventWriter(OutputStream JavaDoc stream)
61     throws XMLStreamException
62   {
63     return new XMLEventWriterImpl(createXMLStreamWriter(stream));
64   }
65
66   public XMLEventWriter createXMLEventWriter(OutputStream JavaDoc stream,
67                                              String JavaDoc encoding)
68     throws XMLStreamException
69   {
70     return new XMLEventWriterImpl(createXMLStreamWriter(stream, encoding));
71   }
72
73   /**
74    * This method is optional.
75    */

76   public XMLEventWriter createXMLEventWriter(Result JavaDoc result)
77     throws XMLStreamException
78   {
79     throw new JAXPNotSupportedInStAXException();
80   }
81
82   public XMLEventWriter createXMLEventWriter(Writer stream)
83     throws XMLStreamException
84   {
85     return new XMLEventWriterImpl(createXMLStreamWriter(stream));
86   }
87
88   //
89
// Stream writer
90
//
91

92   public XMLStreamWriter createXMLStreamWriter(OutputStream JavaDoc stream)
93     throws XMLStreamException
94   {
95     return new XMLStreamWriterImpl(Vfs.openWrite(stream), _repair);
96   }
97
98   public XMLStreamWriter createXMLStreamWriter(OutputStream JavaDoc stream,
99                                                String JavaDoc encoding)
100     throws XMLStreamException
101   {
102     try {
103       OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(stream, encoding);
104       return new XMLStreamWriterImpl(Vfs.openWrite(osw), _repair);
105     }
106     catch (IOException JavaDoc e) {
107       throw new XMLStreamException(e);
108     }
109   }
110
111   /**
112    * This method is optional.
113    */

114   public XMLStreamWriter createXMLStreamWriter(Result JavaDoc result)
115     throws XMLStreamException
116   {
117     if (result instanceof DOMResult JavaDoc) {
118       return new DOMResultXMLStreamWriterImpl((DOMResult JavaDoc) result);
119     }
120     else if (result instanceof StreamResult JavaDoc) {
121       Writer writer = ((StreamResult JavaDoc) result).getWriter();
122       return createXMLStreamWriter(writer);
123     }
124
125     throw new UnsupportedOperationException JavaDoc(L.l("Results of type {0} are not supported", result.getClass().getName()));
126   }
127
128   public XMLStreamWriter createXMLStreamWriter(Writer stream)
129     throws XMLStreamException
130   {
131     return new XMLStreamWriterImpl(Vfs.openWrite(stream), _repair);
132   }
133
134   public Object JavaDoc getProperty(String JavaDoc name)
135     throws IllegalArgumentException JavaDoc
136   {
137     throw new IllegalArgumentException JavaDoc("property \""+name+"\" not supported");
138   }
139
140   public boolean isPropertySupported(String JavaDoc name)
141   {
142     return false;
143   }
144
145   public void setProperty(String JavaDoc name, Object JavaDoc value)
146     throws IllegalArgumentException JavaDoc
147   {
148     if ("javax.xml.stream.isRepairingNamespaces".equals(name)) {
149       if (value instanceof Boolean JavaDoc)
150         _repair = ((Boolean JavaDoc) value).booleanValue();
151       else
152         throw new IllegalArgumentException JavaDoc("value of " + name + " must be Boolean, not " + value);
153     }
154     else
155       throw new IllegalArgumentException JavaDoc("property \""+name+"\" not supported");
156   }
157 }
158
Popular Tags