1 16 19 package org.apache.xpath.functions; 20 21 import org.apache.xml.dtm.DTM; 22 import org.apache.xml.dtm.DTMIterator; 23 import org.apache.xpath.XPathContext; 24 import org.apache.xpath.axes.SubContextList; 25 import org.apache.xpath.compiler.Compiler; 26 import org.apache.xpath.objects.XNumber; 27 import org.apache.xpath.objects.XObject; 28 29 33 public class FuncPosition extends Function 34 { 35 private boolean m_isTopLevel; 36 37 41 public void postCompileStep(Compiler compiler) 42 { 43 m_isTopLevel = compiler.getLocationPathDepth() == -1; 44 } 45 46 54 public int getPositionInContextNodeList(XPathContext xctxt) 55 { 56 57 SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList(); 60 61 if (null != iter) 62 { 63 int prox = iter.getProximityPosition(xctxt); 64 65 return prox; 67 } 68 69 DTMIterator cnl = xctxt.getContextNodeList(); 70 71 if (null != cnl) 72 { 73 int n = cnl.getCurrentNode(); 74 if(n == DTM.NULL) 75 { 76 if(cnl.getCurrentPos() == 0) 77 return 0; 78 79 try 85 { 86 cnl = cnl.cloneWithReset(); 87 } 88 catch(CloneNotSupportedException cnse) 89 { 90 throw new org.apache.xml.utils.WrappedRuntimeException(cnse); 91 } 92 int currentNode = xctxt.getContextNode(); 93 while(DTM.NULL != (n = cnl.nextNode())) 95 { 96 if(n == currentNode) 97 break; 98 } 99 } 100 return cnl.getCurrentPos(); 103 } 104 105 return -1; 107 } 108 109 117 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException 118 { 119 double pos = (double) getPositionInContextNodeList(xctxt); 120 121 return new XNumber(pos); 122 } 123 124 127 public void fixupVariables(java.util.Vector vars, int globalsSize) 128 { 129 } 131 } 132 | Popular Tags |