1 package org.apache.velocity.anakia;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "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 463298 2006-10-12 16:10:32Z henning $
33 */
34 class XPathCache
35 {
36 // Cache of already parsed XPath expressions, keyed by String representations
37 // of the expression as passed to getXPath().
38 private static final Map XPATH_CACHE = new WeakHashMap();
39
40 private XPathCache()
41 {
42 }
43
44 /**
45 * Returns an XPath object representing the requested XPath expression.
46 * A cached object is returned if it already exists for the requested expression.
47 * @param xpathString the XPath expression to parse
48 * @return the XPath object that represents the parsed XPath expression.
49 */
50 static XPath getXPath(String xpathString)
51 {
52 XPath xpath = null;
53 synchronized(XPATH_CACHE)
54 {
55 xpath = (XPath)XPATH_CACHE.get(xpathString);
56 if(xpath == null)
57 {
58 xpath = new XPath(xpathString);
59 XPATH_CACHE.put(xpathString, xpath);
60 }
61 }
62 return xpath;
63 }
64 }