View Javadoc
1   package org.apache.maven.shared.utils.xml;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  /**
26   * <p>The XmlReaderException is thrown by the XmlReader constructors if the charset encoding can not be determined
27   * according to the XML 1.0 specification and RFC 3023.</p>
28   * <p>The exception returns the unconsumed InputStream to allow the application to do an alternate processing with the
29   * stream. Note that the original InputStream given to the XmlReader cannot be used as that one has been already
30   * read.</p>
31   *
32   * @author Alejandro Abdelnur
33   * @version revision 1.1 taken on 26/06/2007 from Rome (see https://rome.dev.java.net/source/browse/rome/src/java/com/sun/syndication/io/XmlReaderException.java)
34   */
35  class XmlReaderException
36      extends IOException
37  {
38      /**
39       * 
40       */
41      private static final long serialVersionUID = 5044326391409597950L;
42  
43      private final String bomEncoding;
44  
45      private final String xmlGuessEncoding;
46  
47      private final String xmlEncoding;
48  
49      private final String contentTypeMime;
50  
51      private final String contentTypeEncoding;
52  
53      private final InputStream is;
54  
55      /**
56       * <p>Creates an exception instance if the charset encoding could not be determined.</p>
57       * <p>Instances of this exception are thrown by the XmlReader.</p>
58       *
59       * @param msg         message describing the reason for the exception.
60       * @param bomEnc      BOM encoding.
61       * @param xmlGuessEnc XML guess encoding.
62       * @param xmlEnc      XML prolog encoding.
63       * @param is          the unconsumed InputStream.
64       */
65      XmlReaderException( String msg, String bomEnc, String xmlGuessEnc, String xmlEnc, InputStream is )
66      {
67          this( msg, null, null, bomEnc, xmlGuessEnc, xmlEnc, is );
68      }
69  
70      /**
71       * <p>Creates an exception instance if the charset encoding could not be determined.</p>
72       * <p>Instances of this exception are thrown by the XmlReader.</p>
73       *
74       * @param msg         message describing the reason for the exception.
75       * @param ctMime      MIME type in the content-type.
76       * @param ctEnc       encoding in the content-type.
77       * @param bomEnc      BOM encoding.
78       * @param xmlGuessEnc XML guess encoding.
79       * @param xmlEnc      XML prolog encoding.
80       * @param is          the unconsumed InputStream.
81       */
82      XmlReaderException( String msg, String ctMime, String ctEnc, String bomEnc, String xmlGuessEnc,
83                                 String xmlEnc, InputStream is )
84      {
85          super( msg );
86          contentTypeMime = ctMime;
87          contentTypeEncoding = ctEnc;
88          bomEncoding = bomEnc;
89          xmlGuessEncoding = xmlGuessEnc;
90          xmlEncoding = xmlEnc;
91          this.is = is;
92      }
93  
94      /**
95       * Returns the BOM encoding found in the InputStream.
96       *
97       * @return the BOM encoding, null if none.
98       */
99      public String getBomEncoding()
100     {
101         return bomEncoding;
102     }
103 
104     /**
105      * Returns the encoding guess based on the first bytes of the InputStream.
106      *
107      * @return the encoding guess, null if it couldn't be guessed.
108      */
109     public String getXmlGuessEncoding()
110     {
111         return xmlGuessEncoding;
112     }
113 
114     /**
115      * Returns the encoding found in the XML prolog of the InputStream.
116      *
117      * @return the encoding of the XML prolog, null if none.
118      */
119     public String getXmlEncoding()
120     {
121         return xmlEncoding;
122     }
123 
124     /**
125      * Returns the MIME type in the content-type used to attempt determining the encoding.
126      *
127      * @return the MIME type in the content-type, null if there was not content-type or the encoding detection did not
128      *         involve HTTP.
129      */
130     public String getContentTypeMime()
131     {
132         return contentTypeMime;
133     }
134 
135     /**
136      * Returns the encoding in the content-type used to attempt determining the encoding.
137      *
138      * @return the encoding in the content-type, null if there was not content-type, no encoding in it or the encoding
139      *         detection did not involve HTTP.
140      */
141     public String getContentTypeEncoding()
142     {
143         return contentTypeEncoding;
144     }
145 
146     /**
147      * Returns the unconsumed InputStream to allow the application to do an alternate encoding detection on the
148      * InputStream.
149      *
150      * @return the unconsumed InputStream.
151      */
152     public InputStream getInputStream()
153     {
154         return is;
155     }
156 }