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