View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.utils.xml;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.Reader;
26  import java.net.URL;
27  import java.net.URLConnection;
28  import java.util.regex.Pattern;
29  
30  /**
31   * @deprecated use org.apache.commons.io.input.XmlStreamReader instead
32   */
33  @Deprecated
34  public class XmlStreamReader extends Reader {
35      private final org.apache.commons.io.input.XmlStreamReader reader;
36  
37      private static String staticDefaultEncoding = null;
38  
39      /**
40       * @param encoding define the default encoding.
41       */
42      public static void setDefaultEncoding(String encoding) {
43          staticDefaultEncoding = encoding;
44      }
45  
46      /**
47       * @return the default encoding.
48       */
49      public static String getDefaultEncoding() {
50          return staticDefaultEncoding;
51      }
52  
53      /**
54       * @param file The file to create it from.
55       * @throws IOException in case of an error
56       */
57      public XmlStreamReader(File file) throws IOException {
58          this(new FileInputStream(file));
59      }
60  
61      /**
62       * @param is {@link InputStream}
63       * @throws IOException in case of an error
64       */
65      public XmlStreamReader(InputStream is) throws IOException {
66          this(is, true);
67      }
68  
69      /**
70       * @param is {@link InputStream}
71       * @param lenient yes/no
72       * @throws IOException in case of an error
73       */
74      public XmlStreamReader(InputStream is, boolean lenient) throws IOException {
75          reader = new org.apache.commons.io.input.XmlStreamReader(is, lenient, staticDefaultEncoding);
76      }
77  
78      /**
79       * @param url {@link URL}
80       * @throws IOException in case of error
81       */
82      public XmlStreamReader(URL url) throws IOException {
83          this(url.openConnection());
84      }
85  
86      /**
87       * @param conn The URL connection {@link URLConnection}
88       * @throws IOException in case of error
89       */
90      public XmlStreamReader(URLConnection conn) throws IOException {
91          reader = new org.apache.commons.io.input.XmlStreamReader(conn, staticDefaultEncoding);
92      }
93  
94      /**
95       * @param is {@link InputStream}
96       * @param httpContentType content type
97       * @throws IOException in case of error
98       */
99      public XmlStreamReader(InputStream is, String httpContentType) throws IOException {
100         this(is, httpContentType, true);
101     }
102 
103     /**
104      * @param is {@link InputStream}
105      * @param httpContentType content type
106      * @param lenient yes/no
107      * @param defaultEncoding the default encoding
108      * @throws IOException in case of error
109      */
110     public XmlStreamReader(InputStream is, String httpContentType, boolean lenient, String defaultEncoding)
111             throws IOException {
112         reader = new org.apache.commons.io.input.XmlStreamReader(
113                 is, httpContentType, lenient, (defaultEncoding == null) ? staticDefaultEncoding : defaultEncoding);
114     }
115 
116     /**
117      * @param is {@link InputStream}
118      * @param httpContentType content type
119      * @param lenient yes/no
120      * @throws IOException in case of error
121      */
122     public XmlStreamReader(InputStream is, String httpContentType, boolean lenient) throws IOException {
123         this(is, httpContentType, lenient, null);
124     }
125 
126     /**
127      * @return the current encoding
128      */
129     public String getEncoding() {
130         return reader.getEncoding();
131     }
132 
133     /** {@inheritDoc} */
134     public int read(char[] buf, int offset, int len) throws IOException {
135         return reader.read(buf, offset, len);
136     }
137 
138     /** {@inheritDoc} */
139     public void close() throws IOException {
140         reader.close();
141     }
142 
143     static final Pattern ENCODING_PATTERN =
144             Pattern.compile("<\\?xml.*encoding[\\s]*=[\\s]*((?:\".[^\"]*\")|(?:'.[^']*'))", Pattern.MULTILINE);
145 }