KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > Utilities


1 /*
2  * $Id: Utilities.java 2763 2007-05-17 17:05:51Z psoares33 $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50 package com.lowagie.text;
51
52 import java.io.File JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.io.InputStream JavaDoc;
55 import java.net.MalformedURLException JavaDoc;
56 import java.net.URL JavaDoc;
57 import java.util.Collections JavaDoc;
58 import java.util.Hashtable JavaDoc;
59 import java.util.Properties JavaDoc;
60 import java.util.Set JavaDoc;
61
62 import com.lowagie.text.pdf.PRTokeniser;
63
64 /**
65  * A collection of convenience methods that were present in many different iText
66  * classes.
67  */

68
69 public class Utilities {
70
71     /**
72      * Gets the keys of a Hashtable
73      *
74      * @param table
75      * a Hashtable
76      * @return the keyset of a Hashtable (or an empty set if table is null)
77      */

78     public static Set JavaDoc getKeySet(Hashtable JavaDoc table) {
79         return (table == null) ? Collections.EMPTY_SET : table.keySet();
80     }
81
82     /**
83      * Utility method to extend an array.
84      *
85      * @param original
86      * the original array or <CODE>null</CODE>
87      * @param item
88      * the item to be added to the array
89      * @return a new array with the item appended
90      */

91     public static Object JavaDoc[][] addToArray(Object JavaDoc original[][], Object JavaDoc item[]) {
92         if (original == null) {
93             original = new Object JavaDoc[1][];
94             original[0] = item;
95             return original;
96         } else {
97             Object JavaDoc original2[][] = new Object JavaDoc[original.length + 1][];
98             System.arraycopy(original, 0, original2, 0, original.length);
99             original2[original.length] = item;
100             return original2;
101         }
102     }
103
104     /**
105      * Checks for a true/false value of a key in a Properties object.
106      * @param attributes
107      * @param key
108      * @return a true/false value of a key in a Properties object
109      */

110     public static boolean checkTrueOrFalse(Properties JavaDoc attributes, String JavaDoc key) {
111         return "true".equalsIgnoreCase(attributes.getProperty(key));
112     }
113
114     /**
115      * Unescapes an URL. All the "%xx" are replaced by the 'xx' hex char value.
116      * @param src the url to unescape
117      * @return the eunescaped value
118      */

119     public static String JavaDoc unEscapeURL(String JavaDoc src) {
120         StringBuffer JavaDoc bf = new StringBuffer JavaDoc();
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 JavaDoc[] excUriEsc = {"%20", "%3C", "%3E", "%23", "%25", "%22", "%7B", "%7D", "%5B", "%5D", "%7C", "%5C", "%5E", "%60"};
145     private static String JavaDoc excUri = " <>#%\"{}[]|\\\u005E\u0060";
146     /**
147      * This method makes a valid URL from a given filename.
148      * <P>
149      * This method makes the conversion of this library from the JAVA 2 platform
150      * to a JDK1.1.x-version easier.
151      *
152      * @param filename
153      * a given filename
154      * @return a valid URL
155      * @throws MalformedURLException
156      */

157     public static URL JavaDoc toURL(String JavaDoc filename) throws MalformedURLException JavaDoc {
158         if (filename.startsWith("file:/") || filename.startsWith("http://")
159                 || filename.startsWith("https://")
160                 || filename.startsWith("jar:")) {
161             return new URL JavaDoc(filename);
162         }
163         File JavaDoc f = new File JavaDoc(filename);
164         String JavaDoc 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 JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc("file", "", sb.toString());
185     }
186
187     /**
188      * This method is an alternative for the <CODE>InputStream.skip()</CODE>
189      * -method that doesn't seem to work properly for big values of <CODE>size
190      * </CODE>.
191      *
192      * @param is
193      * the <CODE>InputStream</CODE>
194      * @param size
195      * the number of bytes to skip
196      * @throws IOException
197      */

198     static public void skip(InputStream JavaDoc is, int size) throws IOException JavaDoc {
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