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    private final String bomEncoding;
039
040    private final String xmlGuessEncoding;
041
042    private final String xmlEncoding;
043
044    private final String contentTypeMime;
045
046    private final String contentTypeEncoding;
047
048    /**
049     * Constructs an exception instance if the charset encoding could not be
050     * determined.
051     * <p>
052     * Instances of this exception are thrown by the XmlStreamReader.
053     * </p>
054     *
055     * @param msg message describing the reason for the exception.
056     * @param bomEnc BOM encoding.
057     * @param xmlGuessEnc XML guess encoding.
058     * @param xmlEnc XML prolog encoding.
059     */
060    public XmlStreamReaderException(final String msg, final String bomEnc,
061            final String xmlGuessEnc, final String xmlEnc) {
062        this(msg, null, null, bomEnc, xmlGuessEnc, xmlEnc);
063    }
064
065    /**
066     * Constructs an exception instance if the charset encoding could not be
067     * determined.
068     * <p>
069     * Instances of this exception are thrown by the XmlStreamReader.
070     * </p>
071     *
072     * @param msg message describing the reason for the exception.
073     * @param ctMime MIME type in the content-type.
074     * @param ctEnc encoding in the content-type.
075     * @param bomEnc BOM encoding.
076     * @param xmlGuessEnc XML guess encoding.
077     * @param xmlEnc XML prolog encoding.
078     */
079    public XmlStreamReaderException(final String msg, final String ctMime, final String ctEnc,
080            final String bomEnc, final String xmlGuessEnc, final String xmlEnc) {
081        super(msg);
082        contentTypeMime = ctMime;
083        contentTypeEncoding = ctEnc;
084        bomEncoding = bomEnc;
085        xmlGuessEncoding = xmlGuessEnc;
086        xmlEncoding = xmlEnc;
087    }
088
089    /**
090     * Returns the BOM encoding found in the InputStream.
091     *
092     * @return the BOM encoding, null if none.
093     */
094    public String getBomEncoding() {
095        return bomEncoding;
096    }
097
098    /**
099     * Returns the encoding in the content-type used to attempt determining the
100     * encoding.
101     *
102     * @return the encoding in the content-type, null if there was not
103     *         content-type, no encoding in it or the encoding detection did not
104     *         involve HTTP.
105     */
106    public String getContentTypeEncoding() {
107        return contentTypeEncoding;
108    }
109
110    /**
111     * Returns the MIME type in the content-type used to attempt determining the
112     * encoding.
113     *
114     * @return the MIME type in the content-type, null if there was not
115     *         content-type or the encoding detection did not involve HTTP.
116     */
117    public String getContentTypeMime() {
118        return contentTypeMime;
119    }
120
121    /**
122     * Returns the encoding found in the XML prolog of the InputStream.
123     *
124     * @return the encoding of the XML prolog, null if none.
125     */
126    public String getXmlEncoding() {
127        return xmlEncoding;
128    }
129
130    /**
131     * Returns the encoding guess based on the first bytes of the InputStream.
132     *
133     * @return the encoding guess, null if it couldn't be guessed.
134     */
135    public String getXmlGuessEncoding() {
136        return xmlGuessEncoding;
137    }
138}