Returns true if the URL represents a path, and false otherwise.
import java.io.File;
import java.net.URL;
/*
* JCommon : a free general purpose class library for the Java(tm) platform
*
*
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jcommon/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------
* IOUtils.java
* ------------
* (C)opyright 2002-2004, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Object Refinery Limited);
*
* $Id: IOUtils.java,v 1.8 2009/01/22 08:34:58 taqua Exp $
*
* Changes
* -------
* 26-Jan-2003 : Initial version
* 23-Feb-2003 : Documentation
* 25-Feb-2003 : Fixed Checkstyle issues (DG);
* 29-Apr-2003 : Moved to jcommon
* 04-Jan-2004 : Fixed JDK 1.2.2 issues with createRelativeURL;
* added support for query strings within these urls (TM);
*/
/**
* The IOUtils provide some IO related helper methods.
*
* @author Thomas Morgner.
*/
public class Main {
/**
* Returns <code>true</code> if the URL represents a path, and
* <code>false</code> otherwise.
*
* @param baseURL the URL.
*
* @return A boolean.
*/
private boolean isPath(final URL baseURL) {
if (getPath(baseURL).endsWith("/")) {
return true;
}
else if (baseURL.getProtocol().equals("file")) {
final File f = new File(getPath(baseURL));
try {
if (f.isDirectory()) {
return true;
}
}
catch (SecurityException se) {
// ignored ...
}
}
return false;
}
/**
* Implements the JDK 1.3 method URL.getPath(). The path is defined
* as URL.getFile() minus the (optional) query.
*
* @param url the URL
* @return the path
*/
private String getPath (final URL url) {
final String file = url.getFile();
final int queryIndex = file.indexOf('?');
if (queryIndex == -1) {
return file;
}
return file.substring(0, queryIndex);
}
}
Related examples in the same category