001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xpath.pattern;
030:
031: import com.caucho.xpath.ExprEnvironment;
032: import com.caucho.xpath.XPathException;
033:
034: import org.w3c.dom.Node;
035:
036: /**
037: * Matches a named node, like foo:para or @foo:id when the prefix
038: * maps to a namespace.
039: */
040: public class NSNamePattern extends AbstractPattern {
041: private NSNamePattern _match;
042:
043: private String _namespace;
044: private String _local;
045: private int _nodeType;
046:
047: /**
048: * Creates the namespace pattern.
049: *
050: * @param parent the parent pattern.
051: * @param namespace the node's namespace URL.
052: * @param local the node's local name.
053: * @param nodeType the node type to match.
054: */
055: public NSNamePattern(AbstractPattern parent, String namespace,
056: String local, int nodeType) {
057: super (parent);
058:
059: _nodeType = nodeType;
060:
061: if (namespace != null)
062: _namespace = namespace.intern();
063: else
064: _namespace = "";
065:
066: _local = local.intern();
067: }
068:
069: /**
070: * Nodes have a higher default priority.
071: */
072: public double getPriority() {
073: return 0;
074: }
075:
076: /**
077: * Matches if the namespace matches and the local name matches.
078: *
079: * @param node the current node
080: * @param env the variable environment
081: *
082: * @return true if the node matches
083: */
084: public boolean match(Node node, ExprEnvironment env)
085: throws XPathException {
086: if (node == null)
087: return false;
088:
089: if (node.getNodeType() != _nodeType)
090: return false;
091:
092: String uri = node.getNamespaceURI();
093: if (uri == null || !node.getNamespaceURI().equals(_namespace))
094: return false;
095:
096: else if (!node.getLocalName().equals(_local))
097: return false;
098:
099: else if (_parent != null && !_parent.match(node, env))
100: return false;
101:
102: return true;
103: }
104:
105: /**
106: * Copies the position (non-axis) portion of the pattern.
107: */
108: public AbstractPattern copyPosition() {
109: if (_match == null) {
110: AbstractPattern parent = null;
111:
112: if (_parent != null)
113: parent = _parent.copyPosition();
114:
115: _match = new NSNamePattern(parent, _namespace, _local,
116: _nodeType);
117: }
118:
119: return _match;
120: }
121:
122: /**
123: * Returns true if the two patterns are equal.
124: */
125: public boolean equals(Object b) {
126: if (!(b instanceof NSNamePattern))
127: return false;
128:
129: NSNamePattern bPattern = (NSNamePattern) b;
130:
131: return (_nodeType == bPattern._nodeType
132: && _local.equals(bPattern._local)
133: && _namespace.equals(bPattern._namespace) && (_parent == bPattern._parent || (_parent != null && _parent
134: .equals(bPattern._parent))));
135: }
136:
137: public String toString() {
138: return _parent + "{" + _namespace + "}" + _local;
139: }
140: }
|