|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| XPathQueryImpl.java | 60% | 77.8% | 75% | 71.9% |
|
||||||||||||||
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | * | |
| 17 | * $Id: XPathQueryImpl.java 712568 2008-11-09 21:55:21Z vgritsenko $ | |
| 18 | */ | |
| 19 | ||
| 20 | package org.apache.xindice.core.xupdate; | |
| 21 | ||
| 22 | import org.apache.xml.utils.PrefixResolver; | |
| 23 | import org.apache.xml.utils.PrefixResolverDefault; | |
| 24 | import org.apache.xpath.XPath; | |
| 25 | import org.apache.xpath.XPathContext; | |
| 26 | ||
| 27 | import org.w3c.dom.Document; | |
| 28 | import org.w3c.dom.Node; | |
| 29 | import org.w3c.dom.traversal.NodeFilter; | |
| 30 | import org.xmldb.common.xml.queries.XObject; | |
| 31 | import org.xmldb.common.xml.queries.XPathQuery; | |
| 32 | ||
| 33 | /** | |
| 34 | * XPath Query Impl to handle Xalan 2 XPath constructs. | |
| 35 | * | |
| 36 | * @version $Revision: 712568 $, $Date: 2008-11-09 21:55:21 +0000 (Sun, 09 Nov 2008) $ | |
| 37 | */ | |
| 38 | public final class XPathQueryImpl implements XPathQuery { | |
| 39 | private String qstring; | |
| 40 | private Node namespace; | |
| 41 | private XPathContext xpc; | |
| 42 | private XPath xpath; | |
| 43 | ||
| 44 | /** | |
| 45 | * Sets the QString attribute of the XPathQueryImpl object | |
| 46 | * | |
| 47 | * @param qstring The new QString value | |
| 48 | * @exception Exception Description of Exception | |
| 49 | */ | |
| 50 | 42 | public void setQString(String qstring) throws Exception { |
| 51 | 42 | this.qstring = qstring; |
| 52 | 42 | this.xpath = null; |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * Sets the Namespace attribute of the XPathQueryImpl object | |
| 57 | * | |
| 58 | * @param namespace The new Namespace value | |
| 59 | * @exception Exception Description of Exception | |
| 60 | */ | |
| 61 | 18 | public void setNamespace(Node namespace) throws Exception { |
| 62 | 18 | this.namespace = namespace; |
| 63 | 18 | this.xpath = null; |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Sets the NodeFilter attribute of the XPathQueryImpl object | |
| 68 | * | |
| 69 | * @param filter The new NodeFilter value | |
| 70 | * @exception Exception Description of Exception | |
| 71 | */ | |
| 72 | 0 | public void setNodeFilter(NodeFilter filter) throws Exception { |
| 73 | // this.filter = filter; | |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * Execute the xpath. | |
| 78 | * | |
| 79 | * @param rootNode The node from which the query should start or null. | |
| 80 | * @return The XObject insulating the query result. | |
| 81 | * @exception Exception | |
| 82 | */ | |
| 83 | 42 | public XObject execute(Node rootNode) throws Exception { |
| 84 | 42 | if (rootNode.getNodeType() == Node.DOCUMENT_NODE) { |
| 85 | 0 | rootNode = ((Document) rootNode).getDocumentElement(); |
| 86 | } | |
| 87 | ||
| 88 | // Since we don't have a XML Parser involved here, install some default | |
| 89 | // support for things like namespaces, etc. | |
| 90 | 42 | if (xpc == null) { |
| 91 | 3 | xpc = new XPathContext(); |
| 92 | } | |
| 93 | ||
| 94 | // Create an object to resolve namespace prefixes. | |
| 95 | 42 | PrefixResolver pfx; |
| 96 | 42 | if (namespace != null) { |
| 97 | 42 | if (namespace.getNodeType() == Node.DOCUMENT_NODE) { |
| 98 | 0 | namespace = ((Document) namespace).getDocumentElement(); |
| 99 | } | |
| 100 | ||
| 101 | 42 | pfx = new PrefixResolverDefault(namespace); |
| 102 | } else { | |
| 103 | 0 | pfx = new PrefixResolverDefault(rootNode); |
| 104 | 0 | xpath = null; |
| 105 | } | |
| 106 | ||
| 107 | // Create the XPath object. | |
| 108 | 42 | if (xpath == null) { |
| 109 | 42 | xpath = new XPath(qstring, null, pfx, XPath.SELECT, null); |
| 110 | } | |
| 111 | ||
| 112 | // execute the XPath query on the specified root node | |
| 113 | 42 | return new XObjectImpl(xpath.execute(xpc, rootNode, pfx)); |
| 114 | } | |
| 115 | } |
|
||||||||||