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 org.apache.any23.extractor.html.DomUtils;
21 import org.w3c.dom.Node;
22
23
24
25
26
27
28
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 }