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