| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
package org.jaxen.pattern; |
| 50 | |
|
| 51 | |
import java.util.Iterator; |
| 52 | |
import java.util.List; |
| 53 | |
import java.util.ListIterator; |
| 54 | |
|
| 55 | |
import org.jaxen.JaxenException; |
| 56 | |
import org.jaxen.JaxenHandler; |
| 57 | |
import org.jaxen.expr.DefaultAllNodeStep; |
| 58 | |
import org.jaxen.expr.DefaultCommentNodeStep; |
| 59 | |
import org.jaxen.expr.DefaultFilterExpr; |
| 60 | |
import org.jaxen.expr.DefaultNameStep; |
| 61 | |
import org.jaxen.expr.DefaultProcessingInstructionNodeStep; |
| 62 | |
import org.jaxen.expr.DefaultStep; |
| 63 | |
import org.jaxen.expr.DefaultTextNodeStep; |
| 64 | |
import org.jaxen.expr.DefaultXPathFactory; |
| 65 | |
import org.jaxen.expr.Expr; |
| 66 | |
import org.jaxen.expr.FilterExpr; |
| 67 | |
import org.jaxen.expr.LocationPath; |
| 68 | |
import org.jaxen.expr.Predicate; |
| 69 | |
import org.jaxen.expr.PredicateSet; |
| 70 | |
import org.jaxen.expr.Step; |
| 71 | |
import org.jaxen.expr.UnionExpr; |
| 72 | |
import org.jaxen.saxpath.Axis; |
| 73 | |
import org.jaxen.saxpath.XPathReader; |
| 74 | |
import org.jaxen.saxpath.helpers.XPathReaderFactory; |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | 0 | public class PatternParser |
| 83 | |
{ |
| 84 | |
private static final boolean TRACE = false; |
| 85 | |
private static final boolean USE_HANDLER = false; |
| 86 | |
public static Pattern parse(String text) throws JaxenException, org.jaxen.saxpath.SAXPathException |
| 87 | |
{ |
| 88 | |
if ( USE_HANDLER ) |
| 89 | |
{ |
| 90 | |
XPathReader reader = XPathReaderFactory.createReader(); |
| 91 | |
PatternHandler handler = new PatternHandler(); |
| 92 | |
|
| 93 | |
handler.setXPathFactory( new DefaultXPathFactory() ); |
| 94 | |
reader.setXPathHandler( handler ); |
| 95 | |
reader.parse( text ); |
| 96 | |
|
| 97 | |
return handler.getPattern(); |
| 98 | |
} |
| 99 | |
else |
| 100 | |
{ |
| 101 | 115 | XPathReader reader = XPathReaderFactory.createReader(); |
| 102 | 115 | JaxenHandler handler = new JaxenHandler(); |
| 103 | |
|
| 104 | 115 | handler.setXPathFactory( new DefaultXPathFactory() ); |
| 105 | 115 | reader.setXPathHandler( handler ); |
| 106 | 115 | reader.parse( text ); |
| 107 | |
|
| 108 | 115 | Pattern pattern = convertExpr( handler.getXPathExpr().getRootExpr() ); |
| 109 | 115 | return pattern.simplify(); |
| 110 | |
} |
| 111 | |
} |
| 112 | |
|
| 113 | |
protected static Pattern convertExpr(Expr expr) throws JaxenException |
| 114 | |
{ |
| 115 | |
if ( TRACE ) |
| 116 | |
{ |
| 117 | |
System.out.println( "Converting: " + expr + " into a pattern." ); |
| 118 | |
} |
| 119 | |
|
| 120 | 145 | if ( expr instanceof LocationPath ) |
| 121 | |
{ |
| 122 | 130 | return convertExpr( (LocationPath) expr ); |
| 123 | |
} |
| 124 | 15 | else if ( expr instanceof FilterExpr ) |
| 125 | |
{ |
| 126 | 0 | LocationPathPattern answer = new LocationPathPattern(); |
| 127 | 0 | answer.addFilter( (FilterExpr) expr ); |
| 128 | 0 | return answer; |
| 129 | |
} |
| 130 | 15 | else if ( expr instanceof UnionExpr ) |
| 131 | |
{ |
| 132 | 15 | UnionExpr unionExpr = (UnionExpr) expr; |
| 133 | 15 | Pattern lhs = convertExpr( unionExpr.getLHS() ); |
| 134 | 15 | Pattern rhs = convertExpr( unionExpr.getRHS() ); |
| 135 | 15 | return new UnionPattern( lhs, rhs ); |
| 136 | |
} |
| 137 | |
else |
| 138 | |
{ |
| 139 | 0 | LocationPathPattern answer = new LocationPathPattern(); |
| 140 | 0 | answer.addFilter( new DefaultFilterExpr( expr, |
| 141 | |
new PredicateSet()) ); |
| 142 | 0 | return answer; |
| 143 | |
} |
| 144 | |
} |
| 145 | |
|
| 146 | |
protected static LocationPathPattern convertExpr(LocationPath locationPath) throws JaxenException |
| 147 | |
{ |
| 148 | 130 | LocationPathPattern answer = new LocationPathPattern(); |
| 149 | |
|
| 150 | 130 | List steps = locationPath.getSteps(); |
| 151 | |
|
| 152 | |
|
| 153 | 130 | LocationPathPattern path = answer; |
| 154 | 130 | boolean first = true; |
| 155 | 130 | for ( ListIterator iter = steps.listIterator( steps.size() ); iter.hasPrevious(); ) |
| 156 | |
{ |
| 157 | 180 | Step step = (Step) iter.previous(); |
| 158 | 180 | if ( first ) |
| 159 | |
{ |
| 160 | 120 | first = false; |
| 161 | 120 | path = convertStep( path, step ); |
| 162 | |
} |
| 163 | |
else |
| 164 | |
{ |
| 165 | 60 | if ( navigationStep( step ) ) |
| 166 | |
{ |
| 167 | 60 | LocationPathPattern parent = new LocationPathPattern(); |
| 168 | 60 | int axis = step.getAxis(); |
| 169 | 60 | if ( axis == Axis.DESCENDANT || axis == Axis.DESCENDANT_OR_SELF ) |
| 170 | |
{ |
| 171 | 10 | path.setAncestorPattern( parent ); |
| 172 | |
} |
| 173 | |
else |
| 174 | |
{ |
| 175 | 50 | path.setParentPattern( parent ); |
| 176 | |
} |
| 177 | 60 | path = parent; |
| 178 | |
} |
| 179 | 60 | path = convertStep( path, step ); |
| 180 | |
} |
| 181 | 180 | } |
| 182 | 130 | if ( locationPath.isAbsolute() ) |
| 183 | |
{ |
| 184 | 20 | LocationPathPattern parent = new LocationPathPattern( NodeTypeTest.DOCUMENT_TEST ); |
| 185 | 20 | path.setParentPattern( parent ); |
| 186 | |
} |
| 187 | 130 | return answer; |
| 188 | |
} |
| 189 | |
|
| 190 | |
protected static LocationPathPattern convertStep(LocationPathPattern path, Step step) throws JaxenException |
| 191 | |
{ |
| 192 | 180 | if ( step instanceof DefaultAllNodeStep ) |
| 193 | |
{ |
| 194 | 10 | int axis = step.getAxis(); |
| 195 | 10 | if ( axis == Axis.ATTRIBUTE ) |
| 196 | |
{ |
| 197 | 0 | path.setNodeTest( NodeTypeTest.ATTRIBUTE_TEST ); |
| 198 | |
} |
| 199 | |
else |
| 200 | |
{ |
| 201 | 10 | path.setNodeTest( NodeTypeTest.ELEMENT_TEST ); |
| 202 | |
} |
| 203 | 10 | } |
| 204 | 170 | else if ( step instanceof DefaultCommentNodeStep ) |
| 205 | |
{ |
| 206 | 0 | path.setNodeTest( NodeTypeTest.COMMENT_TEST ); |
| 207 | |
} |
| 208 | 170 | else if ( step instanceof DefaultProcessingInstructionNodeStep ) |
| 209 | |
{ |
| 210 | 0 | path.setNodeTest( NodeTypeTest.PROCESSING_INSTRUCTION_TEST ); |
| 211 | |
} |
| 212 | 170 | else if ( step instanceof DefaultTextNodeStep ) |
| 213 | |
{ |
| 214 | 10 | path.setNodeTest( TextNodeTest.SINGLETON ); |
| 215 | |
} |
| 216 | 160 | else if ( step instanceof DefaultCommentNodeStep ) |
| 217 | |
{ |
| 218 | 0 | path.setNodeTest( NodeTypeTest.COMMENT_TEST ); |
| 219 | |
} |
| 220 | 160 | else if ( step instanceof DefaultNameStep ) |
| 221 | |
{ |
| 222 | 160 | DefaultNameStep nameStep = (DefaultNameStep) step; |
| 223 | 160 | String localName = nameStep.getLocalName(); |
| 224 | 160 | String prefix = nameStep.getPrefix(); |
| 225 | 160 | int axis = nameStep.getAxis(); |
| 226 | 160 | short nodeType = Pattern.ELEMENT_NODE; |
| 227 | 160 | if ( axis == Axis.ATTRIBUTE ) |
| 228 | |
{ |
| 229 | 10 | nodeType = Pattern.ATTRIBUTE_NODE; |
| 230 | |
} |
| 231 | 160 | if ( nameStep.isMatchesAnyName() ) |
| 232 | |
{ |
| 233 | 40 | if ( prefix.length() == 0 || prefix.equals( "*" ) ) |
| 234 | |
{ |
| 235 | 35 | if ( axis == Axis.ATTRIBUTE ) |
| 236 | |
{ |
| 237 | 10 | path.setNodeTest( NodeTypeTest.ATTRIBUTE_TEST ); |
| 238 | |
} |
| 239 | |
else |
| 240 | |
{ |
| 241 | 25 | path.setNodeTest( NodeTypeTest.ELEMENT_TEST ); |
| 242 | |
} |
| 243 | |
} |
| 244 | |
else |
| 245 | |
{ |
| 246 | 5 | path.setNodeTest( new NamespaceTest( prefix, nodeType ) ); |
| 247 | |
} |
| 248 | |
} |
| 249 | |
else |
| 250 | |
{ |
| 251 | 120 | path.setNodeTest( new NameTest( localName, nodeType ) ); |
| 252 | |
|
| 253 | |
} |
| 254 | 160 | return convertDefaultStep(path, nameStep); |
| 255 | |
} |
| 256 | 0 | else if ( step instanceof DefaultStep ) |
| 257 | |
{ |
| 258 | 0 | return convertDefaultStep(path, (DefaultStep) step); |
| 259 | |
} |
| 260 | |
else |
| 261 | |
{ |
| 262 | 0 | throw new JaxenException( "Cannot convert: " + step + " to a Pattern" ); |
| 263 | |
} |
| 264 | 20 | return path; |
| 265 | |
} |
| 266 | |
|
| 267 | |
protected static LocationPathPattern convertDefaultStep(LocationPathPattern path, DefaultStep step) throws JaxenException |
| 268 | |
{ |
| 269 | 160 | List predicates = step.getPredicates(); |
| 270 | 160 | if ( ! predicates.isEmpty() ) |
| 271 | |
{ |
| 272 | 25 | FilterExpr filter = new DefaultFilterExpr(new PredicateSet()); |
| 273 | 25 | for ( Iterator iter = predicates.iterator(); iter.hasNext(); ) |
| 274 | |
{ |
| 275 | 25 | filter.addPredicate( (Predicate) iter.next() ); |
| 276 | |
} |
| 277 | 25 | path.addFilter( filter ); |
| 278 | |
} |
| 279 | 160 | return path; |
| 280 | |
} |
| 281 | |
|
| 282 | |
protected static boolean navigationStep( Step step ) |
| 283 | |
{ |
| 284 | 60 | if ( step instanceof DefaultNameStep ) |
| 285 | |
{ |
| 286 | 50 | return true; |
| 287 | |
} |
| 288 | |
else |
| 289 | 15 | if ( step.getClass().equals( DefaultStep.class ) ) |
| 290 | |
{ |
| 291 | 0 | return ! step.getPredicates().isEmpty(); |
| 292 | |
} |
| 293 | |
else |
| 294 | |
{ |
| 295 | 10 | return true; |
| 296 | |
} |
| 297 | |
} |
| 298 | |
|
| 299 | |
} |
| 300 | |
|