Coverage Report - org.codehaus.plexus.util.xml.XmlReader
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlReader
0%
0/221
0%
0/184
3,308
 
 1  
 package org.codehaus.plexus.util.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.BufferedInputStream;
 23  
 import java.io.BufferedReader;
 24  
 import java.io.File;
 25  
 import java.io.FileInputStream;
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.io.InputStreamReader;
 29  
 import java.io.Reader;
 30  
 import java.io.StringReader;
 31  
 import java.net.URL;
 32  
 import java.net.URLConnection;
 33  
 import java.net.HttpURLConnection;
 34  
 import java.util.regex.Pattern;
 35  
 import java.util.regex.Matcher;
 36  
 import java.text.MessageFormat;
 37  
 
 38  
 /**
 39  
  * Character stream that handles (or at least attemtps to) all the necessary Voodo to figure out the charset encoding of
 40  
  * the XML document within the stream.
 41  
  * <p>
 42  
  * IMPORTANT: This class is not related in any way to the org.xml.sax.XMLReader. This one IS a character stream.
 43  
  * <p>
 44  
  * All this has to be done without consuming characters from the stream, if not the XML parser will not recognized the
 45  
  * document as a valid XML. This is not 100% true, but it's close enough (UTF-8 BOM is not handled by all parsers right
 46  
  * now, XmlReader handles it and things work in all parsers).
 47  
  * <p>
 48  
  * The XmlReader class handles the charset encoding of XML documents in Files, raw streams and HTTP streams by offering
 49  
  * a wide set of constructors.
 50  
  * <P>
 51  
  * By default the charset encoding detection is lenient, the constructor with the lenient flag can be used for an script
 52  
  * (following HTTP MIME and XML specifications). All this is nicely explained by Mark Pilgrim in his blog, <a
 53  
  * href="http://diveintomark.org/archives/2004/02/13/xml-media-types"> Determining the character encoding of a feed</a>.
 54  
  * <p>
 55  
  * 
 56  
  * @author Alejandro Abdelnur
 57  
  * @version revision 1.17 taken on 26/06/2007 from Rome (see https://rome.dev.java.net/source/browse/rome/src/java/com/sun/syndication/io/XmlReader.java)
 58  
  * @deprecated use XmlStreamReader
 59  
  * @since 1.4.3
 60  
  * @deprecated TO BE REMOVED from here when plexus-utils is upgraded to 1.4.5+ (and prerequisite upgraded to Maven 2.0.6)
 61  
  */
 62  
 public class XmlReader extends Reader
 63  
 {
 64  
     private static final int BUFFER_SIZE = 4096;
 65  
 
 66  
     private static final String UTF_8 = "UTF-8";
 67  
 
 68  
     private static final String US_ASCII = "US-ASCII";
 69  
 
 70  
     private static final String UTF_16BE = "UTF-16BE";
 71  
 
 72  
     private static final String UTF_16LE = "UTF-16LE";
 73  
 
 74  
     private static final String UTF_16 = "UTF-16";
 75  
 
 76  
     private static final String EBCDIC = "CP1047";
 77  
 
 78  0
     private static String _staticDefaultEncoding = null;
 79  
 
 80  
     private Reader _reader;
 81  
 
 82  
     private String _encoding;
 83  
 
 84  
     private String _defaultEncoding;
 85  
 
 86  
     /**
 87  
      * Sets the default encoding to use if none is set in HTTP content-type, XML prolog and the rules based on
 88  
      * content-type are not adequate. <p/> If it is set to NULL the content-type based rules are used. <p/> By default
 89  
      * it is NULL. <p/>
 90  
      * 
 91  
      * @param encoding
 92  
      *            charset encoding to default to.
 93  
      */
 94  
     public static void setDefaultEncoding( String encoding )
 95  
     {
 96  0
         _staticDefaultEncoding = encoding;
 97  0
     }
 98  
 
 99  
     /**
 100  
      * Returns the default encoding to use if none is set in HTTP content-type, XML prolog and the rules based on
 101  
      * content-type are not adequate. <p/> If it is NULL the content-type based rules are used. <p/>
 102  
      * 
 103  
      * @return the default encoding to use.
 104  
      */
 105  
     public static String getDefaultEncoding()
 106  
     {
 107  0
         return _staticDefaultEncoding;
 108  
     }
 109  
 
 110  
     /**
 111  
      * Creates a Reader for a File.
 112  
      * <p>
 113  
      * It looks for the UTF-8 BOM first, if none sniffs the XML prolog charset, if this is also missing defaults to
 114  
      * UTF-8.
 115  
      * <p>
 116  
      * It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
 117  
      * <p>
 118  
      * 
 119  
      * @param file
 120  
      *            File to create a Reader from.
 121  
      * @throws IOException
 122  
      *             thrown if there is a problem reading the file.
 123  
      * 
 124  
      */
 125  
     public XmlReader( File file ) throws IOException
 126  
     {
 127  0
         this( new FileInputStream( file ) );
 128  0
     }
 129  
 
 130  
     /**
 131  
      * Creates a Reader for a raw InputStream.
 132  
      * <p>
 133  
      * It follows the same logic used for files.
 134  
      * <p>
 135  
      * It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
 136  
      * <p>
 137  
      * 
 138  
      * @param is
 139  
      *            InputStream to create a Reader from.
 140  
      * @throws IOException
 141  
      *             thrown if there is a problem reading the stream.
 142  
      * 
 143  
      */
 144  
     public XmlReader( InputStream is ) throws IOException
 145  
     {
 146  0
         this( is, true );
 147  0
     }
 148  
 
 149  
     /**
 150  
      * Creates a Reader for a raw InputStream.
 151  
      * <p>
 152  
      * It follows the same logic used for files.
 153  
      * <p>
 154  
      * If lenient detection is indicated and the detection above fails as per specifications it then attempts the
 155  
      * following:
 156  
      * <p>
 157  
      * If the content type was 'text/html' it replaces it with 'text/xml' and tries the detection again.
 158  
      * <p>
 159  
      * Else if the XML prolog had a charset encoding that encoding is used.
 160  
      * <p>
 161  
      * Else if the content type had a charset encoding that encoding is used.
 162  
      * <p>
 163  
      * Else 'UTF-8' is used.
 164  
      * <p>
 165  
      * If lenient detection is indicated an XmlStreamReaderException is never thrown.
 166  
      * <p>
 167  
      * 
 168  
      * @param is
 169  
      *            InputStream to create a Reader from.
 170  
      * @param lenient
 171  
      *            indicates if the charset encoding detection should be relaxed.
 172  
      * @throws IOException
 173  
      *             thrown if there is a problem reading the stream.
 174  
      * @throws XmlStreamReaderException
 175  
      *             thrown if the charset encoding could not be determined according to the specs.
 176  
      * 
 177  
      */
 178  
     public XmlReader( InputStream is, boolean lenient ) throws IOException, XmlStreamReaderException
 179  0
     {
 180  0
         _defaultEncoding = _staticDefaultEncoding;
 181  
         try
 182  
         {
 183  0
             doRawStream( is, lenient );
 184  
         }
 185  0
         catch ( XmlStreamReaderException ex )
 186  
         {
 187  0
             if ( !lenient )
 188  
             {
 189  0
                 throw ex;
 190  
             }
 191  
             else
 192  
             {
 193  0
                 doLenientDetection( null, ex );
 194  
             }
 195  0
         }
 196  0
     }
 197  
 
 198  
     /**
 199  
      * Creates a Reader using the InputStream of a URL.
 200  
      * <p>
 201  
      * If the URL is not of type HTTP and there is not 'content-type' header in the fetched data it uses the same logic
 202  
      * used for Files.
 203  
      * <p>
 204  
      * If the URL is a HTTP Url or there is a 'content-type' header in the fetched data it uses the same logic used for
 205  
      * an InputStream with content-type.
 206  
      * <p>
 207  
      * It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
 208  
      * <p>
 209  
      * 
 210  
      * @param url
 211  
      *            URL to create a Reader from.
 212  
      * @throws IOException
 213  
      *             thrown if there is a problem reading the stream of the URL.
 214  
      * 
 215  
      */
 216  
     public XmlReader( URL url ) throws IOException
 217  
     {
 218  0
         this( url.openConnection() );
 219  0
     }
 220  
 
 221  
     /**
 222  
      * Creates a Reader using the InputStream of a URLConnection.
 223  
      * <p>
 224  
      * If the URLConnection is not of type HttpURLConnection and there is not 'content-type' header in the fetched data
 225  
      * it uses the same logic used for files.
 226  
      * <p>
 227  
      * If the URLConnection is a HTTP Url or there is a 'content-type' header in the fetched data it uses the same logic
 228  
      * used for an InputStream with content-type.
 229  
      * <p>
 230  
      * It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
 231  
      * <p>
 232  
      * 
 233  
      * @param conn
 234  
      *            URLConnection to create a Reader from.
 235  
      * @throws IOException
 236  
      *             thrown if there is a problem reading the stream of the URLConnection.
 237  
      * 
 238  
      */
 239  
     public XmlReader( URLConnection conn ) throws IOException
 240  0
     {
 241  0
         _defaultEncoding = _staticDefaultEncoding;
 242  0
         boolean lenient = true;
 243  0
         if ( conn instanceof HttpURLConnection )
 244  
         {
 245  
             try
 246  
             {
 247  0
                 doHttpStream( conn.getInputStream(), conn.getContentType(), lenient );
 248  
             }
 249  0
             catch ( XmlStreamReaderException ex )
 250  
             {
 251  0
                 doLenientDetection( conn.getContentType(), ex );
 252  0
             }
 253  
         }
 254  0
         else if ( conn.getContentType() != null )
 255  
         {
 256  
             try
 257  
             {
 258  0
                 doHttpStream( conn.getInputStream(), conn.getContentType(), lenient );
 259  
             }
 260  0
             catch ( XmlStreamReaderException ex )
 261  
             {
 262  0
                 doLenientDetection( conn.getContentType(), ex );
 263  0
             }
 264  
         }
 265  
         else
 266  
         {
 267  
             try
 268  
             {
 269  0
                 doRawStream( conn.getInputStream(), lenient );
 270  
             }
 271  0
             catch ( XmlStreamReaderException ex )
 272  
             {
 273  0
                 doLenientDetection( null, ex );
 274  0
             }
 275  
         }
 276  0
     }
 277  
 
 278  
     /**
 279  
      * Creates a Reader using an InputStream an the associated content-type header.
 280  
      * <p>
 281  
      * First it checks if the stream has BOM. If there is not BOM checks the content-type encoding. If there is not
 282  
      * content-type encoding checks the XML prolog encoding. If there is not XML prolog encoding uses the default
 283  
      * encoding mandated by the content-type MIME type.
 284  
      * <p>
 285  
      * It does a lenient charset encoding detection, check the constructor with the lenient parameter for details.
 286  
      * <p>
 287  
      * 
 288  
      * @param is
 289  
      *            InputStream to create the reader from.
 290  
      * @param httpContentType
 291  
      *            content-type header to use for the resolution of the charset encoding.
 292  
      * @throws IOException
 293  
      *             thrown if there is a problem reading the file.
 294  
      * 
 295  
      */
 296  
     public XmlReader( InputStream is, String httpContentType ) throws IOException
 297  
     {
 298  0
         this( is, httpContentType, true );
 299  0
     }
 300  
 
 301  
     /**
 302  
      * Creates a Reader using an InputStream an the associated content-type header. This constructor is lenient
 303  
      * regarding the encoding detection.
 304  
      * <p>
 305  
      * First it checks if the stream has BOM. If there is not BOM checks the content-type encoding. If there is not
 306  
      * content-type encoding checks the XML prolog encoding. If there is not XML prolog encoding uses the default
 307  
      * encoding mandated by the content-type MIME type.
 308  
      * <p>
 309  
      * If lenient detection is indicated and the detection above fails as per specifications it then attempts the
 310  
      * following:
 311  
      * <p>
 312  
      * If the content type was 'text/html' it replaces it with 'text/xml' and tries the detection again.
 313  
      * <p>
 314  
      * Else if the XML prolog had a charset encoding that encoding is used.
 315  
      * <p>
 316  
      * Else if the content type had a charset encoding that encoding is used.
 317  
      * <p>
 318  
      * Else 'UTF-8' is used.
 319  
      * <p>
 320  
      * If lenient detection is indicated an XmlStreamReaderException is never thrown.
 321  
      * <p>
 322  
      * 
 323  
      * @param is
 324  
      *            InputStream to create the reader from.
 325  
      * @param httpContentType
 326  
      *            content-type header to use for the resolution of the charset encoding.
 327  
      * @param lenient
 328  
      *            indicates if the charset encoding detection should be relaxed.
 329  
      * @throws IOException
 330  
      *             thrown if there is a problem reading the file.
 331  
      * @throws XmlStreamReaderException
 332  
      *             thrown if the charset encoding could not be determined according to the specs.
 333  
      * 
 334  
      */
 335  
     public XmlReader( InputStream is, String httpContentType, boolean lenient, String defaultEncoding )
 336  
         throws IOException, XmlStreamReaderException
 337  0
     {
 338  0
         _defaultEncoding = ( defaultEncoding == null ) ? _staticDefaultEncoding : defaultEncoding;
 339  
         try
 340  
         {
 341  0
             doHttpStream( is, httpContentType, lenient );
 342  
         }
 343  0
         catch ( XmlStreamReaderException ex )
 344  
         {
 345  0
             if ( !lenient )
 346  
             {
 347  0
                 throw ex;
 348  
             }
 349  
             else
 350  
             {
 351  0
                 doLenientDetection( httpContentType, ex );
 352  
             }
 353  0
         }
 354  0
     }
 355  
 
 356  
     /**
 357  
      * Creates a Reader using an InputStream an the associated content-type header. This constructor is lenient
 358  
      * regarding the encoding detection.
 359  
      * <p>
 360  
      * First it checks if the stream has BOM. If there is not BOM checks the content-type encoding. If there is not
 361  
      * content-type encoding checks the XML prolog encoding. If there is not XML prolog encoding uses the default
 362  
      * encoding mandated by the content-type MIME type.
 363  
      * <p>
 364  
      * If lenient detection is indicated and the detection above fails as per specifications it then attempts the
 365  
      * following:
 366  
      * <p>
 367  
      * If the content type was 'text/html' it replaces it with 'text/xml' and tries the detection again.
 368  
      * <p>
 369  
      * Else if the XML prolog had a charset encoding that encoding is used.
 370  
      * <p>
 371  
      * Else if the content type had a charset encoding that encoding is used.
 372  
      * <p>
 373  
      * Else 'UTF-8' is used.
 374  
      * <p>
 375  
      * If lenient detection is indicated an XmlStreamReaderException is never thrown.
 376  
      * <p>
 377  
      * 
 378  
      * @param is
 379  
      *            InputStream to create the reader from.
 380  
      * @param httpContentType
 381  
      *            content-type header to use for the resolution of the charset encoding.
 382  
      * @param lenient
 383  
      *            indicates if the charset encoding detection should be relaxed.
 384  
      * @throws IOException
 385  
      *             thrown if there is a problem reading the file.
 386  
      * @throws XmlStreamReaderException
 387  
      *             thrown if the charset encoding could not be determined according to the specs.
 388  
      * 
 389  
      */
 390  
     public XmlReader( InputStream is, String httpContentType, boolean lenient ) throws IOException, XmlStreamReaderException
 391  
     {
 392  0
         this( is, httpContentType, lenient, null );
 393  0
     }
 394  
 
 395  
     private void doLenientDetection( String httpContentType, XmlStreamReaderException ex ) throws IOException
 396  
     {
 397  0
         if ( httpContentType != null )
 398  
         {
 399  0
             if ( httpContentType.startsWith( "text/html" ) )
 400  
             {
 401  0
                 httpContentType = httpContentType.substring( "text/html".length() );
 402  0
                 httpContentType = "text/xml" + httpContentType;
 403  
                 try
 404  
                 {
 405  0
                     doHttpStream( ex.getInputStream(), httpContentType, true );
 406  0
                     ex = null;
 407  
                 }
 408  0
                 catch ( XmlStreamReaderException ex2 )
 409  
                 {
 410  0
                     ex = ex2;
 411  0
                 }
 412  
             }
 413  
         }
 414  0
         if ( ex != null )
 415  
         {
 416  0
             String encoding = ex.getXmlEncoding();
 417  0
             if ( encoding == null )
 418  
             {
 419  0
                 encoding = ex.getContentTypeEncoding();
 420  
             }
 421  0
             if ( encoding == null )
 422  
             {
 423  0
                 encoding = ( _defaultEncoding == null ) ? UTF_8 : _defaultEncoding;
 424  
             }
 425  0
             prepareReader( ex.getInputStream(), encoding );
 426  
         }
 427  0
     }
 428  
 
 429  
     /**
 430  
      * Returns the charset encoding of the XmlReader.
 431  
      * <p>
 432  
      * 
 433  
      * @return charset encoding.
 434  
      * 
 435  
      */
 436  
     public String getEncoding()
 437  
     {
 438  0
         return _encoding;
 439  
     }
 440  
 
 441  
     public int read( char[] buf, int offset, int len ) throws IOException
 442  
     {
 443  0
         return _reader.read( buf, offset, len );
 444  
     }
 445  
 
 446  
     /**
 447  
      * Closes the XmlReader stream.
 448  
      * <p>
 449  
      * 
 450  
      * @throws IOException
 451  
      *             thrown if there was a problem closing the stream.
 452  
      * 
 453  
      */
 454  
     public void close() throws IOException
 455  
     {
 456  0
         _reader.close();
 457  0
     }
 458  
 
 459  
     private void doRawStream( InputStream is, boolean lenient ) throws IOException
 460  
     {
 461  0
         BufferedInputStream pis = new BufferedInputStream( is, BUFFER_SIZE );
 462  0
         String bomEnc = getBOMEncoding( pis );
 463  0
         String xmlGuessEnc = getXMLGuessEncoding( pis );
 464  0
         String xmlEnc = getXmlProlog( pis, xmlGuessEnc );
 465  0
         String encoding = calculateRawEncoding( bomEnc, xmlGuessEnc, xmlEnc, pis );
 466  0
         prepareReader( pis, encoding );
 467  0
     }
 468  
 
 469  
     private void doHttpStream( InputStream is, String httpContentType, boolean lenient ) throws IOException
 470  
     {
 471  0
         BufferedInputStream pis = new BufferedInputStream( is, BUFFER_SIZE );
 472  0
         String cTMime = getContentTypeMime( httpContentType );
 473  0
         String cTEnc = getContentTypeEncoding( httpContentType );
 474  0
         String bomEnc = getBOMEncoding( pis );
 475  0
         String xmlGuessEnc = getXMLGuessEncoding( pis );
 476  0
         String xmlEnc = getXmlProlog( pis, xmlGuessEnc );
 477  0
         String encoding = calculateHttpEncoding( cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc, pis, lenient );
 478  0
         prepareReader( pis, encoding );
 479  0
     }
 480  
 
 481  
     private void prepareReader( InputStream is, String encoding ) throws IOException
 482  
     {
 483  0
         _reader = new InputStreamReader( is, encoding );
 484  0
         _encoding = encoding;
 485  0
     }
 486  
 
 487  
     // InputStream is passed for XmlStreamReaderException creation only
 488  
     private String calculateRawEncoding( String bomEnc, String xmlGuessEnc, String xmlEnc, InputStream is )
 489  
         throws IOException
 490  
     {
 491  
         String encoding;
 492  0
         if ( bomEnc == null )
 493  
         {
 494  0
             if ( xmlGuessEnc == null || xmlEnc == null )
 495  
             {
 496  0
                 encoding = ( _defaultEncoding == null ) ? UTF_8 : _defaultEncoding;
 497  
             }
 498  0
             else if ( xmlEnc.equals( UTF_16 ) && ( xmlGuessEnc.equals( UTF_16BE ) || xmlGuessEnc.equals( UTF_16LE ) ) )
 499  
             {
 500  0
                 encoding = xmlGuessEnc;
 501  
             }
 502  
             else
 503  
             {
 504  0
                 encoding = xmlEnc;
 505  
             }
 506  
         }
 507  0
         else if ( bomEnc.equals( UTF_8 ) )
 508  
         {
 509  0
             if ( xmlGuessEnc != null && !xmlGuessEnc.equals( UTF_8 ) )
 510  
             {
 511  0
                 throw new XmlStreamReaderException( RAW_EX_1.format( new Object[] { bomEnc, xmlGuessEnc, xmlEnc } ), bomEnc,
 512  
                                               xmlGuessEnc, xmlEnc, is );
 513  
             }
 514  0
             if ( xmlEnc != null && !xmlEnc.equals( UTF_8 ) )
 515  
             {
 516  0
                 throw new XmlStreamReaderException( RAW_EX_1.format( new Object[] { bomEnc, xmlGuessEnc, xmlEnc } ), bomEnc,
 517  
                                               xmlGuessEnc, xmlEnc, is );
 518  
             }
 519  0
             encoding = UTF_8;
 520  
         }
 521  0
         else if ( bomEnc.equals( UTF_16BE ) || bomEnc.equals( UTF_16LE ) )
 522  
         {
 523  0
             if ( xmlGuessEnc != null && !xmlGuessEnc.equals( bomEnc ) )
 524  
             {
 525  0
                 throw new IOException( RAW_EX_1.format( new Object[] { bomEnc, xmlGuessEnc, xmlEnc } ) );
 526  
             }
 527  0
             if ( xmlEnc != null && !xmlEnc.equals( UTF_16 ) && !xmlEnc.equals( bomEnc ) )
 528  
             {
 529  0
                 throw new XmlStreamReaderException( RAW_EX_1.format( new Object[] { bomEnc, xmlGuessEnc, xmlEnc } ), bomEnc,
 530  
                                               xmlGuessEnc, xmlEnc, is );
 531  
             }
 532  0
             encoding = bomEnc;
 533  
         }
 534  
         else
 535  
         {
 536  0
             throw new XmlStreamReaderException( RAW_EX_2.format( new Object[] { bomEnc, xmlGuessEnc, xmlEnc } ), bomEnc,
 537  
                                           xmlGuessEnc, xmlEnc, is );
 538  
         }
 539  0
         return encoding;
 540  
     }
 541  
 
 542  
     // InputStream is passed for XmlStreamReaderException creation only
 543  
     private String calculateHttpEncoding( String cTMime, String cTEnc, String bomEnc, String xmlGuessEnc,
 544  
                                           String xmlEnc, InputStream is, boolean lenient ) throws IOException
 545  
     {
 546  
         String encoding;
 547  0
         if ( lenient & xmlEnc != null )
 548  
         {
 549  0
             encoding = xmlEnc;
 550  
         }
 551  
         else
 552  
         {
 553  0
             boolean appXml = isAppXml( cTMime );
 554  0
             boolean textXml = isTextXml( cTMime );
 555  0
             if ( appXml || textXml )
 556  
             {
 557  0
                 if ( cTEnc == null )
 558  
                 {
 559  0
                     if ( appXml )
 560  
                     {
 561  0
                         encoding = calculateRawEncoding( bomEnc, xmlGuessEnc, xmlEnc, is );
 562  
                     }
 563  
                     else
 564  
                     {
 565  0
                         encoding = ( _defaultEncoding == null ) ? US_ASCII : _defaultEncoding;
 566  
                     }
 567  
                 }
 568  0
                 else if ( bomEnc != null && ( cTEnc.equals( UTF_16BE ) || cTEnc.equals( UTF_16LE ) ) )
 569  
                 {
 570  0
                     throw new XmlStreamReaderException( HTTP_EX_1.format( new Object[] { cTMime, cTEnc, bomEnc, xmlGuessEnc,
 571  
                         xmlEnc } ), cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc, is );
 572  
                 }
 573  0
                 else if ( cTEnc.equals( UTF_16 ) )
 574  
                 {
 575  0
                     if ( bomEnc != null && bomEnc.startsWith( UTF_16 ) )
 576  
                     {
 577  0
                         encoding = bomEnc;
 578  
                     }
 579  
                     else
 580  
                     {
 581  0
                         throw new XmlStreamReaderException( HTTP_EX_2.format( new Object[] { cTMime, cTEnc, bomEnc,
 582  
                             xmlGuessEnc, xmlEnc } ), cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc, is );
 583  
                     }
 584  
                 }
 585  
                 else
 586  
                 {
 587  0
                     encoding = cTEnc;
 588  
                 }
 589  
             }
 590  
             else
 591  
             {
 592  0
                 throw new XmlStreamReaderException( HTTP_EX_3.format( new Object[] { cTMime, cTEnc, bomEnc, xmlGuessEnc,
 593  
                     xmlEnc } ), cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc, is );
 594  
             }
 595  
         }
 596  0
         return encoding;
 597  
     }
 598  
 
 599  
     // returns MIME type or NULL if httpContentType is NULL
 600  
     private static String getContentTypeMime( String httpContentType )
 601  
     {
 602  0
         String mime = null;
 603  0
         if ( httpContentType != null )
 604  
         {
 605  0
             int i = httpContentType.indexOf( ";" );
 606  0
             mime = ( ( i == -1 ) ? httpContentType : httpContentType.substring( 0, i ) ).trim();
 607  
         }
 608  0
         return mime;
 609  
     }
 610  
 
 611  0
     private static final Pattern CHARSET_PATTERN = Pattern.compile( "charset=([.[^; ]]*)" );
 612  
 
 613  
     // returns charset parameter value, NULL if not present, NULL if httpContentType is NULL
 614  
     private static String getContentTypeEncoding( String httpContentType )
 615  
     {
 616  0
         String encoding = null;
 617  0
         if ( httpContentType != null )
 618  
         {
 619  0
             int i = httpContentType.indexOf( ";" );
 620  0
             if ( i > -1 )
 621  
             {
 622  0
                 String postMime = httpContentType.substring( i + 1 );
 623  0
                 Matcher m = CHARSET_PATTERN.matcher( postMime );
 624  0
                 encoding = ( m.find() ) ? m.group( 1 ) : null;
 625  0
                 encoding = ( encoding != null ) ? encoding.toUpperCase() : null;
 626  
             }
 627  
         }
 628  0
         return encoding;
 629  
     }
 630  
 
 631  
     // returns the BOM in the stream, NULL if not present,
 632  
     // if there was BOM the in the stream it is consumed
 633  
     private static String getBOMEncoding( BufferedInputStream is ) throws IOException
 634  
     {
 635  0
         String encoding = null;
 636  0
         int[] bytes = new int[3];
 637  0
         is.mark( 3 );
 638  0
         bytes[0] = is.read();
 639  0
         bytes[1] = is.read();
 640  0
         bytes[2] = is.read();
 641  
 
 642  0
         if ( bytes[0] == 0xFE && bytes[1] == 0xFF )
 643  
         {
 644  0
             encoding = UTF_16BE;
 645  0
             is.reset();
 646  0
             is.read();
 647  0
             is.read();
 648  
         }
 649  0
         else if ( bytes[0] == 0xFF && bytes[1] == 0xFE )
 650  
         {
 651  0
             encoding = UTF_16LE;
 652  0
             is.reset();
 653  0
             is.read();
 654  0
             is.read();
 655  
         }
 656  0
         else if ( bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF )
 657  
         {
 658  0
             encoding = UTF_8;
 659  
         }
 660  
         else
 661  
         {
 662  0
             is.reset();
 663  
         }
 664  0
         return encoding;
 665  
     }
 666  
 
 667  
     // returns the best guess for the encoding by looking the first bytes of the stream, '<?'
 668  
     private static String getXMLGuessEncoding( BufferedInputStream is ) throws IOException
 669  
     {
 670  0
         String encoding = null;
 671  0
         int[] bytes = new int[4];
 672  0
         is.mark( 4 );
 673  0
         bytes[0] = is.read();
 674  0
         bytes[1] = is.read();
 675  0
         bytes[2] = is.read();
 676  0
         bytes[3] = is.read();
 677  0
         is.reset();
 678  
 
 679  0
         if ( bytes[0] == 0x00 && bytes[1] == 0x3C && bytes[2] == 0x00 && bytes[3] == 0x3F )
 680  
         {
 681  0
             encoding = UTF_16BE;
 682  
         }
 683  0
         else if ( bytes[0] == 0x3C && bytes[1] == 0x00 && bytes[2] == 0x3F && bytes[3] == 0x00 )
 684  
         {
 685  0
             encoding = UTF_16LE;
 686  
         }
 687  0
         else if ( bytes[0] == 0x3C && bytes[1] == 0x3F && bytes[2] == 0x78 && bytes[3] == 0x6D )
 688  
         {
 689  0
             encoding = UTF_8;
 690  
         }
 691  0
         else if ( bytes[0] == 0x4C && bytes[1] == 0x6F && bytes[2] == 0xA7 && bytes[3] == 0x94 )
 692  
         {
 693  0
             encoding = EBCDIC;
 694  
         }
 695  0
         return encoding;
 696  
     }
 697  
 
 698  0
     static final Pattern ENCODING_PATTERN =
 699  
         Pattern.compile( "<\\?xml.*encoding[\\s]*=[\\s]*((?:\".[^\"]*\")|(?:'.[^']*'))", Pattern.MULTILINE );
 700  
 
 701  
     // returns the encoding declared in the <?xml encoding=...?>, NULL if none
 702  
     private static String getXmlProlog( BufferedInputStream is, String guessedEnc ) throws IOException
 703  
     {
 704  0
         String encoding = null;
 705  0
         if ( guessedEnc != null )
 706  
         {
 707  0
             byte[] bytes = new byte[BUFFER_SIZE];
 708  0
             is.mark( BUFFER_SIZE );
 709  0
             int offset = 0;
 710  0
             int max = BUFFER_SIZE;
 711  0
             int c = is.read( bytes, offset, max );
 712  0
             int firstGT = -1;
 713  0
             String xmlProlog = null;
 714  0
             while ( c != -1 && firstGT == -1 && offset < BUFFER_SIZE )
 715  
             {
 716  0
                 offset += c;
 717  0
                 max -= c;
 718  0
                 c = is.read( bytes, offset, max );
 719  0
                 xmlProlog = new String( bytes, 0, offset, guessedEnc );
 720  0
                 firstGT = xmlProlog.indexOf( '>' );
 721  
             }
 722  0
             if ( firstGT == -1 )
 723  
             {
 724  0
                 if ( c == -1 )
 725  
                 {
 726  0
                     throw new IOException( "Unexpected end of XML stream" );
 727  
                 }
 728  
                 else
 729  
                 {
 730  0
                     throw new IOException( "XML prolog or ROOT element not found on first " + offset + " bytes" );
 731  
                 }
 732  
             }
 733  0
             int bytesRead = offset;
 734  0
             if ( bytesRead > 0 )
 735  
             {
 736  0
                 is.reset();
 737  0
                 BufferedReader bReader = new BufferedReader( new StringReader( xmlProlog.substring( 0, firstGT + 1 ) ) );
 738  0
                 StringBuffer prolog = new StringBuffer();
 739  0
                 String line = bReader.readLine();
 740  0
                 while ( line != null )
 741  
                 {
 742  0
                     prolog.append( line );
 743  0
                     line = bReader.readLine();
 744  
                 }
 745  0
                 Matcher m = ENCODING_PATTERN.matcher( prolog );
 746  0
                 if ( m.find() )
 747  
                 {
 748  0
                     encoding = m.group( 1 ).toUpperCase();
 749  0
                     encoding = encoding.substring( 1, encoding.length() - 1 );
 750  
                 }
 751  
             }
 752  
         }
 753  0
         return encoding;
 754  
     }
 755  
 
 756  
     // indicates if the MIME type belongs to the APPLICATION XML family
 757  
     private static boolean isAppXml( String mime )
 758  
     {
 759  0
         return mime != null
 760  
                         && ( mime.equals( "application/xml" ) || mime.equals( "application/xml-dtd" )
 761  
                                         || mime.equals( "application/xml-external-parsed-entity" ) || ( mime.startsWith( "application/" ) && mime.endsWith( "+xml" ) ) );
 762  
     }
 763  
 
 764  
     // indicates if the MIME type belongs to the TEXT XML family
 765  
     private static boolean isTextXml( String mime )
 766  
     {
 767  0
         return mime != null
 768  
                         && ( mime.equals( "text/xml" ) || mime.equals( "text/xml-external-parsed-entity" ) || ( mime.startsWith( "text/" ) && mime.endsWith( "+xml" ) ) );
 769  
     }
 770  
 
 771  0
     private static final MessageFormat RAW_EX_1 =
 772  
         new MessageFormat( "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] encoding mismatch" );
 773  
 
 774  0
     private static final MessageFormat RAW_EX_2 =
 775  
         new MessageFormat( "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] unknown BOM" );
 776  
 
 777  0
     private static final MessageFormat HTTP_EX_1 =
 778  
         new MessageFormat(
 779  
                            "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], BOM must be NULL" );
 780  
 
 781  0
     private static final MessageFormat HTTP_EX_2 =
 782  
         new MessageFormat(
 783  
                            "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], encoding mismatch" );
 784  
 
 785  0
     private static final MessageFormat HTTP_EX_3 =
 786  
         new MessageFormat(
 787  
                            "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], Invalid MIME" );
 788  
 
 789  
 }