KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > query > Rewrite


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: Rewrite.java,v 1.9 2005/02/21 11:52:25 andy_seaborne Exp $
5 */

6 package com.hp.hpl.jena.graph.query;
7
8 /**
9      Rewrite - class which does expression rewrites for Query
10      @author kers
11 */

12 public class Rewrite
13     {
14     public static Expression rewriteStringMatch( Expression e )
15         {
16         Expression L = e.getArg(0), R = e.getArg(1);
17         PatternLiteral pattern = Rewrite.getPattern( R );
18         if (pattern == null)
19             return e;
20         else if (isStartsWith( pattern ))
21             return startsWith( L, pattern.getPatternString().substring(1), pattern.getPatternModifiers() );
22         else if (isContains( pattern ))
23             return contains( L, pattern.getPatternString(), pattern.getPatternModifiers() );
24         else if (isEndsWith( pattern ))
25             return endsWith( L, front( pattern.getPatternString() ), pattern.getPatternModifiers() );
26         return e;
27         }
28     
29     protected static String JavaDoc front( String JavaDoc s )
30         { return s.substring( 0, s.length() - 1 ); }
31
32     public static PatternLiteral getPattern( Expression E )
33         {
34         if (E instanceof PatternLiteral)
35             {
36             PatternLiteral L = (PatternLiteral) E;
37             if (L.getPatternLanguage().equals( PatternLiteral.rdql )) return L;
38             }
39         return null;
40         }
41
42     /*
43          This following code for the evaluators is horrid - there's too much uncaptured
44          variation. Perhaps it will all go away when I understand how to use the
45          regex package (note, not the Java 1.4 package, because we still support
46          Java 1.3; the ORO package that RDQL uses).
47          
48          TODO clean this up.
49          
50         @author hedgehog
51     */

52     public static abstract class DyadicLiteral extends Dyadic
53         {
54         public DyadicLiteral( Expression L, String JavaDoc F, String JavaDoc R )
55             { super( L, F, new Expression.Fixed( R ) ); }
56         
57         public boolean evalBool( Object JavaDoc l, Object JavaDoc r )
58             { return evalBool( l.toString(), r.toString() ); }
59         
60         protected abstract boolean evalBool( String JavaDoc l, String JavaDoc r );
61         }
62     
63     public static abstract class DyadicLower extends DyadicLiteral
64         {
65         public DyadicLower( Expression L, String JavaDoc F, String JavaDoc R )
66             { super( L, F, R.toLowerCase() ); }
67         }
68     
69     public static Expression endsWith( Expression L, String JavaDoc content, String JavaDoc modifiers )
70         {
71         if (modifiers.equals( "i" ))
72             {
73             return new DyadicLower( L, ExpressionFunctionURIs.J_endsWithInsensitive, content )
74                 {
75                 public boolean evalBool( String JavaDoc l, String JavaDoc r )
76                     { return l.toLowerCase().endsWith( r ); }
77                 };
78             }
79         else
80             {
81             return new DyadicLiteral( L, ExpressionFunctionURIs.J_EndsWith, content )
82                 {
83                 public boolean evalBool( String JavaDoc l, String JavaDoc r )
84                     { return l.endsWith( r ); }
85                 };
86             }
87         }
88
89     public static Expression startsWith( Expression L, String JavaDoc content, String JavaDoc modifiers )
90         {
91         if (modifiers.equals( "i" ))
92             {
93             return new DyadicLower( L, ExpressionFunctionURIs.J_startsWithInsensitive, content )
94                 {
95                 public boolean evalBool( String JavaDoc l, String JavaDoc r )
96                     { return l.toLowerCase().startsWith( r ); }
97                 };
98             }
99         else
100             {
101             return new DyadicLiteral( L, ExpressionFunctionURIs.J_startsWith, content )
102                 {
103                 public boolean evalBool( String JavaDoc l, String JavaDoc r )
104                     { return l.startsWith( r ); }
105                 };
106             }
107         }
108
109     public static Expression contains( Expression L, String JavaDoc content, String JavaDoc modifiers )
110         {
111         if (modifiers.equals( "i" ))
112             {
113             return new DyadicLower( L, ExpressionFunctionURIs.J_containsInsensitive, content )
114                 {
115                 public boolean evalBool( String JavaDoc l, String JavaDoc r )
116                     { return l.toLowerCase().indexOf( r ) > -1; }
117                 };
118             }
119         else
120             {
121             return new DyadicLiteral( L, ExpressionFunctionURIs.J_contains, content )
122                 {
123                 public boolean evalBool( String JavaDoc l, String JavaDoc r )
124                     { return l.indexOf( r ) > -1; }
125                 };
126             }
127         }
128     
129     public static boolean notSpecial( String JavaDoc pattern )
130         { return pattern.matches( "[A-Za-z0-9-_:/ ]*" ); }
131
132     public static boolean isContains( PatternLiteral pattern )
133         { return notSpecial( pattern.getPatternString() ) && iOnly( pattern.getPatternModifiers() ); }
134     
135     protected static boolean iOnly( String JavaDoc modifiers )
136         { return modifiers.equals( "" ) || modifiers.equals( "i" ); }
137     
138     public static boolean isStartsWith( PatternLiteral pattern )
139         {
140         String JavaDoc s = pattern.getPatternString();
141         return s.startsWith( "^" ) && notSpecial( s.substring( 1 ) );
142         }
143
144     public static boolean isEndsWith( PatternLiteral pattern )
145         {
146         String JavaDoc s = pattern.getPatternString();
147         return s.endsWith( "$" ) && notSpecial( s.substring( 0, s.length() - 1 ) );
148         }
149
150     }
151
152
153 /*
154 (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
155 All rights reserved.
156
157 Redistribution and use in source and binary forms, with or without
158 modification, are permitted provided that the following conditions
159 are met:
160
161 1. Redistributions of source code must retain the above copyright
162    notice, this list of conditions and the following disclaimer.
163
164 2. Redistributions in binary form must reproduce the above copyright
165    notice, this list of conditions and the following disclaimer in the
166    documentation and/or other materials provided with the distribution.
167
168 3. The name of the author may not be used to endorse or promote products
169    derived from this software without specific prior written permission.
170
171 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
173 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
174 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
175 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
176 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
177 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
178 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
179 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
180 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
181 */
Popular Tags