/
JXPathContextReferenceImpl.java
826 lines (755 loc) · 27.5 KB
/
JXPathContextReferenceImpl.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.Map.Entry;
import org.apache.commons.jxpath.CompiledExpression;
import org.apache.commons.jxpath.Function;
import org.apache.commons.jxpath.Functions;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.JXPathFunctionNotFoundException;
import org.apache.commons.jxpath.JXPathInvalidSyntaxException;
import org.apache.commons.jxpath.JXPathNotFoundException;
import org.apache.commons.jxpath.JXPathTypeConversionException;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.axes.InitialContext;
import org.apache.commons.jxpath.ri.axes.RootContext;
import org.apache.commons.jxpath.ri.compiler.Expression;
import org.apache.commons.jxpath.ri.compiler.LocationPath;
import org.apache.commons.jxpath.ri.compiler.Path;
import org.apache.commons.jxpath.ri.compiler.TreeCompiler;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.apache.commons.jxpath.ri.model.NodePointerFactory;
import org.apache.commons.jxpath.ri.model.VariablePointerFactory;
import org.apache.commons.jxpath.ri.model.beans.BeanPointerFactory;
import org.apache.commons.jxpath.ri.model.beans.CollectionPointerFactory;
import org.apache.commons.jxpath.ri.model.container.ContainerPointerFactory;
import org.apache.commons.jxpath.ri.model.dynamic.DynamicPointerFactory;
import org.apache.commons.jxpath.util.ReverseComparator;
import org.apache.commons.jxpath.util.TypeUtils;
/**
* The reference implementation of JXPathContext.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class JXPathContextReferenceImpl extends JXPathContext {
/**
* Change this to <code>false</code> to disable soft caching of
* CompiledExpressions.
*/
public static final boolean USE_SOFT_CACHE = true;
private static final Compiler COMPILER = new TreeCompiler();
private static Map compiled = new HashMap();
private static int cleanupCount = 0;
private static NodePointerFactory[] nodeFactoryArray = null;
// The frequency of the cache cleanup
private static final int CLEANUP_THRESHOLD = 500;
private static final Vector nodeFactories = new Vector();
static {
nodeFactories.add(new CollectionPointerFactory());
nodeFactories.add(new BeanPointerFactory());
nodeFactories.add(new DynamicPointerFactory());
nodeFactories.add(new VariablePointerFactory());
// DOM factory is only registered if DOM support is on the classpath
Object domFactory = allocateConditionally(
"org.apache.commons.jxpath.ri.model.dom.DOMPointerFactory",
"org.w3c.dom.Node");
if (domFactory != null) {
nodeFactories.add(domFactory);
}
// JDOM factory is only registered if JDOM is on the classpath
Object jdomFactory = allocateConditionally(
"org.apache.commons.jxpath.ri.model.jdom.JDOMPointerFactory",
"org.jdom.Document");
if (jdomFactory != null) {
nodeFactories.add(jdomFactory);
}
// DynaBean factory is only registered if BeanUtils are on the classpath
Object dynaBeanFactory =
allocateConditionally(
"org.apache.commons.jxpath.ri.model.dynabeans."
+ "DynaBeanPointerFactory",
"org.apache.commons.beanutils.DynaBean");
if (dynaBeanFactory != null) {
nodeFactories.add(dynaBeanFactory);
}
nodeFactories.add(new ContainerPointerFactory());
createNodeFactoryArray();
}
/**
* Create the default node factory array.
*/
private static synchronized void createNodeFactoryArray() {
if (nodeFactoryArray == null) {
nodeFactoryArray =
(NodePointerFactory[]) nodeFactories.
toArray(new NodePointerFactory[nodeFactories.size()]);
Arrays.sort(nodeFactoryArray, new Comparator() {
public int compare(Object a, Object b) {
int orderA = ((NodePointerFactory) a).getOrder();
int orderB = ((NodePointerFactory) b).getOrder();
return orderA - orderB;
}
});
}
}
/**
* Call this with a custom NodePointerFactory to add support for
* additional types of objects. Make sure the factory returns
* a name that puts it in the right position on the list of factories.
* @param factory NodePointerFactory to add
*/
public static void addNodePointerFactory(NodePointerFactory factory) {
synchronized (nodeFactories) {
nodeFactories.add(factory);
nodeFactoryArray = null;
}
}
/**
* Get the registered NodePointerFactories.
* @return NodePointerFactory[]
*/
public static NodePointerFactory[] getNodePointerFactories() {
return nodeFactoryArray;
}
/** Namespace resolver */
protected NamespaceResolver namespaceResolver;
private Pointer rootPointer;
private Pointer contextPointer;
/**
* Create a new JXPathContextReferenceImpl.
* @param parentContext parent context
* @param contextBean Object
*/
protected JXPathContextReferenceImpl(JXPathContext parentContext,
Object contextBean) {
this(parentContext, contextBean, null);
}
/**
* Create a new JXPathContextReferenceImpl.
* @param parentContext parent context
* @param contextBean Object
* @param contextPointer context pointer
*/
public JXPathContextReferenceImpl(JXPathContext parentContext,
Object contextBean, Pointer contextPointer) {
super(parentContext, contextBean);
synchronized (nodeFactories) {
createNodeFactoryArray();
}
if (contextPointer != null) {
this.contextPointer = contextPointer;
this.rootPointer =
NodePointer.newNodePointer(
new QName(null, "root"),
contextPointer.getRootNode(),
getLocale());
}
else {
this.contextPointer =
NodePointer.newNodePointer(
new QName(null, "root"),
contextBean,
getLocale());
this.rootPointer = this.contextPointer;
}
NamespaceResolver parentNR = null;
if (parentContext instanceof JXPathContextReferenceImpl) {
parentNR = ((JXPathContextReferenceImpl) parentContext).getNamespaceResolver();
}
namespaceResolver = new NamespaceResolver(parentNR);
namespaceResolver
.setNamespaceContextPointer((NodePointer) this.contextPointer);
}
/**
* Returns a static instance of TreeCompiler.
*
* Override this to return an alternate compiler.
* @return Compiler
*/
protected Compiler getCompiler() {
return COMPILER;
}
protected CompiledExpression compilePath(String xpath) {
return new JXPathCompiledExpression(xpath, compileExpression(xpath));
}
/**
* Compile the given expression.
* @param xpath to compile
* @return Expression
*/
private Expression compileExpression(String xpath) {
Expression expr;
synchronized (compiled) {
if (USE_SOFT_CACHE) {
expr = null;
SoftReference ref = (SoftReference) compiled.get(xpath);
if (ref != null) {
expr = (Expression) ref.get();
}
}
else {
expr = (Expression) compiled.get(xpath);
}
}
if (expr != null) {
return expr;
}
expr = (Expression) Parser.parseExpression(xpath, getCompiler());
synchronized (compiled) {
if (USE_SOFT_CACHE) {
if (cleanupCount++ >= CLEANUP_THRESHOLD) {
Iterator it = compiled.entrySet().iterator();
while (it.hasNext()) {
Entry me = (Entry) it.next();
if (((SoftReference) me.getValue()).get() == null) {
it.remove();
}
}
cleanupCount = 0;
}
compiled.put(xpath, new SoftReference(expr));
}
else {
compiled.put(xpath, expr);
}
}
return expr;
}
/**
* Traverses the xpath and returns the resulting object. Primitive
* types are wrapped into objects.
* @param xpath expression
* @return Object found
*/
public Object getValue(String xpath) {
Expression expression = compileExpression(xpath);
// TODO: (work in progress) - trying to integrate with Xalan
// Object ctxNode = getNativeContextNode(expression);
// if (ctxNode != null) {
// System.err.println("WILL USE XALAN: " + xpath);
// CachedXPathAPI api = new CachedXPathAPI();
// try {
// if (expression instanceof Path) {
// Node node = api.selectSingleNode((Node)ctxNode, xpath);
// System.err.println("NODE: " + node);
// if (node == null) {
// return null;
// }
// return new DOMNodePointer(node, null).getValue();
// }
// else {
// XObject object = api.eval((Node)ctxNode, xpath);
// switch (object.getType()) {
// case XObject.CLASS_STRING: return object.str();
// case XObject.CLASS_NUMBER: return new Double(object.num());
// case XObject.CLASS_BOOLEAN: return new Boolean(object.bool());
// default:
// System.err.println("OTHER TYPE: " + object.getTypeString());
// }
// }
// }
// catch (TransformerException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// return
// }
return getValue(xpath, expression);
}
// private Object getNativeContextNode(Expression expression) {
// Object node = getNativeContextNode(getContextBean());
// if (node == null) {
// return null;
// }
//
// List vars = expression.getUsedVariables();
// if (vars != null) {
// return null;
// }
//
// return node;
// }
// private Object getNativeContextNode(Object bean) {
// if (bean instanceof Number || bean instanceof String || bean instanceof Boolean) {
// return bean;
// }
// if (bean instanceof Node) {
// return (Node)bean;
// }
//
// if (bean instanceof Container) {
// bean = ((Container)bean).getValue();
// return getNativeContextNode(bean);
// }
//
// return null;
// }
/**
* Get the value indicated.
* @param xpath String
* @param expr Expression
* @return Object
*/
public Object getValue(String xpath, Expression expr) {
Object result = expr.computeValue(getEvalContext());
if (result == null) {
if (expr instanceof Path && !isLenient()) {
throw new JXPathNotFoundException("No value for xpath: "
+ xpath);
}
return null;
}
if (result instanceof EvalContext) {
EvalContext ctx = (EvalContext) result;
result = ctx.getSingleNodePointer();
if (!isLenient() && result == null) {
throw new JXPathNotFoundException("No value for xpath: "
+ xpath);
}
}
if (result instanceof NodePointer) {
result = ((NodePointer) result).getValuePointer();
if (!isLenient() && !((NodePointer) result).isActual()) {
// We need to differentiate between pointers representing
// a non-existing property and ones representing a property
// whose value is null. In the latter case, the pointer
// is going to have isActual == false, but its parent,
// which is a non-node pointer identifying the bean property,
// will return isActual() == true.
NodePointer parent =
((NodePointer) result).getImmediateParentPointer();
if (parent == null
|| !parent.isContainer()
|| !parent.isActual()) {
throw new JXPathNotFoundException("No value for xpath: "
+ xpath);
}
}
result = ((NodePointer) result).getValue();
}
return result;
}
/**
* Calls getValue(xpath), converts the result to the required type
* and returns the result of the conversion.
* @param xpath expression
* @param requiredType Class
* @return Object
*/
public Object getValue(String xpath, Class requiredType) {
Expression expr = compileExpression(xpath);
return getValue(xpath, expr, requiredType);
}
/**
* Get the value indicated.
* @param xpath expression
* @param expr compiled Expression
* @param requiredType Class
* @return Object
*/
public Object getValue(String xpath, Expression expr, Class requiredType) {
Object value = getValue(xpath, expr);
if (value != null && requiredType != null) {
if (!TypeUtils.canConvert(value, requiredType)) {
throw new JXPathTypeConversionException(
"Invalid expression type. '"
+ xpath
+ "' returns "
+ value.getClass().getName()
+ ". It cannot be converted to "
+ requiredType.getName());
}
value = TypeUtils.convert(value, requiredType);
}
return value;
}
/**
* Traverses the xpath and returns a Iterator of all results found
* for the path. If the xpath matches no properties
* in the graph, the Iterator will not be null.
* @param xpath expression
* @return Iterator
*/
public Iterator iterate(String xpath) {
return iterate(xpath, compileExpression(xpath));
}
/**
* Traverses the xpath and returns a Iterator of all results found
* for the path. If the xpath matches no properties
* in the graph, the Iterator will not be null.
* @param xpath expression
* @param expr compiled Expression
* @return Iterator
*/
public Iterator iterate(String xpath, Expression expr) {
return expr.iterate(getEvalContext());
}
public Pointer getPointer(String xpath) {
return getPointer(xpath, compileExpression(xpath));
}
/**
* Get a pointer to the specified path/expression.
* @param xpath String
* @param expr compiled Expression
* @return Pointer
*/
public Pointer getPointer(String xpath, Expression expr) {
Object result = expr.computeValue(getEvalContext());
if (result instanceof EvalContext) {
result = ((EvalContext) result).getSingleNodePointer();
}
if (result instanceof Pointer) {
if (!isLenient() && !((NodePointer) result).isActual()) {
throw new JXPathNotFoundException("No pointer for xpath: "
+ xpath);
}
return (Pointer) result;
}
return NodePointer.newNodePointer(null, result, getLocale());
}
public void setValue(String xpath, Object value) {
setValue(xpath, compileExpression(xpath), value);
}
/**
* Set the value of xpath to value.
* @param xpath path
* @param expr compiled Expression
* @param value Object
*/
public void setValue(String xpath, Expression expr, Object value) {
try {
setValue(xpath, expr, value, false);
}
catch (Throwable ex) {
throw new JXPathException(
"Exception trying to set value with xpath " + xpath, ex);
}
}
public Pointer createPath(String xpath) {
return createPath(xpath, compileExpression(xpath));
}
/**
* Create the given path.
* @param xpath String
* @param expr compiled Expression
* @return resulting Pointer
*/
public Pointer createPath(String xpath, Expression expr) {
try {
Object result = expr.computeValue(getEvalContext());
Pointer pointer = null;
if (result instanceof Pointer) {
pointer = (Pointer) result;
}
else if (result instanceof EvalContext) {
EvalContext ctx = (EvalContext) result;
pointer = ctx.getSingleNodePointer();
}
else {
checkSimplePath(expr);
// This should never happen
throw new JXPathException("Cannot create path:" + xpath);
}
return ((NodePointer) pointer).createPath(this);
}
catch (Throwable ex) {
throw new JXPathException(
"Exception trying to create xpath " + xpath,
ex);
}
}
public Pointer createPathAndSetValue(String xpath, Object value) {
return createPathAndSetValue(xpath, compileExpression(xpath), value);
}
/**
* Create the given path setting its value to value.
* @param xpath String
* @param expr compiled Expression
* @param value Object
* @return resulting Pointer
*/
public Pointer createPathAndSetValue(String xpath, Expression expr,
Object value) {
try {
return setValue(xpath, expr, value, true);
}
catch (Throwable ex) {
throw new JXPathException(
"Exception trying to create xpath " + xpath,
ex);
}
}
/**
* Set the specified value.
* @param xpath path
* @param expr compiled Expression
* @param value destination value
* @param create whether to create missing node(s)
* @return Pointer created
*/
private Pointer setValue(String xpath, Expression expr, Object value,
boolean create) {
Object result = expr.computeValue(getEvalContext());
Pointer pointer = null;
if (result instanceof Pointer) {
pointer = (Pointer) result;
}
else if (result instanceof EvalContext) {
EvalContext ctx = (EvalContext) result;
pointer = ctx.getSingleNodePointer();
}
else {
if (create) {
checkSimplePath(expr);
}
// This should never happen
throw new JXPathException("Cannot set value for xpath: " + xpath);
}
if (create) {
pointer = ((NodePointer) pointer).createPath(this, value);
}
else {
pointer.setValue(value);
}
return pointer;
}
/**
* Checks if the path follows the JXPath restrictions on the type
* of path that can be passed to create... methods.
* @param expr Expression to check
*/
private void checkSimplePath(Expression expr) {
if (!(expr instanceof LocationPath)
|| !((LocationPath) expr).isSimplePath()) {
throw new JXPathInvalidSyntaxException(
"JXPath can only create a path if it uses exclusively "
+ "the child:: and attribute:: axes and has "
+ "no context-dependent predicates");
}
}
/**
* Traverses the xpath and returns an Iterator of Pointers.
* A Pointer provides easy access to a property.
* If the xpath matches no properties
* in the graph, the Iterator be empty, but not null.
* @param xpath expression
* @return Iterator
*/
public Iterator iteratePointers(String xpath) {
return iteratePointers(xpath, compileExpression(xpath));
}
/**
* Traverses the xpath and returns an Iterator of Pointers.
* A Pointer provides easy access to a property.
* If the xpath matches no properties
* in the graph, the Iterator be empty, but not null.
* @param xpath expression
* @param expr compiled Expression
* @return Iterator
*/
public Iterator iteratePointers(String xpath, Expression expr) {
return expr.iteratePointers(getEvalContext());
}
public void removePath(String xpath) {
removePath(xpath, compileExpression(xpath));
}
/**
* Remove the specified path.
* @param xpath expression
* @param expr compiled Expression
*/
public void removePath(String xpath, Expression expr) {
try {
NodePointer pointer = (NodePointer) getPointer(xpath, expr);
if (pointer != null) {
((NodePointer) pointer).remove();
}
}
catch (Throwable ex) {
throw new JXPathException(
"Exception trying to remove xpath " + xpath,
ex);
}
}
public void removeAll(String xpath) {
removeAll(xpath, compileExpression(xpath));
}
/**
* Remove all matching nodes.
* @param xpath expression
* @param expr compiled Expression
*/
public void removeAll(String xpath, Expression expr) {
try {
ArrayList list = new ArrayList();
Iterator it = expr.iteratePointers(getEvalContext());
while (it.hasNext()) {
list.add(it.next());
}
Collections.sort(list, ReverseComparator.INSTANCE);
it = list.iterator();
if (it.hasNext()) {
NodePointer pointer = (NodePointer) it.next();
pointer.remove();
while (it.hasNext()) {
removePath(((NodePointer) it.next()).asPath());
}
}
}
catch (Throwable ex) {
throw new JXPathException(
"Exception trying to remove all for xpath " + xpath,
ex);
}
}
public JXPathContext getRelativeContext(Pointer pointer) {
Object contextBean = pointer.getNode();
if (contextBean == null) {
throw new JXPathException(
"Cannot create a relative context for a non-existent node: "
+ pointer);
}
return new JXPathContextReferenceImpl(this, contextBean, pointer);
}
public Pointer getContextPointer() {
return contextPointer;
}
/**
* Get absolute root pointer.
* @return NodePointer
*/
private NodePointer getAbsoluteRootPointer() {
return (NodePointer) rootPointer;
}
/**
* Get the evaluation context.
* @return EvalContext
*/
private EvalContext getEvalContext() {
return new InitialContext(new RootContext(this,
(NodePointer) getContextPointer()));
}
/**
* Get the absolute root context.
* @return EvalContext
*/
public EvalContext getAbsoluteRootContext() {
return new InitialContext(new RootContext(this,
getAbsoluteRootPointer()));
}
/**
* Get a VariablePointer for the given variable name.
* @param name variable name
* @return NodePointer
*/
public NodePointer getVariablePointer(QName name) {
return NodePointer.newNodePointer(name, VariablePointerFactory
.contextWrapper(this), getLocale());
}
/**
* Get the named Function.
* @param functionName name
* @param parameters function args
* @return Function
*/
public Function getFunction(QName functionName, Object[] parameters) {
String namespace = functionName.getPrefix();
String name = functionName.getName();
JXPathContext funcCtx = this;
Function func = null;
Functions funcs;
while (funcCtx != null) {
funcs = funcCtx.getFunctions();
if (funcs != null) {
func = funcs.getFunction(namespace, name, parameters);
if (func != null) {
return func;
}
}
funcCtx = funcCtx.getParentContext();
}
throw new JXPathFunctionNotFoundException(
"Undefined function: " + functionName.toString());
}
public void registerNamespace(String prefix, String namespaceURI) {
if (namespaceResolver.isSealed()) {
namespaceResolver = (NamespaceResolver) namespaceResolver.clone();
}
namespaceResolver.registerNamespace(prefix, namespaceURI);
}
public String getNamespaceURI(String prefix) {
return namespaceResolver.getNamespaceURI(prefix);
}
/**
* {@inheritDoc}
* @see org.apache.commons.jxpath.JXPathContext#getPrefix(java.lang.String)
*/
public String getPrefix(String namespaceURI) {
return namespaceResolver.getPrefix(namespaceURI);
}
public void setNamespaceContextPointer(Pointer pointer) {
if (namespaceResolver.isSealed()) {
namespaceResolver = (NamespaceResolver) namespaceResolver.clone();
}
namespaceResolver.setNamespaceContextPointer((NodePointer) pointer);
}
public Pointer getNamespaceContextPointer() {
return namespaceResolver.getNamespaceContextPointer();
}
/**
* Get the namespace resolver.
* @return NamespaceResolver
*/
public NamespaceResolver getNamespaceResolver() {
namespaceResolver.seal();
return namespaceResolver;
}
/**
* Checks if existenceCheckClass exists on the class path. If so, allocates
* an instance of the specified class, otherwise returns null.
* @param className to instantiate
* @param existenceCheckClassName guard class
* @return className instance
*/
public static Object allocateConditionally(String className,
String existenceCheckClassName) {
try {
try {
Class.forName(existenceCheckClassName);
}
catch (ClassNotFoundException ex) {
return null;
}
Class cls = Class.forName(className);
return cls.newInstance();
}
catch (Exception ex) {
throw new JXPathException("Cannot allocate " + className, ex);
}
}
}