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.Document ; 35 import org.w3c.dom.Node ; 36 37 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 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 86 public String getNodeName() 87 { 88 switch (_nodeType) { 89 case Node.TEXT_NODE: 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 106 public int getNodeType() 107 { 108 return _nodeType; 109 } 110 111 114 public boolean isStrictlyAscending() 115 { 116 if (_parent != null) 117 return _parent.isStrictlyAscending(); 118 else 119 return true; 120 } 121 122 130 public boolean match(Node 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 )) { 147 } 148 else 149 return false; 150 151 return _parent == null || _parent.match(node, env); 152 } 153 154 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 173 public boolean equals(Object 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 188 public String toString() 189 { 190 String 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 |