KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > functions > FuncId


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: FuncId.java,v 1.14 2004/02/17 04:34:01 minchau Exp $
18  */

19 package org.apache.xpath.functions;
20
21 import java.util.StringTokenizer JavaDoc;
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 /**
33  * Execute the Id() function.
34  * @xsl.usage advanced
35  */

36 public class FuncId extends FunctionOneArg
37 {
38
39   /**
40    * Fill in a list with nodes that match a space delimited list if ID
41    * ID references.
42    *
43    * @param xctxt The runtime XPath context.
44    * @param docContext The document where the nodes are being looked for.
45    * @param refval A space delimited list of ID references.
46    * @param usedrefs List of references for which nodes were found.
47    * @param nodeSet Node set where the nodes will be added to.
48    * @param mayBeMore true if there is another set of nodes to be looked for.
49    *
50    * @return The usedrefs value.
51    */

52   private StringVector getNodesByID(XPathContext xctxt, int docContext,
53                                     String JavaDoc refval, StringVector usedrefs,
54                                     NodeSetDTM nodeSet, boolean mayBeMore)
55   {
56
57     if (null != refval)
58     {
59       String JavaDoc ref = null;
60 // DOMHelper dh = xctxt.getDOMHelper();
61
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(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   /**
96    * Execute the function. The function must return
97    * a valid object.
98    * @param xctxt The current execution context.
99    * @return A valid XObject.
100    *
101    * @throws javax.xml.transform.TransformerException
102    */

103   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException JavaDoc
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 JavaDoc refval = ndtm.getStringValue(pos).toString();
128
129         pos = ni.nextNode();
130         usedrefs = getNodesByID(xctxt, docContext, refval, usedrefs, nodeSet,
131                                 DTM.NULL != pos);
132       }
133       // ni.detach();
134
}
135     else if (XObject.CLASS_NULL == argType)
136     {
137       return nodes;
138     }
139     else
140     {
141       String JavaDoc refval = arg.str();
142
143       getNodesByID(xctxt, docContext, refval, null, nodeSet, false);
144     }
145
146     return nodes;
147   }
148 }
149
Popular Tags