1 2 17 18 19 package org.apache.poi.generator; 20 21 import java.util.StringTokenizer ; 22 23 29 public class RecordUtil 30 { 31 private static final String CR = "\n"; 32 33 public static String getFieldName(int position, String name, int padTo) 34 { 35 StringBuffer fieldName = new StringBuffer ("field_" + position + "_"); 36 toIdentifier(name, fieldName); 37 pad(fieldName, padTo); 38 39 return fieldName.toString(); 40 } 41 42 protected static StringBuffer pad(StringBuffer fieldName, int padTo) 43 { 44 for (int i = fieldName.length(); i < padTo; i++) 45 fieldName.append(' '); 46 return fieldName; 47 } 48 49 public static String getFieldName(String name, int padTo) 50 { 51 StringBuffer fieldName = new StringBuffer (); 52 toIdentifier(name, fieldName); 53 pad(fieldName, padTo); 54 55 return fieldName.toString(); 56 } 57 58 public static String getFieldName1stCap(String name, int padTo) 59 { 60 StringBuffer fieldName = new StringBuffer (); 61 toIdentifier(name, fieldName); 62 fieldName.setCharAt(0, Character.toUpperCase(fieldName.charAt(0))); 63 pad(fieldName, padTo); 64 65 return fieldName.toString(); 66 } 67 68 public static String clone(String name, String type, int pos) { 69 StringBuffer fieldName = new StringBuffer (); 70 toIdentifier(name, fieldName); 71 72 String javaFieldName = getFieldName(pos, name, 0); 73 74 if (type.startsWith("custom:")) 75 { 76 String javaType = type.substring(7); 77 return "rec." + javaFieldName + " = ((" + javaType + ")" + javaFieldName + ".clone());"; 78 } 79 else 80 { 81 return "rec." + javaFieldName + " = " + javaFieldName; 82 } 83 } 84 85 public static String initializeText(String size, String type) 86 { 87 return ""; 98 } 99 100 private static void toIdentifier(String name, StringBuffer fieldName) 101 { 102 for (int i = 0; i < name.length(); i++) 103 { 104 if (name.charAt(i) == ' ') 105 fieldName.append(Character.toUpperCase(name.charAt(++i))); 106 else 107 fieldName.append(name.charAt(i)); 108 } 109 } 110 111 private static void toConstIdentifier(String name, StringBuffer fieldName) 112 { 113 for (int i = 0; i < name.length(); i++) 114 { 115 if (name.charAt(i) == ' ') 116 fieldName.append('_'); 117 else 118 fieldName.append(Character.toUpperCase(name.charAt(i))); 119 } 120 } 121 122 public static String getType(String size, String type, int padTo) 123 { 124 125 boolean wholeNumber = type.equals("bits") || type.equals("int"); 126 if (wholeNumber && "1".equals(size)) 127 return pad(new StringBuffer ("byte"), padTo).toString(); 128 else if (wholeNumber && "2".equals(size)) 129 return pad(new StringBuffer ("short"), padTo).toString(); 130 else if (type.equals("int") && "varword".equals(size)) 131 return pad(new StringBuffer ("short[]"), padTo).toString(); 132 else if (wholeNumber && "4".equals(size)) 133 return pad(new StringBuffer ("int"), padTo).toString(); 134 else if (type.equals("float") && "8".equals(size)) 135 return pad(new StringBuffer ("double"), padTo).toString(); 136 else if (type.equals("string")) 137 return pad(new StringBuffer ("String"), padTo).toString(); 138 else if (type.equals("hbstring")) 139 return pad(new StringBuffer ("String"), padTo).toString(); 140 else if (type.startsWith("custom:")) 141 { 142 int pos = type.lastIndexOf('.'); 143 return pad(new StringBuffer (type.substring(pos+1)), padTo) 144 .toString(); 145 } 146 147 return "short"; } 149 150 public static String getType1stCap(String size, String type, int padTo) 151 { 152 StringBuffer result; 153 boolean numeric = type.equals("bits") || type.equals("int"); 154 if (numeric && "1".equals(size)) 155 result = pad(new StringBuffer ("byte"), padTo); 156 else if (type.equals("int") && "varword".equals(size)) 157 result = pad(new StringBuffer ("short[]"), padTo); 158 else if (numeric && "2".equals(size)) 159 result = pad(new StringBuffer ("short"), padTo); 160 else if (type.equals("string")) 161 result = pad(new StringBuffer ("String"), padTo); 162 else if (type.equals("hbstring")) 163 result = pad(new StringBuffer ("HighByteString"), padTo); 164 165 else 166 return ""; 167 168 result.setCharAt(0, Character.toUpperCase(result.charAt(0))); 169 170 return result.toString(); 171 } 172 173 public static String getMask(int bit) 174 { 175 int mask = (int)Math.pow(2, bit); 177 178 return "0x" + Integer.toHexString(mask); 179 } 180 181 public static String getConstName(String parentName, String constName, int padTo) 182 { 183 StringBuffer fieldName = new StringBuffer (); 184 toConstIdentifier(parentName, fieldName); 185 fieldName.append('_'); 186 toConstIdentifier(constName, fieldName); 187 pad(fieldName, padTo); 188 return fieldName.toString(); 189 } 190 191 195 public static String getByteArrayString(String data) { 196 StringTokenizer tokenizer = new StringTokenizer (data); 197 StringBuffer retval = new StringBuffer (); 198 199 while (tokenizer.hasMoreTokens()) { 200 retval.append("(byte)0x").append(tokenizer.nextToken()); 201 if (tokenizer.hasMoreTokens()) { 202 retval.append(","); 203 } 204 } 205 return retval.toString(); 206 } 207 208 public static String getToString(String fieldName, String type, String size) { 209 StringBuffer result = new StringBuffer (); 210 result.append(" buffer.append(\" ."); 211 result.append(getFieldName(fieldName, 20)); 212 result.append(" = \")" + CR); 213 if (type.equals("string") == false 214 && type.equals("hbstring") == false 215 && type.equals("float") == false 216 && size.equals("varword") == false 218 && type.startsWith("custom:") == false) 219 { 220 result.append(" .append(\"0x\")"); 221 result.append(".append(HexDump.toHex( "); 222 result.append(" get"); 224 result.append(getFieldName1stCap(fieldName, 0)); 225 result.append(" ()))" + CR); 226 } 227 result.append(" .append(\" (\").append( get"); 228 result.append(getFieldName1stCap(fieldName,0)); 229 result.append("() ).append(\" )\");"); 230 return result.toString(); 231 } 232 233 public static String getRecordId(String recordName, String excelName) 234 { 235 if (excelName == null || excelName.equals("")) 236 return recordName; 237 else 238 return excelName; 239 } 240 } 241 | Popular Tags |