commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly maven.xml project.xml
Date Fri, 04 Oct 2002 11:28:20 GMT
jstrachan    2002/10/04 04:28:20

  Modified:    jelly/src/java/org/apache/commons/jelly Jelly.java
               jelly    maven.xml project.xml
  Added:       jelly/src/test/org/apache/commons/jelly
                        testCmdLineOptions.jelly
  Log:
  Committed patches submitted by Jason Horman to provide a decent command line interface using
commons-cli to Jelly.
  
  So usage is like this...
  Usage: jelly [scriptFile] [-script scriptFile -o outputFile  -Dsysprop=syspropval]
  
  Additional command line options are available in jelly scripts by using the
  commandLine.getOptionValue(optionName) method. Args is also still available.
  
  context.setVariable("args", args);
  context.setVariable("commandLine", cmdLine);
  
  
  Also there's a sample target to demonstrate this in action. Type
  
  	maven demo:cmdline
  
  and you'll see Jason's test script run, using beanshell in a Jelly script to display the
options and system properties etc.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/testCmdLineOptions.jelly
  
  Index: testCmdLineOptions.jelly
  ===================================================================
  <jelly xmlns="jelly:core" xmlns:bsh="jelly:beanshell">
  -a option = ${commandLine.getOptionValue("a")}
  -b option = ${commandLine.getOptionValue("b")}
  -c option = ${commandLine.getOptionValue("c")}
  <bsh:script>
  Properties sysprops = System.getProperties();
  System.out.println("-testsysprop = " + sysprops.get("testsysprop"));
  </bsh:script>
  </jelly>
  
  
  1.17      +100 -22   jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java
  
  Index: Jelly.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- Jelly.java	2 Oct 2002 13:56:23 -0000	1.16
  +++ Jelly.java	4 Oct 2002 11:28:20 -0000	1.17
  @@ -61,26 +61,30 @@
    */
   
   package org.apache.commons.jelly;
  -import java.io.BufferedWriter;
   import java.io.File;
   import java.io.FileWriter;
   import java.io.InputStream;
   import java.io.FileInputStream;
  -import java.io.OutputStreamWriter;
  -import java.io.Writer;
   import java.net.MalformedURLException;
   import java.net.URL;
   import java.util.Enumeration;
   import java.util.Properties;
  +import java.util.StringTokenizer;
  +import java.util.ArrayList;
   
   import org.apache.commons.jelly.parser.XMLParser;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +import org.apache.commons.cli.*;
   
   /** 
    * <p><code>Jelly</code> is a helper class which is capable of
    * running a Jelly script. This class can be used from the command line
  - * or can be used as the basis of an Ant task.</p>
  + * or can be used as the basis of an Ant task.</p> Command line usage is as follows:
  + *
  + * <pre>
  + * jelly [scriptFile] [-script scriptFile -o outputFile -Dsysprop=syspropval]
  + * </pre>
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision$
  @@ -104,31 +108,49 @@
           
       public Jelly() {
       }
  -    
  +
  +    /**
  +     * Usage: jelly [scriptFile] [-script scriptFile -o outputFile -Dsysprop=syspropval]
  +     */
       public static void main(String[] args) throws Exception {
   
           try {
               if (args.length <= 0) {
  -                System.out.println("Usage: Jelly scriptFile [outputFile]");
  +                System.out.println("Usage: jelly [scriptFile] [-script scriptFile -o outputFile
-Dsysprop=syspropval]");
                   return;
               }
   
  -            Jelly jelly = new Jelly();
  -            jelly.setScript(args[0]);
  +            // parse the command line options using CLI
  +            CommandLine cmdLine = parseCommandLineOptions(args);
  +
  +            // get the -script option. If there isn't one then use args[0]
  +            String scriptFile = null;
  +            if (cmdLine.hasOption("script")) {
  +                scriptFile = cmdLine.getOptionValue("script");
  +            } else {
  +                scriptFile = args[0];
  +            }
   
  -            // later we might wanna add some command line arguments 
  -            // checking stuff using commons-cli to specify the output file
  -            // and input file via command line arguments
  -            final XMLOutput output =
  -                (args.length > 1)
  -                    ? XMLOutput.createXMLOutput(new FileWriter(args[1]))
  -                    : XMLOutput.createXMLOutput(System.out);
  +            // check if the script file exists
  +            if (!(new File(scriptFile)).exists()) {
  +                System.out.println("Script file " + scriptFile + " not found");
  +                return;
  +            }
  +
  +            // extract the -o option for the output file to use
  +            final XMLOutput output = cmdLine.hasOption("o") ?
  +                    XMLOutput.createXMLOutput(new FileWriter(cmdLine.getOptionValue("o")))
:
  +                    XMLOutput.createXMLOutput(System.out);
  +
  +            Jelly jelly = new Jelly();
  +            jelly.setScript(scriptFile);
   
               Script script = jelly.compileScript();
   
               // add the system properties and the command line arguments
               JellyContext context = jelly.getJellyContext();
               context.setVariable("args", args);
  +            context.setVariable("commandLine", cmdLine);
               script.run(context, output);
   
               // now lets wait for all threads to close
  @@ -154,8 +176,64 @@
                   e.printStackTrace();
               }
           }
  -    }   
  -    
  +    }
  +
  +    /**
  +     * Parse the command line using CLI. -o and -script are reserved for Jelly.
  +     * -Dsysprop=sysval is support on the command line as well.
  +     */
  +    private static CommandLine parseCommandLineOptions(String[] args) throws ParseException
{
  +        // create the expected options
  +        Options cmdLineOptions = new Options();
  +        cmdLineOptions.addOption("o", true, "Output file");
  +        cmdLineOptions.addOption("script", true, "Jelly script to run");
  +
  +        // -D options will be added to the system properties
  +        Properties sysProps = System.getProperties();
  +
  +        // filter the system property setting from the arg list
  +        // before passing it to the CLI parser
  +        ArrayList filteredArgList = new ArrayList();
  +
  +        for (int i=0;i<args.length;i++) {
  +            String arg = args[i];
  +
  +            // if this is a -D property parse it and add it to the system properties.
  +            // -D args will not be copied into the filteredArgList.
  +            if (arg.startsWith("-D") && (arg.length() > 2)) {
  +                arg = arg.substring(2);
  +                StringTokenizer toks = new StringTokenizer(arg, "=");
  +
  +                if (toks.countTokens() == 2) {
  +                    // add the tokens to the system properties
  +                    sysProps.setProperty(toks.nextToken(), toks.nextToken());
  +                } else {
  +                    System.err.println("Invalid system property: " + arg);
  +                }
  +
  +            } else {
  +                // add this to the filtered list of arguments
  +                filteredArgList.add(arg);
  +
  +                // add additional "-?" options to the options object. if this is not done
  +                // the only options allowed would be "-o" and "-script".
  +                if (arg.startsWith("-") && arg.length() > 1) {
  +                    if (!(arg.equals("-o") && arg.equals("-script"))) {
  +                        cmdLineOptions.addOption(arg.substring(1, arg.length()), true,
"dynamic option");
  +                    }
  +                }
  +            }
  +        }
  +
  +        // make the filteredArgList into an array
  +        String[] filterArgs = new String[filteredArgList.size()];
  +        filteredArgList.toArray(filterArgs);
  +
  +        // parse the command line
  +        Parser parser = new GnuParser();
  +        return parser.parse(cmdLineOptions, filterArgs);
  +    }
  +
       /**
        * Compiles the script
        */
  
  
  
  1.37      +25 -8     jakarta-commons-sandbox/jelly/maven.xml
  
  Index: maven.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/maven.xml,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- maven.xml	30 Sep 2002 17:52:28 -0000	1.36
  +++ maven.xml	4 Oct 2002 11:28:20 -0000	1.37
  @@ -65,17 +65,18 @@
   	
     <!-- creates the XML webpage from the tag XML information -->		
     <goal name="doc:tags" prereqs="doclet:tag, jelly-task" 
  -	description="Generates the tag documentation">
  +		description="Generates the tag documentation">
   	
  -	<mkdir dir="target/generated-xdocs"/>
  +		<mkdir dir="target/generated-xdocs"/>
   
   <!--      
  -	<jelly file="src/script/makeTagDoc.jelly" 
  -		   output="target/generated-xdocs/tags.xml"/> 
  +		<jelly file="src/script/makeTagDoc.jelly" 
  +			   output="target/generated-xdocs/tags.xml"/> 
   -->	
       <java classname="org.apache.commons.jelly.Jelly" fork="yes">
         <classpath refid="test.classpath"/>
         <arg value="src/script/makeTagDoc.jelly"/> 
  +      <arg value="-o"/>
         <arg value="target/generated-xdocs/tags.xml"/> 
       </java>
     </goal>
  @@ -206,7 +207,7 @@
     </goal>
   
     <goal name="demo:jellybean" prereqs="jelly-task" 
  -	description="Runs the example of binding jelly beans to tags">
  +		description="Runs the example of binding jelly beans to tags">
   	
   <!--	
   	<jelly file="src/test/org/apache/commons/jelly/define/jellyBeanSample.jelly"/> 
  @@ -218,13 +219,13 @@
     </goal>
   
     <goal name="demo:static" prereqs="jelly-task" 
  -	description="Outputs some static XML">
  +		description="Outputs some static XML">
   	
   	<jelly file="src/test/org/apache/commons/jelly/testStatic.jelly"/> 
     </goal>
   
     <goal name="demo:html" prereqs="jelly-task" 
  -	description="Runs the example of parsing HTML with jelly">
  +		description="Runs the example of parsing HTML with jelly">
   	
   <!--	
   	<jelly file="src/test/org/apache/commons/jelly/html/example.jelly"/> 
  @@ -236,7 +237,7 @@
     </goal>
   
     <goal name="demo:text" prereqs="jelly-task" 
  -	description="Runs the example of parsing text with tags via jelly">
  +		description="Runs the example of parsing text with tags via jelly">
   	
   <!--	
   	<jelly file="src/test/org/apache/commons/jelly/html/example2.jelly"/> 
  @@ -479,6 +480,22 @@
         <sysproperty key="http.proxyHost" value="${maven.proxy.host}"/>
         <sysproperty key="http.proxyPort" value="${maven.proxy.port}"/>
         <arg value="src/test/org/apache/commons/jelly/swing/showVariables.jelly"/>

  +    </java>
  +  </goal>
  +  
  +	<!-- a sample of using the command line interface to invoke Jelly -->	
  +  <goal name="demo:cmdline" prereqs="jelly-task"
  +    description="Demonstrates how to use the command line interface to Jelly">
  +    <java classname="org.apache.commons.jelly.Jelly" fork="yes">
  +      <classpath refid="test.classpath"/>
  +      <arg value="src/test/org/apache/commons/jelly/testCmdLineOptions.jelly"/> 
  +      <arg value="-a"/> 
  +      <arg value="valueOfA"/> 
  +      <arg value="-b"/> 
  +      <arg value="valueOfB"/> 
  +      <arg value="-c"/> 
  +      <arg value="valueOfC"/> 
  +      <arg value="-Dtestsysprop=valueOfTestSystemProp"/> 
       </java>
     </goal>
   </project>
  
  
  
  1.77      +11 -0     jakarta-commons-sandbox/jelly/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/project.xml,v
  retrieving revision 1.76
  retrieving revision 1.77
  diff -u -r1.76 -r1.77
  --- project.xml	2 Oct 2002 11:03:38 -0000	1.76
  +++ project.xml	4 Oct 2002 11:28:20 -0000	1.77
  @@ -187,6 +187,17 @@
       </dependency>
   
       <dependency>
  +      <id>commons-cli</id>
  +      <version>SNAPSHOT</version>
  +    </dependency>
  +
  +		<!-- this is brought in by the commons-cli dependency -->
  +    <dependency>
  +      <id>commons-lang</id>
  +      <version>SNAPSHOT</version>
  +    </dependency>
  +
  +    <dependency>
         <id>commons-discovery</id>
         <version>SNAPSHOT</version>
       </dependency>
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message