KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 // This facility was provided by Zhexuan Song [zsong@fla.fujitsu.com] (Jeff) by
8
// modifying Q_StringEqual from the original Jena distribution.
9
// Thanks to Jeff for this.
10

11 package com.hp.hpl.jena.rdql.parser;
12
13 import java.io.PrintWriter JavaDoc;
14 import com.hp.hpl.jena.graph.Node;
15 import com.hp.hpl.jena.graph.query.Expression;
16 import com.hp.hpl.jena.graph.query.IndexValues;
17 import com.hp.hpl.jena.rdql.Query;
18 import com.hp.hpl.jena.rdql.QueryException;
19 import com.hp.hpl.jena.rdql.QueryPrintUtils;
20
21 public class Q_StringLangEqual extends ExprNode implements Expr, ExprBoolean
22 {
23     Expr left ;
24     Expr right ;
25
26     protected static String JavaDoc printName = "lang=" ;
27     protected static String JavaDoc opSymbol = "langeq" ;
28     
29     Q_StringLangEqual(int id) { super(id); }
30     
31     Q_StringLangEqual(RDQLParser p, int id) { super(p, id); }
32
33     protected boolean rawEval(NodeValue x, NodeValue y)
34     {
35         if ( x.isNode() && x.getNode().isLiteral() &&
36              y.isNode() && y.getNode().isLiteral() )
37         {
38             Node xNode = x.getNode() ;
39             Node yNode = y.getNode() ;
40             String JavaDoc nodeLang = xNode.getLiteral().language().toUpperCase();
41             String JavaDoc queryLang = yNode.getLiteral().getLexicalForm().toUpperCase();
42             /**
43              * Here is the logic to compare language code
44              * If the query langauge has -, such as zh-tw, we must do
45              * a complete match. So "zh-tw" will not match "zh-t".
46              * If the query language does not have -, such as en, we should do
47              * a substring match. So "en-gb" should match "en".
48              */

49             if (queryLang.indexOf("-") >= 0)
50             {
51                 return nodeLang.equals(queryLang);
52             }
53             else
54             {
55                 int pos = nodeLang.indexOf("-");
56                 if (pos > 0)
57                     nodeLang = nodeLang.substring(0, pos);
58                 return nodeLang.equals(queryLang);
59             }
60         }
61         return false;
62     }
63     
64     public NodeValue eval(Query q, IndexValues env)
65     {
66         NodeValue x = left.eval(q, env) ;
67         NodeValue y = right.eval(q, env) ;
68         
69         boolean b = rawEval(x, y) ;
70                 
71         NodeValueSettable result ;
72         if ( x instanceof NodeValueSettable )
73             result = (NodeValueSettable)x ;
74         else if ( y instanceof NodeValueSettable )
75             result = (NodeValueSettable)y ;
76         else
77             result = new WorkingVar() ;
78         result.setBoolean(b) ;
79         return result ;
80     }
81     public void jjtClose()
82     {
83         int n = jjtGetNumChildren() ;
84         if ( n != 2 )
85             throw new QueryException("Q_StringLangEqual: Wrong number of children: "+n) ;
86         left = (Expr)jjtGetChild(0) ;
87         right = (Expr)jjtGetChild(1) ;
88     }
89     
90     // graph.query.Expression
91
public boolean isApply() { return true ; }
92     public String JavaDoc getFun() { return this.getClass().getName() ; } // For URI of the function
93
public int argCount() { return 2; }
94     public Expression getArg(int i)
95     {
96         if ( i == 0 && left instanceof Expression )
97             return (Expression)left ;
98         if ( i == 1 && right instanceof Expression )
99             return (Expression)right ;
100         return null;
101     }
102     // ----
103

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

156
157 /*
158  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
159  * All rights reserved.
160  *
161  * Redistribution and use in source and binary forms, with or without
162  * modification, are permitted provided that the following conditions
163  * are met:
164  * 1. Redistributions of source code must retain the above copyright
165  * notice, this list of conditions and the following disclaimer.
166  * 2. Redistributions in binary form must reproduce the above copyright
167  * notice, this list of conditions and the following disclaimer in the
168  * documentation and/or other materials provided with the distribution.
169  * 3. The name of the author may not be used to endorse or promote products
170  * derived from this software without specific prior written permission.
171  *
172  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
174  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
175  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
176  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
177  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
178  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
179  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
180  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
181  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
182  */

183
Popular Tags