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.api.server;
038:
039: import com.sun.xml.stream.buffer.XMLStreamBuffer;
040: import com.sun.xml.ws.streaming.TidyXMLStreamReader;
041: import com.sun.xml.ws.api.streaming.XMLStreamReaderFactory;
042:
043: import javax.xml.stream.XMLInputFactory;
044: import javax.xml.stream.XMLStreamException;
045: import javax.xml.stream.XMLStreamReader;
046: import java.io.IOException;
047: import java.io.InputStream;
048: import java.net.URL;
049:
050: /**
051: * SPI that provides the source of {@link SDDocument}.
052: *
053: * <p>
054: * This abstract class could be implemented by appliations, or one of the
055: * {@link #create} methods can be used.
056: *
057: * @author Kohsuke Kawaguchi
058: */
059: public abstract class SDDocumentSource {
060: /**
061: * Returns the {@link XMLStreamReader} that reads the document.
062: *
063: * <p>
064: * This method maybe invoked multiple times concurrently.
065: *
066: * @param xif
067: * The implementation may choose to use this object when it wants to
068: * create a new parser (or it can just ignore this parameter completely.)
069: * @return
070: * The caller is responsible for closing the reader to avoid resource leak.
071: *
072: * @throws XMLStreamException
073: * if something goes wrong while creating a parser.
074: * @throws IOException
075: * if something goes wrong trying to read the document.
076: */
077: public abstract XMLStreamReader read(XMLInputFactory xif)
078: throws IOException, XMLStreamException;
079:
080: /**
081: * Returns the {@link XMLStreamReader} that reads the document.
082: *
083: * <p>
084: * This method maybe invoked multiple times concurrently.
085: *
086: * @return
087: * The caller is responsible for closing the reader to avoid resource leak.
088: *
089: * @throws XMLStreamException
090: * if something goes wrong while creating a parser.
091: * @throws IOException
092: * if something goes wrong trying to read the document.
093: */
094: public abstract XMLStreamReader read() throws IOException,
095: XMLStreamException;
096:
097: /**
098: * System ID of this document.
099: */
100: public abstract URL getSystemId();
101:
102: /**
103: * Creates {@link SDDocumentSource} from an URL.
104: */
105: public static SDDocumentSource create(final URL url) {
106: return new SDDocumentSource() {
107: private final URL systemId = url;
108:
109: public XMLStreamReader read(XMLInputFactory xif)
110: throws IOException, XMLStreamException {
111: InputStream is = url.openStream();
112: return new TidyXMLStreamReader(xif
113: .createXMLStreamReader(systemId
114: .toExternalForm(), is), is);
115: }
116:
117: public XMLStreamReader read() throws IOException,
118: XMLStreamException {
119: InputStream is = url.openStream();
120: return new TidyXMLStreamReader(XMLStreamReaderFactory
121: .create(systemId.toExternalForm(), is, false),
122: is);
123: }
124:
125: public URL getSystemId() {
126: return systemId;
127: }
128: };
129: }
130:
131: /**
132: * Creates a {@link SDDocumentSource} from {@link XMLStreamBuffer}.
133: */
134: public static SDDocumentSource create(final URL systemId,
135: final XMLStreamBuffer xsb) {
136: return new SDDocumentSource() {
137: public XMLStreamReader read(XMLInputFactory xif)
138: throws XMLStreamException {
139: return xsb.readAsXMLStreamReader();
140: }
141:
142: public XMLStreamReader read() throws XMLStreamException {
143: return xsb.readAsXMLStreamReader();
144: }
145:
146: public URL getSystemId() {
147: return systemId;
148: }
149: };
150: }
151: }
|