001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.generator;
019:
020: /**
021: * <p>For iterating through our fields.</p>
022: *
023: * @author Glen Stampoultzis (glens at apache.org)
024: */
025: public class FieldIterator {
026: protected int offset;
027:
028: public FieldIterator() {
029: }
030:
031: /**
032: * This utility function returns a fill method entry for a given field
033: *
034: * @param size - how big of an "int" or the name of the size field for a string
035: * @param type - int or string
036: */
037: public String fillDecoder(String size, String type) {
038: String javaType = RecordUtil.getType(size, type, 0);
039:
040: String result = "";
041: if (javaType.equals("short"))
042: result = "LittleEndian.getShort(data, pos + 0x"
043: + Integer.toHexString(offset) + " + offset)";
044: else if (javaType.equals("short[]"))
045: result = "LittleEndian.getShortArray(data, pos + 0x"
046: + Integer.toHexString(offset) + " + offset)";
047: else if (javaType.equals("int"))
048: result = "LittleEndian.getInt(data, pos + 0x"
049: + Integer.toHexString(offset) + " + offset)";
050: else if (javaType.equals("byte"))
051: result = "data[ pos + 0x" + Integer.toHexString(offset)
052: + " + offset ]";
053: else if (javaType.equals("double"))
054: result = "LittleEndian.getDouble(data, pos + 0x"
055: + Integer.toHexString(offset) + " + offset)";
056: else if (javaType.equals("String") && !type.equals("hbstring"))
057: result = "StringUtil.getFromUnicode(data, pos + 0x"
058: + Integer.toHexString(offset) + " + offset,("
059: + size + "-1)/2)";
060: else if (javaType.equals("String") && type.equals("hbstring"))
061: result = "StringUtil.getFromUnicodeHigh(data, pos + 0x"
062: + Integer.toHexString(offset) + " + offset, ("
063: + size + "/2))";
064:
065: try {
066: offset += Integer.parseInt(size);
067: } catch (NumberFormatException ignore) {
068: }
069: return result;
070: }
071:
072: public String fillDecoder2(int position, String name, String size,
073: String type) {
074: if (type.startsWith("custom:")) {
075: StringBuffer result = new StringBuffer();
076: result.append(RecordUtil.getFieldName(position, name, 0));
077: result.append(" = new ");
078: String javaType = type.substring(7);
079: result.append(javaType);
080: result.append("();\n");
081: result.append(" pos += ");
082: result.append(RecordUtil.getFieldName(position, name, 0))
083: .append(".fillField(data,size,pos + offset + ")
084: .append(offset).append(")");
085: return result.toString();
086: } else {
087: return RecordUtil.getFieldName(position, name, 30) + " = "
088: + fillDecoder(size, type);
089: }
090: }
091:
092: //position(),@name,@size,@type
093: public String serialiseEncoder(int fieldNumber, String fieldName,
094: String size, String type) {
095: String javaType = RecordUtil.getType(size, type, 0);
096: String javaFieldName = RecordUtil.getFieldName(fieldNumber,
097: fieldName, 0);
098:
099: String result = "";
100: if (type.startsWith("custom:"))
101: result = "pos += " + javaFieldName
102: + ".serializeField( pos + " + (offset + 4)
103: + " + offset, data );";
104: else if (javaType.equals("short"))
105: result = "LittleEndian.putShort(data, " + (offset + 4)
106: + " + offset + pos, " + javaFieldName + ");";
107: else if (javaType.equals("short[]"))
108: result = "LittleEndian.putShortArray(data, " + (offset + 4)
109: + " + offset + pos, " + javaFieldName + ");";
110: else if (javaType.equals("int"))
111: result = "LittleEndian.putInt(data, " + (offset + 4)
112: + " + offset + pos, " + javaFieldName + ");";
113: else if (javaType.equals("byte"))
114: result = "data[ " + (offset + 4) + " + offset + pos ] = "
115: + javaFieldName + ";";
116: else if (javaType.equals("double"))
117: result = "LittleEndian.putDouble(data, " + (offset + 4)
118: + " + offset + pos, " + javaFieldName + ");";
119: else if (javaType.equals("String") && !type.equals("hbstring"))
120: result = "StringUtil.putUncompressedUnicode("
121: + javaFieldName + ", data, offset + pos + 4);";
122: else if (javaType.equals("String") && type.equals("hbstring"))
123: result = "StringUtil.putUncompressedUnicodeHigh("
124: + javaFieldName + ", data, " + (offset + 4)
125: + " + offset + pos);";
126:
127: try {
128: offset += Integer.parseInt(size);
129: } catch (NumberFormatException ignore) {
130: }
131: return result;
132:
133: }
134:
135: public String calcSize(int fieldNumber, String fieldName,
136: String size, String type) {
137: String result = " + ";
138: if (type.startsWith("custom:")) {
139: String javaFieldName = RecordUtil.getFieldName(fieldNumber,
140: fieldName, 0);
141: return result + javaFieldName + ".getSize()";
142: } else if ("var".equals(size)) {
143: String javaFieldName = RecordUtil.getFieldName(fieldNumber,
144: fieldName, 0);
145: return result + " (" + javaFieldName + ".length() *2)";
146: } else if ("varword".equals(size)) {
147: String javaFieldName = RecordUtil.getFieldName(fieldNumber,
148: fieldName, 0);
149: return result + javaFieldName + ".length * 2 + 2";
150: } else {
151: return result + size;
152: }
153: }
154:
155: }
|