KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > parsing > TrimNode


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.parsing;
23
24 import oracle.toplink.essentials.expressions.*;
25
26 /**
27  * INTERNAL
28  * <p><b>Purpose</b>: Represent a TRIM
29  * <p><b>Responsibilities</b>:<ul>
30  * <li> Generate the correct expression for TRIM
31  * </ul>
32  */

33 public class TrimNode extends StringFunctionNode {
34
35     private Node trimChar;
36     private boolean leading;
37     private boolean trailing;
38     private boolean both;
39
40     /**
41      * TrimNode constructor.
42      */

43     public TrimNode() {
44         super();
45     }
46
47     /**
48      * INTERNAL
49      * Validate node and calculate its type.
50      */

51     public void validate(ParseTreeContext context) {
52         TypeHelper typeHelper = context.getTypeHelper();
53         if (left != null) {
54             left.validate(context);
55             left.validateParameter(context, typeHelper.getStringType());
56         }
57         if (trimChar != null) {
58             trimChar.validate(context);
59             trimChar.validateParameter(context, typeHelper.getCharType());
60         }
61         setType(typeHelper.getStringType());
62     }
63
64     /**
65      * INTERNAL
66      * Generate the TopLink expression for this node
67      */

68     public Expression generateExpression(GenerationContext context) {
69         Expression whereClause = getLeft().generateExpression(context);
70         if (leading) {
71             // use leftTrim
72
if (trimChar != null) {
73                 Expression trimCharExpr = trimChar.generateExpression(context);
74                 whereClause = whereClause.leftTrim(trimCharExpr);
75             } else {
76                 whereClause = whereClause.leftTrim();
77             }
78         } else if (trailing) {
79             if (trimChar != null) {
80                 Expression trimCharExpr = trimChar.generateExpression(context);
81                 whereClause = whereClause.rightTrim(trimCharExpr);
82             } else {
83                 whereClause = whereClause.rightTrim();
84             }
85         } else {
86             if (trimChar != null) {
87                 Expression trimCharExpr = trimChar.generateExpression(context);
88                 whereClause = whereClause.leftTrim(trimCharExpr).rightTrim(trimCharExpr);
89             } else {
90                 whereClause = whereClause.leftTrim().rightTrim();
91             }
92         }
93         return whereClause;
94     }
95
96     /** */
97     public void setTrimChar(Node trimChar) {
98         this.trimChar = trimChar;
99     }
100
101     /** */
102     public boolean isLeading() {
103         return leading;
104     }
105
106     /** */
107     public void setLeading(boolean newLeading) {
108         this.leading = newLeading;
109     }
110     
111     /** */
112     public boolean isTrailing() {
113         return trailing;
114     }
115     
116     /** */
117     public void setTrailing(boolean newTrailing) {
118         this.trailing = newTrailing;
119     }
120     
121     /** */
122     public boolean isBoth() {
123         return both;
124     }
125     
126     /** */
127     public void setBoth(boolean newBoth) {
128         this.both = newBoth;
129     }
130     
131 }
132
Popular Tags