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