001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.input;
018
019import java.io.IOException;
020
021/**
022 * The XmlStreamReaderException is thrown by the XmlStreamReader constructors if
023 * the charset encoding can not be determined according to the XML 1.0
024 * specification and RFC 3023.
025 * <p>
026 * The exception returns the unconsumed InputStream to allow the application to
027 * do an alternate processing with the stream. Note that the original
028 * InputStream given to the XmlStreamReader cannot be used as that one has been
029 * already read.
030 * </p>
031 *
032 * @since 2.0
033 */
034public class XmlStreamReaderException extends IOException {
035
036    private static final long serialVersionUID = 1L;
037
038    /**
039     * The Byte-Order-Mark (BOM) encoding or null.
040     */
041    private final String bomEncoding;
042
043    /**
044     * The guessed encoding.
045     */
046    private final String xmlGuessEncoding;
047
048    /**
049     * The XML encoding.
050     */
051    private final String xmlEncoding;
052
053    /**
054     * The MIME type in the content type.
055     */
056    private final String contentTypeMime;
057
058    /**
059     * The encoding in the content type.
060     */
061    private final String contentTypeEncoding;
062
063    /**
064     * Constructs an exception instance if the Charset encoding could not be
065     * determined.
066     * <p>
067     * Instances of this exception are thrown by the XmlStreamReader.
068     * </p>
069     *
070     * @param msg message describing the reason for the exception.
071     * @param bomEnc BOM encoding.
072     * @param xmlGuessEnc XML guess encoding.
073     * @param xmlEnc XML prolog encoding.
074     */
075    public XmlStreamReaderException(final String msg, final String bomEnc,
076            final String xmlGuessEnc, final String xmlEnc) {
077        this(msg, null, null, bomEnc, xmlGuessEnc, xmlEnc);
078    }
079
080    /**
081     * Constructs an exception instance if the Charset encoding could not be
082     * determined.
083     * <p>
084     * Instances of this exception are thrown by the XmlStreamReader.
085     * </p>
086     *
087     * @param msg message describing the reason for the exception.
088     * @param ctMime MIME type in the content-type.
089     * @param ctEnc encoding in the content-type.
090     * @param bomEnc BOM encoding.
091     * @param xmlGuessEnc XML guess encoding.
092     * @param xmlEnc XML prolog encoding.
093     */
094    public XmlStreamReaderException(final String msg, final String ctMime, final String ctEnc,
095            final String bomEnc, final String xmlGuessEnc, final String xmlEnc) {
096        super(msg);
097        contentTypeMime = ctMime;
098        contentTypeEncoding = ctEnc;
099        bomEncoding = bomEnc;
100        xmlGuessEncoding = xmlGuessEnc;
101        xmlEncoding = xmlEnc;
102    }
103
104    /**
105     * Gets the BOM encoding found in the InputStream.
106     *
107     * @return the BOM encoding, null if none.
108     */
109    public String getBomEncoding() {
110        return bomEncoding;
111    }
112
113    /**
114     * Gets the encoding in the content-type used to attempt determining the
115     * encoding.
116     *
117     * @return the encoding in the content-type, null if there was not
118     *         content-type, no encoding in it or the encoding detection did not
119     *         involve HTTP.
120     */
121    public String getContentTypeEncoding() {
122        return contentTypeEncoding;
123    }
124
125    /**
126     * Gets the MIME type in the content-type used to attempt determining the
127     * encoding.
128     *
129     * @return the MIME type in the content-type, null if there was not
130     *         content-type or the encoding detection did not involve HTTP.
131     */
132    public String getContentTypeMime() {
133        return contentTypeMime;
134    }
135
136    /**
137     * Gets the encoding found in the XML prolog of the input.
138     *
139     * @return the encoding of the XML prolog, null if none.
140     */
141    public String getXmlEncoding() {
142        return xmlEncoding;
143    }
144
145    /**
146     * Gets the encoding guess based on the first bytes of the input.
147     *
148     * @return the encoding guess, null if it couldn't be guessed.
149     */
150    public String getXmlGuessEncoding() {
151        return xmlGuessEncoding;
152    }
153}