001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------
028: * IOUtils.java
029: * ------------
030: * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: IOUtils.java,v 1.6 2005/11/03 09:55:27 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 26-Jan-2003 : Initial version
040: * 23-Feb-2003 : Documentation
041: * 25-Feb-2003 : Fixed Checkstyle issues (DG);
042: * 29-Apr-2003 : Moved to jcommon
043: * 04-Jan-2004 : Fixed JDK 1.2.2 issues with createRelativeURL;
044: * added support for query strings within these urls (TM);
045: */
046:
047: package org.jfree.io;
048:
049: import java.io.File;
050: import java.io.IOException;
051: import java.io.InputStream;
052: import java.io.OutputStream;
053: import java.io.Reader;
054: import java.io.Writer;
055: import java.net.URL;
056: import java.util.ArrayList;
057: import java.util.Iterator;
058: import java.util.List;
059: import java.util.StringTokenizer;
060:
061: /**
062: * The IOUtils provide some IO related helper methods.
063: *
064: * @author Thomas Morgner.
065: */
066: public class IOUtils {
067:
068: /** the singleton instance of the utility package. */
069: private static IOUtils instance;
070:
071: /**
072: * DefaultConstructor.
073: */
074: private IOUtils() {
075: }
076:
077: /**
078: * Gets the singleton instance of the utility package.
079: *
080: * @return the singleton instance.
081: */
082: public static IOUtils getInstance() {
083: if (instance == null) {
084: instance = new IOUtils();
085: }
086: return instance;
087: }
088:
089: /**
090: * Checks, whether the URL uses a file based protocol.
091: *
092: * @param url the url.
093: * @return true, if the url is file based.
094: */
095: private boolean isFileStyleProtocol(final URL url) {
096: if (url.getProtocol().equals("http")) {
097: return true;
098: }
099: if (url.getProtocol().equals("https")) {
100: return true;
101: }
102: if (url.getProtocol().equals("ftp")) {
103: return true;
104: }
105: if (url.getProtocol().equals("file")) {
106: return true;
107: }
108: if (url.getProtocol().equals("jar")) {
109: return true;
110: }
111: return false;
112: }
113:
114: /**
115: * Parses the given name and returns the name elements as List of Strings.
116: *
117: * @param name the name, that should be parsed.
118: * @return the parsed name.
119: */
120: private List parseName(final String name) {
121: final ArrayList list = new ArrayList();
122: final StringTokenizer strTok = new StringTokenizer(name, "/");
123: while (strTok.hasMoreElements()) {
124: final String s = (String) strTok.nextElement();
125: if (s.length() != 0) {
126: list.add(s);
127: }
128: }
129: return list;
130: }
131:
132: /**
133: * Transforms the name list back into a single string, separated with "/".
134: *
135: * @param name the name list.
136: * @param query the (optional) query for the URL.
137: * @return the constructed name.
138: */
139: private String formatName(final List name, final String query) {
140: final StringBuffer b = new StringBuffer();
141: final Iterator it = name.iterator();
142: while (it.hasNext()) {
143: b.append(it.next());
144: if (it.hasNext()) {
145: b.append("/");
146: }
147: }
148: if (query != null) {
149: b.append('?');
150: b.append(query);
151: }
152: return b.toString();
153: }
154:
155: /**
156: * Compares both name lists, and returns the last common index shared
157: * between the two lists.
158: *
159: * @param baseName the name created using the base url.
160: * @param urlName the target url name.
161: * @return the number of shared elements.
162: */
163: private int startsWithUntil(final List baseName, final List urlName) {
164: final int minIdx = Math.min(urlName.size(), baseName.size());
165: for (int i = 0; i < minIdx; i++) {
166: final String baseToken = (String) baseName.get(i);
167: final String urlToken = (String) urlName.get(i);
168: if (!baseToken.equals(urlToken)) {
169: return i;
170: }
171: }
172: return minIdx;
173: }
174:
175: /**
176: * Checks, whether the URL points to the same service. A service is equal
177: * if the protocol, host and port are equal.
178: *
179: * @param url a url
180: * @param baseUrl an other url, that should be compared.
181: * @return true, if the urls point to the same host and port and use the
182: * same protocol, false otherwise.
183: */
184: private boolean isSameService(final URL url, final URL baseUrl) {
185: if (!url.getProtocol().equals(baseUrl.getProtocol())) {
186: return false;
187: }
188: if (!url.getHost().equals(baseUrl.getHost())) {
189: return false;
190: }
191: if (url.getPort() != baseUrl.getPort()) {
192: return false;
193: }
194: return true;
195: }
196:
197: /**
198: * Creates a relative url by stripping the common parts of the the url.
199: *
200: * @param url the to be stripped url
201: * @param baseURL the base url, to which the <code>url</code> is relative
202: * to.
203: * @return the relative url, or the url unchanged, if there is no relation
204: * beween both URLs.
205: */
206: public String createRelativeURL(final URL url, final URL baseURL) {
207: if (url == null) {
208: throw new NullPointerException(
209: "content url must not be null.");
210: }
211: if (baseURL == null) {
212: throw new NullPointerException("baseURL must not be null.");
213: }
214: if (isFileStyleProtocol(url) && isSameService(url, baseURL)) {
215:
216: // If the URL contains a query, ignore that URL; do not
217: // attemp to modify it...
218: final List urlName = parseName(getPath(url));
219: final List baseName = parseName(getPath(baseURL));
220: final String query = getQuery(url);
221:
222: if (!isPath(baseURL)) {
223: baseName.remove(baseName.size() - 1);
224: }
225:
226: // if both urls are identical, then return the plain file name...
227: if (url.equals(baseURL)) {
228: return (String) urlName.get(urlName.size() - 1);
229: }
230:
231: int commonIndex = startsWithUntil(urlName, baseName);
232: if (commonIndex == 0) {
233: return url.toExternalForm();
234: }
235:
236: if (commonIndex == urlName.size()) {
237: // correct the base index if there is some weird mapping
238: // detected,
239: // fi. the file url is fully included in the base url:
240: //
241: // base: /file/test/funnybase
242: // file: /file/test
243: //
244: // this could be a valid configuration whereever virtual
245: // mappings are allowed.
246: commonIndex -= 1;
247: }
248:
249: final ArrayList retval = new ArrayList();
250: if (baseName.size() >= urlName.size()) {
251: final int levels = baseName.size() - commonIndex;
252: for (int i = 0; i < levels; i++) {
253: retval.add("..");
254: }
255: }
256:
257: retval.addAll(urlName.subList(commonIndex, urlName.size()));
258: return formatName(retval, query);
259: }
260: return url.toExternalForm();
261: }
262:
263: /**
264: * Returns <code>true</code> if the URL represents a path, and
265: * <code>false</code> otherwise.
266: *
267: * @param baseURL the URL.
268: *
269: * @return A boolean.
270: */
271: private boolean isPath(final URL baseURL) {
272: if (getPath(baseURL).endsWith("/")) {
273: return true;
274: } else if (baseURL.getProtocol().equals("file")) {
275: final File f = new File(getPath(baseURL));
276: try {
277: if (f.isDirectory()) {
278: return true;
279: }
280: } catch (SecurityException se) {
281: // ignored ...
282: }
283: }
284: return false;
285: }
286:
287: /**
288: * Implements the JDK 1.3 method URL.getPath(). The path is defined
289: * as URL.getFile() minus the (optional) query.
290: *
291: * @param url the URL
292: * @return the path
293: */
294: private String getQuery(final URL url) {
295: final String file = url.getFile();
296: final int queryIndex = file.indexOf('?');
297: if (queryIndex == -1) {
298: return null;
299: }
300: return file.substring(queryIndex + 1);
301: }
302:
303: /**
304: * Implements the JDK 1.3 method URL.getPath(). The path is defined
305: * as URL.getFile() minus the (optional) query.
306: *
307: * @param url the URL
308: * @return the path
309: */
310: private String getPath(final URL url) {
311: final String file = url.getFile();
312: final int queryIndex = file.indexOf('?');
313: if (queryIndex == -1) {
314: return file;
315: }
316: return file.substring(0, queryIndex);
317: }
318:
319: /**
320: * Copies the InputStream into the OutputStream, until the end of the stream
321: * has been reached. This method uses a buffer of 4096 kbyte.
322: *
323: * @param in the inputstream from which to read.
324: * @param out the outputstream where the data is written to.
325: * @throws IOException if a IOError occurs.
326: */
327: public void copyStreams(final InputStream in, final OutputStream out)
328: throws IOException {
329: copyStreams(in, out, 4096);
330: }
331:
332: /**
333: * Copies the InputStream into the OutputStream, until the end of the stream
334: * has been reached.
335: *
336: * @param in the inputstream from which to read.
337: * @param out the outputstream where the data is written to.
338: * @param buffersize the buffer size.
339: * @throws IOException if a IOError occurs.
340: */
341: public void copyStreams(final InputStream in,
342: final OutputStream out, final int buffersize)
343: throws IOException {
344: // create a 4kbyte buffer to read the file
345: final byte[] bytes = new byte[buffersize];
346:
347: // the input stream does not supply accurate available() data
348: // the zip entry does not know the size of the data
349: int bytesRead = in.read(bytes);
350: while (bytesRead > -1) {
351: out.write(bytes, 0, bytesRead);
352: bytesRead = in.read(bytes);
353: }
354: }
355:
356: /**
357: * Copies the contents of the Reader into the Writer, until the end of the
358: * stream has been reached. This method uses a buffer of 4096 kbyte.
359: *
360: * @param in the reader from which to read.
361: * @param out the writer where the data is written to.
362: * @throws IOException if a IOError occurs.
363: */
364: public void copyWriter(final Reader in, final Writer out)
365: throws IOException {
366: copyWriter(in, out, 4096);
367: }
368:
369: /**
370: * Copies the contents of the Reader into the Writer, until the end of the
371: * stream has been reached.
372: *
373: * @param in the reader from which to read.
374: * @param out the writer where the data is written to.
375: * @param buffersize the buffer size.
376: *
377: * @throws IOException if a IOError occurs.
378: */
379: public void copyWriter(final Reader in, final Writer out,
380: final int buffersize) throws IOException {
381: // create a 4kbyte buffer to read the file
382: final char[] bytes = new char[buffersize];
383:
384: // the input stream does not supply accurate available() data
385: // the zip entry does not know the size of the data
386: int bytesRead = in.read(bytes);
387: while (bytesRead > -1) {
388: out.write(bytes, 0, bytesRead);
389: bytesRead = in.read(bytes);
390: }
391: }
392:
393: /**
394: * Extracts the file name from the URL.
395: *
396: * @param url the url.
397: * @return the extracted filename.
398: */
399: public String getFileName(final URL url) {
400: final String file = url.getFile();
401: final int last = file.lastIndexOf("/");
402: if (last < 0) {
403: return file;
404: }
405: return file.substring(last);
406: }
407:
408: /**
409: * Removes the file extension from the given file name.
410: *
411: * @param file the file name.
412: * @return the file name without the file extension.
413: */
414: public String stripFileExtension(final String file) {
415: final int idx = file.lastIndexOf(".");
416: // handles unix hidden files and files without an extension.
417: if (idx < 1) {
418: return file;
419: }
420: return file.substring(0, idx);
421: }
422:
423: /**
424: * Returns the file extension of the given file name.
425: * The returned value will contain the dot.
426: *
427: * @param file the file name.
428: * @return the file extension.
429: */
430: public String getFileExtension(final String file) {
431: final int idx = file.lastIndexOf(".");
432: // handles unix hidden files and files without an extension.
433: if (idx < 1) {
434: return "";
435: }
436: return file.substring(idx);
437: }
438:
439: /**
440: * Checks, whether the child directory is a subdirectory of the base
441: * directory.
442: *
443: * @param base the base directory.
444: * @param child the suspected child directory.
445: * @return true, if the child is a subdirectory of the base directory.
446: * @throws IOException if an IOError occured during the test.
447: */
448: public boolean isSubDirectory(File base, File child)
449: throws IOException {
450: base = base.getCanonicalFile();
451: child = child.getCanonicalFile();
452:
453: File parentFile = child;
454: while (parentFile != null) {
455: if (base.equals(parentFile)) {
456: return true;
457: }
458: parentFile = parentFile.getParentFile();
459: }
460: return false;
461: }
462: }
|