1 55 56 package org.apache.bsf.engines.xslt; 57 58 import java.util.*; 59 import java.io.*; 60 import java.net.URL ; 61 62 import javax.xml.transform.Transformer ; 63 import javax.xml.transform.TransformerFactory ; 64 import javax.xml.transform.dom.DOMResult ; 65 import javax.xml.transform.dom.DOMSource ; 66 import javax.xml.transform.stream.StreamSource ; 67 68 import org.w3c.dom.*; 69 70 import org.apache.xpath.objects.XObject; 71 72 import org.apache.bsf.*; 73 import org.apache.bsf.util.BSFEngineImpl; 74 import org.apache.bsf.util.BSFFunctions; 75 import org.apache.bsf.debug.util.DebugLog; 76 77 90 public class XSLTEngine extends BSFEngineImpl { 91 TransformerFactory tFactory; 92 Transformer transformer; 93 94 97 public Object call (Object object, String method, Object [] args) 98 throws BSFException { 99 throw new BSFException (BSFException.REASON_UNSUPPORTED_FEATURE, 100 "BSF:XSLTEngine can't call methods"); 101 } 102 103 106 public void declareBean (BSFDeclaredBean bean) throws BSFException { 107 transformer.setParameter (bean.name, new XObject (bean.bean)); 108 } 109 110 114 public Object eval (String source, int lineNo, int columnNo, 115 Object oscript) throws BSFException { 116 Object sbObj = mgr.lookupBean ("xslt:styleBaseURI"); 122 String styleBaseURI = (sbObj == null) ? "." : sbObj.toString (); 123 124 StreamSource styleSource; 126 127 styleSource = 128 new StreamSource (new StringReader(oscript.toString ())); 129 styleSource.setSystemId(styleBaseURI); 130 131 try { 132 transformer = tFactory.newTransformer(styleSource); 133 } catch (Exception e) { 134 e.printStackTrace (DebugLog.getDebugStream()); 135 throw new BSFException (BSFException.REASON_EXECUTION_ERROR, 136 "Exception from Xerces XSLT: " + e, e); 137 } 138 139 Object srcObj = mgr.lookupBean ("xslt:src"); 145 Object xis = null; 146 if (srcObj != null) { 147 if (srcObj instanceof Node) { 148 xis = new DOMSource ((Node)srcObj); 149 } else { 150 try { 151 String mesg = "as anything"; 152 if (srcObj instanceof Reader) { 153 xis = new StreamSource ((Reader) srcObj); 154 mesg = "as a Reader"; 155 } else if (srcObj instanceof File) { 156 xis = new StreamSource ((File) srcObj); 157 mesg = "as a file"; 158 } else { 159 String srcObjstr=srcObj.toString(); 160 xis = new StreamSource (new StringReader(srcObjstr)); 161 if (srcObj instanceof URL ) { 162 mesg = "as a URL"; 163 } else { 164 ((StreamSource ) xis).setPublicId (srcObjstr); 165 mesg = "as an XML string"; 166 } 167 } 168 169 if (xis == null) { 170 throw new Exception ("Unable to get input from '" + 171 srcObj + "' " + mesg); 172 } 173 } catch (Exception e) { 174 throw new BSFException (BSFException.REASON_EXECUTION_ERROR, 175 "BSF:XSLTEngine: unable to get " + 176 "input from '" + srcObj + "' as XML", e); 177 } 178 } 179 } else { 180 xis = new StreamSource (); 184 } 185 186 for (int i = 0; i < declaredBeans.size (); i++) { 188 BSFDeclaredBean b = (BSFDeclaredBean) declaredBeans.elementAt (i); 189 transformer.setParameter (b.name, new XObject (b.bean)); 190 } 191 192 transformer.setParameter ("bsf", 195 new XObject (new BSFFunctions (mgr, this))); 196 197 try { 199 DOMResult result = new DOMResult (); 200 transformer.transform ((StreamSource ) xis, result); 201 return new XSLTResultNode (result.getNode()); 202 } catch (Exception e) { 203 throw new BSFException (BSFException.REASON_EXECUTION_ERROR, 204 "exception while eval'ing XSLT script" + e, e); 205 } 206 } 207 208 211 public void initialize (BSFManager mgr, String lang, 212 Vector declaredBeans) throws BSFException { 213 super.initialize (mgr, lang, declaredBeans); 214 215 tFactory = TransformerFactory.newInstance(); 216 } 217 218 221 public void undeclareBean (BSFDeclaredBean bean) throws BSFException { 222 if ((transformer.getParameter (bean.name)) != null) { 224 transformer.setParameter (bean.name, null); 225 } 226 } 227 } 228 | Popular Tags |