View Javadoc
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  
19  import java.io.IOException;
20  
21  /**
22   * The XmlStreamReaderException is thrown by the XmlStreamReader constructors if
23   * the charset encoding can not be determined according to the XML 1.0
24   * specification and RFC 3023.
25   * <p>
26   * The exception returns the unconsumed InputStream to allow the application to
27   * do an alternate processing with the stream. Note that the original
28   * InputStream given to the XmlStreamReader cannot be used as that one has been
29   * already read.
30   * </p>
31   *
32   * @since 2.0
33   */
34  public class XmlStreamReaderException extends IOException {
35  
36      private static final long serialVersionUID = 1L;
37  
38      /**
39       * The Byte-Order-Mark (BOM) encoding or null.
40       */
41      private final String bomEncoding;
42  
43      /**
44       * The guessed encoding.
45       */
46      private final String xmlGuessEncoding;
47  
48      /**
49       * The XML encoding.
50       */
51      private final String xmlEncoding;
52  
53      /**
54       * The MIME type in the content type.
55       */
56      private final String contentTypeMime;
57  
58      /**
59       * The encoding in the content type.
60       */
61      private final String contentTypeEncoding;
62  
63      /**
64       * Constructs an exception instance if the Charset encoding could not be
65       * determined.
66       * <p>
67       * Instances of this exception are thrown by the XmlStreamReader.
68       * </p>
69       *
70       * @param msg message describing the reason for the exception.
71       * @param bomEnc BOM encoding.
72       * @param xmlGuessEnc XML guess encoding.
73       * @param xmlEnc XML prolog encoding.
74       */
75      public XmlStreamReaderException(final String msg, final String bomEnc,
76              final String xmlGuessEnc, final String xmlEnc) {
77          this(msg, null, null, bomEnc, xmlGuessEnc, xmlEnc);
78      }
79  
80      /**
81       * Constructs an exception instance if the Charset encoding could not be
82       * determined.
83       * <p>
84       * Instances of this exception are thrown by the XmlStreamReader.
85       * </p>
86       *
87       * @param msg message describing the reason for the exception.
88       * @param ctMime MIME type in the content-type.
89       * @param ctEnc encoding in the content-type.
90       * @param bomEnc BOM encoding.
91       * @param xmlGuessEnc XML guess encoding.
92       * @param xmlEnc XML prolog encoding.
93       */
94      public XmlStreamReaderException(final String msg, final String ctMime, final String ctEnc,
95              final String bomEnc, final String xmlGuessEnc, final String xmlEnc) {
96          super(msg);
97          contentTypeMime = ctMime;
98          contentTypeEncoding = ctEnc;
99          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 }