KickJava   Java API By Example, From Geeks To Geeks.

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


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 foo:para or @foo:id when the prefix
38  * maps to a namespace.
39  */

40 public class NSNamePattern extends AbstractPattern {
41   private NSNamePattern _match;
42   
43   private String JavaDoc _namespace;
44   private String JavaDoc _local;
45   private int _nodeType;
46
47   /**
48    * Creates the namespace pattern.
49    *
50    * @param parent the parent pattern.
51    * @param namespace the node's namespace URL.
52    * @param local the node's local name.
53    * @param nodeType the node type to match.
54    */

55   public NSNamePattern(AbstractPattern parent, String JavaDoc namespace, String JavaDoc local,
56                        int nodeType)
57   {
58     super(parent);
59
60     _nodeType = nodeType;
61
62     if (namespace != null)
63       _namespace = namespace.intern();
64     else
65       _namespace = "";
66     
67     _local = local.intern();
68   }
69
70   /**
71    * Nodes have a higher default priority.
72    */

73   public double getPriority()
74   {
75     return 0;
76   }
77
78   /**
79    * Matches if the namespace matches and the local name matches.
80    *
81    * @param node the current node
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
95     String JavaDoc uri = node.getNamespaceURI();
96     if (uri == null || ! node.getNamespaceURI().equals(_namespace))
97       return false;
98     
99     else if (! node.getLocalName().equals(_local))
100       return false;
101
102     else if (_parent != null && ! _parent.match(node, env))
103       return false;
104
105     return true;
106   }
107
108   /**
109    * Copies the position (non-axis) portion of the pattern.
110    */

111   public AbstractPattern copyPosition()
112   {
113     if (_match == null) {
114       AbstractPattern parent = null;
115       
116       if (_parent != null)
117         parent = _parent.copyPosition();
118       
119       _match = new NSNamePattern(parent, _namespace, _local, _nodeType);
120     }
121     
122     return _match;
123   }
124
125   /**
126    * Returns true if the two patterns are equal.
127    */

128   public boolean equals(Object JavaDoc b)
129   {
130     if (! (b instanceof NSNamePattern))
131       return false;
132
133     NSNamePattern bPattern = (NSNamePattern) b;
134     
135     return (_nodeType == bPattern._nodeType &&
136             _local.equals(bPattern._local) &&
137             _namespace.equals(bPattern._namespace) &&
138             (_parent == bPattern._parent ||
139              (_parent != null && _parent.equals(bPattern._parent))));
140   }
141
142   public String JavaDoc toString()
143   {
144     return _parent + "{" + _namespace + "}" + _local;
145   }
146 }
147
Popular Tags