Adding jelly -h and -v. (help and current-version).
paul


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/jelly/trunk@136434 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/maven.xml b/maven.xml
index ef984e5..699281f 100644
--- a/maven.xml
+++ b/maven.xml
@@ -43,10 +43,19 @@
       </copy>
     </util:available>
   </postGoal>
-  <!-- ================================================================== -->
+    <!-- ================================================================== -->
   <!-- END: C O M M O N S - B U I L D                                     -->
   <!-- ================================================================== -->
 
+  <!-- build the timestamp after compiling -->
+  <postGoal name="java:compile">
+    <echo file="${maven.build.dest}/org/apache/commons/jelly/jelly-version.txt"
+      >${pom.currentVersion}</echo>
+    <tstamp><format property="now" pattern="yyyy-MM-dd kk:mm:ss zzz"/></tstamp>
+    <echo file="${maven.build.dest}/org/apache/commons/jelly/jelly-build-date.txt"
+      >${now}</echo>
+    </postGoal>
+  
   <!-- create the lib and bin directories in the binary distro -->
   <preGoal name="dist:build">
     <echo>About to create lib and bin directories in ${maven.dist.assembly.dir}</echo>
@@ -383,6 +392,7 @@
       <property name="doBuildAll" value="false"/>
       <property name="fatJarPath" value="${maven.build.dir}/fatjelly.jar"/>
       <property name="classpathFile" value="${maven.build.dir}/classpath-with-deps.txt"/>
+      <property name="mergedDepsFile" value="${maven.build.dir}/mergedDeps.xml"/>
         
       <!-- if we need to build, we need to build first jelly -->
       <j:if test="${doBuildAll}"><attainGoal name="jar:install-snapshot"/></j:if>
@@ -417,9 +427,9 @@
         
       <j:forEach var="reactorProject" items="${reactorProjects}">
         <j:forEach var="dep" items="${reactorProject.dependencies}">
-					<j:if test="${dep.artifactId eq 'commons-jelly'}">${dep.setVersion("SNAPSHOT")}</j:if>
-					${deps.add(dep)}
-				</j:forEach>
+          <j:if test="${dep.artifactId eq 'commons-jelly'}">${dep.setVersion("SNAPSHOT")}</j:if>
+          ${deps.add(dep)}
+        </j:forEach>
       </j:forEach>
       <j:forEach var="tl" items="${jellyTaglibNames}">
         <j:new var="tlD" className="org.apache.maven.project.Dependency"/>
@@ -456,6 +466,17 @@
           </j:forEach>
         </jar>
       </j:if>
+      
+    <j:if test="${!empty(mergedDepsFile)}">
+      <j:file prettyPrint="true" name="${mergedDepsFile}">
+        <dependencyList project="commons-jelly"
+              withAddOns="${tagLibs}">
+          <j:forEach var="dep" items="${deps}">
+            <dependency groupId="${dep.groupId}" artifactId="${dep.artifactId}" version="${dep.version}"/>
+            </j:forEach>
+          </dependencyList>
+        </j:file>
+      </j:if>
             
     </goal>
 
diff --git a/src/java/org/apache/commons/jelly/Jelly.java b/src/java/org/apache/commons/jelly/Jelly.java
index 2848aac..ce3434d 100644
--- a/src/java/org/apache/commons/jelly/Jelly.java
+++ b/src/java/org/apache/commons/jelly/Jelly.java
@@ -43,7 +43,7 @@
  * </pre>
  *
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.34 $
+ * @version $Revision: 1.35 $
  */
 public class Jelly {
     
@@ -101,6 +101,34 @@
             }
         }
     }
+    
+    
+    public static String getJellyVersion() {
+        return readBuildTimestampResource("jelly-version.txt");
+    }
+    
+    public static String getJellyBuildDate() {
+        return readBuildTimestampResource("jelly-build-date.txt");
+    }
+    
+    private static String readBuildTimestampResource(String name) {
+        java.io.Reader in = null;
+        try {
+            java.io.StringWriter w = new java.io.StringWriter();
+            in = new java.io.InputStreamReader(Jelly.class.getResourceAsStream(name),"utf-8");
+            int r;
+            while ( (r=in.read()) >= 0 ) {
+                w.write((char) r);
+            }
+            return w.toString();
+        } catch(Exception ex) {
+            ex.printStackTrace();
+            try { in.close(); } catch(Exception e) {}
+            throw new IllegalStateException("Resource \"" + name + "\" not found.");
+        }
+    }
+    
+    
 
     /**
      * Compiles the script
diff --git a/src/java/org/apache/commons/jelly/util/CommandLineParser.java b/src/java/org/apache/commons/jelly/util/CommandLineParser.java
index 20fc268..b227f0d 100644
--- a/src/java/org/apache/commons/jelly/util/CommandLineParser.java
+++ b/src/java/org/apache/commons/jelly/util/CommandLineParser.java
@@ -19,7 +19,9 @@
 import java.io.File;
 import java.io.FileWriter;
 import java.net.URL;
+import java.util.Arrays;
 import java.util.ArrayList;
+import java.util.List;
 import java.util.Properties;
 
 import org.apache.commons.cli.CommandLine;
@@ -27,6 +29,7 @@
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.Parser;
+import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.jelly.Jelly;
 import org.apache.commons.jelly.JellyContext;
 import org.apache.commons.jelly.JellyException;
@@ -41,11 +44,13 @@
  *
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  * @author Morgan Delagrange
- * @version $Revision: 1.9 $
+ * @version $Revision: 1.10 $
  */
 public class CommandLineParser {
 
     protected static CommandLineParser _instance = new CommandLineParser();
+    
+    private Options cmdLineOptions = null;
 
     public static CommandLineParser getInstance() {
         return _instance;
@@ -66,6 +71,19 @@
         } catch (ParseException e) {
             throw new JellyException(e);
         }
+        
+        // check for -h or -v
+        if (cmdLine.hasOption("h")) {
+            new HelpFormatter().printHelp("jelly [scriptFile] [-script scriptFile] [-o outputFile] [-Dsysprop=syspropval] [-awt]",
+                cmdLineOptions);
+            System.exit(1);
+        }
+        if (cmdLine.hasOption("v")) {
+            System.err.println("Jelly " + Jelly.getJellyVersion());
+            System.err.println(" compiled: " + Jelly.getJellyBuildDate());
+            System.err.println("");
+            System.exit(1);
+        }
 
         // get the -script option. If there isn't one then use args[0]
         String scriptFile = null;
@@ -74,6 +92,9 @@
         } else {
             scriptFile = args[0];
         }
+        
+        // check the -awt option.
+        boolean runInSwingThread = cmdLine.hasOption("awt") || cmdLine.hasOption("swing");
 
         //
         // Use classloader to find file
@@ -93,13 +114,22 @@
             Jelly jelly = new Jelly();
             jelly.setScript(scriptFile);
 
-            Script script = jelly.compileScript();
+            final Script script = jelly.compileScript();
 
             // add the system properties and the command line arguments
-            JellyContext context = jelly.getJellyContext();
+            final JellyContext context = jelly.getJellyContext();
             context.setVariable("args", args);
             context.setVariable("commandLine", cmdLine);
-            script.run(context, output);
+            if (runInSwingThread) {
+                javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() {
+                    try {
+                        script.run(context, output);
+                    } catch (Exception ex) {
+                        ex.printStackTrace();
+                    }
+            } } ); } else {
+                script.run(context, output);
+            }
 
             // now lets wait for all threads to close
             Runtime.getRuntime().addShutdownHook(new Thread() {
@@ -126,10 +156,17 @@
      */
     public CommandLine parseCommandLineOptions(String[] args) throws ParseException {
         // create the expected options
-        Options cmdLineOptions = new Options();
+        cmdLineOptions = new Options();
         cmdLineOptions.addOption("o", true, "Output file");
         cmdLineOptions.addOption("script", true, "Jelly script to run");
-
+        cmdLineOptions.addOption("h","help", false, "Give this help message");
+        cmdLineOptions.addOption("v","version", false, "prints Jelly's version and exits");
+        cmdLineOptions.addOption("script", true, "Jelly script to run");
+        cmdLineOptions.addOption("awt", false, "Wether to run in the AWT thread.");
+        cmdLineOptions.addOption("swing", false, "Synonym of \"-awt\".");
+        List builtinOptionNames = Arrays.asList(new String[]{
+            "-o","-script","-h","--help","-v","--version","-awt","-swing"});
+        
         // -D options will be added to the system properties
         Properties sysProps = System.getProperties();
 
@@ -144,18 +181,18 @@
             // -D args will not be copied into the filteredArgList.
             if (arg.startsWith("-D") && (arg.length() > 2)) {
                 arg = arg.substring(2);
-				int ePos = arg.indexOf("=");
-				if(ePos==-1 || ePos==0 || ePos==arg.length()-1)
-					System.err.println("Invalid system property: \"" + arg + "\".");
-				sysProps.setProperty(arg.substring(0,ePos), arg.substring(ePos+1));
+                int ePos = arg.indexOf("=");
+                if(ePos==-1 || ePos==0 || ePos==arg.length()-1)
+                    System.err.println("Invalid system property: \"" + arg + "\".");
+                sysProps.setProperty(arg.substring(0,ePos), arg.substring(ePos+1));
             } 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".
+                // the only options allowed would be the builtin-ones.
                 if (arg.startsWith("-") && arg.length() > 1) {
-                    if (!(arg.equals("-o") && arg.equals("-script"))) {
+                    if (!(builtinOptionNames.contains(arg))) {
                         cmdLineOptions.addOption(arg.substring(1, arg.length()), true, "dynamic option");
                     }
                 }
@@ -167,7 +204,7 @@
         filteredArgList.toArray(filterArgs);
 
         // parse the command line
-        Parser parser = new GnuParser();
+        Parser parser = new org.apache.commons.cli.GnuParser();
         return parser.parse(cmdLineOptions, filterArgs);
     }
 
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index 47d5733..9fbd631 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -24,6 +24,10 @@
     <author email="dion@apache.org">dIon Gillard</author>
   </properties>
   <body>
+    <release version="1.0-RC2" date="in CVS">
+      <action dev="polx" type="fix" issue="JELLY-85">Removed instance-based ThreadLocal substituting a JellyContext-based tag-caching.</action>
+      <action dev="polx" type="add" issue="JELLY-145">jelly -h or --help responds a help and jelly -v or --version responds the version.</action>
+      </release>
     <release version="1.0-RC1" date="2004-11-22">
       <action dev="dion" type="fix" issue="JELLY-148" due-to="Hans Gilde">Huge memory leak resulting from the use of ThreadLocal.</action>
       <action dev="dion" type="fix" issue="JELLY-138">Character data is flushed by XMLOuput while XML data isn't.</action>