KickJava   Java API By Example, From Geeks To Geeks.

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


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
9 import java.io.PrintWriter JavaDoc;
10 import com.hp.hpl.jena.graph.query.IndexValues ;
11 import com.hp.hpl.jena.graph.query.Expression ;
12 import com.hp.hpl.jena.rdql.*;
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_StringNoMatch 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_StringNoMatch(int id)
36     { super(id); }
37     
38     Q_StringNoMatch(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.valueString() ;
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     // -----------
69
// graph.query.Expression
70

71     public boolean isApply() { return true ; }
72     public String JavaDoc getFun() { return super.constructURI(this.getClass().getName()) ; }
73     public int argCount() { return 2; }
74     public Expression getArg(int i)
75     {
76         if ( i == 0 && left instanceof Expression )
77             return (Expression)left ;
78         if ( i == 1 && right instanceof Expression )
79             return (Expression)right ;
80         return null;
81     }
82     public void jjtClose()
83     {
84         int n = jjtGetNumChildren() ;
85         if ( n != 2 )
86             throw new QueryException("Q_StringNoMatch: Wrong number of children: "+n) ;
87         
88         left = (Expr)jjtGetChild(0) ;
89         right = (Expr)jjtGetChild(1) ; // Must be a pattern literal
90
if ( ! ( right instanceof Q_PatternLiteral ) )
91             throw new EvalFailureException("Q_StringNoMatch: Pattern error") ;
92         
93         regex = (Q_PatternLiteral)right ;
94         
95         try
96         {
97             pattern = compiler.compile(regex.patternString, regex.mask) ;
98         } catch (MalformedPatternException pEx)
99         {
100             throw new EvalFailureException("Q_StringNoMatch: Pattern exception: "+pEx) ;
101         }
102     }
103     
104     public String JavaDoc asInfixString()
105     {
106         return QueryPrintUtils.asInfixString2(left, right, printName, opSymbol) ;
107     }
108     
109     public String JavaDoc asPrefixString()
110     {
111         return QueryPrintUtils.asPrefixString(left, right, printName, opSymbol) ;
112     }
113     
114     public void print(PrintWriter JavaDoc pw, int level)
115     {
116         QueryPrintUtils.print(pw, left, right, printName, opSymbol, level) ;
117     }
118     
119     public String JavaDoc toString()
120     {
121         return asInfixString() ;
122     }
123 }
124 /*
125  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
126  * All rights reserved.
127  *
128  * Redistribution and use in source and binary forms, with or without
129  * modification, are permitted provided that the following conditions
130  * are met:
131  * 1. Redistributions of source code must retain the above copyright
132  * notice, this list of conditions and the following disclaimer.
133  * 2. Redistributions in binary form must reproduce the above copyright
134  * notice, this list of conditions and the following disclaimer in the
135  * documentation and/or other materials provided with the distribution.
136  * 3. The name of the author may not be used to endorse or promote products
137  * derived from this software without specific prior written permission.
138  *
139  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
141  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
142  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
143  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
144  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
145  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
146  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
147  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
148  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
149  */

150
Popular Tags