KickJava   Java API By Example, From Geeks To Geeks.

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


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.Node JavaDoc;
35
36 /**
37  * Matches a named node, like para or @id.
38  */

39 public class NodePattern extends AbstractPattern {
40   private NodePattern _match;
41   
42   private String JavaDoc _tag;
43   private int _nodeType;
44
45   /**
46    * Creates a new node-matching pattern.
47    */

48   public NodePattern(AbstractPattern parent, String JavaDoc tag, int nodeType)
49   {
50     super(parent);
51
52     _tag = tag.intern();
53     _nodeType = nodeType;
54   }
55
56   /**
57    * All priorities are based on the node priority.
58    */

59   public double getPriority()
60   {
61     if (_parent == null ||
62     _parent instanceof FromChildren &&
63     (_parent._parent instanceof FromAny ||
64      _parent._parent instanceof FromContext))
65       return 0;
66     else
67       return 0.5;
68   }
69
70   /**
71    * Returns the pattern's matching node name.
72    */

73   public String JavaDoc getNodeName()
74   {
75     return _tag;
76   }
77
78   /**
79    * matches if the node type matches and the node name matches.
80    *
81    * @param node the node to test.
82    * @param env the variable environment
83    *
84    * @return true if the node matches
85    */

86   public boolean match(Node JavaDoc node, ExprEnvironment env)
87     throws XPathException
88   {
89     if (node == null)
90       return false;
91
92     if (node.getNodeType() != _nodeType)
93       return false;
94     else if (node.getNodeName() != _tag)
95       return false;
96     else if (node.getNamespaceURI() != null)
97       return false;
98     else if (_parent != null && ! _parent.match(node, env))
99       return false;
100
101     return true;
102   }
103
104   /**
105    * Copies the position (non-axis) portion of the pattern.
106    */

107   public AbstractPattern copyPosition()
108   {
109     if (_match == null) {
110       AbstractPattern parent = null;
111       if (_parent != null)
112         parent = _parent.copyPosition();
113       _match = new NodePattern(parent, _tag, _nodeType);
114     }
115     
116     return _match;
117   }
118
119   /**
120    * Returns true if the two patterns are equal.
121    */

122   public boolean equals(Object JavaDoc b)
123   {
124     if (! (b instanceof NodePattern))
125       return false;
126
127     NodePattern bPattern = (NodePattern) b;
128     
129     return (_nodeType == bPattern._nodeType &&
130             _tag.equals(bPattern._tag) &&
131             (_parent == bPattern._parent ||
132              (_parent != null && _parent.equals(bPattern._parent))));
133   }
134
135   /**
136    * Converts the pattern back to its
137    */

138   public String JavaDoc toString()
139   {
140     String JavaDoc prefix;
141     
142     if (_parent == null || _parent instanceof FromAny)
143       prefix = "";
144     else if (_parent instanceof FromChildren)
145       prefix = _parent.getPrefix();
146     else
147       prefix = _parent.toString();
148
149     switch (_nodeType) {
150     case Node.PROCESSING_INSTRUCTION_NODE:
151       return prefix + "pi('" + _tag + "')";
152       
153     case Node.ATTRIBUTE_NODE:
154       return prefix + _tag;
155       
156     case Node.ELEMENT_NODE:
157       return prefix + _tag;
158
159     default:
160       return super.toString();
161     }
162   }
163 }
164
Popular Tags