KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > EncodedURLHelper


1 /*********************************************************************
2 *
3 * Copyright (C) 2005 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.biff;
21
22 import common.Assert;
23 import common.Logger;
24
25 import jxl.WorkbookSettings;
26
27 import java.io.File JavaDoc;
28
29 /**
30  * Helper to get the Microsoft encoded URL from the given string
31  */

32 public class EncodedURLHelper
33 {
34   /**
35    * The logger
36    */

37   private static Logger logger = Logger.getLogger(EncodedURLHelper.class);
38
39   // The control codes
40
private static byte msDosDriveLetter = 0x01;
41   private static byte sameDrive = 0x02;
42   private static byte endOfSubdirectory = 0x03;
43   private static byte parentDirectory = 0x04;
44   private static byte unencodedUrl = 0x05;
45
46   public static byte[] getEncodedURL(String JavaDoc s, WorkbookSettings ws)
47   {
48     if (s.startsWith("http:"))
49     {
50       return getURL(s, ws);
51     }
52     else
53     {
54       return getFile(s, ws);
55     }
56   }
57
58   private static byte[] getFile(String JavaDoc s, WorkbookSettings ws)
59   {
60     ByteArray byteArray = new ByteArray();
61
62     int pos = 0;
63     if (s.charAt(1) == ':')
64     {
65       // we have a drive letter
66
byteArray.add(msDosDriveLetter);
67       byteArray.add((byte) s.charAt(0));
68       pos = 2;
69     }
70     else if (s.charAt(pos) == '\\' ||
71              s.charAt(pos) == '/')
72     {
73       byteArray.add(sameDrive);
74     }
75
76     while (s.charAt(pos) == '\\' ||
77            s.charAt(pos) == '/')
78     {
79       pos++;
80     }
81
82     while (pos < s.length())
83     {
84       int nextSepIndex1 = s.indexOf('/', pos);
85       int nextSepIndex2 = s.indexOf('\\', pos);
86       int nextSepIndex = 0;
87       String JavaDoc nextFileNameComponent = null;
88
89       if (nextSepIndex1 != -1 && nextSepIndex2 != -1)
90       {
91         // choose the smallest (ie. nearest) separator
92
nextSepIndex = Math.min(nextSepIndex1, nextSepIndex2);
93       }
94       else if (nextSepIndex1 == -1 || nextSepIndex2 == -1)
95       {
96         // chose the maximum separator
97
nextSepIndex = Math.max(nextSepIndex1, nextSepIndex2);
98       }
99
100       if (nextSepIndex == -1)
101       {
102         // no more separators
103
nextFileNameComponent = s.substring(pos);
104         pos = s.length();
105       }
106       else
107       {
108         nextFileNameComponent = s.substring(pos, nextSepIndex);
109         pos = nextSepIndex + 1;
110       }
111
112       if (nextFileNameComponent.equals("."))
113       {
114         // current directory - do nothing
115
}
116       else if (nextFileNameComponent.equals(".."))
117       {
118         // parent directory
119
byteArray.add(parentDirectory);
120       }
121       else
122       {
123         // add the filename component
124
byteArray.add(StringHelper.getBytes(nextFileNameComponent,
125                                             ws));
126       }
127
128       if (pos < s.length())
129       {
130         byteArray.add(endOfSubdirectory);
131       }
132     }
133
134     return byteArray.getBytes();
135   }
136   
137   private static byte[] getURL(String JavaDoc s, WorkbookSettings ws)
138   {
139     ByteArray byteArray = new ByteArray();
140     byteArray.add(unencodedUrl);
141     byteArray.add((byte) s.length());
142     byteArray.add(StringHelper.getBytes(s, ws));
143     return byteArray.getBytes();
144   }
145 }
146
Popular Tags