001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Adam Megacz
028: */
029:
030: package com.caucho.xml.stream;
031:
032: import com.caucho.util.L10N;
033: import com.caucho.vfs.Vfs;
034:
035: import javax.xml.stream.XMLEventWriter;
036: import javax.xml.stream.XMLOutputFactory;
037: import javax.xml.stream.XMLStreamException;
038: import javax.xml.stream.XMLStreamWriter;
039: import javax.xml.transform.Result;
040: import javax.xml.transform.dom.DOMResult;
041: import javax.xml.transform.sax.SAXResult;
042: import javax.xml.transform.stream.StreamResult;
043: import java.io.IOException;
044: import java.io.OutputStream;
045: import java.io.OutputStreamWriter;
046: import java.io.Writer;
047:
048: public class XMLOutputFactoryImpl extends XMLOutputFactory {
049: private static final L10N L = new L10N(XMLOutputFactoryImpl.class);
050:
051: private boolean _repair = false;
052:
053: public XMLOutputFactoryImpl() {
054: }
055:
056: //
057: // Event writer
058: //
059:
060: public XMLEventWriter createXMLEventWriter(OutputStream stream)
061: throws XMLStreamException {
062: return new XMLEventWriterImpl(createXMLStreamWriter(stream));
063: }
064:
065: public XMLEventWriter createXMLEventWriter(OutputStream stream,
066: String encoding) throws XMLStreamException {
067: return new XMLEventWriterImpl(createXMLStreamWriter(stream,
068: encoding));
069: }
070:
071: /**
072: * This method is optional.
073: */
074: public XMLEventWriter createXMLEventWriter(Result result)
075: throws XMLStreamException {
076: return new XMLEventWriterImpl(createXMLStreamWriter(result));
077: }
078:
079: public XMLEventWriter createXMLEventWriter(Writer stream)
080: throws XMLStreamException {
081: return new XMLEventWriterImpl(createXMLStreamWriter(stream));
082: }
083:
084: //
085: // Stream writer
086: //
087:
088: public XMLStreamWriter createXMLStreamWriter(OutputStream stream)
089: throws XMLStreamException {
090: return new XMLStreamWriterImpl(Vfs.openWrite(stream), _repair);
091: }
092:
093: public XMLStreamWriter createXMLStreamWriter(OutputStream stream,
094: String encoding) throws XMLStreamException {
095: try {
096: OutputStreamWriter osw = new OutputStreamWriter(stream,
097: encoding);
098: return new XMLStreamWriterImpl(Vfs.openWrite(osw), _repair);
099: } catch (IOException e) {
100: throw new XMLStreamException(e);
101: }
102: }
103:
104: /**
105: * This method is optional.
106: */
107: public XMLStreamWriter createXMLStreamWriter(Result result)
108: throws XMLStreamException {
109: if (result instanceof DOMResult) {
110: return new DOMResultXMLStreamWriterImpl((DOMResult) result,
111: _repair);
112: } else if (result instanceof SAXResult) {
113: return new SAXResultXMLStreamWriterImpl((SAXResult) result);
114: } else if (result instanceof StreamResult) {
115: Writer writer = ((StreamResult) result).getWriter();
116:
117: if (writer != null)
118: return createXMLStreamWriter(writer);
119:
120: OutputStream os = ((StreamResult) result).getOutputStream();
121:
122: if (os != null)
123: return createXMLStreamWriter(os);
124:
125: throw new XMLStreamException(
126: "StreamResult has no output stream or writer");
127: }
128:
129: throw new UnsupportedOperationException(L.l(
130: "Results of type {0} are not supported", result
131: .getClass().getName()));
132: }
133:
134: public XMLStreamWriter createXMLStreamWriter(Writer stream)
135: throws XMLStreamException {
136: return new XMLStreamWriterImpl(Vfs.openWrite(stream), _repair);
137: }
138:
139: public Object getProperty(String name)
140: throws IllegalArgumentException {
141: throw new IllegalArgumentException("property \"" + name
142: + "\" not supported");
143: }
144:
145: public boolean isPropertySupported(String name) {
146: return false;
147: }
148:
149: public void setProperty(String name, Object value)
150: throws IllegalArgumentException {
151: if (IS_REPAIRING_NAMESPACES.equals(name)) {
152: if (value instanceof Boolean)
153: _repair = ((Boolean) value).booleanValue();
154: else
155: throw new IllegalArgumentException("value of " + name
156: + " must be Boolean, not " + value);
157: } else
158: throw new IllegalArgumentException("property \"" + name
159: + "\" not supported");
160: }
161: }
|