1 6 package com.hp.hpl.jena.graph.query; 7 8 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 front( String 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 52 public static abstract class DyadicLiteral extends Dyadic 53 { 54 public DyadicLiteral( Expression L, String F, String R ) 55 { super( L, F, new Expression.Fixed( R ) ); } 56 57 public boolean evalBool( Object l, Object r ) 58 { return evalBool( l.toString(), r.toString() ); } 59 60 protected abstract boolean evalBool( String l, String r ); 61 } 62 63 public static abstract class DyadicLower extends DyadicLiteral 64 { 65 public DyadicLower( Expression L, String F, String R ) 66 { super( L, F, R.toLowerCase() ); } 67 } 68 69 public static Expression endsWith( Expression L, String content, String modifiers ) 70 { 71 if (modifiers.equals( "i" )) 72 { 73 return new DyadicLower( L, ExpressionFunctionURIs.J_endsWithInsensitive, content ) 74 { 75 public boolean evalBool( String l, String 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 l, String r ) 84 { return l.endsWith( r ); } 85 }; 86 } 87 } 88 89 public static Expression startsWith( Expression L, String content, String modifiers ) 90 { 91 if (modifiers.equals( "i" )) 92 { 93 return new DyadicLower( L, ExpressionFunctionURIs.J_startsWithInsensitive, content ) 94 { 95 public boolean evalBool( String l, String 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 l, String r ) 104 { return l.startsWith( r ); } 105 }; 106 } 107 } 108 109 public static Expression contains( Expression L, String content, String modifiers ) 110 { 111 if (modifiers.equals( "i" )) 112 { 113 return new DyadicLower( L, ExpressionFunctionURIs.J_containsInsensitive, content ) 114 { 115 public boolean evalBool( String l, String 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 l, String r ) 124 { return l.indexOf( r ) > -1; } 125 }; 126 } 127 } 128 129 public static boolean notSpecial( String 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 modifiers ) 136 { return modifiers.equals( "" ) || modifiers.equals( "i" ); } 137 138 public static boolean isStartsWith( PatternLiteral pattern ) 139 { 140 String s = pattern.getPatternString(); 141 return s.startsWith( "^" ) && notSpecial( s.substring( 1 ) ); 142 } 143 144 public static boolean isEndsWith( PatternLiteral pattern ) 145 { 146 String s = pattern.getPatternString(); 147 return s.endsWith( "$" ) && notSpecial( s.substring( 0, s.length() - 1 ) ); 148 } 149 150 } 151 152 153 | Popular Tags |