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