View Javadoc

1   package org.codehaus.mojo.osxappbundle.encoding;
2   
3   /*
4    * Copyright 2001-2008 The Codehaus.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.InputStream;
20  import java.io.Reader;
21  import java.io.InputStreamReader;
22  import java.io.UnsupportedEncodingException;
23  import java.io.IOException;
24  import java.util.regex.Pattern;
25  import java.util.regex.Matcher;
26  
27  /**
28   * Default implementation of EncodingDetector. Reads the first characters of the input stream and uses a regular expression to find any
29   * instances of <?xml encoding=".."?>
30   */
31  public class DefaultEncodingDetector
32      implements EncodingDetector
33  {
34  
35      private Pattern pattern = Pattern.compile( "<?(xml|XML).*encoding=\"(.*)\""); //.*encoding=\"(.*)\"" );
36  
37      private static final String DEFAULT_ENCODING = "utf-8";
38  
39      public String detectXmlEncoding( InputStream inputStream )
40      {
41          Reader reader;
42  
43          try
44          {
45              reader = new InputStreamReader( inputStream, "utf-8" );
46          }
47          catch ( UnsupportedEncodingException e )
48          {
49              throw new IllegalStateException( "utf not supported encoding", e );
50          }
51  
52          char[] buffer = new char[1000];
53  
54          try
55          {
56              int read = reader.read( buffer );
57  
58              String string = new String( buffer, 0, read );
59              Matcher matcher = pattern.matcher( string );
60              if ( matcher.find() )
61              {
62                  return matcher.group( 2 );
63              }
64              else
65              {
66                  return DEFAULT_ENCODING;
67              }
68          }
69          catch ( IOException e )
70          {
71              return DEFAULT_ENCODING;
72          }
73  
74      }
75  }