Part of fix for XALANJ-2294.  When building a KeyIndex for keys or for IDs in
a document, the KeyIndex needs to be informed of the root node of the document
so that it can retrieve only nodes that are in the same document as the context
node and of node handles for nodes rather than the node identities, which will
be reused across different documents.

Reviewed by Christine Li (jycli () ca ! ibm ! com)

diff --git a/src/org/apache/xalan/xsltc/compiler/Key.java b/src/org/apache/xalan/xsltc/compiler/Key.java
index 6c46120..c3c94b6 100644
--- a/src/org/apache/xalan/xsltc/compiler/Key.java
+++ b/src/org/apache/xalan/xsltc/compiler/Key.java
@@ -160,10 +160,6 @@
 
 	// Get the 'parameter' from the stack and store it in a local var.
 	parentNode.setStart(il.append(new ISTORE(parentNode.getIndex())));	
-	il.append(methodGen.loadDOM());
-	il.append(new ILOAD(parentNode.getIndex()));	
-	il.append(new INVOKEINTERFACE(getNodeIdent, 2));
-	il.append(new ISTORE(parentNode.getIndex()));
 
 	// Save current node and current iterator on the stack
 	il.append(methodGen.loadCurrentNode());
@@ -272,11 +268,6 @@
 	    il.append(DUP_X1);
 	    il.append(methodGen.loadCurrentNode());
 	    _use.translate(classGen, methodGen);
-	    il.append(SWAP);
-	    il.append(methodGen.loadDOM());
-	    il.append(SWAP);
-	    il.append(new INVOKEINTERFACE(getNodeIdent, 2));
-	    il.append(SWAP);
 	    il.append(new INVOKEVIRTUAL(key));
 	    
 	    il.append(methodGen.loadDOM());
diff --git a/src/org/apache/xalan/xsltc/runtime/AbstractTranslet.java b/src/org/apache/xalan/xsltc/runtime/AbstractTranslet.java
index 820adbb..1c8b049 100644
--- a/src/org/apache/xalan/xsltc/runtime/AbstractTranslet.java
+++ b/src/org/apache/xalan/xsltc/runtime/AbstractTranslet.java
@@ -123,6 +123,7 @@
      */
     public final DOMAdapter makeDOMAdapter(DOM dom)
 	throws TransletException {
+        setRootForKeys(dom.getDocument());
 	return new DOMAdapter(dom, namesArray, urisArray, typesArray, namespaceArray);
     }
 
@@ -309,7 +310,8 @@
      * The index contains the element node index (int) and Id value (String).
      */
     private final void buildIDIndex(DOM document) {
-        
+        setRootForKeys(document.getDocument());
+
         if (document instanceof DOMEnhancedForDTM) {
             DOMEnhancedForDTM enhancedDOM = (DOMEnhancedForDTM)document;
             
@@ -335,7 +337,10 @@
 
                 while (idValues.hasMoreElements()) {
             	    final Object idValue = idValues.nextElement();
-            	    final int element = ((Integer)elementsByID.get(idValue)).intValue();
+            	    final int element =
+                            document.getNodeHandle(
+                                        ((Integer)elementsByID.get(idValue))
+                                                .intValue());
 
             	    buildKeyIndex(ID_INDEX_NAME, element, idValue);
             	    hasIDValues = true;
@@ -408,6 +413,7 @@
     private Hashtable _keyIndexes = null;
     private KeyIndex  _emptyKeyIndex = null;
     private int       _indexSize = 0;
+    private int       _currentRootForKeys = 0;
 
     /**
      * This method is used to pass the largest DOM size to the translet.
@@ -427,7 +433,7 @@
     /**
      * Adds a value to a key/id index
      *   @param name is the name of the index (the key or ##id)
-     *   @param node is the node id of the node to insert
+     *   @param node is the node handle of the node to insert
      *   @param value is the value that will look up the node in the given index
      */
     public void buildKeyIndex(String name, int node, Object value) {
@@ -437,7 +443,7 @@
 	if (index == null) {
 	    _keyIndexes.put(name, index = new KeyIndex(_indexSize));
 	}
-	index.add(value, node);
+	index.add(value, node, _currentRootForKeys);
     }
 
     /**
@@ -480,6 +486,10 @@
 	return(index);
     }
 
+    private void setRootForKeys(int root) {
+        _currentRootForKeys = root;
+    }
+
     /**
      * This method builds key indexes - it is overridden in the compiled
      * translet in cases where the <xsl:key> element is used