KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > templates > FuncKey


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: FuncKey.java,v 1.19 2004/02/16 20:32:33 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import java.util.Hashtable JavaDoc;
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 /**
36  * Execute the Key() function.
37  * @xsl.usage advanced
38  */

39 public class FuncKey extends Function2Args
40 {
41
42   /** Dummy value to be used in usedrefs hashtable */
43   static private Boolean JavaDoc ISTRUE = new Boolean JavaDoc(true);
44
45   /**
46    * Execute the function. The function must return
47    * a valid object.
48    * @param xctxt The current execution context.
49    * @return A valid XObject.
50    *
51    * @throws javax.xml.transform.TransformerException
52    */

53   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException JavaDoc
54   {
55
56     // TransformerImpl transformer = (TransformerImpl)xctxt;
57
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       // path.error(context, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC); //"context does not have an owner document!");
67
}
68
69     String JavaDoc 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     // Don't bother with nodeset logic if the thing is only one node.
76
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 JavaDoc 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 JavaDoc();
103
104         if (usedrefs.get(ref) != null)
105         {
106           continue; // We already have 'em.
107
}
108         else
109         {
110
111           // ISTRUE being used as a dummy value.
112
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 // try
122
// {
123
upi.addIterator(nl);
124 // }
125
// catch(CloneNotSupportedException cnse)
126
// {
127
// // will never happen.
128
// }
129
//mnodeset.addNodesInDocOrder(nl, xctxt); needed??
130
}
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