1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 package org.jaxen;
50
51 /*** Indicates an error during parsing of an XPath expression.
52 *
53 * @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
54 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
55 */
56 public class XPathSyntaxException extends JaxenException
57 {
58 /***
59 *
60 */
61 private static final long serialVersionUID = 1980601567207604059L;
62
63 /*** The textual XPath expression */
64 private String xpath;
65
66 /*** The position of the error */
67 private int position;
68
69 /***
70 * Create a new XPathSyntaxException wrapping an existing
71 * <code>org.jaxen.saxpath.XPathSyntaxException</code>.
72 *
73 * @param e the exception that caused this exception
74 */
75 public XPathSyntaxException(org.jaxen.saxpath.XPathSyntaxException e)
76 {
77 super( e );
78
79 this.xpath = e.getXPath();
80 this.position = e.getPosition();
81 }
82
83 /*** Constructor
84 *
85 * @param xpath the erroneous XPath expression
86 * @param position the position of the error
87 * @param message the error message
88 */
89 public XPathSyntaxException(String xpath,
90 int position,
91 String message)
92 {
93 super( message );
94
95 this.xpath = xpath;
96 this.position = position;
97 }
98
99 /*** Retrieve the position of the error.
100 *
101 * @return the position of the error
102 */
103 public int getPosition()
104 {
105 return this.position;
106 }
107
108 /*** Retrieve the expression containing the error.
109 *
110 * @return the erroneous expression
111 */
112 public String getXPath()
113 {
114 return this.xpath;
115 }
116
117 /*** Retrieve a string useful for denoting where
118 * the error occurred.
119 *
120 * <p>
121 * This is a string composed of whitespace and
122 * a marker at the position (see {@link #getPosition})
123 * of the error. This is useful for creating
124 * friendly multi-line error displays.
125 * </p>
126 *
127 * @return the error position marker
128 */
129 public String getPositionMarker()
130 {
131 StringBuffer buf = new StringBuffer();
132
133 int pos = getPosition();
134
135 for ( int i = 0 ; i < pos ; ++i )
136 {
137 buf.append(" ");
138 }
139
140 buf.append("^");
141
142 return buf.toString();
143
144 }
145
146 /*** Retrieve the friendly multi-line error message.
147 *
148 * <p>
149 * This returns a multi-line string that contains
150 * the original erroneous XPath expression with a
151 * marker underneath indicating exactly where the
152 * error occurred.
153 * </p>
154 *
155 * @return the multi-line error message
156 */
157 public String getMultilineMessage()
158 {
159 StringBuffer buf = new StringBuffer(getMessage());
160 buf.append( "\n" );
161 buf.append( getXPath() );
162 buf.append( "\n" );
163
164 buf.append( getPositionMarker() );
165
166 return buf.toString();
167 }
168 }