1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28
29
30
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 }