|
91 | 91 | * Address addr = (Address) context.getValue("homeAddress"); |
92 | 92 | * </pre> |
93 | 93 | * |
94 | | - * <h3>Example 3: Collection Subscripts</h3> JXPath can extract elements from arrays and collections. |
| 94 | + * <h3>Example 3: Collection Subscripts</h3> |
| 95 | + * <p> |
| 96 | + * JXPath can extract elements from arrays and collections. |
| 97 | + * </p> |
95 | 98 | * |
96 | 99 | * <pre> |
97 | 100 | * public class Integers { |
|
109 | 112 | * |
110 | 113 | * A collection can be an arbitrary array or an instance of java.util. Collection. |
111 | 114 | * <p> |
112 | | - * Note: in XPath the first element of a collection has index 1, not 0.<br> |
| 115 | + * Note: in XPath the first element of a collection has index 1, not 0. |
| 116 | + * </p> |
113 | 117 | * |
114 | 118 | * <h3>Example 4: Map Element Access</h3> |
115 | 119 | * |
|
136 | 140 | * String homeZipCode = (String)context.getValue("addresses/home/zipCode"); |
137 | 141 | * </pre> |
138 | 142 | * |
| 143 | + * <p> |
139 | 144 | * Often you will need to use the alternative syntax for accessing Map elements: |
| 145 | + * </p> |
140 | 146 | * |
141 | 147 | * <pre> |
142 | 148 | * |
|
192 | 198 | * |
193 | 199 | * </pre> |
194 | 200 | * |
195 | | - * <h3>Example 7: Creating objects</h3> JXPath can be used to create new objects. First, create a subclass of {@link AbstractFactory AbstractFactory} and |
| 201 | + * <h3>Example 7: Creating objects</h3> |
| 202 | + * <p> |
| 203 | + * JXPath can be used to create new objects. First, create a subclass of {@link AbstractFactory AbstractFactory} and |
196 | 204 | * install it on the JXPathContext. Then call {@link JXPathContext#createPath createPathAndSetValue()} instead of "setValue". JXPathContext will invoke your |
197 | 205 | * AbstractFactory when it discovers that an intermediate node of the path is <strong>null</strong>. It will not override existing nodes. |
| 206 | + * </p> |
198 | 207 | * |
199 | 208 | * <pre> |
200 | 209 | * public class AddressFactory extends AbstractFactory { |
|
213 | 222 | * context.createPathAndSetValue("address/zipCode", "90190"); |
214 | 223 | * </pre> |
215 | 224 | * |
216 | | - * <h3>Example 8: Using Variables</h3> JXPath supports the notion of variables. The XPath syntax for accessing variables is <em>"$varName"</em>. |
| 225 | + * <h3>Example 8: Using Variables</h3> |
| 226 | + * <p> |
| 227 | + * JXPath supports the notion of variables. The XPath syntax for accessing variables is <em>"$varName"</em>. |
| 228 | + * </p> |
217 | 229 | * |
218 | 230 | * <pre> |
219 | 231 | * public class Author { |
|
231 | 243 | * Book secondBook = (Book)context.getValue("books[$index]"); |
232 | 244 | * </pre> |
233 | 245 | * |
| 246 | + * <p> |
234 | 247 | * You can also set variables using JXPath: |
| 248 | + * </p> |
235 | 249 | * |
236 | 250 | * <pre> |
237 | 251 | * context.setValue("$index", Integer.valueOf(3)); |
238 | 252 | * </pre> |
239 | 253 | * |
| 254 | + * <p> |
240 | 255 | * Note: you can only <em>change</em> the value of an existing variable this way, you cannot <em>define</em> a new variable. |
| 256 | + * </p> |
241 | 257 | * |
242 | 258 | * <p> |
243 | 259 | * When a variable contains a JavaBean or a collection, you can traverse the bean or collection as well: |
| 260 | + * </p> |
244 | 261 | * |
245 | 262 | * <pre> |
246 | 263 | * ... |
|
254 | 271 | * String title = (String)context.getValue("$books[2]/title); |
255 | 272 | * </pre> |
256 | 273 | * |
257 | | - * <h3>Example 9: Using Nested Contexts</h3> If you need to use the same set of variable while interpreting XPaths with different beans, it makes sense to put |
| 274 | + * <h3>Example 9: Using Nested Contexts</h3> |
| 275 | + * <p> |
| 276 | + * If you need to use the same set of variable while interpreting XPaths with different beans, it makes sense to put |
258 | 277 | * the variables in a separate context and specify that context as a parent context every time you allocate a new JXPathContext for a JavaBean. |
| 278 | + * </p> |
259 | 279 | * |
260 | 280 | * <pre> |
261 | 281 | * JXPathContext varContext = JXPathContext.newContext(null); |
|
264 | 284 | * Iterator javaBooks = context.iterate("books[title = $title]"); |
265 | 285 | * </pre> |
266 | 286 | * |
267 | | - * <h3>Using Custom Variable Pools</h3> By default, JXPathContext creates a HashMap of variables. However, you can substitute a custom implementation of the |
| 287 | + * <h3>Using Custom Variable Pools</h3> |
| 288 | + * <p> |
| 289 | + * By default, JXPathContext creates a HashMap of variables. However, you can substitute a custom implementation of the |
268 | 290 | * Variables interface to make JXPath work with an alternative source of variables. For example, you can define implementations of Variables that cover a |
269 | 291 | * servlet context, HTTP request or any similar structure. |
| 292 | + * </p> |
270 | 293 | * |
271 | 294 | * <h3>Example 10: Using Standard Extension Functions</h3> Using the standard extension functions, you can call methods on objects, static methods on classes |
272 | 295 | * and create objects using any constructor. The class names should be fully qualified. |
273 | 296 | * <p> |
274 | 297 | * Here's how you can create new objects: |
| 298 | + * </p> |
275 | 299 | * |
276 | 300 | * <pre> |
277 | 301 | * |
278 | 302 | * Book book = (Book) context.getValue("org.apache.commons.jxpath.example.Book.new ('John Updike')"); |
279 | 303 | * </pre> |
280 | 304 | * |
| 305 | + * <p> |
281 | 306 | * Here's how you can call static methods: |
| 307 | + * </p> |
282 | 308 | * |
283 | 309 | * <pre> |
284 | 310 | * |
285 | 311 | * Book book = (Book) context.getValue("org. apache.commons.jxpath.example.Book.getBestBook('John Updike')"); |
286 | 312 | * </pre> |
287 | 313 | * |
| 314 | + * <p> |
288 | 315 | * Here's how you can call regular methods: |
| 316 | + * </p> |
289 | 317 | * |
290 | 318 | * <pre> |
291 | 319 | * |
292 | 320 | * String firstName = (String) context.getValue("getAuthorsFirstName($book)"); |
293 | 321 | * </pre> |
294 | 322 | * |
| 323 | + * <p> |
295 | 324 | * As you can see, the target of the method is specified as the first parameter of the function. |
| 325 | + * </p> |
296 | 326 | * |
297 | | - * <h3>Example 11: Using Custom Extension Functions</h3> Collections of custom extension functions can be implemented as {@link Functions Functions} objects or |
| 327 | + * <h3>Example 11: Using Custom Extension Functions</h3> |
| 328 | + * <p> |
| 329 | + * Collections of custom extension functions can be implemented as {@link Functions Functions} objects or |
298 | 330 | * as Java classes, whose methods become extenstion functions. |
| 331 | + * </p> |
299 | 332 | * <p> |
300 | 333 | * Let's say the following class implements various formatting operations: |
| 334 | + * </p> |
301 | 335 | * |
302 | 336 | * <pre> |
303 | 337 | * public class Formats { |
|
308 | 342 | * } |
309 | 343 | * </pre> |
310 | 344 | * |
| 345 | + * <p> |
311 | 346 | * We can register this class with a JXPathContext: |
| 347 | + * </p> |
312 | 348 | * |
313 | 349 | * <pre> |
314 | 350 | * context.setFunctions(new ClassFunctions(Formats.class, "format")); |
|
319 | 355 | * |
320 | 356 | * </pre> |
321 | 357 | * |
| 358 | + * <p> |
322 | 359 | * You can also register whole packages of Java classes using PackageFunctions. |
| 360 | + * </p> |
323 | 361 | * <p> |
324 | 362 | * Also, see {@link FunctionLibrary FunctionLibrary}, which is a class that allows you to register multiple sets of extension functions with the same |
325 | 363 | * JXPathContext. |
| 364 | + * </p> |
326 | 365 | * |
327 | 366 | * <h2>Configuring JXPath</h2> |
328 | 367 | * |
| 368 | + * <p> |
329 | 369 | * JXPath uses JavaBeans introspection to discover properties of JavaBeans. You can provide alternative property lists by supplying custom JXPathBeanInfo |
330 | 370 | * classes (see {@link JXPathBeanInfo JXPathBeanInfo}). |
| 371 | + * </p> |
331 | 372 | * |
332 | 373 | * <h2>Notes</h2> |
333 | 374 | * <ul> |
334 | 375 | * <li>JXPath does not support DOM attributes for non-DOM objects. Even though XPaths like "para[@type='warning']" are legitimate, they will always produce |
335 | | - * empty results. The only attribute supported for JavaBeans is "name". The XPath "foo/bar" is equivalent to "foo[@name='bar']". |
| 376 | + * empty results. The only attribute supported for JavaBeans is "name". The XPath "foo/bar" is equivalent to "foo[@name='bar']".</li> |
336 | 377 | * |
337 | 378 | * <li id='matches_no_property_in_the_graph'>The term <b>matches no property in the graph</b> is used throughout the documentation. It describes a property or |
338 | 379 | * path that can be determined as not belonging to the graph. Determining whether a property or path belongs to the graph depends on the type of object being |
|
351 | 392 | * </li> |
352 | 393 | * </ul> |
353 | 394 | * |
354 | | - * See <a href="http://www.w3schools.com/xpath">XPath Tutorial by W3Schools</a><br> |
355 | | - * . Also see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a><br> |
356 | | - * <br> |
| 395 | + * <p> |
| 396 | + * See also: |
| 397 | + * </p> |
| 398 | + * <ul> |
| 399 | + * <li>See <a href="http://www.w3schools.com/xpath">XPath Tutorial by W3Schools</a></li> |
| 400 | + * <li>See also <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a></li> |
| 401 | + * </ul> |
357 | 402 | * |
358 | | - * You will also find more information and examples in <a href="https://commons.apache.org/jxpath/users-guide.html"> JXPath User's Guide</a> |
| 403 | + * <p> |
| 404 | + * You will also find more information and examples in the <a href="https://commons.apache.org/proper/jxpath/apidocs/index.html">JXPath User's Guide</a> |
| 405 | + * </p> |
359 | 406 | */ |
360 | 407 | public abstract class JXPathContext { |
361 | 408 |
|
|
0 commit comments