1 50 package com.lowagie.text; 51 52 import java.io.File ; 53 import java.io.IOException ; 54 import java.io.InputStream ; 55 import java.net.MalformedURLException ; 56 import java.net.URL ; 57 import java.util.Collections ; 58 import java.util.Hashtable ; 59 import java.util.Properties ; 60 import java.util.Set ; 61 62 import com.lowagie.text.pdf.PRTokeniser; 63 64 68 69 public class Utilities { 70 71 78 public static Set getKeySet(Hashtable table) { 79 return (table == null) ? Collections.EMPTY_SET : table.keySet(); 80 } 81 82 91 public static Object [][] addToArray(Object original[][], Object item[]) { 92 if (original == null) { 93 original = new Object [1][]; 94 original[0] = item; 95 return original; 96 } else { 97 Object original2[][] = new Object [original.length + 1][]; 98 System.arraycopy(original, 0, original2, 0, original.length); 99 original2[original.length] = item; 100 return original2; 101 } 102 } 103 104 110 public static boolean checkTrueOrFalse(Properties attributes, String key) { 111 return "true".equalsIgnoreCase(attributes.getProperty(key)); 112 } 113 114 119 public static String unEscapeURL(String src) { 120 StringBuffer bf = new StringBuffer (); 121 char[] s = src.toCharArray(); 122 for (int k = 0; k < s.length; ++k) { 123 char c = s[k]; 124 if (c == '%') { 125 if (k + 2 >= s.length) { 126 bf.append(c); 127 continue; 128 } 129 int a0 = PRTokeniser.getHex((int)s[k + 1]); 130 int a1 = PRTokeniser.getHex((int)s[k + 2]); 131 if (a0 < 0 || a1 < 0) { 132 bf.append(c); 133 continue; 134 } 135 bf.append((char)(a0 * 16 + a1)); 136 k += 2; 137 } 138 else 139 bf.append(c); 140 } 141 return bf.toString(); 142 } 143 144 private static String [] excUriEsc = {"%20", "%3C", "%3E", "%23", "%25", "%22", "%7B", "%7D", "%5B", "%5D", "%7C", "%5C", "%5E", "%60"}; 145 private static String excUri = " <>#%\"{}[]|\\\u005E\u0060"; 146 157 public static URL toURL(String filename) throws MalformedURLException { 158 if (filename.startsWith("file:/") || filename.startsWith("http://") 159 || filename.startsWith("https://") 160 || filename.startsWith("jar:")) { 161 return new URL (filename); 162 } 163 File f = new File (filename); 164 String path = f.getAbsolutePath(); 165 if (File.separatorChar != '/') { 166 path = path.replace(File.separatorChar, '/'); 167 } 168 if (!path.startsWith("/")) { 169 path = "/" + path; 170 } 171 if (!path.endsWith("/") && f.isDirectory()) { 172 path = path + "/"; 173 } 174 char[] t = path.toCharArray(); 175 StringBuffer sb = new StringBuffer (); 176 for (int k = 0; k < t.length; ++k) { 177 char c = t[k]; 178 int a = Utilities.excUri.indexOf(c); 179 if (a >= 0) 180 sb.append(Utilities.excUriEsc[a]); 181 else 182 sb.append(c); 183 } 184 return new URL ("file", "", sb.toString()); 185 } 186 187 198 static public void skip(InputStream is, int size) throws IOException { 199 long n; 200 while (size > 0) { 201 n = is.skip(size); 202 if (n <= 0) 203 break; 204 size -= n; 205 } 206 } 207 208 } 209 | Popular Tags |