KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hdf > generator > HDFRecordUtil


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.hdf.generator;
20
21 import org.apache.poi.generator.RecordUtil;
22
23 /**
24  * This class overrides RecordUtil to handle HDF specific types
25  */

26 public class HDFRecordUtil extends RecordUtil
27 {
28
29     public HDFRecordUtil()
30     {
31     }
32
33     public static String JavaDoc getType(String JavaDoc size, String JavaDoc type, int padTo)
34     {
35
36         return type;
37     }
38
39     public static String JavaDoc getType1stCap(String JavaDoc size, String JavaDoc type, int padTo)
40     {
41         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
42         result.append(type);
43         result = pad(result, padTo);
44         result.setCharAt(0, Character.toUpperCase(result.charAt(0)));
45
46         return result.toString();
47     }
48
49     public static String JavaDoc getBitFieldFunction(String JavaDoc name, String JavaDoc bitMask, String JavaDoc parentType, String JavaDoc withType)
50     {
51         String JavaDoc type = getBitFieldType(name, bitMask, parentType);
52
53         String JavaDoc retVal = new String JavaDoc();
54         if(withType.equals("true"))
55         {
56             retVal = type + " ";
57         }
58         if(type.equals("boolean"))
59         {
60             retVal += "is" + getFieldName1stCap(name, 0);
61         }
62         else
63         {
64             retVal +="get" + getFieldName1stCap(name, 0);
65         }
66         return retVal;
67
68     }
69
70     public static String JavaDoc getBitFieldGet(String JavaDoc name, String JavaDoc bitMask, String JavaDoc parentType, String JavaDoc parentField)
71     {
72         String JavaDoc type = getBitFieldType(name, bitMask, parentType);
73
74         String JavaDoc retVal = null;
75
76         if(type.equals("boolean"))
77             retVal = name + ".isSet(" + parentField + ");";
78         else
79             retVal = "( " + type + " )" + name + ".getValue(" + parentField + ");";
80
81         return retVal;
82
83     }
84     public static String JavaDoc getBitFieldSet(String JavaDoc name, String JavaDoc bitMask, String JavaDoc parentType, String JavaDoc parentField)
85     {
86         String JavaDoc type = getBitFieldType(name, bitMask, parentType);
87
88         String JavaDoc retVal = null;
89
90         if(type.equals("boolean"))
91             retVal = "(" + parentType + ")" + getFieldName(name, 0) + ".setBoolean(" + parentField + ", value)";
92         else
93             retVal = "(" + parentType + ")" + getFieldName(name, 0) + ".setValue(" + parentField + ", value)";
94         return retVal;
95     }
96
97     public static String JavaDoc getBitFieldType(String JavaDoc name, String JavaDoc bitMask, String JavaDoc parentType)
98     {
99         byte parentSize = 0;
100         byte numBits = 0;
101         int mask = (int)Long.parseLong(bitMask.substring(2), 16);
102
103         if (parentType.equals("byte"))
104             parentSize = 8;
105         else if (parentType.equals("short"))
106             parentSize = 16;
107         else if (parentType.equals("int"))
108             parentSize = 32;
109
110         for (int x = 0; x < parentSize; x++)
111         {
112             int temp = mask;
113             numBits += (temp >> x) & 0x1;
114         }
115
116         if(numBits == 1)
117         {
118             return "boolean";
119         }
120         else if (numBits < 8)
121         {
122             return "byte";
123         }
124         else if (numBits < 16)
125         {
126             return "short";
127         }
128         else
129         {
130             return "int";
131         }
132     }
133
134
135 }
136
Popular Tags