KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > idl > FixedPointType


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.idl;
22
23 import java.io.File JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25
26 /**
27  * @author Gerald Brose
28  * @version $Id: FixedPointType.java,v 1.24 2004/05/06 12:39:58 nicolas Exp $
29  */

30
31 public class FixedPointType
32     extends TemplateTypeSpec
33 {
34     public ConstExpr digit_expr = null;
35     public ConstExpr scale_expr = null;
36     private int digits = 0;
37     private int scale = 0;
38
39     public FixedPointType(int num)
40     {
41         super(num);
42     }
43
44     public Object JavaDoc clone()
45     {
46         FixedPointType ft = new FixedPointType(new_num());
47         ft.name = this.name;
48         ft.pack_name = this.pack_name;
49         ft.digit_expr = this.digit_expr;
50         ft.digits = this.digits;
51         ft.scale_expr = this.scale_expr;
52         ft.scale = this.scale;
53         ft.included = this.included;
54         ft.typedefd = this.typedefd;
55         ft.set_token(this.get_token());
56         ft.setEnclosingSymbol(this.getEnclosingSymbol());
57         return ft;
58     }
59
60     private String JavaDoc helperName()
61     {
62         if (pack_name.length() > 0)
63         {
64             String JavaDoc s = ScopedName.unPseudoName(pack_name + "." + name);
65             if (!s.startsWith("org.omg"))
66             {
67                 return omg_package_prefix + s;
68             }
69             else
70                 return s;
71         }
72         else
73             return ScopedName.unPseudoName(name);
74     }
75
76     public String JavaDoc typeName()
77     {
78         return "java.math.BigDecimal";
79     }
80
81     public String JavaDoc getIDLTypeName()
82     {
83         return "fixed";
84     }
85
86     public TypeSpec typeSpec()
87     {
88         return this;
89     }
90
91
92     public void setEnclosingSymbol(IdlSymbol s)
93     {
94         if (enclosing_symbol != null && enclosing_symbol != s)
95             throw new RuntimeException JavaDoc("Compiler Error: trying to reassign container for " + name);
96         enclosing_symbol = s;
97     }
98
99
100     private void printHelperClass(String JavaDoc className, PrintWriter JavaDoc ps)
101     {
102         if (Environment.JAVA14 && pack_name.equals(""))
103             lexer.emit_warn
104                 ("No package defined for " + className + " - illegal in JDK1.4", token);
105         if (!pack_name.equals(""))
106             ps.println("package " + pack_name + ";");
107
108         ps.println("public" + parser.getFinalString() + " class " + className + "Helper");
109         ps.println("{");
110         ps.println("\tprivate static org.omg.CORBA.TypeCode _type = " + getTypeCodeExpression() + ";");
111
112         ps.println("\tpublic static void insert(org.omg.CORBA.Any any, java.math.BigDecimal s)");
113         ps.println("\t{");
114         ps.println("\t\tany.insert_fixed(s, type());");
115         ps.println("\t}");
116
117         ps.println("\tpublic static java.math.BigDecimal extract(org.omg.CORBA.Any any)");
118         ps.println("\t{");
119         ps.println("\t\treturn any.extract_fixed();");
120         ps.println("\t}");
121
122         ps.println("\tpublic static org.omg.CORBA.TypeCode type()");
123         ps.println("\t{");
124         ps.println("\t\treturn _type;");
125         ps.println("\t}");
126
127         printIdMethod(ps); // from IdlSymbol
128

129         /** read */
130
131         ps.println("\tpublic static java.math.BigDecimal read (final org.omg.CORBA.portable.InputStream in)");
132         ps.println("\t{");
133         ps.println("\t\tjava.math.BigDecimal result = in.read_fixed();");
134         ps.println("\t\treturn result.movePointLeft(" + scale + ");");
135         ps.println("\t}");
136
137         /** write */
138
139         ps.println("\tpublic static void write (final org.omg.CORBA.portable.OutputStream out, final java.math.BigDecimal s)");
140         ps.println("\t{");
141
142         StringBuffer JavaDoc mb = new StringBuffer JavaDoc("1");
143         for (int m = 0; m < digits - scale; m++)
144             mb.append("0");
145
146         ps.println("\t\tif (s.scale() != " + scale + ")");
147         ps.println("\t\t\tthrow new org.omg.CORBA.DATA_CONVERSION();");
148
149         ps.println("\t\tjava.math.BigDecimal max = new java.math.BigDecimal(\"" + mb.toString() + "\");");
150         ps.println("\t\tif (s.compareTo(max) != -1)");
151         ps.println("\t\t\tthrow new org.omg.CORBA.DATA_CONVERSION();");
152         ps.println("\t\tout.write_fixed(s);");
153         ps.println("\t}");
154
155         ps.println("}");
156     }
157
158     public void print(java.io.PrintWriter JavaDoc pw)
159     {
160         try
161         {
162             // write helper file
163

164             String JavaDoc fullName = helperName();
165             String JavaDoc className;
166             if (fullName.indexOf('.') > 0)
167             {
168                 pack_name = fullName.substring(0, fullName.lastIndexOf('.'));
169                 className = fullName.substring(fullName.lastIndexOf('.') + 1);
170             }
171             else
172             {
173                 pack_name = "";
174                 className = fullName;
175             }
176
177             String JavaDoc path = parser.out_dir + fileSeparator + pack_name.replace('.', fileSeparator);
178             File JavaDoc dir = new File JavaDoc(path);
179             if (!dir.exists())
180             {
181                 if (!dir.mkdirs())
182                 {
183                     org.jacorb.idl.parser.fatal_error("Unable to create " + path, null);
184                 }
185             }
186
187             String JavaDoc fname = className + "Helper.java";
188             File JavaDoc f = new File JavaDoc(dir, fname);
189
190             if (GlobalInputStream.isMoreRecentThan(f))
191             {
192                 PrintWriter JavaDoc ps = new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc(f));
193                 printHelperClass(className, ps);
194                 ps.close();
195             }
196
197         }
198         catch(java.io.IOException JavaDoc i)
199         {
200             throw new RuntimeException JavaDoc("File IO error" + i);
201         }
202     }
203
204     public void setPackage(String JavaDoc s)
205     {
206         s = parser.pack_replace(s);
207         if (pack_name.length() > 0)
208             pack_name = s + "." + pack_name;
209         else
210             pack_name = s;
211     }
212
213     public String JavaDoc toString()
214     {
215         return typeName();
216     }
217
218     public String JavaDoc holderName()
219     {
220         return "org.omg.CORBA.FixedHolder";
221     }
222
223     public String JavaDoc getTypeCodeExpression()
224     {
225         return "org.omg.CORBA.ORB.init().create_fixed_tc((short)" + digits + ",(short)" + scale + ")";
226     }
227
228
229     public String JavaDoc printReadExpression(String JavaDoc strname)
230     {
231         return helperName() + "Helper.read(" + strname + ")";
232     }
233
234     public String JavaDoc printReadStatement(String JavaDoc var_name, String JavaDoc strname)
235     {
236         String JavaDoc fixedName = null;
237         if (hashCode() > 0)
238             fixedName = "_fixed" + hashCode();
239         else
240             fixedName = "_fixed" + (-1 * hashCode());
241
242         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
243
244         sb.append("\t\tjava.math.BigDecimal " + fixedName + "=" + strname + ".read_fixed();\n");
245         sb.append("\t\t" + var_name + " = " + fixedName + ".movePointLeft(" + scale + ");\n");
246         return sb.toString();
247     }
248
249     public String JavaDoc printWriteStatement(String JavaDoc var_name, String JavaDoc strname)
250     {
251         StringBuffer JavaDoc mb = new StringBuffer JavaDoc("1");
252         for (int m = 0; m < digits - scale; m++)
253             mb.append("0");
254
255         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
256         sb.append("\n\t\tif (" + var_name + ".scale() != " + scale + ")\n");
257         sb.append("\t\t\tthrow new org.omg.CORBA.DATA_CONVERSION(\"wrong scale in fixed point value, expecting " + scale + ", got \" + " + var_name + ".scale());\n");
258
259         String JavaDoc max = null;
260         if (hashCode() > 0)
261             max = "_max" + hashCode();
262         else
263             max = "_max" + (-1 * hashCode());
264
265         sb.append("\t\tjava.math.BigDecimal " + max + "= new java.math.BigDecimal(\"" + mb.toString() + "\");\n");
266         sb.append("\t\tif (" + var_name + ".compareTo(" + max + ") != -1)\n");
267         sb.append("\t\t\tthrow new org.omg.CORBA.DATA_CONVERSION(\"more than " + digits + " digits in fixed point value\");\n");
268         sb.append("\t\t" + strname + ".write_fixed(" + var_name + ");\n");
269
270         return sb.toString();
271     }
272
273     public void parse()
274     {
275         digits = digit_expr.pos_int_const();
276         scale = scale_expr.pos_int_const();
277
278         if ((scale < 0) || (digits <= 0) || (scale > digits))
279         {
280             parser.error("Error in fixed point type " + typeName() +
281                           ", invalid format: <" + digits + "," + scale + ">");
282         }
283     }
284 }
285
Popular Tags