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