package net.sf.jpluck.apps.cmdline;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import net.sf.jpluck.ClientConfiguration;
import net.sf.jpluck.VersionInfo;
import net.sf.jpluck.apps.ExitCodes;
import net.sf.jpluck.apps.OptionsUtil;
import net.sf.jpluck.conversion.Conversion;
import net.sf.jpluck.conversion.Destination;
import net.sf.jpluck.jxl.Document;
import net.sf.jpluck.jxl.JXL;
import net.sf.jpluck.util.LogUtil;
import net.sf.jpluck.util.SystemOutHandler;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.xml.sax.InputSource;
public class JPluckC {
public static void main(String[] arguments) {
try {
System.setProperty("java.awt.headless", "true");
Options options = OptionsUtil.createGeneric();
options.addOption("destination", true, "Destination directory for PDBs. (This overrides the default destination.)");
options.addOption("ignoreschedule", false,
"Ignore schedule. Convert documents regardless of whether they are due or not.");
options.addOption("name", true,
"Convert named site or feed. Use multiple -name options to specify multiple documents.");
options.addOption("stdin", false, "Pass JXL through stdin.");
CommandLine cl = new GnuParser().parse(options, arguments);
if (cl.hasOption("help")) {
OptionsUtil.printHelp("java -jar jpluckc.jar [options] <jxl files>",
"Converts sites and feeds to Plucker documents.", options);
System.exit(ExitCodes.OK);
}
if (cl.hasOption("version")) {
OptionsUtil.printVersion("JPluckC", VersionInfo.VERSION, VersionInfo.RELEASE_DATE);
System.exit(ExitCodes.OK);
}
LogUtil logUtil = new LogUtil(LogUtil.DEFAULT_LOGGER_NAMES);
logUtil.addHandler(new SystemOutHandler());
logUtil.setUseParentHandler(false);
String[] args = cl.getArgs();
JXL jxl = null;
Destination destination = ClientConfiguration.getDefault().getDestination();
if (cl.hasOption("destination")) {
File dir = new File(cl.getOptionValue("destination"));
if (!dir.exists()) {
System.err.println("ERROR: destination directory " + dir.getAbsolutePath() + " \"does not exist\".");
System.exit(-1);
}
dir = dir.getCanonicalFile();
destination = Destination.createForDirectory(dir);
}
File file = null;
if (cl.hasOption("stdin")) {
URI uri = new File(System.getProperty("user.dir")).toURI();
if (cl.hasOption("uri")) {
uri = uri.resolve(cl.getOptionValue("uri"));
System.err.println(uri);
} else {
uri = uri.resolve("stdin.jxl");
}
InputSource inputSource = new InputSource();
inputSource.setByteStream(System.in);
inputSource.setSystemId(uri.toString());
jxl = new JXL(inputSource);
} else {
if ((args.length == 0) && !cl.hasOption("stdin")) {
System.err.println("ERROR: JXL not specified.");
System.exit(ExitCodes.ERROR_INVALID_NUMBER_OF_ARGUMENTS);
}
file = new File(args[0]);
jxl = new JXL(file);
}
String[] names = cl.getOptionValues("name");
List documentList = new ArrayList();
if (names != null) {
for (int i = 0; i < names.length; i++) {
String name = names[i];
Document document = jxl.getDocument(name);
if (document == null) {
System.err.println("ERROR: Document \"" + name + "\" not found.");
System.exit(1);
}
documentList.add(document);
}
} else {
documentList.addAll(Arrays.asList(jxl.getDocuments()));
if (documentList.size() == 0) {
System.out.println("No documents found in JXL.");
System.exit(0);
}
}
if (!cl.hasOption("ignoreschedule")) {
List newDocumentList = new ArrayList();
for (Iterator it = documentList.iterator(); it.hasNext();) {
Document document = (Document) it.next();
if (!document.isScheduled() || document.isDue()) {
newDocumentList.add(document);
}
}
documentList = newDocumentList;
}
if (documentList.size() == 0) {
System.out.println("No documents are due.");
System.exit(0);
}
Document[] documents = (Document[]) documentList.toArray(new Document[documentList.size()]);
Conversion conversion = new Conversion(documents, destination);
conversion.run();
System.exit(0);
} catch (FileNotFoundException e) {
System.err.println("ERROR: " + e.getClass().getName() + ": " + e.getMessage());
System.exit(ExitCodes.ERROR_FILE_NOT_FOUND);
} catch (ParseException e) {
System.err.println("ERROR: " + e.getClass().getName() + ": " + e.getMessage());
System.exit(ExitCodes.ERROR_INVALID_OPTION);
} catch (Exception e) {
System.err.println("ERROR: " + e.getClass().getName() + ": " + e.getMessage());
e.printStackTrace();
System.exit(ExitCodes.UNSPECIFIED);
}
}
}