KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > expressions > ExpressionJavaPrinter


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, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.expressions;
23
24 import java.io.*;
25 import java.math.*;
26 import java.util.Calendar JavaDoc;
27 import oracle.toplink.essentials.internal.databaseaccess.*;
28 import oracle.toplink.essentials.internal.helper.ClassConstants;
29 import oracle.toplink.essentials.internal.helper.ConversionManager;
30
31 /**
32  * <p><b>Purpose</b>: Expression Java printer.
33  * <p><b>Responsibilities</b>:<ul>
34  * <li> Print an expression in Java format.
35  * <li> Used in project class generation.
36  * </ul>
37  * @since TOPLink10.1.3
38  */

39 public class ExpressionJavaPrinter {
40
41     // What we write on
42
protected StringWriter writer;
43
44     // ExpressionBuilder string, e.g. expBuilder
45
protected String JavaDoc builderString;
46
47     // Database platform for the project
48
protected DatabasePlatform platform;
49
50     public ExpressionJavaPrinter(String JavaDoc builderString, StringWriter writer, DatabasePlatform platform) {
51         this.builderString = builderString;
52         this.writer = writer;
53         this.platform = platform;
54     }
55
56     public String JavaDoc getBuilderString() {
57         return builderString;
58     }
59
60     public DatabasePlatform getPlatform() {
61         return platform;
62     }
63
64     public StringWriter getWriter() {
65         return writer;
66     }
67
68     public void printString(String JavaDoc value) {
69         getWriter().write(value);
70    }
71
72     //Used to convert a java object to java print format
73
public void printJava(Object JavaDoc object) {
74         if (object == null) {
75             printString("null");
76         } else if (object.getClass() == ClassConstants.STRING) {
77             printString("\"");
78             printString((String JavaDoc)object);
79             printString("\"");
80         } else if (object instanceof Calendar JavaDoc) {
81             printString("new java.util.Date(");
82             printString(String.valueOf(((Calendar JavaDoc)object).getTimeInMillis()));
83             printString("L)");
84         } else if (object.getClass() == ClassConstants.TIMESTAMP) {
85             printString("new java.sql.Timestamp(");
86             printString(String.valueOf(((java.sql.Timestamp JavaDoc)object).getTime()));
87             printString("L)");
88         } else if (object.getClass() == ClassConstants.SQLDATE) {
89             printString("new java.sql.Date(");
90             printString(String.valueOf(((java.sql.Date JavaDoc)object).getTime()));
91             printString("L)");
92         } else if (object.getClass() == ClassConstants.TIME) {
93             printString("new java.sql.Time(");
94             printString(String.valueOf(((java.sql.Time JavaDoc)object).getTime()));
95             printString("L)");
96         } else if (object.getClass() == ClassConstants.UTILDATE) {
97             printString("new java.util.Date(");
98             printString(String.valueOf(((java.util.Date JavaDoc)object).getTime()));
99             printString("L)");
100         } else if (object.getClass() == ClassConstants.BYTE) {
101             printByte((Byte JavaDoc)object);
102         } else if (object.getClass() == ClassConstants.APBYTE) {
103             printString("new byte[] {");
104             byte[] bytes = (byte[])object;
105             if (bytes.length > 0) {
106                 printString(String.valueOf((int)bytes[0]));
107                 for (int index = 1; index < bytes.length; index++) {
108                     printString(",");
109                     printString(String.valueOf((int)bytes[index]));
110                 }
111             }
112             printString("}");
113         } else if (object.getClass() == ClassConstants.ABYTE) {
114             printString("new Byte[] {");
115             Byte JavaDoc[] bytes = (Byte JavaDoc[])object;
116             if (bytes.length > 0) {
117                 printByte(bytes[0]);
118                 for (int index = 1; index < bytes.length; index++) {
119                     printString(",");
120                     printByte(bytes[index]);
121                 }
122             }
123             printString("}");
124         } else if (object.getClass() == ClassConstants.CHAR) {
125             printCharacter((Character JavaDoc)object);
126         } else if (object.getClass() == ClassConstants.APCHAR) {
127             printString("new char[] {");
128             char[] chars = (char[])object;
129             if (chars.length > 0) {
130                 printString("'");
131                 printString(String.valueOf(chars[0]));
132                 printString("'");
133                 for (int index = 1; index < chars.length; index++) {
134                     printString(",");
135                     printString("'");
136                     printString(String.valueOf(chars[index]));
137                     printString("'");
138                 }
139             }
140             printString("}");
141         } else if (object.getClass() == ClassConstants.ACHAR) {
142             printString("new Character[] {");
143             Character JavaDoc[] chars = (Character JavaDoc[])object;
144             if (chars.length > 0) {
145                 printCharacter(chars[0]);
146                 for (int index = 1; index < chars.length; index++) {
147                     printString(",");
148                     printCharacter(chars[index]);
149                 }
150             }
151             printString("}");
152         } else if (object.getClass() == ClassConstants.BIGDECIMAL) {
153             printString("new java.math.BigDecimal(\"");
154             printString(((BigDecimal)object).toString());
155             printString("\")");
156         } else if (object.getClass() == ClassConstants.BIGINTEGER) {
157             printString("new java.math.BigInteger(\"");
158             printString(((BigInteger)object).toString());
159             printString("\")");
160         } else {
161             printString((String JavaDoc)ConversionManager.getDefaultManager().convertObject(object, String JavaDoc.class));
162         }
163         
164     }
165     
166     public void printByte(Byte JavaDoc aByte) {
167             printString("new Byte((byte)");
168             printString((aByte).toString());
169             printString(")");
170     }
171
172     public void printCharacter(Character JavaDoc aCharacter) {
173             printString("new Character('");
174             printString((aCharacter).toString());
175             printString("')");
176     }
177 }
178
Popular Tags