1 16 19 package org.apache.xpath.functions; 20 21 import java.util.StringTokenizer ; 22 23 import org.apache.xml.dtm.DTM; 24 import org.apache.xml.dtm.DTMIterator; 25 import org.apache.xml.utils.StringVector; 26 import org.apache.xpath.NodeSetDTM; 27 import org.apache.xpath.XPathContext; 28 import org.apache.xpath.objects.XNodeSet; 29 import org.apache.xpath.objects.XObject; 30 import org.apache.xpath.res.XPATHErrorResources; 31 32 36 public class FuncId extends FunctionOneArg 37 { 38 39 52 private StringVector getNodesByID(XPathContext xctxt, int docContext, 53 String refval, StringVector usedrefs, 54 NodeSetDTM nodeSet, boolean mayBeMore) 55 { 56 57 if (null != refval) 58 { 59 String ref = null; 60 StringTokenizer tokenizer = new StringTokenizer (refval); 62 boolean hasMore = tokenizer.hasMoreTokens(); 63 DTM dtm = xctxt.getDTM(docContext); 64 65 while (hasMore) 66 { 67 ref = tokenizer.nextToken(); 68 hasMore = tokenizer.hasMoreTokens(); 69 70 if ((null != usedrefs) && usedrefs.contains(ref)) 71 { 72 ref = null; 73 74 continue; 75 } 76 77 int node = dtm.getElementById(ref); 78 79 if (DTM.NULL != node) 80 nodeSet.addNodeInDocOrder(node, xctxt); 81 82 if ((null != ref) && (hasMore || mayBeMore)) 83 { 84 if (null == usedrefs) 85 usedrefs = new StringVector(); 86 87 usedrefs.addElement(ref); 88 } 89 } 90 } 91 92 return usedrefs; 93 } 94 95 103 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException 104 { 105 106 int context = xctxt.getCurrentNode(); 107 DTM dtm = xctxt.getDTM(context); 108 int docContext = dtm.getDocument(); 109 110 if (DTM.NULL == docContext) 111 error(xctxt, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC, null); 112 113 XObject arg = m_arg0.execute(xctxt); 114 int argType = arg.getType(); 115 XNodeSet nodes = new XNodeSet(xctxt.getDTMManager()); 116 NodeSetDTM nodeSet = nodes.mutableNodeset(); 117 118 if (XObject.CLASS_NODESET == argType) 119 { 120 DTMIterator ni = arg.iter(); 121 StringVector usedrefs = null; 122 int pos = ni.nextNode(); 123 124 while (DTM.NULL != pos) 125 { 126 DTM ndtm = ni.getDTM(pos); 127 String refval = ndtm.getStringValue(pos).toString(); 128 129 pos = ni.nextNode(); 130 usedrefs = getNodesByID(xctxt, docContext, refval, usedrefs, nodeSet, 131 DTM.NULL != pos); 132 } 133 } 135 else if (XObject.CLASS_NULL == argType) 136 { 137 return nodes; 138 } 139 else 140 { 141 String refval = arg.str(); 142 143 getNodesByID(xctxt, docContext, refval, null, nodeSet, false); 144 } 145 146 return nodes; 147 } 148 } 149 | Popular Tags |