All Downloads are FREE. Search and download functionalities are using the official Maven repository.

us.monoid.web.XPathQuery Maven / Gradle / Ivy

The newest version!
package us.monoid.web;

import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathException;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/** PathQuery for XPath.
 * Use this to access XMLResource objects.
 * Many times you might want to use Resty.xpath(...) to create instances of this class.
 * 
 * @author beders
 *
 */
public class XPathQuery extends PathQuery {
	protected XPathExpression xPathExpression;

	public XPathQuery(String anXPath) throws XPathException {
		XPathFactory factory = XPathFactory.newInstance();
		XPath xPath = factory.newXPath();
		xPathExpression = xPath.compile(anXPath);
	}

	private  QName getConstant(Class aReturnType) {
		QName returnType = null;
		if (aReturnType == String.class) {
			returnType = XPathConstants.STRING;
		} else if (aReturnType == Boolean.class) {
			returnType = XPathConstants.BOOLEAN;
		} else if (aReturnType == Double.class) {
			returnType = XPathConstants.NUMBER;
		} else if (aReturnType == NodeList.class) {
			returnType = XPathConstants.NODESET;
		} else if (aReturnType == Node.class) {
			returnType = XPathConstants.NODE;
		} else throw new IllegalArgumentException("" + aReturnType + " is not supported as result of an XPath expression");
		return returnType;
	}

	/** Eval to a NodeList */
	@Override
	NodeList eval(XMLResource resource) throws Exception {
		NodeList retVal = (NodeList) xPathExpression.evaluate(resource.doc(), XPathConstants.NODESET);
		return retVal;
	}
	
	/** Evaluate the XPath on an XMLResource and convert the result into aReturnType.
	 * 
	 * @param  the expected type of the result
	 * @param resource
	 * @param aReturnType 
	 * @return
	 * @throws Exception
	 */
	public  T eval(XMLResource resource, ClassaReturnType) throws Exception {
		T retVal = (T) xPathExpression.evaluate(resource.doc(), getConstant(aReturnType));
		return retVal;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy