XmlStreamReaderException.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io.input;

  18. import java.io.IOException;

  19. /**
  20.  * The XmlStreamReaderException is thrown by the XmlStreamReader constructors if
  21.  * the charset encoding cannot be determined according to the XML 1.0
  22.  * specification and RFC 3023.
  23.  * <p>
  24.  * The exception returns the unconsumed InputStream to allow the application to
  25.  * do an alternate processing with the stream. Note that the original
  26.  * InputStream given to the XmlStreamReader cannot be used as that one has been
  27.  * already read.
  28.  * </p>
  29.  *
  30.  * @since 2.0
  31.  */
  32. public class XmlStreamReaderException extends IOException {

  33.     private static final long serialVersionUID = 1L;

  34.     /**
  35.      * The Byte-Order-Mark (BOM) encoding or null.
  36.      */
  37.     private final String bomEncoding;

  38.     /**
  39.      * The guessed encoding.
  40.      */
  41.     private final String xmlGuessEncoding;

  42.     /**
  43.      * The XML encoding.
  44.      */
  45.     private final String xmlEncoding;

  46.     /**
  47.      * The MIME type in the content type.
  48.      */
  49.     private final String contentTypeMime;

  50.     /**
  51.      * The encoding in the content type.
  52.      */
  53.     private final String contentTypeEncoding;

  54.     /**
  55.      * Constructs an exception instance if the Charset encoding could not be
  56.      * determined.
  57.      * <p>
  58.      * Instances of this exception are thrown by the XmlStreamReader.
  59.      * </p>
  60.      *
  61.      * @param msg message describing the reason for the exception.
  62.      * @param bomEnc BOM encoding.
  63.      * @param xmlGuessEnc XML guess encoding.
  64.      * @param xmlEnc XML prolog encoding.
  65.      */
  66.     public XmlStreamReaderException(final String msg, final String bomEnc,
  67.             final String xmlGuessEnc, final String xmlEnc) {
  68.         this(msg, null, null, bomEnc, xmlGuessEnc, xmlEnc);
  69.     }

  70.     /**
  71.      * Constructs an exception instance if the Charset encoding could not be
  72.      * determined.
  73.      * <p>
  74.      * Instances of this exception are thrown by the XmlStreamReader.
  75.      * </p>
  76.      *
  77.      * @param msg message describing the reason for the exception.
  78.      * @param ctMime MIME type in the content-type.
  79.      * @param ctEnc encoding in the content-type.
  80.      * @param bomEnc BOM encoding.
  81.      * @param xmlGuessEnc XML guess encoding.
  82.      * @param xmlEnc XML prolog encoding.
  83.      */
  84.     public XmlStreamReaderException(final String msg, final String ctMime, final String ctEnc,
  85.             final String bomEnc, final String xmlGuessEnc, final String xmlEnc) {
  86.         super(msg);
  87.         contentTypeMime = ctMime;
  88.         contentTypeEncoding = ctEnc;
  89.         bomEncoding = bomEnc;
  90.         xmlGuessEncoding = xmlGuessEnc;
  91.         xmlEncoding = xmlEnc;
  92.     }

  93.     /**
  94.      * Gets the BOM encoding found in the InputStream.
  95.      *
  96.      * @return the BOM encoding, null if none.
  97.      */
  98.     public String getBomEncoding() {
  99.         return bomEncoding;
  100.     }

  101.     /**
  102.      * Gets the encoding in the content-type used to attempt determining the
  103.      * encoding.
  104.      *
  105.      * @return the encoding in the content-type, null if there was not
  106.      *         content-type, no encoding in it or the encoding detection did not
  107.      *         involve HTTP.
  108.      */
  109.     public String getContentTypeEncoding() {
  110.         return contentTypeEncoding;
  111.     }

  112.     /**
  113.      * Gets the MIME type in the content-type used to attempt determining the
  114.      * encoding.
  115.      *
  116.      * @return the MIME type in the content-type, null if there was not
  117.      *         content-type or the encoding detection did not involve HTTP.
  118.      */
  119.     public String getContentTypeMime() {
  120.         return contentTypeMime;
  121.     }

  122.     /**
  123.      * Gets the encoding found in the XML prolog of the input.
  124.      *
  125.      * @return the encoding of the XML prolog, null if none.
  126.      */
  127.     public String getXmlEncoding() {
  128.         return xmlEncoding;
  129.     }

  130.     /**
  131.      * Gets the encoding guess based on the first bytes of the input.
  132.      *
  133.      * @return the encoding guess, null if it couldn't be guessed.
  134.      */
  135.     public String getXmlGuessEncoding() {
  136.         return xmlGuessEncoding;
  137.     }
  138. }