1 16 19 package org.apache.xalan.extensions; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.lang.reflect.Method ; 24 import java.net.URL ; 25 import java.net.URLConnection ; 26 import java.util.Hashtable ; 27 import java.util.Vector ; 28 29 import javax.xml.transform.TransformerException ; 30 31 import org.apache.xalan.res.XSLMessages; 32 import org.apache.xalan.res.XSLTErrorResources; 33 import org.apache.xalan.templates.ElemTemplateElement; 34 import org.apache.xalan.templates.Stylesheet; 35 import org.apache.xalan.transformer.TransformerImpl; 36 import org.apache.xml.dtm.DTMIterator; 37 import org.apache.xml.dtm.ref.DTMNodeList; 38 import org.apache.xml.utils.StringVector; 39 import org.apache.xml.utils.SystemIDResolver; 40 import org.apache.xpath.XPathProcessorException; 41 import org.apache.xpath.functions.FuncExtFunction; 42 import org.apache.xpath.objects.XObject; 43 44 51 public class ExtensionHandlerGeneral extends ExtensionHandler 52 { 53 54 55 private String m_scriptSrc; 56 57 58 private String m_scriptSrcURL; 59 60 61 private Hashtable m_functions = new Hashtable (); 62 63 64 private Hashtable m_elements = new Hashtable (); 65 66 69 70 private Object m_engine; 71 72 73 private Method m_engineCall = null; 74 75 77 78 private static final String BSF_MANAGER = "com.ibm.bsf.BSFManager"; 79 80 81 private static final String BSF_ENGINE = "com.ibm.bsf.BSFEngine"; 82 83 84 private static final Integer NEG1INT = new Integer (-1); 85 86 103 public ExtensionHandlerGeneral( 104 String namespaceUri, StringVector elemNames, StringVector funcNames, String scriptLang, String scriptSrcURL, String scriptSrc, String systemId) 105 throws TransformerException 106 { 107 108 super(namespaceUri, scriptLang); 109 110 if (elemNames != null) 111 { 112 Object junk = new Object (); 113 int n = elemNames.size(); 114 115 for (int i = 0; i < n; i++) 116 { 117 String tok = elemNames.elementAt(i); 118 119 m_elements.put(tok, junk); } 121 } 122 123 if (funcNames != null) 124 { 125 Object junk = new Object (); 126 int n = funcNames.size(); 127 128 for (int i = 0; i < n; i++) 129 { 130 String tok = funcNames.elementAt(i); 131 132 m_functions.put(tok, junk); } 134 } 135 136 m_scriptSrcURL = scriptSrcURL; 137 m_scriptSrc = scriptSrc; 138 139 if (m_scriptSrcURL != null) 140 { 141 URL url = null; 142 try{ 143 url = new URL (m_scriptSrcURL); 144 } 145 catch (java.net.MalformedURLException mue) 146 { 147 int indexOfColon = m_scriptSrcURL.indexOf(':'); 148 int indexOfSlash = m_scriptSrcURL.indexOf('/'); 149 150 if ((indexOfColon != -1) && (indexOfSlash != -1) 151 && (indexOfColon < indexOfSlash)) 152 { 153 url = null; 155 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_COULD_NOT_FIND_EXTERN_SCRIPT, new Object []{m_scriptSrcURL}), mue); } 158 else 159 { 160 try{ 161 url = new URL (new URL (SystemIDResolver.getAbsoluteURI(systemId)), m_scriptSrcURL); 162 } 163 catch (java.net.MalformedURLException mue2) 164 { 165 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_COULD_NOT_FIND_EXTERN_SCRIPT, new Object []{m_scriptSrcURL}), mue2); } 168 } 169 } 170 if (url != null) 171 { 172 try 173 { 174 URLConnection uc = url.openConnection(); 175 InputStream is = uc.getInputStream(); 176 byte []bArray = new byte[uc.getContentLength()]; 177 is.read(bArray); 178 m_scriptSrc = new String (bArray); 179 180 } 181 catch (IOException ioe) 182 { 183 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_COULD_NOT_FIND_EXTERN_SCRIPT, new Object []{m_scriptSrcURL}), ioe); } 186 } 187 188 } 189 190 Object manager = null; 191 try 192 { 193 manager = ObjectFactory.newInstance( 194 BSF_MANAGER, ObjectFactory.findClassLoader(), true); 195 } 196 catch (ObjectFactory.ConfigurationError e) 197 { 198 e.printStackTrace(); 199 } 200 201 if (manager == null) 202 { 203 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_INIT_BSFMGR, null)); } 205 206 try 207 { 208 Method loadScriptingEngine = manager.getClass() 209 .getMethod("loadScriptingEngine", new Class []{ String .class }); 210 211 m_engine = loadScriptingEngine.invoke(manager, 212 new Object []{ scriptLang }); 213 214 Method engineExec = m_engine.getClass().getMethod("exec", 215 new Class []{ String .class, Integer.TYPE, Integer.TYPE, Object .class }); 216 217 engineExec.invoke(m_engine, 219 new Object []{ "XalanScript", NEG1INT, NEG1INT, m_scriptSrc }); 220 } 221 catch (Exception e) 222 { 223 e.printStackTrace(); 224 225 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_CMPL_EXTENSN, null), e); } 227 } 228 229 234 public boolean isFunctionAvailable(String function) 235 { 236 return (m_functions.get(function) != null); 237 } 238 239 246 public boolean isElementAvailable(String element) 247 { 248 return (m_elements.get(element) != null); 249 } 250 251 263 public Object callFunction( 264 String funcName, Vector args, Object methodKey, ExpressionContext exprContext) 265 throws TransformerException 266 { 267 268 Object [] argArray; 269 270 try 271 { 272 argArray = new Object [args.size()]; 273 274 for (int i = 0; i < argArray.length; i++) 275 { 276 Object o = args.elementAt(i); 277 278 argArray[i] = (o instanceof XObject) ? ((XObject) o).object() : o; 279 o = argArray[i]; 280 if(null != o && o instanceof DTMIterator) 281 { 282 argArray[i] = new DTMNodeList((DTMIterator)o); 283 } 284 } 285 286 if (m_engineCall == null) { 287 m_engineCall = m_engine.getClass().getMethod("call", 288 new Class []{ Object .class, String .class, Object [].class }); 289 } 290 291 return m_engineCall.invoke(m_engine, 292 new Object []{ null, funcName, argArray }); 293 } 294 catch (Exception e) 295 { 296 e.printStackTrace(); 297 298 String msg = e.getMessage(); 299 300 if (null != msg) 301 { 302 if (msg.startsWith("Stopping after fatal error:")) 303 { 304 msg = msg.substring("Stopping after fatal error:".length()); 305 } 306 307 throw new TransformerException (e); 309 } 310 else 311 { 312 313 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_CREATE_EXTENSN, new Object []{funcName, e })); } 317 } 318 } 319 320 329 public Object callFunction(FuncExtFunction extFunction, 330 Vector args, 331 ExpressionContext exprContext) 332 throws TransformerException 333 { 334 return callFunction(extFunction.getFunctionName(), args, 335 extFunction.getMethodKey(), exprContext); 336 } 337 338 359 public void processElement( 360 String localPart, ElemTemplateElement element, TransformerImpl transformer, 361 Stylesheet stylesheetTree, Object methodKey) 362 throws TransformerException , IOException 363 { 364 365 Object result = null; 366 XSLProcessorContext xpc = new XSLProcessorContext(transformer, stylesheetTree); 367 368 try 369 { 370 Vector argv = new Vector (2); 371 372 argv.addElement(xpc); 373 argv.addElement(element); 374 375 result = callFunction(localPart, argv, methodKey, 376 transformer.getXPathContext().getExpressionContext()); 377 } 378 catch (XPathProcessorException e) 379 { 380 381 throw new TransformerException (e.getMessage(), e); 383 } 384 385 if (result != null) 386 { 387 xpc.outputToResultTree(stylesheetTree, result); 388 } 389 } 390 } 391 | Popular Tags |