KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > util > xml > NodeFunction


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.util.xml;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.mmbase.bridge.*;
17 import org.mmbase.util.logging.*;
18 import org.mmbase.util.functions.*;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20
21 import org.apache.xpath.XPathAPI;
22
23 /**
24  * Nodes of the bridge can have `virtual fields', which are in fact
25  * functions on the node. An example of such a function is "gui()". If
26  * you want to use these functions in an XSL transformation it is
27  * necessary to use this Xalan extension. The XSLT looks like this then:
28  *
29  <pre>
30   &lt;xsl:stylesheet version = "1.0"
31     xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"
32     xmlns:node ="org.mmbase.bridge.util.xml.NodeFunction"
33   &gt;
34   ..
35   </pre>
36   You can then use a function like this if in the current template is a DOM Node with a 'field' subnode.
37   <pre>
38   ..
39   &lt;img SRC="{$formatter_imgdb}{node:function(., 'cache(s(180x180))')}" /&gt;
40   </pre>
41   And otherwise you can also feed it a number:
42   <pre>
43   ..
44   &lt;img SRC="{$formatter_imgdb}{node:function(string(@number), 'cache(s(180x180))')}" /&gt;
45   </pre>
46   Possibly even with the name of MMBase:
47   <pre>
48   ..
49   &lt;img SRC="{$formatter_imgdb}{node:function('mmbase', string(@number), 'cache(s(180x180))')}" /&gt;
50   </pre>
51  *
52  *
53  * @author Michiel Meeuwissen
54  * @version $Id: NodeFunction.java,v 1.16 2005/11/04 13:11:06 nklasens Exp $
55  * @since MMBase-1.6
56  */

57
58 public class NodeFunction {
59     private static final Logger log = Logging.getLoggerInstance(NodeFunction.class);
60
61
62
63     /**
64      * Supposes the default cloud 'mmbase'.
65      * @param node The number (or alias) of the Node
66      * @param function The function (with arguments).
67      * @return The result of the function (as a String)
68      * @see #function(String, String, String)
69      */

70     public static String JavaDoc function(String JavaDoc node, String JavaDoc function) {
71         if (log.isDebugEnabled()) {
72             log.debug("calling with string '" + node + "' function: " + function);
73         }
74         return function("mmbase", node, function);
75     }
76
77     /**
78      * @param cloudName The name of the Cloud.
79      * @param number The number (or alias) of the Node
80      * @param function The function (with arguments).
81      * @return The result of the function (as a String)
82      */

83     public static String JavaDoc function(String JavaDoc cloudName, String JavaDoc number, String JavaDoc function) {
84         log.debug("calling base for cloud " + cloudName);
85         try {
86             Cloud cloud = ContextProvider.getDefaultCloudContext().getCloud(cloudName);
87             return function(cloud, number, function);
88         } catch (Exception JavaDoc e) {
89             return "could not execute '" + function + "' on node '" + number + "' (" + e.toString() + ")";
90         }
91     }
92
93     
94     /**
95      * @since MMBase-1.8
96      */

97     public static org.w3c.dom.Element JavaDoc nodeFunction(org.w3c.dom.NodeList JavaDoc destination, Cloud cloud, String JavaDoc number, String JavaDoc function, String JavaDoc arguments) {
98         // it only want to work withh a NodeList. I think my book sais that it should also work with
99
// Element, but no..
100

101         try {
102             Node node = cloud.getNode(number);
103             Generator gen = new Generator(destination.item(0).getOwnerDocument());
104             java.util.List JavaDoc args = org.mmbase.util.StringSplitter.splitFunctions(arguments);
105             if (log.isDebugEnabled()) {
106                 log.debug("Executing " + function+ " " + args + " on " + node.getNumber());
107             }
108             Node resultNode = node.getFunctionValue(function, args).toNode();
109             org.w3c.dom.Element JavaDoc element = gen.add(resultNode);
110             if (log.isDebugEnabled()) {
111                 log.debug("Returning " + org.mmbase.util.xml.XMLWriter.write(element, false));
112             }
113             return element;
114         } catch (Exception JavaDoc e) {
115             log.error("" + e + " " + Logging.stackTrace(e));
116             return null;
117         }
118     }
119
120     /**
121      * It can be handy to supply a whole node, it will search for the field 'number' itself.
122      * @param node The number (or alias) of the Node
123      * @param function The function (with arguments).
124      * @return The result of the function (as a String)
125      * @throws javax.xml.transform.TransformerException if xpath fails
126      */

127     public static String JavaDoc function(org.w3c.dom.Node JavaDoc node, String JavaDoc function) throws javax.xml.transform.TransformerException JavaDoc {
128         log.debug("calling with dom node");
129         String JavaDoc number = XPathAPI.eval(node, "./field[@name='number']").toString();
130         return function(number, function);
131     }
132
133     public static String JavaDoc function(Cloud cloud, String JavaDoc number, String JavaDoc function) {
134         return function(cloud, number, function, "");
135     }
136
137     /**
138      * @param request Meant to be an HttpServletRequest. If not, will be ignored (empty string e.g.).
139      * @since MMBase-1.8
140      */

141     public static String JavaDoc function(Cloud cloud, String JavaDoc number, String JavaDoc function, Object JavaDoc request) {
142         log.debug("calling base on " + number + " for " + function);
143         Node node;
144         try {
145             node = cloud.getNode(number);
146
147             Function func = null;
148             Parameters params = null;
149             if (function.indexOf("(") > -1) {
150                 List JavaDoc args = new ArrayList JavaDoc();
151                 String JavaDoc functionName = org.mmbase.util.functions.NodeFunction.getFunctionNameAndFillArgs(function, args);
152                 func = node.getFunction(functionName);
153                 params = func.createParameters();
154                 params.setAll(args);
155             }
156             else {
157                 func = node.getFunction(function);
158                 params = func.createParameters();
159             }
160             
161             params.setIfDefined(Parameter.CLOUD, cloud);
162             if (request instanceof HttpServletRequest JavaDoc) {
163                 params.setIfDefined(Parameter.REQUEST, request);
164             }
165             return func.getFunctionValue(params).toString();
166         } catch (Throwable JavaDoc e) {
167             log.info("could not execute '" + function + "' on node '" + number + "'");
168             log.info(Logging.stackTrace(e) + Logging.stackTrace());
169             return "could not execute " + function + " on node " + number + "(" + e.getClass() + " " + e.getMessage() + ")";
170         }
171     }
172
173     /**
174      * It can be handy to supply a whole node, it will search for the field 'number' itself.
175      * @param cloud cloud to execute in
176      * @param node The number (or alias) of the Node
177      * @param function The function (with arguments).
178      * @return The result of the function (as a String)
179      * @throws javax.xml.transform.TransformerException if xpath fails
180      */

181     public static String JavaDoc function(Cloud cloud, org.w3c.dom.Node JavaDoc node, String JavaDoc function) throws javax.xml.transform.TransformerException JavaDoc {
182         log.debug("calling with dom node");
183         String JavaDoc number = XPathAPI.eval(node, "./field[@name='number']").toString();
184         return function(cloud, number, function);
185     }
186
187     /**
188      * @since MMBase-1.8
189      */

190     public static String JavaDoc guiName(Cloud cloud, String JavaDoc node) {
191         return cloud.getNode(node).getNodeManager().getGUIName();
192     }
193
194 }
195
Popular Tags