|
Lines 34-42
Link Here
|
| 34 |
import org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo; |
34 |
import org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo; |
| 35 |
import org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceTable; |
35 |
import org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceTable; |
| 36 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument; |
36 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument; |
|
|
37 |
import org.eclipse.wst.xml.xpath.core.util.NodeListImpl; |
| 37 |
import org.eclipse.wst.xml.xpath.core.util.XSLTXPathHelper; |
38 |
import org.eclipse.wst.xml.xpath.core.util.XSLTXPathHelper; |
| 38 |
import org.eclipse.wst.xml.xpath.ui.internal.Messages; |
39 |
import org.eclipse.wst.xml.xpath.ui.internal.Messages; |
| 39 |
import org.eclipse.wst.xml.xpath.ui.internal.XPathUIPlugin; |
40 |
import org.eclipse.wst.xml.xpath.ui.internal.XPathUIPlugin; |
|
|
41 |
import org.eclipse.wst.xml.xpath2.processor.DefaultDynamicContext; |
| 42 |
import org.eclipse.wst.xml.xpath2.processor.DefaultEvaluator; |
| 43 |
import org.eclipse.wst.xml.xpath2.processor.DynamicContext; |
| 44 |
import org.eclipse.wst.xml.xpath2.processor.Evaluator; |
| 45 |
import org.eclipse.wst.xml.xpath2.processor.JFlexCupParser; |
| 46 |
import org.eclipse.wst.xml.xpath2.processor.ResultSequence; |
| 47 |
import org.eclipse.wst.xml.xpath2.processor.StaticChecker; |
| 48 |
import org.eclipse.wst.xml.xpath2.processor.StaticNameResolver; |
| 49 |
import org.eclipse.wst.xml.xpath2.processor.XPathParser; |
| 50 |
import org.eclipse.wst.xml.xpath2.processor.function.FnFunctionLibrary; |
| 51 |
import org.eclipse.wst.xml.xpath2.processor.function.XSCtrLibrary; |
| 40 |
import org.w3c.dom.Document; |
52 |
import org.w3c.dom.Document; |
| 41 |
import org.w3c.dom.Node; |
53 |
import org.w3c.dom.Node; |
| 42 |
import org.w3c.dom.NodeList; |
54 |
import org.w3c.dom.NodeList; |
|
Lines 44-49
Link Here
|
| 44 |
public class XPathComputer { |
56 |
public class XPathComputer { |
| 45 |
private static final int UPDATE_DELAY = 500; |
57 |
private static final int UPDATE_DELAY = 500; |
| 46 |
private static final byte[] XPATH_LOCK = new byte[0]; |
58 |
private static final byte[] XPATH_LOCK = new byte[0]; |
|
|
59 |
private boolean xpath20 = true; |
| 47 |
private XPathView xpathView; |
60 |
private XPathView xpathView; |
| 48 |
private IModelStateListener modelStateListener = new IModelStateListener() { |
61 |
private IModelStateListener modelStateListener = new IModelStateListener() { |
| 49 |
|
62 |
|
|
Lines 165-174
Link Here
|
| 165 |
try { |
178 |
try { |
| 166 |
if ((xp != null) && (node != null)) { |
179 |
if ((xp != null) && (node != null)) { |
| 167 |
synchronized (XPATH_LOCK) { |
180 |
synchronized (XPATH_LOCK) { |
| 168 |
status = evaluateXPath(xp); |
181 |
if (xpath20) { |
|
|
182 |
status = evaluateXPath2(xp); |
| 183 |
} else { |
| 184 |
status = evaluateXPath(xp); |
| 185 |
} |
| 169 |
} |
186 |
} |
| 170 |
} |
187 |
} |
| 171 |
} catch (XPathExpressionException e) { |
188 |
} catch (XPathExpressionException e) { |
| 172 |
return Status.CANCEL_STATUS; |
189 |
return Status.CANCEL_STATUS; |
| 173 |
} |
190 |
} |
| 174 |
return status; |
191 |
return status; |
|
Lines 180-202
Link Here
|
| 180 |
if (node.getNodeType() == Node.DOCUMENT_NODE) { |
197 |
if (node.getNodeType() == Node.DOCUMENT_NODE) { |
| 181 |
doc = (IDOMDocument) node; |
198 |
doc = (IDOMDocument) node; |
| 182 |
} else { |
199 |
} else { |
| 183 |
doc = (IDOMDocument)node.getOwnerDocument(); |
200 |
doc = (IDOMDocument) node.getOwnerDocument(); |
| 184 |
} |
201 |
} |
| 185 |
final List<NamespaceInfo> namespaces = XPathUIPlugin.getDefault().getNamespaceInfo(doc); |
202 |
final List<NamespaceInfo> namespaces = XPathUIPlugin.getDefault() |
|
|
203 |
.getNamespaceInfo(doc); |
| 186 |
if (namespaces != null) { |
204 |
if (namespaces != null) { |
| 187 |
newXPath.setNamespaceContext(new DefaultNamespaceContext(namespaces)); |
205 |
newXPath |
|
|
206 |
.setNamespaceContext(new DefaultNamespaceContext(namespaces)); |
| 188 |
} |
207 |
} |
| 189 |
XPathExpression xpExp = newXPath.compile(xp); |
208 |
XPathExpression xpExp = newXPath.compile(xp); |
| 190 |
|
209 |
|
| 191 |
try { |
210 |
try { |
| 192 |
this.nodeList = (NodeList) xpExp.evaluate(node, XPathConstants.NODESET); |
211 |
this.nodeList = (NodeList) xpExp.evaluate(node, |
|
|
212 |
XPathConstants.NODESET); |
| 193 |
} catch (XPathExpressionException xee) { |
213 |
} catch (XPathExpressionException xee) { |
| 194 |
return Status.CANCEL_STATUS; |
214 |
return Status.CANCEL_STATUS; |
| 195 |
} |
215 |
} |
| 196 |
return Status.OK_STATUS; |
216 |
return Status.OK_STATUS; |
| 197 |
} |
217 |
} |
| 198 |
|
218 |
|
| 199 |
|
219 |
protected IStatus evaluateXPath2(String xp) throws XPathExpressionException { |
|
|
220 |
|
| 221 |
IDOMDocument doc; |
| 222 |
if (node.getNodeType() == Node.DOCUMENT_NODE) { |
| 223 |
doc = (IDOMDocument) node; |
| 224 |
} else { |
| 225 |
doc = (IDOMDocument) node.getOwnerDocument(); |
| 226 |
} |
| 227 |
|
| 228 |
// Initializing the DynamicContext. |
| 229 |
DynamicContext dc = new DefaultDynamicContext(null, doc); |
| 230 |
final List<NamespaceInfo> namespaces = XPathUIPlugin.getDefault() |
| 231 |
.getNamespaceInfo(doc); |
| 232 |
dc.add_namespace("xs", "http://www.w3.org/2001/XMLSchema"); |
| 233 |
|
| 234 |
if (namespaces != null) { |
| 235 |
// Add the defined namespaces |
| 236 |
for (NamespaceInfo namespaceinfo : namespaces) { |
| 237 |
dc.add_namespace(namespaceinfo.prefix, namespaceinfo.uri); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
dc.add_function_library(new FnFunctionLibrary()); |
| 242 |
dc.add_function_library(new XSCtrLibrary()); |
| 243 |
|
| 244 |
XPathParser xpp = new JFlexCupParser(); |
| 245 |
|
| 246 |
try { |
| 247 |
// Parses the XPath expression. |
| 248 |
org.eclipse.wst.xml.xpath2.processor.ast.XPath xpath = xpp.parse(xp); |
| 249 |
|
| 250 |
StaticChecker namecheck = new StaticNameResolver(dc); |
| 251 |
namecheck.check(xpath); |
| 252 |
|
| 253 |
// Static Checking the Xpath expression ’Hello World!’ namecheck.check(xp); |
| 254 |
/** |
| 255 |
* Evaluate the XPath 2.0 expression |
| 256 |
*/ |
| 257 |
|
| 258 |
// Initializing the evaluator with DynamicContext and the name |
| 259 |
// of the XML document XPexample.xml as parameters. |
| 260 |
Evaluator eval = new DefaultEvaluator(dc, doc); |
| 261 |
|
| 262 |
ResultSequence rs = eval.evaluate(xpath); |
| 263 |
|
| 264 |
this.nodeList = new NodeListImpl(rs); |
| 265 |
|
| 266 |
} catch (Exception ex) { |
| 267 |
throw new XPathExpressionException(ex); |
| 268 |
} |
| 269 |
|
| 270 |
return Status.OK_STATUS; |
| 271 |
} |
| 272 |
|
| 200 |
public void dispose() { |
273 |
public void dispose() { |
| 201 |
if (model != null) { |
274 |
if (model != null) { |
| 202 |
model.removeModelStateListener(modelStateListener); |
275 |
model.removeModelStateListener(modelStateListener); |