KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > types > ValuePattern


1 package com.quadcap.sql.types;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import antlr.RecognitionException;
47
48 import com.quadcap.util.Debug;
49
50 /**
51  * A <b>LIKE-pattern</b> value.
52  *
53  * @author Stan Bailes
54  */

55 public class ValuePattern extends Value implements Externalizable JavaDoc {
56     String JavaDoc pattern;
57     char escape = 0;
58     public static final String JavaDoc defaultEscape = "\\";
59
60     public ValuePattern() {}
61     
62     public ValuePattern(String JavaDoc pattern, String JavaDoc esc) throws ValueException {
63     this.pattern = pattern;
64     if (esc != null) {
65         if (esc.length() != 1) {
66         throw new ValueException(
67             "Pattern escape must be a single character");
68         }
69         escape = esc.charAt(0);
70     }
71     }
72
73     public Value binop(int op, Value l) throws ValueException {
74     return l.binop(op, this);
75     }
76
77     public static final Value binop(int op, ValueString l, ValuePattern r)
78     throws ValueException
79     {
80     if (op != Op.LIKE) {
81         throw new ValueException("Not implemented: " +
82                      Op.toString(op));
83     }
84     String JavaDoc ls = l.stringValue();
85     String JavaDoc rs = r.pattern;
86     return new ValueBoolean(pMatch(ls, 0, rs, 0, r.escape));
87     }
88
89     static final char getChar(final String JavaDoc s, int i) {
90         if (isCaseSensitive) {
91             return s.charAt(i);
92         } else {
93             return Character.toLowerCase(s.charAt(i));
94         }
95     }
96
97     /**
98      * Match the string(a) to the pattern(b)
99      */

100     static boolean pMatch(String JavaDoc va, int a, String JavaDoc vb, int b, char escape)
101         throws ValueException
102     {
103     while (b < vb.length()) {
104         char p = getChar(vb, b++);
105         switch (p) {
106         case '%':
107                 if (b >= vb.length()) return true;
108         while (a < va.length()) {
109             if (pMatch(va, a++, vb, b, escape)) return true;
110         }
111         return false;
112         case '_':
113         if (a++ >= va.length()) return false;
114         break;
115         default:
116                 if (a >= va.length()) return false;
117         if (p == escape) {
118             if (b >= vb.length()) {
119                         throw new ValueException("Invalid escape sequence");
120                     }
121             p = getChar(vb, b++);
122                     if (p != '_' && p != '%' && p != escape) {
123                         throw new ValueException("Invalid escape sequence");
124                     }
125         }
126                 if (getChar(va, a++) != p) {
127                     return false;
128                 }
129         }
130     }
131     return a >= va.length();
132     }
133         
134     public void readExternal(ObjectInput JavaDoc in)
135     throws IOException JavaDoc, ClassNotFoundException JavaDoc
136     {
137     pattern = (String JavaDoc)in.readObject();
138     escape = in.readChar();
139     }
140     
141     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
142     out.writeObject(pattern);
143     out.writeChar(escape);
144     }
145
146     public String JavaDoc toString() {
147     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("'");
148     sb.append(pattern);
149     sb.append('\'');
150     if (escape != 0) {
151         sb.append(" ESCAPE '");
152         sb.append(escape);
153         sb.append('\'');
154     }
155     return sb.toString();
156     }
157
158     public Object JavaDoc asJavaObject() {
159     return pattern;
160     }
161
162     public void fromJavaObject(Object JavaDoc obj) throws ValueException {
163     throw new ValueException("bad type: " + obj);
164     }
165
166     public Type getType() {
167     return TypeVarChar.typeVarChar;
168     }
169
170     public void serializeKey(KeyStream out) throws IOException JavaDoc {
171     throw new IOException JavaDoc("Can't use 'pattern' as key");
172     }
173 }
174
Popular Tags