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