1 16 19 package org.apache.xalan.templates; 20 21 import java.util.Hashtable ; 22 23 import org.apache.xalan.transformer.KeyManager; 24 import org.apache.xalan.transformer.TransformerImpl; 25 import org.apache.xml.dtm.DTM; 26 import org.apache.xml.dtm.DTMIterator; 27 import org.apache.xml.utils.QName; 28 import org.apache.xml.utils.XMLString; 29 import org.apache.xpath.XPathContext; 30 import org.apache.xpath.axes.UnionPathIterator; 31 import org.apache.xpath.functions.Function2Args; 32 import org.apache.xpath.objects.XNodeSet; 33 import org.apache.xpath.objects.XObject; 34 35 39 public class FuncKey extends Function2Args 40 { 41 42 43 static private Boolean ISTRUE = new Boolean (true); 44 45 53 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException 54 { 55 56 TransformerImpl transformer = (TransformerImpl) xctxt.getOwnerObject(); 58 XNodeSet nodes = null; 59 int context = xctxt.getCurrentNode(); 60 DTM dtm = xctxt.getDTM(context); 61 int docContext = dtm.getDocumentRoot(context); 62 63 if (DTM.NULL == docContext) 64 { 65 66 } 68 69 String xkeyname = getArg0().execute(xctxt).str(); 70 QName keyname = new QName(xkeyname, xctxt.getNamespaceContext()); 71 XObject arg = getArg1().execute(xctxt); 72 boolean argIsNodeSetDTM = (XObject.CLASS_NODESET == arg.getType()); 73 KeyManager kmgr = transformer.getKeyManager(); 74 75 if(argIsNodeSetDTM) 77 { 78 XNodeSet ns = (XNodeSet)arg; 79 ns.setShouldCacheNodes(true); 80 int len = ns.getLength(); 81 if(len <= 1) 82 argIsNodeSetDTM = false; 83 } 84 85 if (argIsNodeSetDTM) 86 { 87 Hashtable usedrefs = null; 88 DTMIterator ni = arg.iter(); 89 int pos; 90 UnionPathIterator upi = new UnionPathIterator(); 91 upi.exprSetParent(this); 92 93 while (DTM.NULL != (pos = ni.nextNode())) 94 { 95 dtm = xctxt.getDTM(pos); 96 XMLString ref = dtm.getStringValue(pos); 97 98 if (null == ref) 99 continue; 100 101 if (null == usedrefs) 102 usedrefs = new Hashtable (); 103 104 if (usedrefs.get(ref) != null) 105 { 106 continue; } 108 else 109 { 110 111 usedrefs.put(ref, ISTRUE); 113 } 114 115 XNodeSet nl = 116 kmgr.getNodeSetDTMByKey(xctxt, docContext, keyname, ref, 117 xctxt.getNamespaceContext()); 118 119 nl.setRoot(xctxt.getCurrentNode(), xctxt); 120 121 upi.addIterator(nl); 124 } 131 132 int current = xctxt.getCurrentNode(); 133 upi.setRoot(current, xctxt); 134 135 nodes = new XNodeSet(upi); 136 } 137 else 138 { 139 XMLString ref = arg.xstr(); 140 nodes = kmgr.getNodeSetDTMByKey(xctxt, docContext, keyname, 141 ref, 142 xctxt.getNamespaceContext()); 143 nodes.setRoot(xctxt.getCurrentNode(), xctxt); 144 } 145 146 return nodes; 147 } 148 } 149 | Popular Tags |