1 19 20 package jxl.write.biff; 21 22 import common.Assert; 23 import common.Logger; 24 25 import jxl.biff.Type; 26 import jxl.biff.IntegerHelper; 27 import jxl.biff.StringHelper; 28 import jxl.biff.EncodedURLHelper; 29 import jxl.biff.WritableRecordData; 30 import jxl.WorkbookSettings; 31 32 36 class SupbookRecord extends WritableRecordData 37 { 38 41 private static Logger logger = Logger.getLogger(SupbookRecord.class); 42 43 46 private SupbookType type; 47 48 51 private byte[] data; 52 53 56 private int numSheets; 57 58 61 private String fileName; 62 63 66 private String [] sheetNames; 67 68 71 private WorkbookSettings workbookSettings; 72 73 76 private static class SupbookType {}; 77 78 public final static SupbookType INTERNAL = new SupbookType(); 79 public final static SupbookType EXTERNAL = new SupbookType(); 80 public final static SupbookType ADDIN = new SupbookType(); 81 public final static SupbookType LINK = new SupbookType(); 82 public final static SupbookType UNKNOWN = new SupbookType(); 83 84 87 public SupbookRecord(int sheets, WorkbookSettings ws) 88 { 89 super(Type.SUPBOOK); 90 91 numSheets = sheets; 92 type = INTERNAL; 93 workbookSettings = ws; 94 } 95 96 102 public SupbookRecord(String fn, WorkbookSettings ws) 103 { 104 super(Type.SUPBOOK); 105 106 fileName = fn; 107 numSheets = 1; 108 sheetNames = new String [0]; 109 workbookSettings = ws; 110 111 type = EXTERNAL; 112 } 113 114 117 public SupbookRecord(jxl.read.biff.SupbookRecord sr, WorkbookSettings ws) 118 { 119 super(Type.SUPBOOK); 120 121 workbookSettings = ws; 122 if (sr.getType() == sr.INTERNAL) 123 { 124 type = INTERNAL; 125 numSheets = sr.getNumberOfSheets(); 126 } 127 else if (sr.getType() == sr.EXTERNAL) 128 { 129 type = EXTERNAL; 130 numSheets = sr.getNumberOfSheets(); 131 fileName = sr.getFileName(); 132 sheetNames = new String [numSheets]; 133 134 for (int i = 0; i < numSheets; i++) 135 { 136 sheetNames[i] = sr.getSheetName(i); 137 } 138 } 139 } 140 141 146 private void initInternal(jxl.read.biff.SupbookRecord sr) 147 { 148 numSheets = sr.getNumberOfSheets(); 149 initInternal(); 150 } 151 152 155 private void initInternal() 156 { 157 data = new byte[4]; 158 159 IntegerHelper.getTwoBytes(numSheets, data, 0); 160 data[2] = 0x1; 161 data[3] = 0x4; 162 type = INTERNAL; 163 } 164 165 171 void adjustInternal(int sheets) 172 { 173 Assert.verify(type == INTERNAL); 174 numSheets = sheets; 175 initInternal(); 176 } 177 178 181 private void initExternal() 182 { 183 int totalSheetNameLength = 0; 184 for (int i = 0; i < numSheets; i++) 185 { 186 totalSheetNameLength += sheetNames[i].length(); 187 } 188 189 byte[] fileNameData = EncodedURLHelper.getEncodedURL(fileName, 190 workbookSettings); 191 int dataLength = 2 + 4 + fileNameData.length + 193 numSheets * 3 + totalSheetNameLength * 2; 194 195 data = new byte[dataLength]; 196 197 IntegerHelper.getTwoBytes(numSheets, data, 0); 198 199 int pos = 2; 202 IntegerHelper.getTwoBytes(fileNameData.length+1, data, pos); 203 data[pos+2] = 0; data[pos+3] = 1; System.arraycopy(fileNameData, 0, data, pos+4, fileNameData.length); 206 207 pos += 4 + fileNameData.length; 208 209 for (int i = 0; i < sheetNames.length; i++) 211 { 212 IntegerHelper.getTwoBytes(sheetNames[i].length(), data, pos); 213 data[pos+2] = 1; StringHelper.getUnicodeBytes(sheetNames[i], data, pos+3); 215 pos += 3 + sheetNames[i].length() * 2; 216 } 217 } 218 219 224 public byte[] getData() 225 { 226 if (type == INTERNAL) 227 { 228 initInternal(); 229 } 230 else if (type == EXTERNAL) 231 { 232 initExternal(); 233 } 234 else 235 { 236 logger.warn("unsupported supbook type - defaulting to internal"); 237 initInternal(); 238 } 239 240 return data; 241 } 242 243 248 public SupbookType getType() 249 { 250 return type; 251 } 252 253 259 public int getNumberOfSheets() 260 { 261 return numSheets; 262 } 263 264 269 public String getFileName() 270 { 271 return fileName; 272 } 273 274 280 public int getSheetIndex(String s) 281 { 282 boolean found = false; 283 int sheetIndex = 0; 284 for (int i = 0; i < sheetNames.length && !found; i++) 285 { 286 if (sheetNames[i].equals(s)) 287 { 288 found = true; 289 sheetIndex = 0; 290 } 291 } 292 293 if (found) 294 { 295 return sheetIndex; 296 } 297 298 String [] names = new String [sheetNames.length + 1]; 300 names[sheetNames.length] = s; 301 sheetNames = names; 302 return sheetNames.length - 1; 303 } 304 305 310 public String getSheetName(int s) 311 { 312 return sheetNames[s]; 313 } 314 } 315 | Popular Tags |