001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     * 
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *      
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *          
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *  
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/zipUtilities/JarUtilities.java,v 1.2 2004/07/01 05:50:21 tj_willis Exp $
019     */
020    package net.sourceforge.pavlov.zipUtilities;
021    
022    import java.util.zip.ZipEntry;
023    import java.io.File;
024    import java.net.*;
025    import org.apache.log4j.*;
026    
027    //FIXED: add to CVS
028    
029    public class JarUtilities {
030    
031        /**
032         * Return true if it looks like a JAR file.
033         */
034        public static boolean isAJar(final File g) {
035            // FIXME: logic can be improved on
036            if(g==null)
037                return false;
038            String gg = g.getName();
039            if(gg==null)
040                return false;
041            if(gg.endsWith(".jar"))
042                return true;
043            if(gg.endsWith(".JAR"))
044                return true;
045            if(gg.endsWith(".Jar"))
046                return true;
047            if(gg.indexOf('!')>0)
048                return true;
049            return false;
050        }
051    
052        public static boolean isInAJar(final File f) {
053            // FIXME: logic can be improved on
054            String x = f.getAbsolutePath(); //directory
055            if(x.indexOf('!')>0)
056                return true;
057            return false;
058        }
059    
060        public static boolean isInAJar(final URL url) {
061            // FIXME: logic can be improved on
062            if(url==null)
063                return false;
064            String bar = url.toString();
065            if(bar==null)
066                return false;
067            //FIXME: if url.getProtocol().equals("jar")
068            if(bar.startsWith("jar:file:"))
069                return true;
070            return false;
071        }
072    
073    
074        public static URL makeJarURL(final String jarFile, final String entry)
075        throws MalformedURLException {
076            String s = "jar:file:" + jarFile +"!/" + entry;
077            return new URL(s);
078        }
079    
080        public static String getJarFileName(final String jarURL) {
081            String foo = new String(jarURL);
082            int y0 = foo.indexOf('!');
083            if(y0>0) {
084                String bb = foo.substring(0,y0);
085                foo = bb;
086    
087            }
088            y0 = foo.indexOf(':');
089            if(y0>0) {
090                String bb = foo.substring(y0+1);
091                foo = bb;
092    
093            }
094            return foo;
095        }
096    
097    }