KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > parser > Q_StringMatch


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

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 JavaDoc;
13
14 //import org.apache.oro.text.* ;
15
import org.apache.oro.text.regex.* ;
16 //import org.apache.oro.text.perl.Perl5Util ;
17
//import org.apache.oro.text.perl.MalformedPerl5PatternException ;
18

19
20 public class Q_StringMatch extends ExprNode implements Expr, ExprBoolean
21 {
22     Expr left ;
23     Expr right ;
24     Q_PatternLiteral regex = null ;
25     //Perl5Util matcher = new Perl5Util() ;
26
PatternCompiler compiler = new Perl5Compiler();
27     PatternMatcher matcher = new Perl5Matcher();
28     
29     // Cache the compiled regular expression.
30

31     private String JavaDoc printName = "strMatch" ;
32     private String JavaDoc 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         // There is a decision here : do we allow anything to be
45
// tested as string or do restrict ourselves to things
46
// that started as strings. Example: A URI is not string
47
// so should be it be possible to have:
48
// ?x ne <uri>
49
// Decision here is to allow string tests on anything.
50

51         NodeValue x = left.eval(q, env) ;
52         //Value y = right.eval(q, env) ; // Must be a pattern literal
53

54         // Allow anything to be forced to be a string.
55

56         String JavaDoc xx = x.valueString() ;
57         // Had better be the pattern string!
58
//String yy = y.toString() ;
59

60         NodeValueSettable result = new WorkingVar() ;
61         
62         // Actually do it!
63
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) ; // Must be a pattern literal
76
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     // -----------
91
// graph.query.Expression
92

93     public boolean isApply() { return true ; }
94     public String JavaDoc 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     // -----------
106

107     public String JavaDoc asInfixString()
108     {
109         return QueryPrintUtils.asInfixString2(left, right, printName, opSymbol) ;
110     }
111     
112     public String JavaDoc asPrefixString()
113     {
114         return QueryPrintUtils.asPrefixString(left, right, printName, opSymbol) ;
115     }
116     
117     public void print(PrintWriter JavaDoc pw, int level)
118     {
119         QueryPrintUtils.print(pw, left, right, printName, opSymbol, level) ;
120     }
121     
122     public String JavaDoc toString()
123     {
124         return asInfixString() ;
125     }
126 }
127 /*
128  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
129  * All rights reserved.
130  *
131  * Redistribution and use in source and binary forms, with or without
132  * modification, are permitted provided that the following conditions
133  * are met:
134  * 1. Redistributions of source code must retain the above copyright
135  * notice, this list of conditions and the following disclaimer.
136  * 2. Redistributions in binary form must reproduce the above copyright
137  * notice, this list of conditions and the following disclaimer in the
138  * documentation and/or other materials provided with the distribution.
139  * 3. The name of the author may not be used to endorse or promote products
140  * derived from this software without specific prior written permission.
141  *
142  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
143  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
144  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
145  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
146  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
147  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
148  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
149  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
150  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
151  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
152  */

153
Popular Tags