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  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 }