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.hdf.generator;
019:
020: import org.apache.poi.generator.FieldIterator;
021: import org.apache.poi.generator.RecordUtil;
022:
023: /**
024: * This class overrides FieldIterator to handle HDF specific types
025: */
026: public class HDFFieldIterator extends FieldIterator {
027:
028: public HDFFieldIterator() {
029: }
030:
031: public String fillDecoder(String size, String type) {
032:
033: String result = "";
034:
035: if (type.equals("short[]"))
036: result = "LittleEndian.getSimpleShortArray(data, 0x"
037: + Integer.toHexString(offset) + " + offset," + size
038: + ")";
039: else if (type.equals("byte[]"))
040: result = "LittleEndian.getByteArray(data, 0x"
041: + Integer.toHexString(offset) + " + offset," + size
042: + ")";
043: else if (type.equals("BorderCode"))
044: result = "new BorderCode(data, 0x"
045: + Integer.toHexString(offset) + " + offset)";
046: else if (type.equals("DateAndTime"))
047: result = "new DateAndTime(data, 0x"
048: + Integer.toHexString(offset) + " + offset)";
049: else if (size.equals("2"))
050: result = "LittleEndian.getShort(data, 0x"
051: + Integer.toHexString(offset) + " + offset)";
052: else if (size.equals("4"))
053: result = "LittleEndian.getInt(data, 0x"
054: + Integer.toHexString(offset) + " + offset)";
055: else if (size.equals("1"))
056: result = "data[ 0x" + Integer.toHexString(offset)
057: + " + offset ]";
058: else if (type.equals("double"))
059: result = "LittleEndian.getDouble(data, 0x"
060: + Integer.toHexString(offset) + " + offset)";
061:
062: try {
063: offset += Integer.parseInt(size);
064: } catch (NumberFormatException ignore) {
065: }
066: return result;
067: }
068:
069: public String serialiseEncoder(int fieldNumber, String fieldName,
070: String size, String type) {
071: //String javaType = RecordUtil.getType(size, type, 0);
072: String javaFieldName = RecordUtil.getFieldName(fieldNumber,
073: fieldName, 0);
074:
075: String result = "";
076:
077: if (type.equals("short[]"))
078: result = "LittleEndian.putShortArray(data, 0x"
079: + Integer.toHexString(offset) + " + offset, "
080: + javaFieldName + ");";
081: else if (type.equals("byte[]"))
082: result = "System.arraycopy(" + javaFieldName
083: + ", 0, data, 0x" + Integer.toHexString(offset)
084: + " + offset, " + javaFieldName + ".length);";
085: else if (type.equals("BorderCode"))
086: result = javaFieldName + ".serialize(data, 0x"
087: + Integer.toHexString(offset) + " + offset);";
088: else if (type.equals("DateAndTime"))
089: result = javaFieldName + ".serialize(data, 0x"
090: + Integer.toHexString(offset) + " + offset);";
091: else if (size.equals("2"))
092: result = "LittleEndian.putShort(data, 0x"
093: + Integer.toHexString(offset)
094: + " + offset, (short)" + javaFieldName + ");";
095: else if (size.equals("4"))
096: result = "LittleEndian.putInt(data, 0x"
097: + Integer.toHexString(offset) + " + offset, "
098: + javaFieldName + ");";
099: else if (size.equals("1"))
100: result = "data[ 0x" + Integer.toHexString(offset)
101: + " + offset] = " + javaFieldName + ";";
102: else if (type.equals("double"))
103: result = "LittleEndian.putDouble(data, 0x"
104: + Integer.toHexString(offset) + " + offset, "
105: + javaFieldName + ");";
106:
107: try {
108: offset += Integer.parseInt(size);
109: } catch (NumberFormatException ignore) {
110: }
111: return result;
112:
113: }
114:
115: }
|