1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.facade;
|
11
|
|
|
12
|
|
import java.util.ArrayList;
|
13
|
|
import java.util.Iterator;
|
14
|
|
import java.util.Map;
|
15
|
|
|
16
|
|
import javax.xml.transform.TransformerException;
|
17
|
|
|
18
|
|
import org.apache.xml.utils.PrefixResolver;
|
19
|
|
import org.apache.xpath.XPathAPI;
|
20
|
|
import org.apache.xpath.objects.XObject;
|
21
|
|
import org.jbind.xml.core.bridge.IDataImpl;
|
22
|
|
import org.jbind.xml.core.bridge.IXPath;
|
23
|
|
import org.jbind.xml.core.bridge.IXPathFactory;
|
24
|
|
import org.jbind.xml.core.bridge.XPath;
|
25
|
|
import org.jbind.xml.dom3.types.IDomAttrOrElm;
|
26
|
|
import org.jbind.xml.dom3.types.IDomNode;
|
27
|
|
import org.jbind.xml.msg.XmlException;
|
28
|
|
import org.jbind.xml.msg.XmlMessages;
|
29
|
|
import org.w3c.dom.Node;
|
30
|
|
import org.w3c.dom.NodeList;
|
31
|
|
|
32
|
|
public class XalanXPathFactory implements IXPathFactory {
|
33
|
|
|
34
|
0
|
public XalanXPathFactory() {}
|
35
|
|
|
36
|
0
|
public IXPath createXPath(String anXPath, Map aPrefixMappings, boolean aRestrictedForm) throws Exception {
|
37
|
0
|
return new XalanXPath(anXPath, aPrefixMappings);
|
38
|
|
}
|
39
|
|
|
40
|
|
private static class XalanXPath extends XPath implements IXPath, PrefixResolver {
|
41
|
|
|
42
|
|
private Map myPrefixMappings = null;
|
43
|
|
|
44
|
0
|
public XalanXPath(String anXPath, Map aPrefixMappings) throws Exception {
|
45
|
0
|
super(anXPath);
|
46
|
0
|
myPrefixMappings = aPrefixMappings;
|
47
|
|
}
|
48
|
|
|
49
|
0
|
public Iterator select(IDataImpl aContext) throws XmlException {
|
50
|
0
|
Iterator res = null;
|
51
|
0
|
IDomNode node = (IDomNode)aContext;
|
52
|
0
|
try {
|
53
|
0
|
XObject xObject = XPathAPI.eval(node, getXPath(), this);
|
54
|
0
|
NodeList nodeList = xObject.nodelist();
|
55
|
0
|
ArrayList l = new ArrayList();
|
56
|
0
|
for (int i = 0; i < nodeList.getLength(); i++) {
|
57
|
0
|
IDomAttrOrElm n = (IDomAttrOrElm)nodeList.item(i);
|
58
|
0
|
l.add(n);
|
59
|
|
}
|
60
|
0
|
res = l.iterator();
|
61
|
|
} catch (TransformerException e) {
|
62
|
0
|
throw new XmlException(XmlMessages.xPathException(getXPath(), e, node));
|
63
|
|
}
|
64
|
0
|
return res;
|
65
|
|
}
|
66
|
|
|
67
|
0
|
public boolean test(IDataImpl aContext) throws XmlException {
|
68
|
0
|
boolean res;
|
69
|
0
|
try {
|
70
|
0
|
XObject xObject = eval(aContext);
|
71
|
0
|
res = xObject.bool();
|
72
|
|
} catch (TransformerException e) {
|
73
|
0
|
throw new XmlException(XmlMessages.xPathException(getXPath(), e, aContext));
|
74
|
|
}
|
75
|
0
|
return res;
|
76
|
|
}
|
77
|
|
|
78
|
0
|
public double number(IDataImpl aContext) throws XmlException {
|
79
|
0
|
double res;
|
80
|
0
|
try {
|
81
|
0
|
XObject xObject = eval(aContext);
|
82
|
0
|
res = xObject.num();
|
83
|
|
} catch (TransformerException e) {
|
84
|
0
|
throw new XmlException(XmlMessages.xPathException(getXPath(), e, aContext));
|
85
|
|
}
|
86
|
0
|
return res;
|
87
|
|
}
|
88
|
|
|
89
|
0
|
public String string(IDataImpl aContext) throws XmlException {
|
90
|
0
|
String res;
|
91
|
0
|
try {
|
92
|
0
|
XObject xObject = eval(aContext);
|
93
|
0
|
res = xObject.str();
|
94
|
|
} catch (TransformerException e) {
|
95
|
0
|
throw new XmlException(XmlMessages.xPathException(getXPath(), e, aContext));
|
96
|
|
}
|
97
|
0
|
return res;
|
98
|
|
}
|
99
|
|
|
100
|
0
|
private XObject eval(IDataImpl aContext) throws TransformerException {
|
101
|
0
|
IDomNode node = (IDomNode)aContext;
|
102
|
0
|
return XPathAPI.eval(node, getXPath(), this);
|
103
|
|
}
|
104
|
|
|
105
|
|
//
|
106
|
|
// Implementation of prefix resolver
|
107
|
|
//
|
108
|
|
|
109
|
0
|
public String getBaseIdentifier() {
|
110
|
0
|
return null;
|
111
|
|
}
|
112
|
0
|
public String getNamespaceForPrefix(String aPrefix) {
|
113
|
0
|
String res = (String)myPrefixMappings.get(aPrefix);
|
114
|
0
|
return res;
|
115
|
|
}
|
116
|
0
|
public String getNamespaceForPrefix(String aPrefix, Node aNode) {
|
117
|
0
|
String res = ((IDomNode)aNode).lookupNamespaceURI(aPrefix);
|
118
|
0
|
return res;
|
119
|
|
}
|
120
|
0
|
public boolean handlesNullPrefixes() {
|
121
|
0
|
return false;
|
122
|
|
}
|
123
|
|
}
|
124
|
|
|
125
|
|
}
|
126
|
|
|