home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Main.bin
/
TypeValidator.java
< prev
next >
Wrap
Text File
|
1998-11-03
|
6KB
|
261 lines
package com.symantec.itools.tools.archive;
import java.io.File;
import com.symantec.itools.io.FileSystem;
import com.symantec.itools.lang.Classpath;
/**
* @author Symantec Internet Tools Division
* @version 1.0
* @since VCafe 3.0
*/
public abstract class TypeValidator
{
/**
* @since VCafe 3.0
*/
protected Options options;
/**
* @since VCafe 3.0
*/
protected boolean canceled;
protected TypeValidator(Options opts)
{
options = opts;
}
/**
* @param errorMsg TODO
* @since VCafe 3.0
*/
public abstract boolean validate(StringBuffer errorMsg);
/**
* @param file TODO
* @param errorMsg TODO
* @since VCafe 3.0
*/
public boolean validate(boolean file, StringBuffer errorMsg)
{
if(file)
{
if(!(validateOutputFile(errorMsg)))
{
return (false);
}
}
else
{
if(!(validateOutputDirectory(errorMsg)))
{
return (false);
}
}
if(!(validateInput(errorMsg)))
{
return (false);
}
if(!(validateFiles(errorMsg)))
{
return (false);
}
// THIS MAKES US _WAY_ TOO SLOW - it is handled in the Dir copy now
// if(!(validateEntries(errorMsg)))
// {
// return (false);
// }
return (true);
}
/**
* @param errorMsg TODO
* @since VCafe 3.0
*/
protected boolean validateOutputFile(StringBuffer errorMsg)
{
String fileName;
File file;
fileName = options.getOutputLocation();
if(fileName == null)
{
errorMsg.append("You must specify an output file");
return (false);
}
if(!(fileName.toLowerCase().endsWith('.' + options.getType())))
{
fileName += '.' + options.getType();
options.setOutputLocation(fileName);
}
file = new File(FileSystem.getCanonicalPath(fileName, true, false));
if(file.exists())
{
if(file.isDirectory())
{
errorMsg.append(FileSystem.getCanonicalPath(file, true)).append(" is a directory");
return (false);
}
if(!(options.isOverwrite()))
{
errorMsg.append(FileSystem.getCanonicalPath(file, true)).append(" exists already you must specify that you want to overwrite it");
return (false);
}
// jdk 1.1.5 (and higher?) allow you to delete read only files on
// (WinNT at least)... need to check that it isn't writeable before
// deleting it
if((!(file.canWrite())) && (!(file.delete())))
{
errorMsg.append("Unable to delete ").append(FileSystem.getCanonicalPath(file, true));
return (false);
}
}
else
{
file = new File(file.getParent());
if(!(file.exists()))
{
if(!(file.mkdirs()))
{
errorMsg.append("Unable to create ").append(FileSystem.getCanonicalPath(file, true));
return (false);
}
}
}
return (true);
}
/**
* @param errorMsg TODO
* @since VCafe 3.0
*/
protected boolean validateOutputDirectory(StringBuffer errorMsg)
{
String dirName;
File file;
dirName = options.getOutputLocation();
if(dirName == null)
{
errorMsg.append("You must specify an output directory");
return (false);
}
file = new File(FileSystem.getCanonicalPath(dirName, true));
if(file.exists())
{
if(file.isFile())
{
errorMsg.append(dirName).append(" is a file");
return (false);
}
}
return (true);
}
/**
* @param errorMsg TODO
* @since VCafe 3.0
*/
protected boolean validateInput(StringBuffer errorMsg)
{
if((options.getFiles().length == 0) && (options.getEntries().length == 0))
{
errorMsg.append("no files or entries specified");
return (false);
}
return (true);
}
/**
* @param errorMsg TODO
* @since VCafe 3.0
*/
protected boolean validateFiles(StringBuffer errorMsg)
{
String[] files;
files = options.getFiles();
for(int i = 0; i < files.length; i++)
{
if(canceled)
{
return (false);
}
if(!(new File(files[i]).exists()))
{
errorMsg.append("File ").append(files[i]).append(" does not exist");
return (false);
}
}
return (true);
}
/**
* @param errorMsg TODO
* @since VCafe 3.0
*/
protected boolean validateEntries(StringBuffer errorMsg)
{
String[] entries;
Classpath classpath;
entries = options.getEntries();
classpath = new Classpath(options.getClasspath());
for(int i = 0; i < entries.length; i++)
{
if(canceled)
{
return (false);
}
if(classpath.find(entries[i]) == null)
{
errorMsg.append("Entry ").append(entries[i]).append(" does not exist");
return (false);
}
}
return (true);
}
/**
* @since VCafe 3.0
*/
public void cancel()
{
canceled = true;
}
}