KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > xpath > XPathConnection


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.driver.xpath;
17
18 import org.w3c.dom.Document JavaDoc;
19 import org.xml.sax.InputSource JavaDoc;
20 import scriptella.spi.AbstractConnection;
21 import scriptella.spi.ConnectionParameters;
22 import scriptella.spi.ParametersCallback;
23 import scriptella.spi.ProviderException;
24 import scriptella.spi.QueryCallback;
25 import scriptella.spi.Resource;
26
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.IdentityHashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Represents a connection to an XML file.
34  * <p>For configuration details and examples see <a HREF="package-summary.html">overview page</a>.
35  *
36  * @author Fyodor Kupolov
37  * @version 1.0
38  */

39 public class XPathConnection extends AbstractConnection {
40     private Map JavaDoc<Resource, XPathQueryExecutor> queriesCache = new IdentityHashMap JavaDoc<Resource, XPathQueryExecutor>();
41     private XPathExpressionCompiler compiler = new XPathExpressionCompiler();
42     private Document JavaDoc document;
43     private URL JavaDoc url;
44     static final DocumentBuilderFactory JavaDoc DBF = DocumentBuilderFactory.newInstance();
45
46     /**
47      * For testing purposes only.
48      */

49     protected XPathConnection() {
50     }
51
52     public XPathConnection(ConnectionParameters parameters) {
53         super(Driver.DIALECT, parameters);
54         url = parameters.getResolvedUrl();
55         //TODO implement trim option
56
}
57
58     public void executeScript(final Resource scriptContent, final ParametersCallback parametersCallback) throws ProviderException {
59         throw new XPathProviderException("Script execution is not supported yet");
60     }
61
62     public void executeQuery(Resource queryContent, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException {
63         XPathQueryExecutor exec = queriesCache.get(queryContent);
64         if (exec == null) {
65             exec = new XPathQueryExecutor(getDocument(), queryContent, compiler, counter);
66             queriesCache.put(queryContent, exec);
67         }
68         exec.execute(queryCallback, parametersCallback);
69     }
70
71     private Document JavaDoc getDocument() {
72         if (document == null) {
73             try {
74                 document = DBF.newDocumentBuilder().parse(new InputSource JavaDoc(url.toString()));
75             } catch (Exception JavaDoc e) {
76                 throw new XPathProviderException("Unable to parse document " + url, e);
77             }
78         }
79         return document;
80     }
81
82     public void close() throws ProviderException {
83         queriesCache = null;
84         document = null;
85     }
86 }
87
Popular Tags