package ca.umontreal.iro.rali; import java.util.HashMap; import java.util.Map; /** * Parsing of command line options and keep their values that can be shared by the Explore and Extract classes. * * @author Guy Lapalme, Université de Montréal, 2009 * */ public class Options { protected static final String XML_NS = "http://www.w3.org/2000/xmlns/"; // values set by command line arguments // common /** * Global option (-d): will show debugging information during execution */ public static boolean debug=false; /** * Global option (-st): will show statistics information at the end of execution */ protected static boolean statistics=false; /** * Last parameter: Name of the input file, if it is null then standard input is used */ protected static String inputFileName=null; /** * Global option (-o): Name of the output file, if it is null then standard output is used */ protected static String outputFileName=null; protected static boolean sax=false; // Xplore /** * Explore option (-a): output attribute names in XPath expression */ protected static boolean outAttr=false; /** * Explore option (-av): output attribute names and values in XPath expression */ protected static boolean outAttrVal=false; /** * Explore option (-v): output element values in XPath expression */ protected static boolean outVal=false; /** * Explore option (-sa): sort the XPath expressions alphabetically */ protected static boolean sortAlpha=false; /** * Explore option (-sf): sort the XPath expression by decreasing frequency */ protected static boolean sortFreq=false; /** * Explore option (-ns): show the values of the namespaces used in XPath expressions */ protected static boolean showNamespaces=false; // Xtract /** * Extract option (-xp): XPath expression used to select nodes to extract */ protected static String xpathStr=null; /** * Extract option (-xsl): XSLT stylesheet to apply to each extracted node */ protected static String xsltFile=null; /** * Extract option (-xslf): XSLT stylesheet to apply to the resulting document */ protected static String xsltFileFinal=null; /** * List of parameters for the xsl stylesheets (they are set to both individual and final stylesheets) */ protected static Map xslParams; /** * Extract option (-r): name of the root element (root if not given) */ protected static String rootName="root"; /** * Extract option (-c): return only the number of extracted nodes */ protected static boolean onlyCount=false; /** * Extract option (-pp): output a prettyprinted version of the extracted node */ protected static boolean prettyPrint=false; /** * Extract option (-max): stop reading the file after a given number of results (infinite if not specified) */ protected static int maxRes = -1; // ie infinite... /** * display of usage of the program with the list of possible command line arguments */ private static String usageStr="usage: [-h] [-d] [-st] [-o output file name]" + // common options "[-a] [-av] [-v] [-sf] [-sa] [-ns] " + // xplore options // xtract options "[-xp XPathExpr] [-xpt XPathExpr] [-max N] [-xsl xsltFile] [-xslf xsltFile] [-r rootname] [-nr] [-c] [-pp] {-xmlns:pref=URI} "+ "[input file name] "+ "{param_i=val_i}"; private static String moreHelp="\n Use -h option for more details"; /** * Parse program arguments and set appropriate options * will stop on -h : prints usage of the program * -xpt: prints the xpath expression as it is seen by the program * will stop on an unrecognized arguments * * @param args String array as given to the main program */ protected static void parseArgs(String[] args){ int i=0; String arg; while (i"+args[i++]+"<"); System.exit(1); } else if (arg.equals("-xsl"))xsltFile=args[i++]; else if (arg.equals("-xslf"))xsltFileFinal=args[i++]; else if (arg.equals("-r")) rootName=args[i++]; else if (arg.equals("-nr"))rootName=null; else if (arg.equals("-c")) onlyCount=true; else if (arg.equals("-pp")) prettyPrint=true; else if (arg.equals("-max")){ try { maxRes=Integer.parseInt(args[i++]); } catch (NumberFormatException e){ System.out.println("Error in number parsing for -max option :"+args[i-1]); } } else if (arg.startsWith("-xmlns:")){ String[]nsparts=arg.substring(7).split("="); if(nsparts.length!=2){ System.err.println("-xmlns option should of the form xmlns:prefix=URI"); System.exit(1); } nsContext.addPrefixURI(nsparts[0], nsparts[1]); } else { System.err.println("illegal option:"+arg); System.err.println(usageStr+moreHelp); System.exit(1); } } if(i(); while (i