1 package org.apache.maven.shared.utils.xml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 public class XmlStreamReader
32 extends Reader
33 {
34 private final org.apache.commons.io.input.XmlStreamReader reader;
35
36 private static String _staticDefaultEncoding = null;
37
38 public static void setDefaultEncoding( String encoding )
39 {
40 _staticDefaultEncoding = encoding;
41 }
42
43 public static String getDefaultEncoding()
44 {
45 return _staticDefaultEncoding;
46 }
47
48 public XmlStreamReader( File file )
49 throws IOException
50 {
51 this( new FileInputStream( file ) );
52 }
53
54 public XmlStreamReader( InputStream is )
55 throws IOException
56 {
57 this( is, true );
58 }
59
60 public XmlStreamReader( InputStream is, boolean lenient )
61 throws IOException, XmlStreamReaderException
62 {
63 reader = new org.apache.commons.io.input.XmlStreamReader( is, lenient, _staticDefaultEncoding );
64 }
65
66 public XmlStreamReader( URL url )
67 throws IOException
68 {
69 this( url.openConnection() );
70 }
71
72 public XmlStreamReader( URLConnection conn )
73 throws IOException
74 {
75 reader = new org.apache.commons.io.input.XmlStreamReader( conn, _staticDefaultEncoding );
76 }
77
78 public XmlStreamReader( InputStream is, String httpContentType )
79 throws IOException
80 {
81 this( is, httpContentType, true );
82 }
83
84 public XmlStreamReader( InputStream is, String httpContentType, boolean lenient, String defaultEncoding )
85 throws IOException, XmlStreamReaderException
86 {
87 reader = new org.apache.commons.io.input.XmlStreamReader( is, httpContentType, lenient,
88 ( defaultEncoding == null )
89 ? _staticDefaultEncoding
90 : defaultEncoding );
91 }
92
93 public XmlStreamReader( InputStream is, String httpContentType, boolean lenient )
94 throws IOException, XmlStreamReaderException
95 {
96 this( is, httpContentType, lenient, null );
97 }
98
99 public String getEncoding()
100 {
101 return reader.getEncoding();
102 }
103
104 public int read( char[] buf, int offset, int len )
105 throws IOException
106 {
107 return reader.read( buf, offset, len );
108 }
109
110 public void close()
111 throws IOException
112 {
113 reader.close();
114 }
115
116 static final Pattern ENCODING_PATTERN =
117 Pattern.compile( "<\\?xml.*encoding[\\s]*=[\\s]*((?:\".[^\"]*\")|(?:'.[^']*'))", Pattern.MULTILINE );
118 }