001 /* 002 Copyright (c) 2009 Olivier Chafik, All Rights Reserved 003 004 This file is part of JNAerator (http://jnaerator.googlecode.com/). 005 006 JNAerator is free software: you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation, either version 3 of the License, or 009 (at your option) any later version. 010 011 JNAerator is distributed in the hope that it will be useful, 012 but WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 GNU General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with JNAerator. If not, see <http://www.gnu.org/licenses/>. 018 */ 019 package com.ochafik.xml; 020 021 import java.lang.ref.SoftReference; 022 import java.util.HashMap; 023 import java.util.List; 024 import java.util.Map; 025 026 import javax.xml.xpath.XPath; 027 import javax.xml.xpath.XPathConstants; 028 import javax.xml.xpath.XPathExpression; 029 import javax.xml.xpath.XPathExpressionException; 030 import javax.xml.xpath.XPathFactory; 031 032 import org.w3c.dom.Node; 033 import org.w3c.dom.NodeList; 034 035 import com.ochafik.lang.SyntaxUtils; 036 037 public class XPathUtils { 038 public static Map<String, SoftReference<XPathExpression>> xPathExpressionsCache = new HashMap<String, SoftReference<XPathExpression>>(); 039 private static SoftReference<XPath> sharedXPath; 040 public static XPath getSharedXPath() { 041 XPath xPath = null; 042 if (sharedXPath != null) 043 xPath = sharedXPath.get(); 044 if (xPath == null) { 045 XPathFactory xPathFactory = XPathFactory.newInstance(); 046 sharedXPath = new SoftReference<XPath>(xPath = xPathFactory.newXPath()); 047 } 048 return xPath; 049 } 050 public static List<Node> findNodesByXPath(String xPathString, Object source) throws XPathExpressionException { 051 return XMLUtils.list((NodeList)getXPathExpression(xPathString).evaluate(source, XPathConstants.NODESET)); 052 } 053 public static Iterable<Node> findNodesIterableByXPath(String xPathString, Object source) throws XPathExpressionException { 054 return SyntaxUtils.iterable((NodeList)getXPathExpression(xPathString).evaluate(source, XPathConstants.NODESET)); 055 } 056 public static Node findNodeByXPath(String xPathString, Object source) throws XPathExpressionException { 057 NodeList list = (NodeList)getXPathExpression(xPathString).evaluate(source, XPathConstants.NODESET); 058 int len = list.getLength(); 059 return len == 0 ? null : list.item(0); 060 } 061 public static String findStringByXPath(String xPathString, Object source) throws XPathExpressionException { 062 return (String)getXPathExpression(xPathString).evaluate(source, XPathConstants.STRING); 063 } 064 public static XPathExpression getXPathExpression(String xPathString) throws XPathExpressionException { 065 SoftReference<XPathExpression> ref = xPathExpressionsCache.get(xPathString); 066 XPathExpression expression = ref == null ? null : ref.get(); 067 if (expression == null) 068 xPathExpressionsCache.put(xPathString, new SoftReference<XPathExpression>(expression = getSharedXPath().compile(xPathString))); 069 070 return expression; 071 } 072 073 }