01: package org.apache.anakia;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21:
22: import com.werken.xpath.XPath;
23: import java.util.Map;
24: import java.util.WeakHashMap;
25:
26: /**
27: * Provides a cache for XPath expressions. Used by {@link NodeList} and
28: * {@link AnakiaElement} to minimize XPath parsing in their
29: * <code>selectNodes()</code> methods.
30: *
31: * @author <a href="mailto:szegedia@freemail.hu">Attila Szegedi</a>
32: * @version $Id: XPathCache.java 524478 2007-03-31 20:51:49Z wglass $
33: */
34: class XPathCache {
35: // Cache of already parsed XPath expressions, keyed by String representations
36: // of the expression as passed to getXPath().
37: private static final Map XPATH_CACHE = new WeakHashMap();
38:
39: private XPathCache() {
40: }
41:
42: /**
43: * Returns an XPath object representing the requested XPath expression.
44: * A cached object is returned if it already exists for the requested expression.
45: * @param xpathString the XPath expression to parse
46: * @return the XPath object that represents the parsed XPath expression.
47: */
48: static XPath getXPath(String xpathString) {
49: XPath xpath = null;
50: synchronized (XPATH_CACHE) {
51: xpath = (XPath) XPATH_CACHE.get(xpathString);
52: if (xpath == null) {
53: xpath = new XPath(xpathString);
54: XPATH_CACHE.put(xpathString, xpath);
55: }
56: }
57: return xpath;
58: }
59: }
|