KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > expr > IdExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xpath.expr;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.xml.QDocumentType;
33 import com.caucho.xml.XmlChar;
34 import com.caucho.xml.XmlUtil;
35 import com.caucho.xpath.Expr;
36 import com.caucho.xpath.ExprEnvironment;
37 import com.caucho.xpath.XPathException;
38 import com.caucho.xpath.pattern.NodeIterator;
39
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.w3c.dom.NodeList JavaDoc;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 public class IdExpr extends Expr {
49   private Expr _expr;
50
51   private ExprEnvironment _lastEnv;
52   private int _lastUseCount;
53   private Node JavaDoc _lastContext;
54   private ArrayList JavaDoc _lastList;
55
56   public IdExpr(ArrayList JavaDoc<Expr> args)
57   {
58     if (args.size() > 0)
59       _expr = args.get(0);
60   }
61
62   public boolean isNodeSet()
63   {
64     return true;
65   }
66
67   /**
68    * Evaluates the expression as a number
69    *
70    * @param node the current node
71    * @param env the variable environment.
72    *
73    * @return the number representation of id
74    */

75   public double evalNumber(Node JavaDoc node, ExprEnvironment env)
76     throws XPathException
77   {
78     String JavaDoc string = evalString(node, env);
79
80     return stringToNumber(string);
81   }
82
83   /**
84    * Evaluates the expression as a boolean
85    *
86    * @param node the current node
87    * @param env the variable environment.
88    *
89    * @return true if the node exists
90    */

91   public boolean evalBoolean(Node JavaDoc node, ExprEnvironment env)
92     throws XPathException
93   {
94     return id(node, env).size() > 0;
95   }
96
97   /**
98    * The string value of the id expression is just the text value of the
99    * first node.
100    *
101    * @param node the current node
102    * @param env the variable environment.
103    *
104    * @return true if the node exists
105    */

106   public String JavaDoc evalString(Node JavaDoc node, ExprEnvironment env)
107     throws XPathException
108   {
109     NodeIterator iter = evalNodeSet(node, env);
110
111     if (! iter.hasNext())
112       return "";
113
114     Node JavaDoc qNode = (Node JavaDoc) iter.next();
115     return XmlUtil.textValue(qNode);
116   }
117
118   /**
119    * The string value of the id expression is just the list of nodes.
120    *
121    * @param node the current node
122    * @param env the variable environment.
123    *
124    * @return true if the node exists
125    */

126   public Object JavaDoc evalObject(Node JavaDoc node, ExprEnvironment env)
127     throws XPathException
128   {
129     ArrayList JavaDoc<Element JavaDoc> list = id(node, env);
130
131     return list;
132   }
133
134   /**
135    * Returns the list of string ids.
136    */

137   private ArrayList JavaDoc<Element JavaDoc> id(Node JavaDoc context, ExprEnvironment env)
138     throws XPathException
139   {
140     ArrayList JavaDoc idList = getIdList(context, env);
141     ArrayList JavaDoc<Element JavaDoc> list = new ArrayList JavaDoc<Element JavaDoc>();
142
143     if (idList == null || idList.size() == 0)
144       return list;
145     
146     Node JavaDoc ptr;
147
148     if (context instanceof Document JavaDoc)
149       ptr = context;
150     else
151       ptr = context.getOwnerDocument();
152     
153     while ((ptr = XmlUtil.getNext(ptr)) != null) {
154       if (ptr instanceof Element JavaDoc) {
155     Element JavaDoc elt = (Element JavaDoc) ptr;
156
157     QDocumentType dtd;
158     dtd = (QDocumentType) elt.getOwnerDocument().getDoctype();
159     String JavaDoc id = null;
160     if (dtd != null)
161       id = (String JavaDoc) dtd.getElementId(elt.getNodeName());
162
163     if (id != null) {
164       String JavaDoc idValue = elt.getAttribute(id);
165       if (idList.contains(idValue) && ! list.contains(elt))
166         list.add(elt);
167     }
168       }
169     }
170
171     return list;
172   }
173
174   /**
175    * Evaluates the id expression, returning a list of strings.
176    *
177    * @param env the XPath environment
178    * @param node the context node.
179    *
180    * @return a list of string values.
181    */

182   private ArrayList JavaDoc<String JavaDoc> getIdList(Node JavaDoc node, ExprEnvironment env)
183     throws XPathException
184   {
185     ArrayList JavaDoc<String JavaDoc> idList = new ArrayList JavaDoc<String JavaDoc>();
186
187     Object JavaDoc obj = _expr.evalObject(node, env);
188     if (obj instanceof NodeList JavaDoc) {
189       NodeList JavaDoc list = (NodeList JavaDoc) obj;
190
191       int length = list.getLength();
192       for (int i = 0; i < length; i++) {
193     Node JavaDoc value = list.item(i);
194
195         addText(idList, XmlUtil.textValue(value));
196       }
197     }
198     else if (obj instanceof ArrayList JavaDoc) {
199       ArrayList JavaDoc list = (ArrayList JavaDoc) obj;
200
201       for (int i = 0; i < list.size(); i++) {
202     Node JavaDoc value = (Node JavaDoc) list.get(i);
203
204         addText(idList, XmlUtil.textValue(value));
205       }
206     }
207     else if (obj instanceof Iterator) {
208       Iterator iter = (Iterator) obj;
209
210       while (iter.hasNext()) {
211     Node JavaDoc value = (Node JavaDoc) iter.next();
212
213     addText(idList, XmlUtil.textValue(value));
214       }
215     }
216     else
217       addText(idList, toString(obj));
218
219     return idList;
220   }
221
222   private void addText(ArrayList JavaDoc<String JavaDoc> idList, String JavaDoc text)
223   {
224     int len = text.length();
225     CharBuffer cb = new CharBuffer();
226     int i = 0;
227     int ch = 0;
228     for (; i < len && XmlChar.isWhitespace(text.charAt(i)); i++) {
229     }
230     
231     if (i == len)
232       return;
233     
234     while (i < len) {
235       cb.clear();
236       for (; i < len && ! XmlChar.isWhitespace(text.charAt(i)); i++)
237     cb.append(text.charAt(i));
238
239       idList.add(cb.toString());
240
241       for (; i < len && XmlChar.isWhitespace(text.charAt(i)); i++) {
242       }
243     }
244   }
245
246   public String JavaDoc toString()
247   {
248     if (_expr != null)
249       return "id(" + _expr + ")";
250     else
251       return "id()";
252   }
253 }
254
Popular Tags