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