    001    package org.maltparser.core.helper;
    002
    003
    004    import java.io.File;
    005    import java.io.IOException;
    006    import java.io.InputStream;
    007    import java.net.MalformedURLException;
    008    import java.net.URL;
    009
    010    import org.maltparser.core.exception.MaltChainedException;
    011    import org.maltparser.core.plugin.Plugin;
    012    import org.maltparser.core.plugin.PluginLoader;
    013
    014    /**
    015    *
    016    *
    017    * @author Johan Hall
    018    */
    019    public class URLFinder {
    020            /**
    021             * Search for a file according the following priority:
    022             * <ol>
    023             * <li>The local file system
    024             * <li>Specified as an URL (starting with http:, file:, ftp: or jar:
    025             * <li>MaltParser distribution file (malt.jar)
    026             * <li>MaltParser plugins
    027             * </ol>
    028             *
    029             * If the file string is found, an URL object is returned, otherwise <b>null</b>
    030             *
    031             * @param fileString    the file string to convert into an URL.
    032             * @return an URL object, if the file string is found, otherwise <b>null</b>
    033             * @throws MaltChainedException
    034             */
    035            public URL findURL(String fileString) throws MaltChainedException {
    036                    File specFile = new File(fileString);
    037
    038                    try {
    039                            if (specFile.exists()) {
    040                                    // found the file in the file system
    041                                    return new URL("file:///"+specFile.getAbsolutePath());
    042                            } else if (fileString.startsWith("http:") || fileString.startsWith("file:") || fileString.startsWith("ftp:") || fileString.startsWith("jar:")) {
    043                                    // the input string is an URL string starting with http, file, ftp or jar
    044                                    return new URL(fileString);
    045                            } else {
    046                                    return findURLinJars(fileString);
    047                            }
    048                    } catch (MalformedURLException e) {
    049                            throw new MaltChainedException("Malformed URL: "+fileString, e);
    050                    }
    051            }
    052
    053            public URL findURLinJars(String fileString) throws MaltChainedException {
    054                    try {
    055                            // search in malt.jar and its plugins
    056                            if (getClass().getResource(fileString) != null) {
    057                                    // found the input string in the malt.jar file
    058                                    return getClass().getResource(fileString);
    059                            } else {
    060                                     for (Plugin plugin : PluginLoader.instance()) {
    061                                            URL url = null;
    062                                            if (!fileString.startsWith("/")) {
    063                                                    url = new URL("jar:"+plugin.getUrl() + "!/" + fileString);
    064                                            } else {
    065                                                    url = new URL("jar:"+plugin.getUrl() + "!" + fileString);
    066                                            }
    067
    068                                            try {
    069                                                    InputStream is = url.openStream();
    070                                                    is.close();
    071                                            } catch (IOException e) {
    072                                                    continue;
    073                                            }
    074                                            // found the input string in one of the plugins
    075                                            return url;
    076                                    }
    077                                    // could not convert the input string into an URL
    078                                    return null;
    079                            }
    080                    } catch (MalformedURLException e) {
    081                            throw new MaltChainedException("Malformed URL: "+fileString, e);
    082                    }
    083            }
    084    }