1 2 17 18 19 20 package org.apache.poi.generator; 21 22 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 47 public String fillDecoder(String size, String type) 48 { 49 String javaType = RecordUtil.getType(size, type, 0); 50 51 String 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 ignore) 72 { 73 } 74 return result; 75 } 76 77 public String fillDecoder2(int position, String name, String size, String type) 78 { 79 if (type.startsWith("custom:")) 80 { 81 StringBuffer result = new StringBuffer (); 82 result.append( RecordUtil.getFieldName( position, name, 0 ) ); 83 result.append( " = new " ); 84 String 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 public String serialiseEncoder( int fieldNumber, String fieldName, String size, String type) 104 { 105 String javaType = RecordUtil.getType(size, type, 0); 106 String javaFieldName = RecordUtil.getFieldName(fieldNumber,fieldName,0); 107 108 String 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 ignore) 132 { 133 } 134 return result; 135 136 } 137 138 public String calcSize( int fieldNumber, String fieldName, String size, String type) 139 { 140 String result = " + "; 141 if (type.startsWith("custom:")) 142 { 143 String javaFieldName = RecordUtil.getFieldName(fieldNumber, fieldName, 0); 144 return result + javaFieldName + ".getSize()"; 145 } 146 else if ("var".equals(size)) 147 { 148 String javaFieldName = RecordUtil.getFieldName(fieldNumber,fieldName,0); 149 return result + " ("+javaFieldName + ".length() *2)"; 150 } 151 else if ("varword".equals(size)) 152 { 153 String 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 |