1 10 11 package org.mmbase.bridge.util.xml; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.mmbase.bridge.*; 17 import org.mmbase.util.logging.*; 18 import org.mmbase.util.functions.*; 19 import javax.servlet.http.HttpServletRequest ; 20 21 import org.apache.xpath.XPathAPI; 22 23 57 58 public class NodeFunction { 59 private static final Logger log = Logging.getLoggerInstance(NodeFunction.class); 60 61 62 63 70 public static String function(String node, String function) { 71 if (log.isDebugEnabled()) { 72 log.debug("calling with string '" + node + "' function: " + function); 73 } 74 return function("mmbase", node, function); 75 } 76 77 83 public static String function(String cloudName, String number, String 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 e) { 89 return "could not execute '" + function + "' on node '" + number + "' (" + e.toString() + ")"; 90 } 91 } 92 93 94 97 public static org.w3c.dom.Element nodeFunction(org.w3c.dom.NodeList destination, Cloud cloud, String number, String function, String arguments) { 98 101 try { 102 Node node = cloud.getNode(number); 103 Generator gen = new Generator(destination.item(0).getOwnerDocument()); 104 java.util.List 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 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 e) { 115 log.error("" + e + " " + Logging.stackTrace(e)); 116 return null; 117 } 118 } 119 120 127 public static String function(org.w3c.dom.Node node, String function) throws javax.xml.transform.TransformerException { 128 log.debug("calling with dom node"); 129 String number = XPathAPI.eval(node, "./field[@name='number']").toString(); 130 return function(number, function); 131 } 132 133 public static String function(Cloud cloud, String number, String function) { 134 return function(cloud, number, function, ""); 135 } 136 137 141 public static String function(Cloud cloud, String number, String function, Object 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 args = new ArrayList (); 151 String 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 ) { 163 params.setIfDefined(Parameter.REQUEST, request); 164 } 165 return func.getFunctionValue(params).toString(); 166 } catch (Throwable 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 181 public static String function(Cloud cloud, org.w3c.dom.Node node, String function) throws javax.xml.transform.TransformerException { 182 log.debug("calling with dom node"); 183 String number = XPathAPI.eval(node, "./field[@name='number']").toString(); 184 return function(cloud, number, function); 185 } 186 187 190 public static String guiName(Cloud cloud, String node) { 191 return cloud.getNode(node).getNodeManager().getGUIName(); 192 } 193 194 } 195 | Popular Tags |