package net.sf.jpluck.apps.cmdline;
import java.io.File;
import net.sf.jpluck.apps.ExitCodes;
import net.sf.jpluck.apps.OptionsUtil;
import net.sf.jpluck.jxl.Document;
import net.sf.jpluck.jxl.JXL;
import net.sf.jpluck.util.RandomGUID;
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.w3c.dom.Element;
public class AutoID {
public static void main(String[] args) {
try {
Options options = OptionsUtil.createGeneric();
CommandLine cl = new GnuParser().parse(options, args);
args = cl.getArgs();
if (args.length != 1) {
System.out.println("ERROR: Invalid number of arguments");
System.exit(ExitCodes.ERROR_INVALID_NUMBER_OF_ARGUMENTS);
}
if (cl.hasOption("help")) {
OptionsUtil.printHelp("java -jar autoid.jar <jxl filename>",
"Generates and assigns unique IDs to sites and feeds in JXLs", options);
System.exit(ExitCodes.OK);
}
System.out.println("Opening " + args[0]);
JXL jxl = new JXL(new File(args[0]));
Document[] documents = jxl.getDocuments();
System.out.println("Generating IDs.");
int count = 0;
for (int i = 0; i < documents.length; i++) {
Element elem = documents[i].getElement();
if (!elem.hasAttribute("id")) {
RandomGUID randomGUID = new RandomGUID();
elem.setAttribute("id", randomGUID.toString());
count++;
}
}
if (count > 0) {
System.out.println("Generated " + count + " new ID(s).");
} else {
System.out.println("No new IDs generated.");
}
System.out.println("Saving JXL.");
jxl.save();
System.out.println("Done!");
} catch (ParseException e) {
System.out.println("ERROR: " + e.getMessage());
System.exit(ExitCodes.UNSPECIFIED);
} catch (Exception e) {
e.printStackTrace();
System.exit(ExitCodes.UNSPECIFIED);
}
}
}