KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > fun > TrimFunExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.expr.fun;
30
31 import com.caucho.amber.expr.AmberExpr;
32 import com.caucho.amber.query.QueryParser;
33 import com.caucho.util.CharBuffer;
34 import com.caucho.util.L10N;
35
36 import java.util.ArrayList JavaDoc;
37
38
39 /**
40  * TRIM function expression
41  */

42 public class TrimFunExpr extends FunExpr {
43   private static final L10N L = new L10N(TrimFunExpr.class);
44
45   public enum TrimSemantics { LEADING, TRAILING, BOTH }
46
47   private TrimSemantics _trimSemantics = TrimSemantics.BOTH;
48
49   private AmberExpr _trimChar;
50
51   /**
52    * Creates a new expression
53    */

54   protected TrimFunExpr(QueryParser parser,
55                         ArrayList JavaDoc<AmberExpr> args)
56   {
57     super(parser, "trim", args, false);
58   }
59
60   public static TrimFunExpr create(QueryParser parser,
61                                    ArrayList JavaDoc<AmberExpr> args)
62   {
63     return new TrimFunExpr(parser, args);
64   }
65
66   /**
67    * Sets the trim character.
68    */

69   public void setTrimChar(AmberExpr trimChar)
70   {
71     _trimChar = trimChar;
72   }
73
74   /**
75    * Sets the trim semantics.
76    */

77   public void setTrimSemantics(TrimSemantics trimSemantics)
78   {
79     _trimSemantics = trimSemantics;
80   }
81
82   /**
83    * Generates the where expression.
84    */

85   public void generateWhere(CharBuffer cb)
86   {
87     generateInternalWhere(cb, true);
88   }
89
90   /**
91    * Generates the (update) where expression.
92    */

93   public void generateUpdateWhere(CharBuffer cb)
94   {
95     generateInternalWhere(cb, false);
96   }
97
98   //
99
// private/protected
100

101   void generateInternalWhere(CharBuffer cb,
102                              boolean select)
103   {
104     ArrayList JavaDoc<AmberExpr> args = getArgs();
105
106     int n = args.size();
107
108     // XXX: this validation should be moved to QueryParser
109
// if (n != 1)
110
// throw new QueryParseException(L.l("expected 1 string argument for TRIM"));
111

112     if (_parser.isDerbyDBMS()) {
113
114       // Derby.
115

116       switch (_trimSemantics) {
117
118       case LEADING:
119         cb.append("ltrim(");
120         break;
121
122       case TRAILING:
123         cb.append("rtrim(");
124         break;
125
126       default:
127         cb.append("ltrim(rtrim(");
128       }
129
130       if (_trimChar != null) {
131         if (select)
132           _trimChar.generateWhere(cb);
133         else
134           _trimChar.generateUpdateWhere(cb);
135
136         cb.append(" from ");
137       }
138
139       if (select)
140         args.get(0).generateWhere(cb);
141       else
142         args.get(0).generateUpdateWhere(cb);
143
144       cb.append(')');
145
146       if (_trimSemantics == TrimSemantics.BOTH)
147         cb.append(')');
148
149       return;
150     }
151
152     cb.append("trim(");
153
154     switch (_trimSemantics) {
155
156     case LEADING:
157       cb.append("leading ");
158       break;
159
160     case TRAILING:
161       cb.append("trailing ");
162       break;
163
164     default:
165       cb.append("both ");
166     }
167
168     if (_trimChar != null) {
169       if (select)
170         _trimChar.generateWhere(cb);
171       else
172         _trimChar.generateUpdateWhere(cb);
173     }
174
175     cb.append(" from ");
176
177     if (select)
178       args.get(0).generateWhere(cb);
179     else
180       args.get(0).generateUpdateWhere(cb);
181
182     cb.append(")");
183   }
184 }
185
Popular Tags