KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > read > biff > HyperlinkRecord


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.read.biff;
21
22 import java.net.URL JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.io.File JavaDoc;
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 /**
39  * A number record. This is stored as 8 bytes, as opposed to the
40  * 4 byte RK record
41  */

42 public class HyperlinkRecord extends RecordData implements Hyperlink
43 {
44   /**
45    * The logger
46    */

47   private static Logger logger = Logger.getLogger(HyperlinkRecord.class);
48
49   /**
50    * The first row
51    */

52   private int firstRow;
53   /**
54    * The last row
55    */

56   private int lastRow;
57   /**
58    * The first column
59    */

60   private int firstColumn;
61   /**
62    * The last column
63    */

64   private int lastColumn;
65
66   /**
67    * The URL referred to by this hyperlink
68    */

69   private URL JavaDoc url;
70
71   /**
72    * The local file referred to by this hyperlink
73    */

74   private File file;
75
76   /**
77    * The location in this workbook referred to by this hyperlink
78    */

79   private String JavaDoc location;
80
81   /**
82    * The range of cells which activate this hyperlink
83    */

84   private SheetRangeImpl range;
85
86   /**
87    * The type of this hyperlink
88    */

89   private LinkType linkType;
90
91   /**
92    * The excel type of hyperlink
93    */

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   /**
102    * Constructs this object from the raw data
103    *
104    * @param t the raw data
105    * @param s the sheet
106    * @param ws the workbook settings
107    */

108   HyperlinkRecord(Record t, Sheet s, WorkbookSettings ws)
109   {
110     super(t);
111
112     linkType = unknown;
113
114     byte[] data = getRecord().getData();
115
116     // Build up the range of cells occupied by this hyperlink
117
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     // Try and determine the type
139
if ((options & 0x3) == 0x03)
140     {
141       linkType = urlLink;
142       // check the guid monicker
143
if (data[startpos + descbytes] == 0x03)
144       {
145         linkType = fileLink;
146       }
147     }
148     else if ((options & 0x01) != 0)
149     {
150       linkType = fileLink;
151       // check the guid monicker
152
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     // Try and determine the type
163
if (linkType == urlLink)
164     {
165       String JavaDoc urlString = null;
166       try
167       {
168         startpos += 16 + descbytes;
169
170         // Get the url, ignoring the 0 char at the end
171
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 JavaDoc(urlString);
179       }
180       catch (MalformedURLException JavaDoc 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 JavaDoc e3)
189         {
190           logger.warn("Cannot set to file. Setting a default URL");
191
192           // Set a default URL
193
try
194           {
195             linkType = urlLink;
196             url = new URL JavaDoc("http://www.andykhan.com/jexcelapi/index.html");
197           }
198           catch (MalformedURLException JavaDoc e2)
199           {
200             // fail silently
201
}
202         }
203       }
204       catch (Throwable JavaDoc e)
205       {
206         StringBuffer JavaDoc sb1 = new StringBuffer JavaDoc();
207         StringBuffer JavaDoc sb2 = new StringBuffer JavaDoc();
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         // Set a default URL
215
try
216         {
217           url = new URL JavaDoc("http://www.andykhan.com/jexcelapi/index.html");
218         }
219         catch (MalformedURLException JavaDoc e2)
220         {
221           // fail silently
222
}
223       }
224     }
225     else if (linkType == fileLink)
226     {
227       try
228       {
229         startpos += 16 + descbytes;
230
231         // Get the name of the local file, ignoring the zero character at the
232
// end
233
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 JavaDoc fileName = StringHelper.getString(data, chars - 1,
240                                                  startpos + 6, ws);
241
242         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc 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       // give up
269
logger.warn("Cannot determine link type");
270       return;
271     }
272   }
273
274   /**
275    * Determines whether this is a hyperlink to a file
276    *
277    * @return TRUE if this is a hyperlink to a file, FALSE otherwise
278    */

279   public boolean isFile()
280   {
281     return linkType == fileLink;
282   }
283
284   /**
285    * Determines whether this is a hyperlink to a web resource
286    *
287    * @return TRUE if this is a URL
288    */

289   public boolean isURL()
290   {
291     return linkType == urlLink;
292   }
293
294   /**
295    * Determines whether this is a hyperlink to a location in this workbook
296    *
297    * @return TRUE if this is a link to an internal location
298    */

299   public boolean isLocation()
300   {
301     return linkType == workbookLink;
302   }
303
304   /**
305    * Returns the row number of the top left cell
306    *
307    * @return the row number of this cell
308    */

309   public int getRow()
310   {
311     return firstRow;
312   }
313
314   /**
315    * Returns the column number of the top left cell
316    *
317    * @return the column number of this cell
318    */

319   public int getColumn()
320   {
321     return firstColumn;
322   }
323
324   /**
325    * Returns the row number of the bottom right cell
326    *
327    * @return the row number of this cell
328    */

329   public int getLastRow()
330   {
331     return lastRow;
332   }
333
334   /**
335    * Returns the column number of the bottom right cell
336    *
337    * @return the column number of this cell
338    */

339   public int getLastColumn()
340   {
341     return lastColumn;
342   }
343
344   /**
345    * Gets the URL referenced by this Hyperlink
346    *
347    * @return the URL, or NULL if this hyperlink is not a URL
348    */

349   public URL JavaDoc getURL()
350   {
351     return url;
352   }
353
354   /**
355    * Returns the local file eferenced by this Hyperlink
356    *
357    * @return the file, or NULL if this hyperlink is not a file
358    */

359   public File getFile()
360   {
361     return file;
362   }
363
364   /**
365    * Exposes the base class method. This is used when copying hyperlinks
366    *
367    * @return the Record data
368    */

369   public Record getRecord()
370   {
371     return super.getRecord();
372   }
373
374   /**
375    * Gets the range of cells which activate this hyperlink
376    * The get sheet index methods will all return -1, because the
377    * cells will all be present on the same sheet
378    *
379    * @return the range of cells which activate the hyperlink
380    */

381   public Range getRange()
382   {
383     return range;
384   }
385
386   /**
387    * Gets the location referenced by this hyperlink
388    *
389    * @return the location
390    */

391   public String JavaDoc getLocation()
392   {
393     return location;
394   }
395 }
396
397
398
399
400
401
402
403
Popular Tags