View Javadoc
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      @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 }