KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > pattern > FromAttributes


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.pattern;
30
31 import com.caucho.xml.CauchoElement;
32 import com.caucho.xml.CauchoNode;
33 import com.caucho.xml.QAttr;
34 import com.caucho.xml.QElement;
35 import com.caucho.xpath.Env;
36 import com.caucho.xpath.ExprEnvironment;
37 import com.caucho.xpath.XPathException;
38
39 import org.w3c.dom.Attr JavaDoc;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42
43 /**
44  * matches attributes of an element.
45  */

46 public class FromAttributes extends Axis {
47   public FromAttributes(AbstractPattern parent)
48   {
49     super(parent);
50   }
51
52   /**
53    * matches if the node is an attribute.
54    *
55    * @param node the current node
56    * @param env the variable environment
57    *
58    * @return true if the pattern matches
59    */

60   public boolean match(Node node, ExprEnvironment env)
61     throws XPathException
62   {
63     if (! (node instanceof Attr))
64       return false;
65
66     if (node instanceof QAttr &&
67         ((QAttr) node).getNamespaceURI() == XMLNS)
68       return false;
69
70     return _parent == null || _parent.match(node.getParentNode(), env);
71   }
72
73   /**
74    * The position of the child is the count of previous siblings
75    * matching the pattern.
76    *
77    * Technically, attributes don't have positions, but our DOM allows it.
78    */

79   public int position(Node node, Env env, AbstractPattern pattern)
80     throws XPathException
81   {
82     int count = 1;
83
84     for (node = node.getPreviousSibling();
85      node != null;
86      node = node.getPreviousSibling()) {
87       if (pattern.match(node, env))
88     count++;
89     }
90
91     return count;
92   }
93   
94   /**
95    * counts all siblings matching the pattern.
96    */

97   public int count(Node node, Env env, AbstractPattern pattern)
98     throws XPathException
99   {
100     int count = 0;
101
102     CauchoElement parent = (CauchoElement) node.getParentNode();
103
104     for (node = parent.getFirstAttribute();
105          node != null;
106          node = node.getNextSibling()) {
107       if (pattern.match(node, env))
108     count++;
109     }
110
111     return count;
112   }
113   
114   /**
115    * Creates a new node iterator.
116    *
117    * @param node the starting node
118    * @param env the variable environment
119    * @param match the axis match pattern
120    *
121    * @return the node iterator
122    */

123   public NodeIterator createNodeIterator(Node node, ExprEnvironment env,
124                                          AbstractPattern match)
125     throws XPathException
126   {
127     if (node instanceof CauchoNode) {
128       if (_parent == null)
129         return new AttributeIterator(null, this, node, env, match);
130       else if (_parent instanceof FromRoot) {
131         if (node instanceof Document JavaDoc)
132           return new AttributeIterator(null, this, node, env, match);
133         else
134           return new AttributeIterator(null, this, node.getOwnerDocument(),
135                                        env, match);
136       }
137
138       NodeIterator parentIter;
139       parentIter = _parent.createNodeIterator(node, env,
140                           _parent.copyPosition());
141
142       return new AttributeIterator(parentIter, this, null, env, match);
143     }
144     
145     if (_parent == null)
146       return new AttributeListIterator(null, env, match);
147     else if (_parent instanceof FromRoot) {
148       if (node instanceof Document JavaDoc)
149         return new AttributeListIterator(null, env, match);
150       else
151         return new AttributeListIterator(null, env, match);
152     }
153
154     NodeIterator parentIter;
155     parentIter = _parent.createNodeIterator(node, env, _parent.copyPosition());
156
157     return new AttributeListIterator(parentIter, env, match);
158   }
159
160   /**
161    * Returns the first node in the selection order.
162    *
163    * @param node the current node
164    *
165    * @return the first node
166    */

167   public Node firstNode(Node node, ExprEnvironment env)
168   {
169     if (node instanceof QElement)
170       return ((QElement) node).getFirstAttribute();
171     else
172       return null;
173   }
174
175   /**
176    * Returns the next node in the selection order.
177    *
178    * @param node the current node
179    * @param lastNode the last node
180    *
181    * @return the next node
182    */

183   public Node nextNode(Node node, Node lastNode)
184   {
185     return node.getNextSibling();
186   }
187
188   /**
189    * Returns true if the pattern is strictly ascending.
190    */

191   public boolean isStrictlyAscending()
192   {
193     if (_parent == null)
194       return true;
195     else
196       return _parent.isStrictlyAscending();
197   }
198
199   /**
200    * Returns true if the two patterns are equal.
201    */

202   public boolean equals(Object JavaDoc b)
203   {
204     if (! (b instanceof FromAttributes))
205       return false;
206
207     FromAttributes bPattern = (FromAttributes) b;
208     
209     return (_parent == bPattern._parent ||
210             (_parent != null && _parent.equals(bPattern._parent)));
211   }
212
213   public String JavaDoc toString()
214   {
215     return getPrefix() + "attribute::";
216   }
217 }
218
Popular Tags