KickJava   Java API By Example, From Geeks To Geeks.

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


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
10 import java.io.PrintWriter JavaDoc;
11 import com.hp.hpl.jena.graph.Node ;
12 import com.hp.hpl.jena.graph.query.IndexValues;
13 import com.hp.hpl.jena.graph.query.Expression;
14 import com.hp.hpl.jena.rdql.*;
15
16 public class Q_StringEqual extends ExprNode implements Expr, ExprBoolean
17 {
18     Expr left ;
19     Expr right ;
20
21     static protected boolean enableRDFLiteralSameValueAs = true ;
22
23     protected static String JavaDoc printName = "str=" ;
24     protected static String JavaDoc opSymbol = "eq" ;
25
26     Q_StringEqual(int id) { super(id); }
27
28     Q_StringEqual(RDQLParser p, int id) { super(p, id); }
29
30     protected boolean rawEval(NodeValue x, NodeValue y)
31     {
32         // There is a decision here : do we allow anything to be
33
// tested as string or do restrict ourselves to things
34
// that started as strings. Example: A URI is not string
35
// so should be it be possible to have:
36
// ?x ne <uri>
37
// Decision here is to allow string tests on anything.
38

39         // Jena2 - another decision point.
40
// If we know left and right are types/lang-tagged literals,
41
// do we apply a stricter test of "equal"?
42

43         if ( enableRDFLiteralSameValueAs )
44         {
45             if ( x.isNode() && x.getNode().isLiteral() &&
46                  y.isNode() && y.getNode().isLiteral() )
47             {
48                 Node xNode = x.getNode() ;
49                 Node yNode = y.getNode() ;
50                 return xNode.sameValueAs(yNode) ;
51             }
52         }
53
54         // Allow anything to be forced to be a string.
55
String JavaDoc xx = x.valueString() ;
56         String JavaDoc yy = y.valueString() ;
57
58         return (xx.equals(yy)) ;
59     }
60
61     public NodeValue eval(Query q, IndexValues env)
62     {
63         NodeValue x = left.eval(q, env) ;
64         NodeValue y = right.eval(q, env) ;
65         
66         boolean b = rawEval(x, y) ;
67                 
68         NodeValueSettable result ;
69         if ( x instanceof NodeValueSettable )
70             result = (NodeValueSettable)x ;
71         else if ( y instanceof NodeValueSettable )
72             result = (NodeValueSettable)y ;
73         else
74             result = new WorkingVar() ;
75
76         result.setBoolean(b) ;
77         return result ;
78     }
79
80     public void jjtClose()
81     {
82         int n = jjtGetNumChildren() ;
83         if ( n != 2 )
84             throw new QueryException("Q_StringEqual: Wrong number of children: "+n) ;
85
86         left = (Expr)jjtGetChild(0) ;
87         right = (Expr)jjtGetChild(1) ;
88     }
89
90     // graph.query.Expression
91

92     public boolean isApply() { return true ; }
93     public String JavaDoc getFun() { return this.getClass().getName() ; } // For URI of the function
94
public int argCount() { return 2; }
95     public Expression getArg(int i)
96     {
97         if ( i == 0 && left instanceof Expression )
98             return (Expression)left ;
99         if ( i == 1 && right instanceof Expression )
100             return (Expression)right ;
101         return null;
102     }
103
104     // ----
105

106     public String JavaDoc asInfixString()
107     {
108         return QueryPrintUtils.asInfixString2(left, right, printName, opSymbol) ;
109     }
110
111     public String JavaDoc asPrefixString()
112     {
113         return QueryPrintUtils.asPrefixString(left, right, printName, opSymbol) ;
114     }
115
116     public void print(PrintWriter JavaDoc pw, int level)
117     {
118         QueryPrintUtils.print(pw, left, right, printName, opSymbol, level) ;
119     }
120
121     public String JavaDoc toString()
122     {
123         return asInfixString() ;
124     }
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