001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.ws.streaming;
027:
028: import com.sun.xml.internal.ws.util.FastInfosetReflection;
029: import com.sun.xml.internal.ws.util.xml.XmlUtil;
030:
031: import java.io.Reader;
032: import java.io.InputStream;
033: import java.io.InputStreamReader;
034:
035: import javax.xml.transform.Source;
036: import javax.xml.transform.Transformer;
037: import javax.xml.transform.dom.DOMSource;
038: import javax.xml.transform.dom.DOMResult;
039: import javax.xml.transform.sax.SAXSource;
040: import javax.xml.transform.stream.StreamSource;
041: import javax.xml.stream.XMLStreamReader;
042:
043: /**
044: * @author Santiago.PericasGeertsen@sun.com
045: */
046: public class SourceReaderFactory {
047:
048: /**
049: * Thread variable used to store DOMStreamReader for current thread.
050: */
051: static ThreadLocal<DOMStreamReader> domStreamReader = new ThreadLocal<DOMStreamReader>();
052:
053: public static XMLStreamReader createSourceReader(Source source,
054: boolean rejectDTDs) {
055: return createSourceReader(source, rejectDTDs, null);
056: }
057:
058: public static XMLStreamReader createSourceReader(Source source,
059: boolean rejectDTDs, String charsetName) {
060: try {
061: if (source instanceof StreamSource) {
062: StreamSource streamSource = (StreamSource) source;
063: InputStream is = streamSource.getInputStream();
064:
065: if (is != null) {
066: // Wrap input stream in Reader if charset is specified
067: if (charsetName != null) {
068: return XMLStreamReaderFactory
069: .createXMLStreamReader(
070: new InputStreamReader(is,
071: charsetName),
072: rejectDTDs);
073: } else {
074: return XMLStreamReaderFactory
075: .createXMLStreamReader(is, rejectDTDs);
076: }
077: } else {
078: Reader reader = streamSource.getReader();
079: if (reader != null) {
080: return XMLStreamReaderFactory
081: .createXMLStreamReader(reader,
082: rejectDTDs);
083: } else {
084: throw new XMLReaderException(
085: "sourceReader.invalidSource",
086: new Object[] { source.getClass()
087: .getName() });
088: }
089: }
090: } else if (FastInfosetReflection
091: .isFastInfosetSource(source)) {
092: return XMLStreamReaderFactory
093: .createFIStreamReader((InputStream) FastInfosetReflection
094: .FastInfosetSource_getInputStream(source));
095: } else if (source instanceof DOMSource) {
096: DOMStreamReader dsr = domStreamReader.get();
097: if (dsr == null) {
098: domStreamReader.set(dsr = new DOMStreamReader());
099: }
100: dsr.setCurrentNode(((DOMSource) source).getNode());
101: return dsr;
102: } else if (source instanceof SAXSource) {
103: // TODO: need SAX to StAX adapter here -- Use transformer for now
104: Transformer tx = XmlUtil.newTransformer();
105: DOMResult domResult = new DOMResult();
106: tx.transform(source, domResult);
107: return createSourceReader(new DOMSource(domResult
108: .getNode()), rejectDTDs);
109: } else {
110: throw new XMLReaderException(
111: "sourceReader.invalidSource",
112: new Object[] { source.getClass().getName() });
113: }
114: } catch (Exception e) {
115: throw new XMLReaderException(e);
116: }
117: }
118:
119: }
|