1 16 19 package org.apache.xpath.operations; 20 21 import javax.xml.transform.TransformerException ; 22 23 import org.apache.xalan.res.XSLMessages; 24 import org.apache.xml.utils.QName; 25 import org.apache.xpath.Expression; 26 import org.apache.xpath.ExpressionOwner; 27 import org.apache.xpath.XPath; 28 import org.apache.xpath.XPathContext; 29 import org.apache.xpath.XPathVisitor; 30 import org.apache.xpath.axes.PathComponent; 31 import org.apache.xpath.axes.WalkerFactory; 32 import org.apache.xpath.objects.XNodeSet; 33 import org.apache.xpath.objects.XObject; 34 import org.apache.xpath.res.XPATHErrorResources; 35 36 37 40 public class Variable extends Expression implements PathComponent 41 { 42 44 private boolean m_fixUpWasCalled = false; 45 46 48 protected QName m_qname; 49 50 55 protected int m_index; 56 57 63 public void setIndex(int index) 64 { 65 m_index = index; 66 } 67 68 73 public int getIndex() 74 { 75 return m_index; 76 } 77 78 83 public void setIsGlobal(boolean isGlobal) 84 { 85 m_isGlobal = isGlobal; 86 } 87 88 93 public boolean getGlobal() 94 { 95 return m_isGlobal; 96 } 97 98 99 100 101 102 protected boolean m_isGlobal = false; 103 104 114 public void fixupVariables(java.util.Vector vars, int globalsSize) 115 { 116 m_fixUpWasCalled = true; 117 int sz = vars.size(); 118 119 for (int i = vars.size()-1; i >= 0; i--) 120 { 121 QName qn = (QName)vars.elementAt(i); 122 if(qn.equals(m_qname)) 124 { 125 126 if(i < globalsSize) 127 { 128 m_isGlobal = true; 129 m_index = i; 130 } 131 else 132 { 133 m_index = i-globalsSize; 134 } 135 136 return; 137 } 138 } 139 140 java.lang.String msg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_COULD_NOT_FIND_VAR, 141 new Object []{m_qname.toString()}); 142 143 TransformerException te = new TransformerException (msg, this); 144 145 throw new org.apache.xml.utils.WrappedRuntimeException(te); 146 147 } 148 149 150 155 public void setQName(QName qname) 156 { 157 m_qname = qname; 158 } 159 160 165 public QName getQName() 166 { 167 return m_qname; 168 } 169 170 182 public XObject execute(XPathContext xctxt) 183 throws javax.xml.transform.TransformerException 184 { 185 return execute(xctxt, false); 186 } 187 188 189 201 public XObject execute(XPathContext xctxt, boolean destructiveOK) throws javax.xml.transform.TransformerException 202 { 203 org.apache.xml.utils.PrefixResolver xprefixResolver = xctxt.getNamespaceContext(); 204 205 if(m_fixUpWasCalled) 208 { 209 XObject result; 210 if(m_isGlobal) 211 result = xctxt.getVarStack().getGlobalVariable(xctxt, m_index, destructiveOK); 212 else 213 result = xctxt.getVarStack().getLocalVariable(xctxt, m_index, destructiveOK); 214 215 if (null == result) 216 { 217 warn(xctxt, XPATHErrorResources.WG_ILLEGAL_VARIABLE_REFERENCE, 219 new Object []{ m_qname.getLocalPart() }); 224 result = new XNodeSet(xctxt.getDTMManager()); 225 } 226 227 return result; 228 } 229 else 230 { 231 synchronized(this) 234 { 235 org.apache.xalan.templates.ElemVariable vvar= getElemVariable(); 236 if(null != vvar) 237 { 238 m_index = vvar.getIndex(); 239 m_isGlobal = vvar.getIsTopLevel(); 240 m_fixUpWasCalled = true; 241 return execute(xctxt); 242 } 243 } 244 throw new javax.xml.transform.TransformerException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VAR_NOT_RESOLVABLE, new Object []{m_qname.toString()})); } 246 } 247 248 253 public org.apache.xalan.templates.ElemVariable getElemVariable() 254 { 255 256 262 org.apache.xpath.ExpressionNode owner = getExpressionOwner(); 263 264 if (null != owner && owner instanceof org.apache.xalan.templates.ElemTemplateElement) 265 { 266 267 org.apache.xalan.templates.ElemVariable vvar; 268 269 org.apache.xalan.templates.ElemTemplateElement prev = 270 (org.apache.xalan.templates.ElemTemplateElement) owner; 271 272 if (!(prev instanceof org.apache.xalan.templates.Stylesheet)) 273 { 274 while ( !(prev.getParentNode() instanceof org.apache.xalan.templates.Stylesheet) ) 275 { 276 org.apache.xalan.templates.ElemTemplateElement savedprev = prev; 277 278 while (null != (prev = prev.getPreviousSiblingElem())) 279 { 280 if(prev instanceof org.apache.xalan.templates.ElemVariable) 281 { 282 vvar = (org.apache.xalan.templates.ElemVariable) prev; 283 284 if (vvar.getName().equals(m_qname)) 285 { 286 return vvar; 287 } 288 } 289 } 290 prev = savedprev.getParentElem(); 291 } 292 } 293 294 vvar = prev.getStylesheetRoot().getVariableOrParamComposed(m_qname); 295 if (null != vvar) 296 { 297 return vvar; 298 } 299 300 } 301 return null; 302 303 } 304 305 313 public boolean isStableNumber() 314 { 315 return true; 316 } 317 318 322 public int getAnalysisBits() 323 { 324 org.apache.xalan.templates.ElemVariable vvar = getElemVariable(); 325 if(null != vvar) 326 { 327 XPath xpath = vvar.getSelect(); 328 if(null != xpath) 329 { 330 Expression expr = xpath.getExpression(); 331 if(null != expr && expr instanceof PathComponent) 332 { 333 return ((PathComponent)expr).getAnalysisBits(); 334 } 335 } 336 } 337 return WalkerFactory.BIT_FILTER; 338 } 339 340 341 344 public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) 345 { 346 visitor.visitVariableRef(owner, this); 347 } 348 351 public boolean deepEquals(Expression expr) 352 { 353 if(!isSameClass(expr)) 354 return false; 355 356 if(!m_qname.equals(((Variable)expr).m_qname)) 357 return false; 358 359 if(getElemVariable() != ((Variable)expr).getElemVariable()) 362 return false; 363 364 return true; 365 } 366 367 static final java.lang.String PSUEDOVARNAMESPACE = "http://xml.apache.org/xalan/psuedovar"; 368 369 373 public boolean isPsuedoVarRef() 374 { 375 java.lang.String ns = m_qname.getNamespaceURI(); 376 if((null != ns) && ns.equals(PSUEDOVARNAMESPACE)) 377 { 378 if(m_qname.getLocalName().startsWith("#")) 379 return true; 380 } 381 return false; 382 } 383 384 385 } 386 | Popular Tags |