KickJava   Java API By Example, From Geeks To Geeks.

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


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.xpath.ExprEnvironment;
32 import com.caucho.xpath.XPathException;
33
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36
37 /**
38  * Matches a node without checking the name. e.g. * or @*
39  */

40 public class NodeTypePattern extends AbstractPattern {
41   public final static int NODE = -2;
42   public final static int ANY = -1;
43
44   private AbstractPattern _match;
45   private int _nodeType;
46
47   private NodeTypePattern(AbstractPattern parent, int nodeType)
48   {
49     super(parent);
50
51     _nodeType = nodeType;
52   }
53
54   public static AbstractPattern create(AbstractPattern parent, int nodeType)
55   {
56     if (nodeType == NODE &&
57         parent instanceof FromParent &&
58         parent._parent instanceof FromContext) {
59       FromContext context = (FromContext) parent._parent;
60
61       return new FromContext(context.getCount() + 1);
62     }
63     else
64       return new NodeTypePattern(parent, nodeType);
65   }
66
67   /**
68    * The node-type priority is less than nodes.
69    */

70   public double getPriority()
71   {
72     if (_parent instanceof Axis &&
73     _parent.getParent() != null &&
74     ! (_parent.getParent() instanceof FromRoot) &&
75     ! (_parent.getParent() instanceof FromAny))
76       return 0.5;
77     else
78       return -0.5;
79   }
80
81   /**
82    * Returns the name of the matching node or '*' if many nodes match.
83    *
84    * <p>The Xsl package uses this to speed template matching.
85    */

86   public String JavaDoc getNodeName()
87   {
88     switch (_nodeType) {
89     case Node.TEXT_NODE: // cdata, too?
90
return "#text";
91       
92     case Node.DOCUMENT_NODE:
93       return "#document";
94       
95     case Node.COMMENT_NODE:
96       return "#comment";
97       
98     default:
99       return "*";
100     }
101   }
102
103   /**
104    * Returns the matching node type.
105    */

106   public int getNodeType()
107   {
108     return _nodeType;
109   }
110   
111   /**
112    * Returns true if the pattern is strictly ascending.
113    */

114   public boolean isStrictlyAscending()
115   {
116     if (_parent != null)
117       return _parent.isStrictlyAscending();
118     else
119       return true;
120   }
121
122   /**
123    * Matches if the node type matches.
124    *
125    * @param node the current node
126    * @param env the variable environment
127    *
128    * @return true if the node matches the node type
129    */

130   public boolean match(Node JavaDoc node, ExprEnvironment env)
131     throws XPathException
132   {
133     if (node == null)
134       return false;
135
136     if (_nodeType == Node.ATTRIBUTE_NODE) {
137       if (node.getNodeType() != Node.ATTRIBUTE_NODE)
138         return false;
139       else if (XMLNS.equals(node.getNamespaceURI()))
140         return false;
141     }
142     else if (node.getNodeType() == _nodeType) {
143     }
144     else if (_nodeType == ANY) {
145     }
146     else if (_nodeType == NODE && ! (node instanceof Document JavaDoc)) {
147     }
148     else
149       return false;
150     
151     return _parent == null || _parent.match(node, env);
152   }
153
154   /**
155    * Copies the node matching portion of the pattern, i.e. the section
156    * only applying to the current axis.
157    */

158   public AbstractPattern copyPosition()
159   {
160     if (_match == null) {
161       AbstractPattern parent = null;
162       if (_parent != null)
163         parent = _parent.copyPosition();
164       _match = new NodeTypePattern(parent, _nodeType);
165     }
166     
167     return _match;
168   }
169
170   /**
171    * Returns true if the two patterns are equal.
172    */

173   public boolean equals(Object JavaDoc b)
174   {
175     if (! (b instanceof NodeTypePattern))
176       return false;
177
178     NodeTypePattern bPattern = (NodeTypePattern) b;
179     
180     return (_nodeType == bPattern._nodeType &&
181             (_parent == bPattern._parent ||
182              (_parent != null && _parent.equals(bPattern._parent))));
183   }
184
185   /**
186    * Returns the printable representation of the pattern.
187    */

188   public String JavaDoc toString()
189   {
190     String JavaDoc prefix;
191     
192     if (_parent == null)
193       prefix = "";
194     else if (_parent instanceof FromChildren)
195       prefix = _parent.getPrefix();
196     else
197       prefix = _parent.toString();
198     
199     switch (_nodeType) {
200     case ANY:
201       if (! (_parent instanceof FromSelf))
202     return prefix + "node()";
203       else if (_parent._parent == null)
204     return ".";
205       else
206         return _parent.getPrefix() + ".";
207
208     case NODE:
209       return prefix + "node()";
210
211     case Node.PROCESSING_INSTRUCTION_NODE:
212       return prefix + "pi()";
213     case Node.ATTRIBUTE_NODE:
214       return prefix + "*";
215     case Node.ELEMENT_NODE:
216       return prefix + "*";
217     case Node.COMMENT_NODE:
218       return prefix + "comment()";
219     case Node.TEXT_NODE:
220       return prefix + "text()";
221     case Node.ENTITY_REFERENCE_NODE:
222       return prefix + "er()";
223     default:
224       return super.toString();
225     }
226   }
227 }
228
Popular Tags