KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.apache.poi.generator;
21
22 /**
23  * <p>For iterating through our fields.</p>
24  *
25  * @author Glen Stampoultzis (glens at apache.org)
26  */

27 public class FieldIterator
28 {
29     protected int offset;
30
31     public FieldIterator()
32     {
33     }
34
35     public void init(org.apache.xalan.extensions.XSLProcessorContext context,
36                      org.apache.xalan.templates.ElemExtensionCall extElem)
37     {
38         offset = 0;
39     }
40
41     /**
42      * This utility function returns a fill method entry for a given field
43      *
44      * @param size - how big of an "int" or the name of the size field for a string
45      * @param type - int or string
46      */

47     public String JavaDoc fillDecoder(String JavaDoc size, String JavaDoc type)
48     {
49         String JavaDoc javaType = RecordUtil.getType(size, type, 0);
50
51         String JavaDoc result = "";
52         if (javaType.equals("short"))
53             result = "LittleEndian.getShort(data, pos + 0x" + Integer.toHexString(offset) + " + offset)";
54         else if (javaType.equals("short[]"))
55             result = "LittleEndian.getShortArray(data, pos + 0x" + Integer.toHexString(offset) + " + offset)";
56         else if (javaType.equals("int"))
57             result = "LittleEndian.getInt(data, pos + 0x" + Integer.toHexString(offset) + " + offset)";
58         else if (javaType.equals("byte"))
59             result = "data[ pos + 0x" + Integer.toHexString(offset) + " + offset ]";
60         else if (javaType.equals("double"))
61             result = "LittleEndian.getDouble(data, pos + 0x" + Integer.toHexString(offset) + " + offset)";
62         else if (javaType.equals("String") && !type.equals("hbstring"))
63             result = "StringUtil.getFromUnicode(data, pos + 0x" + Integer.toHexString(offset) + " + offset,("+ size + "-1)/2)";
64         else if (javaType.equals("String") && type.equals("hbstring"))
65             result = "StringUtil.getFromUnicodeHigh(data, pos + 0x" + Integer.toHexString(offset) + " + offset, ("+ size+"/2))";
66
67         try
68         {
69             offset += Integer.parseInt(size);
70         }
71         catch (NumberFormatException JavaDoc ignore)
72         {
73         }
74         return result;
75     }
76
77     public String JavaDoc fillDecoder2(int position, String JavaDoc name, String JavaDoc size, String JavaDoc type)
78     {
79         if (type.startsWith("custom:"))
80         {
81             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
82             result.append( RecordUtil.getFieldName( position, name, 0 ) );
83             result.append( " = new " );
84             String JavaDoc javaType = type.substring( 7 );
85             result.append(javaType);
86             result.append( "();\n");
87             result.append( " pos += " );
88             result.append(RecordUtil.getFieldName(position, name, 0))
89                     .append(".fillField(data,size,pos + offset + ")
90                     .append(offset)
91                     .append(")");
92             return result.toString();
93         }
94         else
95         {
96             return RecordUtil.getFieldName(position, name, 30) +
97                     " = " + fillDecoder(size, type);
98         }
99     }
100
101
102     //position(),@name,@size,@type
103
public String JavaDoc serialiseEncoder( int fieldNumber, String JavaDoc fieldName, String JavaDoc size, String JavaDoc type)
104     {
105         String JavaDoc javaType = RecordUtil.getType(size, type, 0);
106         String JavaDoc javaFieldName = RecordUtil.getFieldName(fieldNumber,fieldName,0);
107
108         String JavaDoc result = "";
109         if (type.startsWith("custom:"))
110             result = "pos += " + javaFieldName + ".serializeField( pos + " + (offset+4) + " + offset, data );";
111         else if (javaType.equals("short"))
112             result = "LittleEndian.putShort(data, " + (offset+4) + " + offset + pos, " + javaFieldName + ");";
113         else if (javaType.equals("short[]"))
114             result = "LittleEndian.putShortArray(data, " + (offset+4) + " + offset + pos, " + javaFieldName + ");";
115         else if (javaType.equals("int"))
116             result = "LittleEndian.putInt(data, " + (offset+4) + " + offset + pos, " + javaFieldName + ");";
117         else if (javaType.equals("byte"))
118             result = "data[ " + (offset+4) + " + offset + pos ] = " + javaFieldName + ";";
119         else if (javaType.equals("double"))
120             result = "LittleEndian.putDouble(data, " + (offset+4) + " + offset + pos, " + javaFieldName + ");";
121         else if (javaType.equals("String") && !type.equals("hbstring"))
122             result = "StringUtil.putUncompressedUnicode("+ javaFieldName +", data, offset + pos + 4);";
123         else if (javaType.equals("String") && type.equals("hbstring"))
124             result = "StringUtil.putUncompressedUnicodeHigh("+ javaFieldName +", data, "+(offset+4)+" + offset + pos);";
125
126
127         try
128         {
129             offset += Integer.parseInt(size);
130         }
131         catch (NumberFormatException JavaDoc ignore)
132         {
133         }
134         return result;
135
136     }
137
138     public String JavaDoc calcSize( int fieldNumber, String JavaDoc fieldName, String JavaDoc size, String JavaDoc type)
139     {
140         String JavaDoc result = " + ";
141         if (type.startsWith("custom:"))
142         {
143             String JavaDoc javaFieldName = RecordUtil.getFieldName(fieldNumber, fieldName, 0);
144             return result + javaFieldName + ".getSize()";
145         }
146         else if ("var".equals(size))
147         {
148             String JavaDoc javaFieldName = RecordUtil.getFieldName(fieldNumber,fieldName,0);
149             return result + " ("+javaFieldName + ".length() *2)";
150         }
151         else if ("varword".equals(size))
152         {
153             String JavaDoc javaFieldName = RecordUtil.getFieldName(fieldNumber,fieldName,0);
154             return result + javaFieldName + ".length * 2 + 2";
155         } else
156         {
157             return result + size;
158         }
159     }
160
161
162 }
163
Popular Tags