001: /*
002: * $Id: Utilities.java 2860 2007-06-28 23:04:10Z psoares33 $
003: * $Name: $
004: *
005: * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050: package com.lowagie.text;
051:
052: import java.io.File;
053: import java.io.IOException;
054: import java.io.InputStream;
055: import java.net.MalformedURLException;
056: import java.net.URL;
057: import java.util.Collections;
058: import java.util.Hashtable;
059: import java.util.Properties;
060: import java.util.Set;
061:
062: import com.lowagie.text.pdf.PRTokeniser;
063:
064: /**
065: * A collection of convenience methods that were present in many different iText
066: * classes.
067: */
068:
069: public class Utilities {
070:
071: /**
072: * Gets the keys of a Hashtable
073: *
074: * @param table
075: * a Hashtable
076: * @return the keyset of a Hashtable (or an empty set if table is null)
077: */
078: public static Set getKeySet(Hashtable table) {
079: return (table == null) ? Collections.EMPTY_SET : table.keySet();
080: }
081:
082: /**
083: * Utility method to extend an array.
084: *
085: * @param original
086: * the original array or <CODE>null</CODE>
087: * @param item
088: * the item to be added to the array
089: * @return a new array with the item appended
090: */
091: public static Object[][] addToArray(Object original[][],
092: Object item[]) {
093: if (original == null) {
094: original = new Object[1][];
095: original[0] = item;
096: return original;
097: } else {
098: Object original2[][] = new Object[original.length + 1][];
099: System
100: .arraycopy(original, 0, original2, 0,
101: original.length);
102: original2[original.length] = item;
103: return original2;
104: }
105: }
106:
107: /**
108: * Checks for a true/false value of a key in a Properties object.
109: * @param attributes
110: * @param key
111: * @return a true/false value of a key in a Properties object
112: */
113: public static boolean checkTrueOrFalse(Properties attributes,
114: String key) {
115: return "true".equalsIgnoreCase(attributes.getProperty(key));
116: }
117:
118: /**
119: * Unescapes an URL. All the "%xx" are replaced by the 'xx' hex char value.
120: * @param src the url to unescape
121: * @return the eunescaped value
122: */
123: public static String unEscapeURL(String src) {
124: StringBuffer bf = new StringBuffer();
125: char[] s = src.toCharArray();
126: for (int k = 0; k < s.length; ++k) {
127: char c = s[k];
128: if (c == '%') {
129: if (k + 2 >= s.length) {
130: bf.append(c);
131: continue;
132: }
133: int a0 = PRTokeniser.getHex((int) s[k + 1]);
134: int a1 = PRTokeniser.getHex((int) s[k + 2]);
135: if (a0 < 0 || a1 < 0) {
136: bf.append(c);
137: continue;
138: }
139: bf.append((char) (a0 * 16 + a1));
140: k += 2;
141: } else
142: bf.append(c);
143: }
144: return bf.toString();
145: }
146:
147: /**
148: * This method makes a valid URL from a given filename.
149: * <P>
150: * This method makes the conversion of this library from the JAVA 2 platform
151: * to a JDK1.1.x-version easier.
152: *
153: * @param filename
154: * a given filename
155: * @return a valid URL
156: * @throws MalformedURLException
157: */
158: public static URL toURL(String filename)
159: throws MalformedURLException {
160: try {
161: return new URL(filename);
162: } catch (Exception e) {
163: return new File(filename).toURL();
164: }
165: }
166:
167: /**
168: * This method is an alternative for the <CODE>InputStream.skip()</CODE>
169: * -method that doesn't seem to work properly for big values of <CODE>size
170: * </CODE>.
171: *
172: * @param is
173: * the <CODE>InputStream</CODE>
174: * @param size
175: * the number of bytes to skip
176: * @throws IOException
177: */
178: static public void skip(InputStream is, int size)
179: throws IOException {
180: long n;
181: while (size > 0) {
182: n = is.skip(size);
183: if (n <= 0)
184: break;
185: size -= n;
186: }
187: }
188:
189: }
|