1 16 19 package org.apache.xalan.extensions; 20 21 import java.io.IOException ; 22 import java.util.Vector ; 23 24 import javax.xml.transform.TransformerException ; 25 26 import org.apache.xalan.res.XSLMessages; 27 import org.apache.xalan.res.XSLTErrorResources; 28 import org.apache.xalan.templates.Constants; 29 import org.apache.xalan.templates.ElemExsltFuncResult; 30 import org.apache.xalan.templates.ElemExsltFunction; 31 import org.apache.xalan.templates.ElemTemplate; 32 import org.apache.xalan.templates.ElemTemplateElement; 33 import org.apache.xalan.templates.Stylesheet; 34 import org.apache.xalan.templates.StylesheetRoot; 35 import org.apache.xalan.transformer.TransformerImpl; 36 import org.apache.xml.utils.QName; 37 import org.apache.xpath.ExpressionNode; 38 import org.apache.xpath.XPathContext; 39 import org.apache.xpath.functions.FuncExtFunction; 40 import org.apache.xpath.objects.XObject; 41 import org.apache.xpath.objects.XString; 42 43 47 public class ExtensionHandlerExsltFunction extends ExtensionHandler 48 { 49 private String m_namespace; 50 private StylesheetRoot m_stylesheet; 51 private static final QName RESULTQNAME = 52 new QName(Constants.S_EXSLT_FUNCTIONS_URL, 53 Constants.EXSLT_ELEMNAME_FUNCRESULT_STRING); 54 57 public ExtensionHandlerExsltFunction(String ns, StylesheetRoot stylesheet) 58 { 59 super(ns, "xml"); m_namespace = ns; 61 m_stylesheet = stylesheet; 62 } 63 64 67 public void processElement( 68 String localPart, ElemTemplateElement element, TransformerImpl transformer, 69 Stylesheet stylesheetTree, Object methodKey) throws TransformerException , IOException 70 {} 71 72 80 public ElemExsltFunction getFunction(String funcName) 81 { 82 QName qname = new QName(m_namespace, funcName); 83 ElemTemplate templ = m_stylesheet.getTemplateComposed(qname); 84 if (templ != null && templ instanceof ElemExsltFunction) 85 return (ElemExsltFunction) templ; 86 else 87 return null; 88 } 89 90 91 97 public boolean isFunctionAvailable(String funcName) 98 { 99 return getFunction(funcName)!= null; 100 } 101 102 111 public boolean isElementAvailable(String elemName) 112 { 113 if (!(new QName(m_namespace, elemName).equals(RESULTQNAME))) 114 { 115 return false; 116 } 117 else 118 { 119 ElemTemplateElement elem = m_stylesheet.getFirstChildElem(); 120 while (elem != null && elem != m_stylesheet) 121 { 122 if (elem instanceof ElemExsltFuncResult && ancestorIsFunction(elem)) 123 return true; 124 ElemTemplateElement nextElem = elem.getFirstChildElem(); 125 if (nextElem == null) 126 nextElem = elem.getNextSiblingElem(); 127 if (nextElem == null) 128 nextElem = elem.getParentElem(); 129 elem = nextElem; 130 } 131 } 132 return false; 133 } 134 135 139 private boolean ancestorIsFunction(ElemTemplateElement child) 140 { 141 while (child.getParentElem() != null 142 && !(child.getParentElem() instanceof StylesheetRoot)) 143 { 144 if (child.getParentElem() instanceof ElemExsltFunction) 145 return true; 146 child = child.getParentElem(); 147 } 148 return false; 149 } 150 151 161 public Object callFunction( 162 String funcName, Vector args, Object methodKey, 163 ExpressionContext exprContext) throws TransformerException 164 { 165 throw new TransformerException ("This method should not be called."); 166 } 167 168 177 public Object callFunction(FuncExtFunction extFunction, 178 Vector args, 179 ExpressionContext exprContext) 180 throws TransformerException 181 { 182 ExpressionNode parent = extFunction.exprGetParent(); 184 while (parent != null && !(parent instanceof ElemTemplate)) 185 { 186 parent = parent.exprGetParent(); 187 } 188 189 ElemTemplate callerTemplate = (parent != null) ? (ElemTemplate)parent: null; 190 191 XObject[] methodArgs; 192 methodArgs = new XObject[args.size()]; 193 try 194 { 195 for (int i = 0; i < methodArgs.length; i++) 196 { 197 methodArgs[i] = XObject.create(args.elementAt(i)); 198 } 199 200 ElemExsltFunction elemFunc = getFunction(extFunction.getFunctionName()); 201 202 if (null != elemFunc) { 203 XPathContext context = exprContext.getXPathContext(); 204 TransformerImpl transformer = (TransformerImpl)context.getOwnerObject(); 205 transformer.pushCurrentFuncResult(null); 206 207 elemFunc.execute(transformer, methodArgs); 208 209 XObject val = (XObject)transformer.popCurrentFuncResult(); 210 return (val == null) ? new XString("") : val; 212 } 213 else { 214 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_FUNCTION_NOT_FOUND, new Object [] {extFunction.getFunctionName()})); 215 } 216 } 217 catch (TransformerException e) 218 { 219 throw e; 220 } 221 catch (Exception e) 222 { 223 throw new TransformerException (e); 224 } 225 } 226 227 } 228 | Popular Tags |