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.tika.sax;
18  
19  import org.xml.sax.SAXException;
20  
21  /**
22   * A {@link SAXException} wrapper that tags the wrapped exception with
23   * a given object reference. Both the tag and the wrapped original exception
24   * can be used to determine further processing when this exception is caught.
25   */
26  public class TaggedSAXException extends SAXException {
27  
28      /**
29       * The object reference used to tag the exception.
30       */
31      private final Object tag;
32  
33      /**
34       * Creates a tagged wrapper for the given exception.
35       *
36       * @param original the exception to be tagged
37       * @param tag tag object
38       */
39      public TaggedSAXException(SAXException original, Object tag) {
40          super(original.getMessage(), original);
41          initCause(original); // SAXException has it's own chaining mechanism!
42          this.tag = tag;
43      }
44  
45      /**
46       * Returns the object reference used as the tag this exception.
47       *
48       * @return tag object
49       */
50      public Object getTag() {
51          return tag;
52      }
53  
54      /**
55       * Returns the wrapped exception. The only difference to the overridden
56       * {@link Throwable#getCause()} method is the narrower return type.
57       *
58       * @return wrapped exception
59       */
60      @Override
61      public SAXException getCause() {
62          return (SAXException) super.getCause();
63      }
64  
65  }