1 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 ; 35 36 39 public class NodePattern extends AbstractPattern { 40 private NodePattern _match; 41 42 private String _tag; 43 private int _nodeType; 44 45 48 public NodePattern(AbstractPattern parent, String tag, int nodeType) 49 { 50 super(parent); 51 52 _tag = tag.intern(); 53 _nodeType = nodeType; 54 } 55 56 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 73 public String getNodeName() 74 { 75 return _tag; 76 } 77 78 86 public boolean match(Node 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 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 122 public boolean equals(Object 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 138 public String toString() 139 { 140 String 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 |