001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.runtime.evaluator;
046:
047: import org.jaxen.*;
048: import org.jaxen.dom.DOMXPath;
049: import org.obe.client.api.model.MIMETypes;
050: import org.obe.client.api.repository.FunctionSetMetaData;
051: import org.obe.client.api.repository.RepositoryException;
052: import org.obe.spi.WorkflowContext;
053: import org.obe.spi.evaluator.EvaluatorException;
054: import org.obe.spi.event.ApplicationEvent;
055: import org.obe.spi.model.AttributeInstance;
056: import org.obe.spi.model.ProcessInstance;
057: import org.obe.spi.service.FunctionFactory;
058: import org.obe.spi.service.ResourceRepository;
059: import org.obe.spi.service.ServiceManager;
060: import org.w3c.dom.Node;
061: import org.w3c.dom.Text;
062:
063: import java.lang.reflect.InvocationTargetException;
064: import java.lang.reflect.Method;
065: import java.lang.reflect.Modifier;
066: import java.util.Collections;
067: import java.util.List;
068: import java.util.Map;
069:
070: /**
071: * Invokes Jaxen to evaluate an XPath expression against the workflow data.
072: *
073: * @author Adrian Price
074: */
075: public final class JaxenEvaluator extends AbstractEvaluator {
076: // private static final Map _xpathCache = new HashMap();
077: private static FunctionContext _funcCtx;
078: private static NamespaceContext _nsCtx;
079:
080: private static final class JaxenNamespaceContext extends
081: SimpleNamespaceContext {
082:
083: private NamespaceContext _nsCtx;
084: private Map _pkgNamespaces;
085:
086: private JaxenNamespaceContext(NamespaceContext nsCtx,
087: Map pkgNamespaces) {
088:
089: _nsCtx = nsCtx;
090: _pkgNamespaces = pkgNamespaces == null ? Collections.EMPTY_MAP
091: : pkgNamespaces;
092: }
093:
094: public String translateNamespacePrefixToUri(String prefix) {
095: String uri = (String) _pkgNamespaces.get(prefix);
096: if (uri == null)
097: uri = _nsCtx.translateNamespacePrefixToUri(prefix);
098: if (uri == null)
099: uri = super .translateNamespacePrefixToUri(prefix);
100: return uri;
101: }
102: }
103:
104: private static final class JaxenFunctionContext extends
105: SimpleFunctionContext {
106:
107: private static final FunctionContext _xpathFunctionCtx = XPathFunctionContext
108: .getInstance();
109:
110: private static class JavaFunction implements Function {
111: private static final ThreadLocal _context = new ThreadLocal();
112: private Method _method;
113:
114: public static Object getContext() {
115: return _context.get();
116: }
117:
118: private JavaFunction(Method method) {
119: _method = method;
120: }
121:
122: public Object call(Context context, List args)
123: throws FunctionCallException {
124:
125: try {
126: _context.set(context);
127:
128: return _method.invoke(null, args == null ? null
129: : args.toArray());
130: } catch (IllegalAccessException e) {
131: throw new FunctionCallException(e);
132: } catch (InvocationTargetException e) {
133: throw new FunctionCallException(e.getCause());
134: } finally {
135: _context.set(null);
136: }
137: }
138: }
139:
140: JaxenFunctionContext() {
141: }
142:
143: public Function getFunction(String namespaceURI, String prefix,
144: String localName) throws UnresolvableException {
145:
146: Function function;
147: try {
148: function = _xpathFunctionCtx.getFunction(namespaceURI,
149: prefix, localName);
150: } catch (UnresolvableException e) {
151: function = super .getFunction(namespaceURI, prefix,
152: localName);
153: }
154: return function;
155: }
156:
157: public void registerClass(String namespaceURI, Class fnClass) {
158: Method[] methods = fnClass.getMethods();
159: for (int i = 0; i < methods.length; i++) {
160: Method method = methods[i];
161: int modifiers = method.getModifiers();
162: if (Modifier.isStatic(modifiers)
163: && Modifier.isPublic(modifiers)) {
164:
165: registerFunction(namespaceURI, method.getName(),
166: new JavaFunction(method));
167: }
168: }
169: }
170: }
171:
172: private static final class JaxenVariableContext implements
173: VariableContext {
174: private final WorkflowContext _ctx;
175: private final Map _vars;
176:
177: private JaxenVariableContext(WorkflowContext ctx)
178: throws RepositoryException {
179:
180: // Provide access to the workflow instance data.
181: _ctx = ctx;
182: ProcessInstance processInstance = ctx.getProcessInstance();
183: _vars = processInstance == null ? Collections.EMPTY_MAP
184: : processInstance.getAttributeInstances();
185: }
186:
187: public Object getVariableValue(String namespaceURI,
188: String prefix, String localName)
189: throws UnresolvableException {
190:
191: // TODO: figure out whether we need to bother with NS prefixes.
192: if (namespaceURI != null || prefix.length() != 0) {
193: throw new UnresolvableException(
194: prefix == null ? localName : prefix + ':'
195: + localName);
196: }
197:
198: if (localName.equals(EvaluationContext.CONTEXT))
199: return _ctx;
200:
201: AttributeInstance attr = (AttributeInstance) _vars
202: .get(localName);
203: if (attr == null) {
204: throw new UnresolvableException(
205: prefix == null ? localName : prefix + ':'
206: + localName);
207: }
208: return attr.getValue();
209: }
210: }
211:
212: private static synchronized void initialize(ServiceManager svcMgr)
213: throws RepositoryException {
214:
215: if (_funcCtx == null) {
216: JaxenFunctionContext funcCtx = new JaxenFunctionContext();
217: SimpleNamespaceContext nsCtx = new SimpleNamespaceContext();
218: FunctionFactory fr = svcMgr.getFunctionFactory();
219: ResourceRepository resourceRepository = svcMgr
220: .getResourceRepository();
221: FunctionSetMetaData[] metaData = fr.findFunctionMetaData();
222: for (int i = 0; i < metaData.length; i++) {
223: FunctionSetMetaData fsmd = metaData[i];
224: Class fnClass = fsmd.createInstance(resourceRepository)
225: .getClass();
226: funcCtx.registerClass(fsmd.getNsURI(), fnClass);
227: nsCtx.addNamespace(fsmd.getNsPrefix(), fsmd.getNsURI());
228: }
229: _funcCtx = funcCtx;
230: _nsCtx = nsCtx;
231: }
232: }
233:
234: private static XPath getXPath(String expr, WorkflowContext ctx)
235: throws JaxenException, RepositoryException {
236:
237: // Ensure namespace and function contexts are initialized.
238: if (_funcCtx == null)
239: initialize(ctx.getServiceManager());
240:
241: // Check the cache first.
242: XPath xpath = null;//(XPath)_xpathCache.get(expr);
243: if (xpath == null) {
244: xpath = new DOMXPath(expr);
245: xpath.setFunctionContext(_funcCtx);
246: // _xpathCache.put(expr, xpath);
247: }
248: xpath.setNamespaceContext(new JaxenNamespaceContext(_nsCtx, ctx
249: .getWorkflow() == null ? null : ctx.getWorkflow()
250: .getPackage().getNamespaces()));
251: xpath.setVariableContext(new JaxenVariableContext(ctx));
252: return xpath;
253: }
254:
255: public JaxenEvaluator() {
256: }
257:
258: public Object evaluateExpression(String expr, WorkflowContext ctx)
259: throws EvaluatorException {
260:
261: try {
262: Object xmlNode = null;
263: XPath xpath;
264: ApplicationEvent event = ctx.getEvent();
265: if (event != null
266: && MIMETypes.XML.equals(event.getContentType())) {
267: xmlNode = ctx.getServiceManager().getDataConverter()
268: .toDocument(event.getSource());
269: }
270: xpath = getXPath(expr, ctx);
271:
272: // Evaluate the expression. If the result is a list containing a
273: // single DOM Node, return the node itself instead of the list
274: // unless it is a text node, in which case return the textual value.
275: Object result = xpath.evaluate(xmlNode);
276: if (result instanceof List) {
277: List results = (List) result;
278: if (results.isEmpty()) {
279: result = null;
280: } else if (results.size() == 1) {
281: result = results.get(0);
282: if (result instanceof Text)
283: result = ((Node) result).getNodeValue();
284: }
285: }
286: return result;
287: } catch (RepositoryException e) {
288: throw new EvaluatorException(e);
289: } catch (JaxenException e) {
290: throw new EvaluatorException(e);
291: }
292: }
293: }
|