1 16 19 package org.apache.xalan.transformer; 20 21 import java.util.Hashtable ; 22 import java.util.Vector ; 23 24 import javax.xml.transform.TransformerException ; 25 26 import org.apache.xalan.templates.KeyDeclaration; 27 import org.apache.xml.dtm.DTM; 28 import org.apache.xml.dtm.DTMIterator; 29 import org.apache.xml.utils.PrefixResolver; 30 import org.apache.xml.utils.QName; 31 import org.apache.xml.utils.WrappedRuntimeException; 32 import org.apache.xml.utils.XMLString; 33 import org.apache.xpath.XPathContext; 34 import org.apache.xpath.objects.XNodeSet; 35 import org.apache.xpath.objects.XObject; 36 37 43 public class KeyTable 44 { 45 49 private int m_docKey; 50 51 54 private Vector m_keyDeclarations; 55 56 61 private Hashtable m_refsTable = null; 62 63 68 public int getDocKey() 69 { 70 return m_docKey; 71 } 72 73 77 private XNodeSet m_keyNodes; 78 79 KeyIterator getKeyIterator() 80 { 81 return (KeyIterator)(m_keyNodes.getContainedIter()); 82 } 83 84 94 public KeyTable( 95 int doc, PrefixResolver nscontext, QName name, Vector keyDeclarations, XPathContext xctxt) 96 throws javax.xml.transform.TransformerException 97 { 98 m_docKey = doc; 99 m_keyDeclarations = keyDeclarations; 100 KeyIterator ki = new KeyIterator(name, keyDeclarations); 101 102 m_keyNodes = new XNodeSet(ki); 103 m_keyNodes.allowDetachToRelease(false); 104 m_keyNodes.setRoot(doc, xctxt); 105 } 106 107 114 public XNodeSet getNodeSetDTMByKey(QName name, XMLString ref) 115 116 { 117 XNodeSet refNodes = (XNodeSet) getRefsTable().get(ref); 118 try 120 { 121 if (refNodes != null) 122 { 123 refNodes = (XNodeSet) refNodes.cloneWithReset(); 124 } 125 } 126 catch (CloneNotSupportedException e) 127 { 128 refNodes = null; 129 } 130 131 if (refNodes == null) { 132 KeyIterator ki = (KeyIterator) (m_keyNodes).getContainedIter(); 134 XPathContext xctxt = ki.getXPathContext(); 135 refNodes = new XNodeSet(xctxt.getDTMManager()) { 136 public void setRoot(int nodeHandle, Object environment) { 137 } 139 }; 140 refNodes.reset(); 141 } 142 143 return refNodes; 144 } 145 146 151 public QName getKeyTableName() 152 { 153 return getKeyIterator().getName(); 154 } 155 156 159 private KeyDeclaration getKeyDeclaration() { 160 int nDeclarations = m_keyDeclarations.size(); 161 162 for (int i = 0; i < nDeclarations; i++) 164 { 165 KeyDeclaration kd = (KeyDeclaration) m_keyDeclarations.elementAt(i); 166 167 if (kd.getName().equals(getKeyTableName())) 170 { 171 return kd; 172 } 173 } 174 175 return null; 177 } 178 179 183 private Hashtable getRefsTable() 184 { 185 if (m_refsTable == null) 186 { 187 m_refsTable = new Hashtable (89); 189 KeyIterator ki = (KeyIterator) (m_keyNodes).getContainedIter(); 190 XPathContext xctxt = ki.getXPathContext(); 191 192 KeyDeclaration keyDeclaration = getKeyDeclaration(); 193 194 int currentNode; 195 m_keyNodes.reset(); 196 while (DTM.NULL != (currentNode = m_keyNodes.nextNode())) 197 { 198 try 199 { 200 XObject xuse = keyDeclaration.getUse().execute(xctxt, currentNode, ki.getPrefixResolver()); 201 202 if (xuse.getType() != xuse.CLASS_NODESET) 203 { 204 XMLString exprResult = xuse.xstr(); 205 addValueInRefsTable(xctxt, exprResult, currentNode); 206 } 207 else 208 { 209 DTMIterator i = ((XNodeSet)xuse).iterRaw(); 210 int currentNodeInUseClause; 211 212 while (DTM.NULL != (currentNodeInUseClause = i.nextNode())) 213 { 214 DTM dtm = xctxt.getDTM(currentNodeInUseClause); 215 XMLString exprResult = dtm.getStringValue(currentNodeInUseClause); 216 addValueInRefsTable(xctxt, exprResult, currentNode); 217 } 218 } 219 } 220 catch (TransformerException te) 221 { 222 throw new WrappedRuntimeException(te); 223 } 224 } 225 } 226 return m_refsTable; 227 } 228 229 236 private void addValueInRefsTable(XPathContext xctxt, XMLString ref, int node) { 237 238 XNodeSet nodes = (XNodeSet) m_refsTable.get(ref); 239 if (nodes == null) 240 { 241 nodes = new XNodeSet(node, xctxt.getDTMManager()); 242 nodes.nextNode(); 243 m_refsTable.put(ref, nodes); 244 } 245 else 246 { 247 if (nodes.getCurrentNode() != node) { 252 nodes.mutableNodeset().addNode(node); 253 nodes.nextNode(); 254 } 255 } 256 } 257 } 258 | Popular Tags |