View Javadoc
1   /*
2    * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.xml.internal.ws.api.server;
27  
28  import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
29  import com.sun.xml.internal.ws.streaming.TidyXMLStreamReader;
30  import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
31  
32  import javax.xml.stream.XMLInputFactory;
33  import javax.xml.stream.XMLStreamException;
34  import javax.xml.stream.XMLStreamReader;
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.net.URL;
38  
39  /**
40   * SPI that provides the source of {@link SDDocument}.
41   *
42   * <p>
43   * This abstract class could be implemented by appliations, or one of the
44   * {@link #create} methods can be used.
45   *
46   * @author Kohsuke Kawaguchi
47   */
48  public abstract class SDDocumentSource {
49      /**
50       * Returns the {@link XMLStreamReader} that reads the document.
51       *
52       * <p>
53       * This method maybe invoked multiple times concurrently.
54       *
55       * @param xif
56       *      The implementation may choose to use this object when it wants to
57       *      create a new parser (or it can just ignore this parameter completely.)
58       * @return
59       *      The caller is responsible for closing the reader to avoid resource leak.
60       *
61       * @throws XMLStreamException
62       *      if something goes wrong while creating a parser.
63       * @throws IOException
64       *      if something goes wrong trying to read the document.
65       */
66      public abstract XMLStreamReader read(XMLInputFactory xif) throws IOException, XMLStreamException;
67  
68      /**
69       * Returns the {@link XMLStreamReader} that reads the document.
70       *
71       * <p>
72       * This method maybe invoked multiple times concurrently.
73       *
74       * @return
75       *      The caller is responsible for closing the reader to avoid resource leak.
76       *
77       * @throws XMLStreamException
78       *      if something goes wrong while creating a parser.
79       * @throws IOException
80       *      if something goes wrong trying to read the document.
81       */
82      public abstract XMLStreamReader read() throws IOException, XMLStreamException;
83  
84      /**
85       * System ID of this document.
86       */
87      public abstract URL getSystemId();
88  
89      /**
90       * Creates {@link SDDocumentSource} from an URL.
91       */
92      public static SDDocumentSource create(final URL url) {
93          return new SDDocumentSource() {
94              private final URL systemId = url;
95  
96              public XMLStreamReader read(XMLInputFactory xif) throws IOException, XMLStreamException {
97                  InputStream is = url.openStream();
98                  return new TidyXMLStreamReader(
99                      xif.createXMLStreamReader(systemId.toExternalForm(),is), is);
100             }
101 
102             public XMLStreamReader read() throws IOException, XMLStreamException {
103                 InputStream is = url.openStream();
104                 return new TidyXMLStreamReader(
105                    XMLStreamReaderFactory.create(systemId.toExternalForm(),is,false), is);
106             }
107 
108             public URL getSystemId() {
109                 return systemId;
110             }
111         };
112     }
113 
114     /**
115      * Creates a {@link SDDocumentSource} from {@link XMLStreamBuffer}.
116      */
117     public static SDDocumentSource create(final URL systemId, final XMLStreamBuffer xsb) {
118         return new SDDocumentSource() {
119             public XMLStreamReader read(XMLInputFactory xif) throws XMLStreamException {
120                 return xsb.readAsXMLStreamReader();
121             }
122 
123             public XMLStreamReader read() throws XMLStreamException {
124                 return xsb.readAsXMLStreamReader();
125             }
126 
127             public URL getSystemId() {
128                 return systemId;
129             }
130         };
131     }
132 }