1 19 20 package jxl.read.biff; 21 22 import java.net.URL ; 23 import java.net.MalformedURLException ; 24 import java.io.File ; 25 26 import common.Logger; 27 28 import jxl.Hyperlink; 29 import jxl.Range; 30 import jxl.Sheet; 31 import jxl.WorkbookSettings; 32 import jxl.CellReferenceHelper; 33 import jxl.biff.IntegerHelper; 34 import jxl.biff.StringHelper; 35 import jxl.biff.RecordData; 36 import jxl.biff.SheetRangeImpl; 37 38 42 public class HyperlinkRecord extends RecordData implements Hyperlink 43 { 44 47 private static Logger logger = Logger.getLogger(HyperlinkRecord.class); 48 49 52 private int firstRow; 53 56 private int lastRow; 57 60 private int firstColumn; 61 64 private int lastColumn; 65 66 69 private URL url; 70 71 74 private File file; 75 76 79 private String location; 80 81 84 private SheetRangeImpl range; 85 86 89 private LinkType linkType; 90 91 94 private static class LinkType {}; 95 96 private static final LinkType urlLink = new LinkType(); 97 private static final LinkType fileLink = new LinkType(); 98 private static final LinkType workbookLink = new LinkType(); 99 private static final LinkType unknown = new LinkType(); 100 101 108 HyperlinkRecord(Record t, Sheet s, WorkbookSettings ws) 109 { 110 super(t); 111 112 linkType = unknown; 113 114 byte[] data = getRecord().getData(); 115 116 firstRow = IntegerHelper.getInt(data[0], data[1]); 118 lastRow = IntegerHelper.getInt(data[2], data[3]); 119 firstColumn = IntegerHelper.getInt(data[4], data[5]); 120 lastColumn = IntegerHelper.getInt(data[6], data[7]); 121 range = new SheetRangeImpl(s, 122 firstColumn, firstRow, 123 lastColumn, lastRow); 124 125 int options = IntegerHelper.getInt(data[28], data[29], data[30], data[31]); 126 127 boolean description = (options & 0x14) != 0; 128 int startpos = 32; 129 int descbytes = 0; 130 if (description) 131 { 132 int descchars = IntegerHelper.getInt 133 (data[startpos], data[startpos + 1], 134 data[startpos + 2], data[startpos + 3]); 135 descbytes = descchars * 2 + 4; 136 } 137 138 if ((options & 0x3) == 0x03) 140 { 141 linkType = urlLink; 142 if (data[startpos + descbytes] == 0x03) 144 { 145 linkType = fileLink; 146 } 147 } 148 else if ((options & 0x01) != 0) 149 { 150 linkType = fileLink; 151 if (data[startpos + descbytes] == (byte) 0xe0) 153 { 154 linkType = urlLink; 155 } 156 } 157 else if ((options & 0x08) != 0) 158 { 159 linkType = workbookLink; 160 } 161 162 if (linkType == urlLink) 164 { 165 String urlString = null; 166 try 167 { 168 startpos += 16 + descbytes; 169 170 int bytes = IntegerHelper.getInt(data[startpos], 172 data[startpos + 1], 173 data[startpos + 2], 174 data[startpos + 3]); 175 176 urlString = StringHelper.getUnicodeString(data, bytes / 2 - 1, 177 startpos + 4); 178 url = new URL (urlString); 179 } 180 catch (MalformedURLException e) 181 { 182 logger.warn("URL " + urlString + " is malformed. Trying a file"); 183 try 184 { 185 linkType = fileLink; 186 file = new File(urlString); 187 } 188 catch (Exception e3) 189 { 190 logger.warn("Cannot set to file. Setting a default URL"); 191 192 try 194 { 195 linkType = urlLink; 196 url = new URL ("http://www.andykhan.com/jexcelapi/index.html"); 197 } 198 catch (MalformedURLException e2) 199 { 200 } 202 } 203 } 204 catch (Throwable e) 205 { 206 StringBuffer sb1 = new StringBuffer (); 207 StringBuffer sb2 = new StringBuffer (); 208 CellReferenceHelper.getCellReference(firstColumn, firstRow, sb1); 209 CellReferenceHelper.getCellReference(lastColumn, lastRow, sb2); 210 sb1.insert(0, "Exception when parsing URL "); 211 sb1.append('\"').append(sb2.toString()).append("\". Using default."); 212 logger.warn(sb1, e); 213 214 try 216 { 217 url = new URL ("http://www.andykhan.com/jexcelapi/index.html"); 218 } 219 catch (MalformedURLException e2) 220 { 221 } 223 } 224 } 225 else if (linkType == fileLink) 226 { 227 try 228 { 229 startpos += 16 + descbytes; 230 231 int upLevelCount = IntegerHelper.getInt(data[startpos], 234 data[startpos + 1]); 235 int chars = IntegerHelper.getInt(data[startpos + 2], 236 data[startpos + 3], 237 data[startpos + 4], 238 data[startpos + 5]); 239 String fileName = StringHelper.getString(data, chars - 1, 240 startpos + 6, ws); 241 242 StringBuffer sb = new StringBuffer (); 243 244 for (int i = 0; i < upLevelCount; i++) 245 { 246 sb.append("..\\"); 247 } 248 249 sb.append(fileName); 250 251 file = new File(sb.toString()); 252 } 253 catch (Throwable e) 254 { 255 e.printStackTrace(); 256 logger.warn("Exception when parsing file " + 257 e.getClass().getName() + "."); 258 file = new File("."); 259 } 260 } 261 else if (linkType == workbookLink) 262 { 263 int chars = IntegerHelper.getInt(data[32], data[33], data[34], data[35]); 264 location = StringHelper.getUnicodeString(data, chars - 1, 36); 265 } 266 else 267 { 268 logger.warn("Cannot determine link type"); 270 return; 271 } 272 } 273 274 279 public boolean isFile() 280 { 281 return linkType == fileLink; 282 } 283 284 289 public boolean isURL() 290 { 291 return linkType == urlLink; 292 } 293 294 299 public boolean isLocation() 300 { 301 return linkType == workbookLink; 302 } 303 304 309 public int getRow() 310 { 311 return firstRow; 312 } 313 314 319 public int getColumn() 320 { 321 return firstColumn; 322 } 323 324 329 public int getLastRow() 330 { 331 return lastRow; 332 } 333 334 339 public int getLastColumn() 340 { 341 return lastColumn; 342 } 343 344 349 public URL getURL() 350 { 351 return url; 352 } 353 354 359 public File getFile() 360 { 361 return file; 362 } 363 364 369 public Record getRecord() 370 { 371 return super.getRecord(); 372 } 373 374 381 public Range getRange() 382 { 383 return range; 384 } 385 386 391 public String getLocation() 392 { 393 return location; 394 } 395 } 396 397 398 399 400 401 402 403 | Popular Tags |