001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixcore.scriptedflow.vm;
020:
021: import java.util.Map;
022:
023: import javax.xml.namespace.QName;
024: import javax.xml.xpath.XPath;
025: import javax.xml.xpath.XPathConstants;
026: import javax.xml.xpath.XPathExpressionException;
027: import javax.xml.xpath.XPathFactory;
028: import javax.xml.xpath.XPathVariableResolver;
029:
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Node;
032:
033: import de.schlund.pfixxml.SPDocument;
034: import de.schlund.pfixxml.util.Xml;
035:
036: /**
037: * Used by the VM to evaluate XPath expressions for different instructions.
038: *
039: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
040: */
041: public class XPathResolver {
042: private class MyXPathVariableResolver implements
043: XPathVariableResolver {
044: public Object resolveVariable(QName variableName) {
045: String value = null;
046: if (variableName.getLocalPart().equals("__pagename")
047: && spdoc != null) {
048: value = spdoc.getPagename();
049: if (value == null) {
050: return "";
051: } else {
052: return value;
053: }
054: }
055:
056: if (params != null
057: && variableName.getLocalPart().startsWith(
058: "__param_")) {
059: value = params.get(variableName.getLocalPart()
060: .substring(8));
061: if (value == null) {
062: return "";
063: } else {
064: return value;
065: }
066: }
067:
068: if (vars != null) {
069: value = vars.get(variableName.getLocalPart());
070: if (value == null) {
071: return "";
072: } else {
073: return value;
074: }
075: }
076:
077: return "";
078: }
079: }
080:
081: private XPath xpath;
082:
083: private MyXPathVariableResolver resolver;
084:
085: private SPDocument spdoc;
086:
087: private Map<String, String> params;
088:
089: private Map<String, String> vars;
090:
091: public XPathResolver() {
092: resolver = new MyXPathVariableResolver();
093: XPathFactory xpfac = XPathFactory.newInstance();
094: xpfac.setXPathVariableResolver(resolver);
095: xpath = xpfac.newXPath();
096: }
097:
098: public void setSPDocument(SPDocument spdoc) {
099: this .spdoc = spdoc;
100: }
101:
102: public void setParams(Map<String, String> params) {
103: this .params = params;
104: }
105:
106: public void setVariables(Map<String, String> params) {
107: this .vars = params;
108: }
109:
110: public boolean evalXPathBoolean(String expr) {
111: Document doc;
112: if (spdoc == null) {
113: doc = Xml.createDocument();
114: } else {
115: doc = spdoc.getDocument();
116: }
117: try {
118: Boolean ret = (Boolean) xpath.evaluate(expr, doc,
119: XPathConstants.BOOLEAN);
120: return ret.booleanValue();
121: } catch (XPathExpressionException e) {
122: throw new RuntimeException("Invalid XPath expression: \""
123: + expr + "\"", e);
124: }
125: }
126:
127: public String evalXPathString(String expr) {
128: Document doc;
129: if (spdoc == null) {
130: doc = Xml.createDocument();
131: } else {
132: doc = spdoc.getDocument();
133: }
134: try {
135: String ret = (String) xpath.evaluate(expr, doc,
136: XPathConstants.STRING);
137: return ret;
138: } catch (XPathExpressionException e) {
139: throw new RuntimeException("Invalid XPath expression: \""
140: + expr + "\"", e);
141: }
142: }
143:
144: public Node evalXPathNode(String expr) {
145: Document doc;
146: if (spdoc == null) {
147: doc = Xml.createDocument();
148: } else {
149: doc = spdoc.getDocument();
150: }
151: try {
152: Node ret = (Node) xpath.evaluate(expr, doc,
153: XPathConstants.NODE);
154: return ret;
155: } catch (XPathExpressionException e) {
156: throw new RuntimeException("Invalid XPath expression: \""
157: + expr + "\"", e);
158: }
159: }
160:
161: }
|