01: /*
02: * Copyright 2006-2007 The Scriptella Project Team.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
19: import org.xml.sax.InputSource;
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;
28: import java.net.URL;
29: import java.util.IdentityHashMap;
30: import java.util.Map;
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<Resource, XPathQueryExecutor> queriesCache = new IdentityHashMap<Resource, XPathQueryExecutor>();
41: private XPathExpressionCompiler compiler = new XPathExpressionCompiler();
42: private Document document;
43: private URL url;
44: static final DocumentBuilderFactory DBF = DocumentBuilderFactory
45: .newInstance();
46:
47: /**
48: * For testing purposes only.
49: */
50: protected XPathConnection() {
51: }
52:
53: public XPathConnection(ConnectionParameters parameters) {
54: super (Driver.DIALECT, parameters);
55: url = parameters.getResolvedUrl();
56: //TODO implement trim option
57: }
58:
59: public void executeScript(final Resource scriptContent,
60: final ParametersCallback parametersCallback)
61: throws ProviderException {
62: throw new XPathProviderException(
63: "Script execution is not supported yet");
64: }
65:
66: public void executeQuery(Resource queryContent,
67: ParametersCallback parametersCallback,
68: QueryCallback queryCallback) throws ProviderException {
69: XPathQueryExecutor exec = queriesCache.get(queryContent);
70: if (exec == null) {
71: exec = new XPathQueryExecutor(getDocument(), queryContent,
72: compiler, counter);
73: queriesCache.put(queryContent, exec);
74: }
75: exec.execute(queryCallback, parametersCallback);
76: }
77:
78: private Document getDocument() {
79: if (document == null) {
80: try {
81: document = DBF.newDocumentBuilder().parse(
82: new InputSource(url.toString()));
83: } catch (Exception e) {
84: throw new XPathProviderException(
85: "Unable to parse document " + url, e);
86: }
87: }
88: return document;
89: }
90:
91: public void close() throws ProviderException {
92: queriesCache = null;
93: document = null;
94: }
95: }
|