1 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 ; 28 29 32 public class EncodedURLHelper 33 { 34 37 private static Logger logger = Logger.getLogger(EncodedURLHelper.class); 38 39 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 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 s, WorkbookSettings ws) 59 { 60 ByteArray byteArray = new ByteArray(); 61 62 int pos = 0; 63 if (s.charAt(1) == ':') 64 { 65 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 nextFileNameComponent = null; 88 89 if (nextSepIndex1 != -1 && nextSepIndex2 != -1) 90 { 91 nextSepIndex = Math.min(nextSepIndex1, nextSepIndex2); 93 } 94 else if (nextSepIndex1 == -1 || nextSepIndex2 == -1) 95 { 96 nextSepIndex = Math.max(nextSepIndex1, nextSepIndex2); 98 } 99 100 if (nextSepIndex == -1) 101 { 102 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 } 116 else if (nextFileNameComponent.equals("..")) 117 { 118 byteArray.add(parentDirectory); 120 } 121 else 122 { 123 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 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 |