1 5 6 package com.hp.hpl.jena.rdql.parser; 7 8 import com.hp.hpl.jena.graph.query.Expression; 9 import com.hp.hpl.jena.graph.query.IndexValues ; 10 import com.hp.hpl.jena.rdql.*; 11 12 import java.io.PrintWriter ; 13 14 import org.apache.oro.text.regex.* ; 16 19 20 public class Q_StringMatch extends ExprNode implements Expr, ExprBoolean 21 { 22 Expr left ; 23 Expr right ; 24 Q_PatternLiteral regex = null ; 25 PatternCompiler compiler = new Perl5Compiler(); 27 PatternMatcher matcher = new Perl5Matcher(); 28 29 31 private String printName = "strMatch" ; 32 private String opSymbol = "=~" ; 33 Pattern pattern = null ; 34 35 Q_StringMatch(int id) 36 { super(id); } 37 38 Q_StringMatch(RDQLParser p, int id) 39 { super(p, id); } 40 41 42 public NodeValue eval(Query q, IndexValues env) 43 { 44 51 NodeValue x = left.eval(q, env) ; 52 54 56 String xx = x.valueString() ; 57 60 NodeValueSettable result = new WorkingVar() ; 61 62 boolean b = matcher.contains(xx, pattern) ; 64 result.setBoolean(b) ; 65 return result ; 66 } 67 68 public void jjtClose() 69 { 70 int n = jjtGetNumChildren() ; 71 if ( n != 2 ) 72 throw new QueryException("Q_StringMatch: Wrong number of children: "+n) ; 73 74 left = (Expr)jjtGetChild(0) ; 75 right = (Expr)jjtGetChild(1) ; if ( ! ( right instanceof Q_PatternLiteral ) ) 77 throw new EvalFailureException("Q_StringMatch: Pattern error") ; 78 79 regex = (Q_PatternLiteral)right ; 80 81 try 82 { 83 pattern = compiler.compile(regex.patternString, regex.mask) ; 84 } catch (MalformedPatternException pEx) 85 { 86 throw new EvalFailureException("Q_StringMatch: Pattern exception: "+pEx) ; 87 } 88 } 89 90 93 public boolean isApply() { return true ; } 94 public String getFun() { return super.constructURI(this.getClass().getName()) ; } 95 public int argCount() { return 2; } 96 public Expression getArg(int i) 97 { 98 if ( i == 0 && left instanceof Expression ) 99 return (Expression)left ; 100 if ( i == 1 && right instanceof Expression ) 101 return (Expression)right ; 102 return null; 103 } 104 105 107 public String asInfixString() 108 { 109 return QueryPrintUtils.asInfixString2(left, right, printName, opSymbol) ; 110 } 111 112 public String asPrefixString() 113 { 114 return QueryPrintUtils.asPrefixString(left, right, printName, opSymbol) ; 115 } 116 117 public void print(PrintWriter pw, int level) 118 { 119 QueryPrintUtils.print(pw, left, right, printName, opSymbol, level) ; 120 } 121 122 public String toString() 123 { 124 return asInfixString() ; 125 } 126 } 127 153 | Popular Tags |