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  
18  package org.apache.any23.extractor.microdata;
19  
20  import org.apache.any23.extractor.html.DomUtils;
21  import org.w3c.dom.Node;
22  
23  /**
24   * Defines an exception occurring while parsing
25   * <i>Microdata</i>.
26   *
27   * @see MicrodataParser
28   * @author Michele Mostarda (mostarda@fbk.eu)
29   */
30  public class MicrodataParserException extends Exception {
31  
32      private String errorPath;
33      private int[]  errorLocation;
34  
35      public MicrodataParserException(String message, Node errorNode) {
36          super(message);
37          setErrorNode(errorNode);
38      }
39  
40      public MicrodataParserException(String message, Throwable cause, Node errorNode) {
41          super(message, cause);
42          setErrorNode(errorNode);
43      }
44  
45      public String getErrorPath() {
46          return errorPath;
47      }
48  
49      public int getErrorLocationBeginRow() {
50          return errorLocation == null ? -1 : errorLocation[0];
51      }
52  
53      public int getErrorLocationBeginCol() {
54          return errorLocation == null ? -1 : errorLocation[1];
55      }
56  
57      public int getErrorLocationEndRow() {
58          return errorLocation == null ? -1 : errorLocation[2];
59      }
60  
61      public int getErrorLocationEndCol() {
62          return errorLocation == null ? -1 : errorLocation[3];
63      }
64  
65      public String toJSON() {
66          return String.format(
67                  "{ \"message\" : \"%s\", " +
68                    "\"path\" : \"%s\", " +
69                    "\"begin_row\" : %d, \"begin_col\" : %d, " +
70                    "\"end_row\" : %d, \"end_col\" : %d }",
71                  getMessage().replaceAll("\"", ""),
72                  getErrorPath(),
73                  getErrorLocationBeginRow(),
74                  getErrorLocationBeginCol(),
75                  getErrorLocationEndRow(),
76                  getErrorLocationEndCol()
77          );
78      }
79  
80      @Override
81      public String toString() {
82          return toJSON();
83      }
84  
85      protected void setErrorNode(Node n) {
86          if(n == null) {
87              errorPath     = null;
88              errorLocation = null;
89              return;
90          }
91  
92          errorPath     = DomUtils.getXPathForNode(n);
93          errorLocation = DomUtils.getNodeLocation(n);
94      }
95  
96  }