001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.streaming;
038:
039: import com.sun.xml.ws.api.streaming.XMLStreamReaderFactory;
040: import com.sun.xml.ws.util.FastInfosetUtil;
041: import com.sun.xml.ws.util.xml.XmlUtil;
042:
043: import javax.xml.stream.XMLStreamReader;
044: import javax.xml.transform.Source;
045: import javax.xml.transform.Transformer;
046: import javax.xml.transform.dom.DOMResult;
047: import javax.xml.transform.dom.DOMSource;
048: import javax.xml.transform.sax.SAXSource;
049: import javax.xml.transform.stream.StreamSource;
050: import java.io.InputStream;
051: import java.io.InputStreamReader;
052: import java.io.Reader;
053: import java.lang.reflect.Method;
054: import java.net.URL;
055:
056: /**
057: * @author Santiago.PericasGeertsen@sun.com
058: */
059: public class SourceReaderFactory {
060:
061: /**
062: * FI FastInfosetSource class.
063: */
064: static Class fastInfosetSourceClass;
065:
066: /**
067: * FI <code>StAXDocumentSerializer.setEncoding()</code> method via reflection.
068: */
069: static Method fastInfosetSource_getInputStream;
070:
071: static {
072: // Use reflection to avoid static dependency with FI jar
073: try {
074: fastInfosetSourceClass = Class
075: .forName("org.jvnet.fastinfoset.FastInfosetSource");
076: fastInfosetSource_getInputStream = fastInfosetSourceClass
077: .getMethod("getInputStream");
078: } catch (Exception e) {
079: fastInfosetSourceClass = null;
080: }
081: }
082:
083: public static XMLStreamReader createSourceReader(Source source,
084: boolean rejectDTDs) {
085: return createSourceReader(source, rejectDTDs, null);
086: }
087:
088: public static XMLStreamReader createSourceReader(Source source,
089: boolean rejectDTDs, String charsetName) {
090: try {
091: if (source instanceof StreamSource) {
092: StreamSource streamSource = (StreamSource) source;
093: InputStream is = streamSource.getInputStream();
094:
095: if (is != null) {
096: // Wrap input stream in Reader if charset is specified
097: if (charsetName != null) {
098: return XMLStreamReaderFactory.create(source
099: .getSystemId(), new InputStreamReader(
100: is, charsetName), rejectDTDs);
101: } else {
102: return XMLStreamReaderFactory.create(source
103: .getSystemId(), is, rejectDTDs);
104: }
105: } else {
106: Reader reader = streamSource.getReader();
107: if (reader != null) {
108: return XMLStreamReaderFactory.create(source
109: .getSystemId(), reader, rejectDTDs);
110: } else {
111: return XMLStreamReaderFactory.create(source
112: .getSystemId(), new URL(source
113: .getSystemId()).openStream(),
114: rejectDTDs);
115: }
116: }
117: } else if (source.getClass() == fastInfosetSourceClass) {
118: return FastInfosetUtil
119: .createFIStreamReader((InputStream) fastInfosetSource_getInputStream
120: .invoke(source));
121: } else if (source instanceof DOMSource) {
122: DOMStreamReader dsr = new DOMStreamReader();
123: dsr.setCurrentNode(((DOMSource) source).getNode());
124: return dsr;
125: } else if (source instanceof SAXSource) {
126: // TODO: need SAX to StAX adapter here -- Use transformer for now
127: Transformer tx = XmlUtil.newTransformer();
128: DOMResult domResult = new DOMResult();
129: tx.transform(source, domResult);
130: return createSourceReader(new DOMSource(domResult
131: .getNode()), rejectDTDs);
132: } else {
133: throw new XMLReaderException(
134: "sourceReader.invalidSource", source.getClass()
135: .getName());
136: }
137: } catch (Exception e) {
138: throw new XMLReaderException(e);
139: }
140: }
141:
142: }
|