KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > generator > RecordUtil


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.generator;
20
21 import java.util.StringTokenizer JavaDoc;
22
23 /**
24  * Helper functions for the record transformations.
25  *
26  * @author Glen Stampoultzis (glens at apache.org)
27  * @author Andrew C. Oliver (acoliver at apache dot org)
28  */

29 public class RecordUtil
30 {
31     private static final String JavaDoc CR = "\n";
32
33     public static String JavaDoc getFieldName(int position, String JavaDoc name, int padTo)
34     {
35         StringBuffer JavaDoc fieldName = new StringBuffer JavaDoc("field_" + position + "_");
36         toIdentifier(name, fieldName);
37         pad(fieldName, padTo);
38
39         return fieldName.toString();
40     }
41
42     protected static StringBuffer JavaDoc pad(StringBuffer JavaDoc fieldName, int padTo)
43     {
44         for (int i = fieldName.length(); i < padTo; i++)
45             fieldName.append(' ');
46         return fieldName;
47     }
48
49     public static String JavaDoc getFieldName(String JavaDoc name, int padTo)
50     {
51         StringBuffer JavaDoc fieldName = new StringBuffer JavaDoc();
52         toIdentifier(name, fieldName);
53         pad(fieldName, padTo);
54
55         return fieldName.toString();
56     }
57
58     public static String JavaDoc getFieldName1stCap(String JavaDoc name, int padTo)
59     {
60         StringBuffer JavaDoc fieldName = new StringBuffer JavaDoc();
61         toIdentifier(name, fieldName);
62         fieldName.setCharAt(0, Character.toUpperCase(fieldName.charAt(0)));
63         pad(fieldName, padTo);
64
65         return fieldName.toString();
66     }
67
68     public static String JavaDoc clone(String JavaDoc name, String JavaDoc type, int pos) {
69         StringBuffer JavaDoc fieldName = new StringBuffer JavaDoc();
70         toIdentifier(name, fieldName);
71
72         String JavaDoc javaFieldName = getFieldName(pos, name, 0);
73
74         if (type.startsWith("custom:"))
75         {
76             String JavaDoc javaType = type.substring(7);
77             return "rec." + javaFieldName + " = ((" + javaType + ")" + javaFieldName + ".clone());";
78         }
79         else
80         {
81             return "rec." + javaFieldName + " = " + javaFieldName;
82         }
83     }
84
85     public static String JavaDoc initializeText(String JavaDoc size, String JavaDoc type)
86     {
87         // Removed because of wierdo initialization sequence in constructors.
88
// if (type.startsWith("custom:"))
89
// {
90
// String javaType = type.substring( 7 );
91
// return " = new " + javaType + "()";
92
// }
93
// else
94
// {
95
// return "";
96
// }
97
return "";
98     }
99
100     private static void toIdentifier(String JavaDoc name, StringBuffer JavaDoc fieldName)
101     {
102         for (int i = 0; i < name.length(); i++)
103         {
104             if (name.charAt(i) == ' ')
105                 fieldName.append(Character.toUpperCase(name.charAt(++i)));
106             else
107                 fieldName.append(name.charAt(i));
108         }
109     }
110
111     private static void toConstIdentifier(String JavaDoc name, StringBuffer JavaDoc fieldName)
112     {
113         for (int i = 0; i < name.length(); i++)
114         {
115             if (name.charAt(i) == ' ')
116                 fieldName.append('_');
117             else
118                 fieldName.append(Character.toUpperCase(name.charAt(i)));
119         }
120     }
121
122     public static String JavaDoc getType(String JavaDoc size, String JavaDoc type, int padTo)
123     {
124
125         boolean wholeNumber = type.equals("bits") || type.equals("int");
126         if (wholeNumber && "1".equals(size))
127             return pad(new StringBuffer JavaDoc("byte"), padTo).toString();
128         else if (wholeNumber && "2".equals(size))
129             return pad(new StringBuffer JavaDoc("short"), padTo).toString();
130         else if (type.equals("int") && "varword".equals(size))
131             return pad(new StringBuffer JavaDoc("short[]"), padTo).toString();
132         else if (wholeNumber && "4".equals(size))
133             return pad(new StringBuffer JavaDoc("int"), padTo).toString();
134         else if (type.equals("float") && "8".equals(size))
135             return pad(new StringBuffer JavaDoc("double"), padTo).toString();
136         else if (type.equals("string"))
137             return pad(new StringBuffer JavaDoc("String"), padTo).toString();
138         else if (type.equals("hbstring"))
139             return pad(new StringBuffer JavaDoc("String"), padTo).toString();
140         else if (type.startsWith("custom:"))
141         {
142             int pos = type.lastIndexOf('.');
143             return pad(new StringBuffer JavaDoc(type.substring(pos+1)), padTo)
144                     .toString();
145         }
146
147         return "short"; // if we don't know, default to short
148
}
149
150     public static String JavaDoc getType1stCap(String JavaDoc size, String JavaDoc type, int padTo)
151     {
152         StringBuffer JavaDoc result;
153         boolean numeric = type.equals("bits") || type.equals("int");
154         if (numeric && "1".equals(size))
155             result = pad(new StringBuffer JavaDoc("byte"), padTo);
156         else if (type.equals("int") && "varword".equals(size))
157             result = pad(new StringBuffer JavaDoc("short[]"), padTo);
158         else if (numeric && "2".equals(size))
159             result = pad(new StringBuffer JavaDoc("short"), padTo);
160         else if (type.equals("string"))
161             result = pad(new StringBuffer JavaDoc("String"), padTo);
162         else if (type.equals("hbstring"))
163             result = pad(new StringBuffer JavaDoc("HighByteString"), padTo);
164         
165         else
166             return "";
167
168         result.setCharAt(0, Character.toUpperCase(result.charAt(0)));
169
170         return result.toString();
171     }
172
173     public static String JavaDoc getMask(int bit)
174     {
175     //if (bit > 1) bit--;
176
int mask = (int)Math.pow(2, bit);
177
178         return "0x" + Integer.toHexString(mask);
179     }
180
181     public static String JavaDoc getConstName(String JavaDoc parentName, String JavaDoc constName, int padTo)
182     {
183         StringBuffer JavaDoc fieldName = new StringBuffer JavaDoc();
184         toConstIdentifier(parentName, fieldName);
185         fieldName.append('_');
186         toConstIdentifier(constName, fieldName);
187         pad(fieldName, padTo);
188         return fieldName.toString();
189     }
190     
191     /**
192      * @return a byte array formatted string from a HexDump formatted string
193      * for example (byte)0x00,(byte)0x01 instead of 00 01
194      */

195     public static String JavaDoc getByteArrayString(String JavaDoc data) {
196         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(data);
197         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
198         
199         while (tokenizer.hasMoreTokens()) {
200             retval.append("(byte)0x").append(tokenizer.nextToken());
201             if (tokenizer.hasMoreTokens()) {
202                 retval.append(",");
203             }
204         }
205         return retval.toString();
206     }
207
208     public static String JavaDoc getToString(String JavaDoc fieldName, String JavaDoc type, String JavaDoc size) {
209         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
210         result.append(" buffer.append(\" .");
211         result.append(getFieldName(fieldName, 20));
212         result.append(" = \")" + CR);
213         if (type.equals("string") == false
214                 && type.equals("hbstring") == false
215                 && type.equals("float") == false
216 // && type.equals("varword") == false
217
&& size.equals("varword") == false
218                 && type.startsWith("custom:") == false)
219         {
220             result.append(" .append(\"0x\")");
221             result.append(".append(HexDump.toHex( ");
222 // result.append(getType(size, type, 0));
223
result.append(" get");
224             result.append(getFieldName1stCap(fieldName, 0));
225             result.append(" ()))" + CR);
226         }
227         result.append(" .append(\" (\").append( get");
228         result.append(getFieldName1stCap(fieldName,0));
229         result.append("() ).append(\" )\");");
230         return result.toString();
231     }
232
233     public static String JavaDoc getRecordId(String JavaDoc recordName, String JavaDoc excelName)
234     {
235         if (excelName == null || excelName.equals(""))
236             return recordName;
237         else
238             return excelName;
239     }
240 }
241
Popular Tags