org.opensourcephysics.tools
Class JarTool

java.lang.Object
  extended by org.opensourcephysics.tools.JarTool
All Implemented Interfaces:
java.lang.Runnable, java.rmi.Remote, Tool

public class JarTool
extends java.lang.Object
implements Tool, java.lang.Runnable

This provides a simple way to package files in a single JAR or ZIP file

Version:
1.0
Author:
Francisco Esquembre (http://fem.um.es), Doug Brown

Field Summary
static int NO
           
static int NO_TO_ALL
           
static int YES
           
static int YES_TO_ALL
           
 
Method Summary
static void alwaysOverwrite()
          Sets the overwrite feature to true.
 java.io.File append(java.util.ArrayList<java.lang.String> sources, java.io.File parent, java.lang.String target)
          Appends to an existing compressed file the list of contents provided.
static boolean compress(java.util.ArrayList<java.io.File> sources, java.io.File target, java.util.jar.Manifest manifest)
          Compresses a list of files and/or directories into a single JAR or ZIP file.
static boolean compress(java.io.File source, java.io.File target, java.util.jar.Manifest manifest)
          Compresses a directory into a single JAR or ZIP file.
static int confirmOverwrite(java.lang.String filename)
          Whether to overwrite an existing file.
static boolean copy(java.io.File source, java.io.File target)
          Copies a file.
 java.io.File create(java.util.ArrayList<java.lang.String> sources, java.io.File parent, java.io.File target, java.util.jar.Manifest manifest)
          Creates a JAR file out of the list of contents provided.
static java.util.jar.Manifest createManifest(java.lang.String classpath, java.lang.String mainclass)
          Creates a Manifest for a JAR file with the given parameters
static void disableWarning()
          This method is kept for backwards compatibility.
static java.io.File extract(java.lang.ClassLoader classLoader, java.lang.String filename, java.io.File target)
          Extracts a file using the given class loader
static java.io.File extract(java.io.File source, java.lang.String filename, java.io.File target)
          Extracts a given file from a compressed (ZIP or JAR) file Extensive changes by D Brown 2007-10-31
static java.io.File extract(java.io.File source, java.lang.String filename, java.lang.String destination)
          Extracts a given file from a compressed (ZIP or JAR) file
static boolean extract(java.lang.Object source, java.util.List<?> files, java.io.File targetDirectory)
          Extract a list of files (given by their relative names) to the given target directory.
static java.io.File extract(java.lang.String filename, java.io.File target)
          Extracts a file using the ResourceLoader utility
static java.util.Collection<java.io.File> getContents(java.io.File directory)
          Returns all the files under a given directory
static java.util.jar.Manifest getManifest(java.io.File file)
          Gets the Manifest of an existing JAR file
static java.lang.String getString(java.lang.String key)
           
static JarTool getTool()
          Gets the shared JarTool.
static void neverOverwrite()
          Sets the overwrite feature to false.
static boolean remove(java.io.File directory)
          Completely removes a directory (without warning!)
 void run()
           
 void send(Job job, Tool replyTo)
          Sends a job to this tool and specifies a tool to reply to.
static void setLocale(java.util.Locale locale)
           
static void setOwnerFrame(java.awt.Frame owner)
          Sets the owner frame for progress dialogs that may appear when creating a JAR file.
static boolean unzip(java.io.File source, java.io.File targetDirectory)
          Uncompresses a ZIP or JAR file into a given directory.
static boolean unzipNoOverwrite(java.io.File source, java.io.File targetDirectory)
          Uncompresses a ZIP or JAR file into a given directory.
static boolean unzipWithAWarning(java.io.File source, java.io.File targetDirectory)
          Uncompresses a ZIP or JAR file into a given directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

YES

public static final int YES
See Also:
Constant Field Values

NO

public static final int NO
See Also:
Constant Field Values

YES_TO_ALL

public static final int YES_TO_ALL
See Also:
Constant Field Values

NO_TO_ALL

public static final int NO_TO_ALL
See Also:
Constant Field Values
Method Detail

setLocale

public static void setLocale(java.util.Locale locale)

getString

public static java.lang.String getString(java.lang.String key)

getTool

public static JarTool getTool()
Gets the shared JarTool.

Returns:
the shared JarTool

run

public void run()
Specified by:
run in interface java.lang.Runnable

send

public void send(Job job,
                 Tool replyTo)
          throws java.rmi.RemoteException
Sends a job to this tool and specifies a tool to reply to.

Specified by:
send in interface Tool
Parameters:
job - the Job
replyTo - the tool to notify when the job is complete (may be null)
Throws:
java.rmi.RemoteException

disableWarning

public static void disableWarning()
This method is kept for backwards compatibility. It is equivalent to alwaysOverwrite().


neverOverwrite

public static void neverOverwrite()
Sets the overwrite feature to false. By default, the packaging methods create() and append() will warn the user before overwritting a duplicated file. Calling this method before running those methods disables this warning and makes sure a new file will NEVER overwrite an older one. The methods create() and append() set the warning feature back.

See Also:
alwayOverwrite

alwaysOverwrite

public static void alwaysOverwrite()
Sets the overwrite feature to true. By default, the packaging methods create() and append() will warn the user before overwritting a duplicated file. Calling this method before running those methods disables this warning AND makes sure a new file will ALWAYS overwrite an older one. The methods create() and append() set the warning feature back.

See Also:
neverOverwrite

setOwnerFrame

public static void setOwnerFrame(java.awt.Frame owner)
Sets the owner frame for progress dialogs that may appear when creating a JAR file.

Parameters:
owner - Frame

create

public java.io.File create(java.util.ArrayList<java.lang.String> sources,
                           java.io.File parent,
                           java.io.File target,
                           java.util.jar.Manifest manifest)
Creates a JAR file out of the list of contents provided. Each entry in the sources list can be either a single file, a directory, or a compressed (ZIP or JAR) file. Regular files and directories must exist under the given parent directory, and are saved with the same relative path as provided. As an exception, you can also add files in parent directories, such as, "../../../WhateverDir/WhateverFile.xml", but ALL leading "../" will be removed when saved. Directories are added recursively. Compressed files can, on the contrary, be placed anywhere and their contents are stored with the same directory structure as in the original jar file, irrespective of the original location of the source compressed file. If a file appears more than once, succesive files could overwrite previous ones. The user will be warned of this, except if either neverOverwrite() or alwaysOverwrite() have been invoked immediately before calling this method. (Calling this method resets the warning feature back.)

Parameters:
sources - ArrayList The list of content files to add. Each item in the list is a String with the relative name of a file or directory under the given parent directory, or of a compressed file anywhere in the hard disk.
parent - File The parent directory for all relative filenames
target - File The target compressed file. Its name must ends either in .zip or .jar. The user will be prompted to confirm the target.
manifest - Manifest A manifest for the newly created JAR file.
Returns:
File The file that will eventually be created. Note that the main work is done using a separate thread, hence the method returns BEFORE the JAR file is actually created.
See Also:
Manifest

append

public java.io.File append(java.util.ArrayList<java.lang.String> sources,
                           java.io.File parent,
                           java.lang.String target)
Appends to an existing compressed file the list of contents provided. Works similarly to create(), but uses an existing compressed file and respects its manifest (if a JAR file).

Parameters:
sources - ArrayList The list of content files to add. Each item in the list is a String with the relative name of a file or directory under the current parent directory, or of a compressed file anywhere in the hard disk.
parent - File The parent directory for all relative filenames
target - String The name of an existing compressed file, relative to the parent directory.

createManifest

public static java.util.jar.Manifest createManifest(java.lang.String classpath,
                                                    java.lang.String mainclass)
Creates a Manifest for a JAR file with the given parameters

Parameters:
classpath - String
mainclass - String
Returns:
Manifest

getManifest

public static java.util.jar.Manifest getManifest(java.io.File file)
Gets the Manifest of an existing JAR file

Parameters:
file - File the jar file from which to obtain the manifest
Returns:
Manifest the manifest found, null if failed.

extract

public static java.io.File extract(java.io.File source,
                                   java.lang.String filename,
                                   java.lang.String destination)
Extracts a given file from a compressed (ZIP or JAR) file

Parameters:
source - File The compressed file to extract the file from
filename - String The path of the file to extract
destination - String The full (or relative to whatever the current user directory is) path where to save the extracted file
Returns:
File The extracted file, null if failed

extract

public static java.io.File extract(java.io.File source,
                                   java.lang.String filename,
                                   java.io.File target)
Extracts a given file from a compressed (ZIP or JAR) file Extensive changes by D Brown 2007-10-31

Parameters:
source - File The compressed file to extract the file from
filename - String The path of the file to extract
target - File The target file for the extracted file
Returns:
File The extracted file, null if failed

extract

public static java.io.File extract(java.lang.ClassLoader classLoader,
                                   java.lang.String filename,
                                   java.io.File target)
Extracts a file using the given class loader

Parameters:
_classLoader - ClassLoader The class loader to extract the files from
filename - String The path of the file to extract
target - File The target file for the extracted file
Returns:
File The extracted file, null if failed

extract

public static java.io.File extract(java.lang.String filename,
                                   java.io.File target)
Extracts a file using the ResourceLoader utility

Parameters:
filename - String The path of the file to extract
target - File The target file for the extracted file
Returns:
File The extracted file, null if failed

extract

public static boolean extract(java.lang.Object source,
                              java.util.List<?> files,
                              java.io.File targetDirectory)
Extract a list of files (given by their relative names) to the given target directory. If files exist, the user will be warned.

Parameters:
source - Object Either a compressed java.io.File with the given resources, a ClassLoader object which will be used to extract the files, or null, in which case, the ResourceLoader will be used.
files - AbstractList The list of String with the relative names of the files to extract
targetDirectory - File The target directory where to extract the files
Returns:
boolean

copy

public static boolean copy(java.io.File source,
                           java.io.File target)
Copies a file. If the target file exists, it will be overwritten.

Parameters:
source - File The file to copy
target - File destination file
Returns:
boolean true if successful

compress

public static boolean compress(java.io.File source,
                               java.io.File target,
                               java.util.jar.Manifest manifest)
Compresses a directory into a single JAR or ZIP file. If the target file exists it will be overwritten.

Parameters:
source - File The directory to compress
target - File The output file
manifest - Manifest The manifest (in case of a JAR file)
Returns:
boolean

compress

public static boolean compress(java.util.ArrayList<java.io.File> sources,
                               java.io.File target,
                               java.util.jar.Manifest manifest)
Compresses a list of files and/or directories into a single JAR or ZIP file. All files/dirs must be in the same directory. If the target file exists it will be overwritten.

Parameters:
sources - ArrayList The list of files or directories to compress
target - File The output file
manifest - Manifest The manifest (in case of a JAR file)
Returns:
boolean

remove

public static boolean remove(java.io.File directory)
Completely removes a directory (without warning!)

Parameters:
directory - File The directory to delete

getContents

public static java.util.Collection<java.io.File> getContents(java.io.File directory)
Returns all the files under a given directory

Parameters:
directory - File
Returns:
ArrayList

unzip

public static boolean unzip(java.io.File source,
                            java.io.File targetDirectory)
Uncompresses a ZIP or JAR file into a given directory. Duplicated files will be overwritten.

Parameters:
source - File The compressed file to uncompress
targetDirectory - File The target directory
Returns:
boolean
See Also:
unzipNoOverwrite, unzipWithAWarning

unzipNoOverwrite

public static boolean unzipNoOverwrite(java.io.File source,
                                       java.io.File targetDirectory)
Uncompresses a ZIP or JAR file into a given directory. Duplicated files will NOT be overwriten.

Parameters:
source - File The compressed file to uncompress
targetDirectory - File The target directory
Returns:
boolean
See Also:
unzip, unzipWithAWarning

unzipWithAWarning

public static boolean unzipWithAWarning(java.io.File source,
                                        java.io.File targetDirectory)
Uncompresses a ZIP or JAR file into a given directory. The system will issue a warning before duplicating existing files.

Parameters:
source - File The compressed file to uncompress
targetDirectory - File The target directory
Returns:
boolean
See Also:
unzip, unzipNoOverwrite

confirmOverwrite

public static int confirmOverwrite(java.lang.String filename)
Whether to overwrite an existing file.

Parameters:
file - File
Returns:
boolean