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 *
33 */
34 public class XmlStreamReader
35 extends Reader
36 {
37 private final org.apache.commons.io.input.XmlStreamReader reader;
38
39 private static String staticDefaultEncoding = null;
40
41 /**
42 * @param encoding define the default encoding.
43 */
44 public static void setDefaultEncoding( String encoding )
45 {
46 staticDefaultEncoding = encoding;
47 }
48
49 /**
50 * @return the default encoding.
51 */
52 public static String getDefaultEncoding()
53 {
54 return staticDefaultEncoding;
55 }
56
57 /**
58 * @param file The file to create it from.
59 * @throws IOException in case of an error.
60 */
61 public XmlStreamReader( File file )
62 throws IOException
63 {
64 this( new FileInputStream( file ) );
65 }
66
67 /**
68 * @param is {@link InputStream}
69 * @throws IOException in case of an error.
70 */
71 public XmlStreamReader( InputStream is )
72 throws IOException
73 {
74 this( is, true );
75 }
76
77 /**
78 * @param is {@link InputStream}
79 * @param lenient yes/no
80 * @throws IOException in case of an error.
81 * @throws XmlStreamReaderException in case of an error.
82 */
83 public XmlStreamReader( InputStream is, boolean lenient )
84 throws IOException, XmlStreamReaderException
85 {
86 reader = new org.apache.commons.io.input.XmlStreamReader( is, lenient, staticDefaultEncoding );
87 }
88
89 /**
90 * @param url {@link URL}
91 * @throws IOException in case of error.
92 */
93 public XmlStreamReader( URL url )
94 throws IOException
95 {
96 this( url.openConnection() );
97 }
98
99 /**
100 * @param conn The URL connection {@link URLConnection}.
101 * @throws IOException in case of error.
102 */
103 public XmlStreamReader( URLConnection conn )
104 throws IOException
105 {
106 reader = new org.apache.commons.io.input.XmlStreamReader( conn, staticDefaultEncoding );
107 }
108
109 /**
110 * @param is {@link InputStream}
111 * @param httpContentType content type.
112 * @throws IOException in case of error.
113 */
114 public XmlStreamReader( InputStream is, String httpContentType )
115 throws IOException
116 {
117 this( is, httpContentType, true );
118 }
119
120 /**
121 * @param is {@link InputStream}
122 * @param httpContentType content type.
123 * @param lenient yes/no.
124 * @param defaultEncoding The default encoding.
125 * @throws IOException in case of error.
126 * @throws XmlStreamReaderException in case of error.
127 */
128 public XmlStreamReader( InputStream is, String httpContentType, boolean lenient, String defaultEncoding )
129 throws IOException, XmlStreamReaderException
130 {
131 reader = new org.apache.commons.io.input.XmlStreamReader( is, httpContentType, lenient,
132 ( defaultEncoding == null )
133 ? staticDefaultEncoding
134 : defaultEncoding );
135 }
136
137 /**
138 * @param is {@link InputStream}
139 * @param httpContentType content type.
140 * @param lenient yes/no.
141 * @throws IOException in case of error.
142 * @throws XmlStreamReaderException in case of error.
143 */
144 public XmlStreamReader( InputStream is, String httpContentType, boolean lenient )
145 throws IOException, XmlStreamReaderException
146 {
147 this( is, httpContentType, lenient, null );
148 }
149
150 /**
151 * @return The current encoding.
152 */
153 public String getEncoding()
154 {
155 return reader.getEncoding();
156 }
157
158 /** {@inheritDoc} */
159 public int read( char[] buf, int offset, int len )
160 throws IOException
161 {
162 return reader.read( buf, offset, len );
163 }
164
165 /** {@inheritDoc} */
166 public void close()
167 throws IOException
168 {
169 reader.close();
170 }
171
172 static final Pattern ENCODING_PATTERN =
173 Pattern.compile( "<\\?xml.*encoding[\\s]*=[\\s]*((?:\".[^\"]*\")|(?:'.[^']*'))", Pattern.MULTILINE );
174 }