Re: [PATCH] xindice/tools/XMLTools.java

Todd Byrne <[email protected]>
Newsgroups gmane.text.xml.xindice.devel
Message-ID <[email protected]>
Forgot the files. :(

Todd

Todd Byrne wrote:
> I fixed up some issues with the way the Help menu was being printed. I 
> found that when a command didn't run for some reason that the helpful 
> stuff would get shoved off screen. I also refactored the printHelp out 
> of the XMLTools.java and created a new class HelpClass to print the help 
> menu. The help refactored allowed me to get --help to print the help 
> menu also.
> 
> Included is:
> 
> XMLTools.java.patch
> Some refactoring and and wrapping of a class variable in the table 
> Hashtable.
> 
> commands.xml.patch
> Added two more commands to handle both the -h action and --help action.
> 
> HelpClass.java
> New class that extends Command and implements it execute method. Just 
> takes a command NodeList from the Hashtable and prints out the same help 
>  menu.
> 
> 
> Todd Byrne
XMLTools.java.patch (text/x-patch, 7.2 KB)
Index: ./java/src/org/apache/xindice/tools/XMLTools.java
===================================================================
RCS file: /home/cvspublic/xml-xindice/java/src/org/apache/xindice/tools/XMLTools.java,v
retrieving revision 1.29
diff -u -r1.29 XMLTools.java
--- ./java/src/org/apache/xindice/tools/XMLTools.java	21 Feb 2004 14:29:45 -0000	1.29
+++ ./java/src/org/apache/xindice/tools/XMLTools.java	6 Jun 2005 17:56:44 -0000
@@ -67,6 +67,7 @@
     public static final String AUTO_KEY = "autoKey";
     public static final String NAMESPACES = "namespaces";
     public static final String IMPL_CLASS = "implClass";
+    public static final String COMMAND_LIST = "commandList";
 
     private Hashtable table;
     protected String location = null;
@@ -74,7 +75,6 @@
     private static boolean verbose = false;
 
     private Document commandsDocument = null;
-    protected NodeList commandsList = null;
 
 
     public static void main(String[] args) {
@@ -111,8 +111,10 @@
             list = node.getElementsByTagName("command");
         }
 
-        // Return the list generated
-        commandsList = list;
+        // to allow the commands access to the command list. Specifically
+        // the help command needs access to the entire command list.
+        table.put(COMMAND_LIST,list);
+
     }
 
     /** Return true if this class has admin access
@@ -155,7 +157,7 @@
      * tool can execute.
      **/
     protected NodeList getCommands() {
-        return commandsList;
+        return (NodeList)table.get(COMMAND_LIST);
     }
 
     /**
@@ -169,11 +171,13 @@
             parseArguments(args);
 
             if (!execute()) {
-                printHelp();
+                // instead of printing out the whole help menu when
+                // something goes wrong just inform the user with
+                // a short menu.
+                System.out.println("Try -h for help with that command.");
             }
         } catch (IllegalArgumentException e) {
-            printHelp();
-            throw new XindiceException("ERROR : " + e.getMessage(), e);
+            throw new XindiceException(e.getMessage(), e);
         } catch (NoSuchElementException e) {
             throw new NoSuchElementException("ERROR : " + e + " Switch found. Parameter missing.");
         } catch (NullPointerException e) {
@@ -197,7 +201,7 @@
         ArgTokenizer at = new ArgTokenizer(args);
 
         if (!at.hasMoreTokens()) {
-            throw new IllegalArgumentException("No argument found");
+            throw new IllegalArgumentException("No arguments found: Try -h or --help for Help.");
         }
 
         // Action should always be the second token, if not there show help
@@ -579,83 +583,4 @@
     public String getPassword() {
         return (String) table.get(PASSWORD);
     }
-
-
-    public void printHelp() {
-        NodeList list = getCommands();
-
-        // This method relies on two things to format the output for help
-        //    Method isAdmin() - Tells us if this is an admin instance, used to hide certain output
-        //    XML file Commands.xml attribute "helpclass" - used to order output
-
-        String helpClass;  // Holds the helpclass for the current <command> node
-        String desc;       // Holds the description for the current <command> node
-        String cmdswitch;  // Holds the switch for the current <command> node
-
-
-        // Show the header and switch commands
-        System.out.println();
-        System.out.println("Xindice Command Tools v" + Xindice.Version);
-        System.out.println();
-        System.out.println("Format: xindice action [switch] [parameter]");
-        System.out.println();
-        System.out.println("Switches:");
-        System.out.println("    -c           Collection context (must always be specified),");
-        System.out.println("                 can be either canonical name of the collection");
-        System.out.println("                 or complete xmldb URL");
-        System.out.println("    -e           File extension for multiple documents");
-        System.out.println("    -f           File path for document retrieval and storage");
-        System.out.println("    -n           Name");
-        System.out.println("    -p           Index pattern");
-        System.out.println("    -q           Query string");
-        System.out.println("    -s           Semi-colon delimited list of namespaces for query in ");
-        System.out.println("                 the form prefix=namespace-uri");
-        System.out.println("    -l           Use a local database rather then going over the network.");
-        System.out.println("                 Should be combined with -d to specify the configuration to use.");
-        System.out.println("    -d           Path to the database configuration to use for the local ");
-        System.out.println("                 database. Only applies if -l is specified.");
-        System.out.println("    -t           Specify the data type in collection index");
-        System.out.println("    -v           Verbose");
-        System.out.println("    --pagesize   Page size for file pages (default: 4096)");
-        System.out.println("    --maxkeysize The maximum size for file keys (default: 0=none)");
-        System.out.println();
-
-        System.out.println("Actions:");
-
-        // Show all elements with helpclass=document
-        // Loop over the commands, printing test from description attribute
-        for (int i = 0; i < list.getLength(); i++) {
-            helpClass = ((Element) list.item(i)).getAttribute("helpclass");
-
-            if (helpClass.equals("document")) {
-                desc = ((Element) list.item(i)).getAttribute("description");
-                cmdswitch = ((Element) list.item(i)).getAttribute("switch");
-                System.out.println("    " + StringUtilities.leftJustify(cmdswitch, 13) + desc);
-            }
-        }
-
-        // Loop over the commands, printing text from description attribute
-        for (int i = 0; i < list.getLength(); i++) {
-            helpClass = ((Element) list.item(i)).getAttribute("helpclass");
-
-            if (helpClass.equals("security")) {
-                desc = ((Element) list.item(i)).getAttribute("description");
-                cmdswitch = ((Element) list.item(i)).getAttribute("switch");
-                System.out.println("    " + StringUtilities.leftJustify(cmdswitch, 13) + desc);
-            }
-        }
-
-        System.out.println();
-        System.out.println("Examples:");
-        System.out.println("    xindice ad -c /db/test -f /tmp/xmldocument -n myxmldocument");
-        System.out.println("    xindice dd -c /db/test -n myxmldocument");
-        System.out.println("    xindice rd -c /db/test/ocs -f a:\\file.xml -n file.xml");
-        System.out.println("    xindice xpath -c /db/test/ocs -q test");
-        System.out.println("    xindice xpath -c /db/test -s a=http://somedomain.com/schema.xsd -q /a:foo");
-        System.out.println("    xindice xupdate -c /db/test -f /path/to/xupdate.xml");
-        System.out.println("    xindice xupdate -c /db/test -n document-to-update.xml -f /path/to/xupdate.xml");
-        System.out.println();
-        System.out.println("For more information, please read the Xindice - Tools Reference Guide");
-        System.out.println();
-    }
 }
HelpClass.java (text/x-java, 5.9 KB)
/*
 * Copyright 1999-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.apache.xindice.tools.command;

import java.util.*;
import org.apache.xindice.server.Xindice;
import org.apache.xindice.util.StringUtilities;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.apache.xindice.tools.XMLTools;

public class HelpClass extends Command {
    /**
     * HelpClass is designed to take a command list that gets setup in
     * XMLTools.
     */
    public HelpClass() {
        super();
    }

    /**
     * execute
     *
     * @param table Hashtable with XMLTools.COMMAND_LIST set.
     * @return boolean
     * @throws Exception
     */
    public boolean execute(Hashtable table) throws Exception {
        if(table.containsKey(XMLTools.COMMAND_LIST))
        {
            printHelp((NodeList)table.get(XMLTools.COMMAND_LIST));
            return true;
        }
        else
        {
            System.out.println("Couldn't print the help" +
                " menu because the key \"XMLTools.COMMAND_LIST\" wasn't found.");
            return false;
        }

    }
    public void printHelp(NodeList list) {
      // This method relies on two things to format the output for help
      //    Method isAdmin() - Tells us if this is an admin instance, used to hide certain output
      //    XML file Commands.xml attribute "helpclass" - used to order output

      String helpClass;  // Holds the helpclass for the current <command> node
      String desc;       // Holds the description for the current <command> node
      String cmdswitch;  // Holds the switch for the current <command> node


      // Show the header and switch commands
      System.out.println();
      System.out.println("Xindice Command Tools v" + Xindice.Version);
      System.out.println();
      System.out.println("Format: xindice action [switch] [parameter]");
      System.out.println();
      System.out.println("Switches:");
      System.out.println("    -c           Collection context (must always be specified),");
      System.out.println("                 can be either canonical name of the collection");
      System.out.println("                 or complete xmldb URL");
      System.out.println("    -e           File extension for multiple documents");
      System.out.println("    -f           File path for document retrieval and storage");
      System.out.println("    -n           Name");
      System.out.println("    -p           Index pattern");
      System.out.println("    -q           Query string");
      System.out.println("    -s           Semi-colon delimited list of namespaces for query in ");
      System.out.println("                 the form prefix=namespace-uri");
      System.out.println("    -l           Use a local database rather then going over the network.");
      System.out.println("                 Should be combined with -d to specify the configuration to use.");
      System.out.println("    -d           Path to the database configuration to use for the local ");
      System.out.println("                 database. Only applies if -l is specified.");
      System.out.println("    -t           Specify the data type in collection index");
      System.out.println("    -v           Verbose");
      System.out.println("    --pagesize   Page size for file pages (default: 4096)");
      System.out.println("    --maxkeysize The maximum size for file keys (default: 0=none)");
      System.out.println();

      System.out.println("Actions:");

      // Show all elements with helpclass=document
      // Loop over the commands, printing test from description attribute
      for (int i = 0; i < list.getLength(); i++) {
          helpClass = ((Element) list.item(i)).getAttribute("helpclass");

          if (helpClass.equals("document")) {
              desc = ((Element) list.item(i)).getAttribute("description");
              cmdswitch = ((Element) list.item(i)).getAttribute("switch");
              System.out.println("    " + StringUtilities.leftJustify(cmdswitch, 13) + desc);
          }
      }

      // Loop over the commands, printing text from description attribute
      for (int i = 0; i < list.getLength(); i++) {
          helpClass = ((Element) list.item(i)).getAttribute("helpclass");

          if (helpClass.equals("security")) {
              desc = ((Element) list.item(i)).getAttribute("description");
              cmdswitch = ((Element) list.item(i)).getAttribute("switch");
              System.out.println("    " + StringUtilities.leftJustify(cmdswitch, 13) + desc);
          }
      }

      System.out.println();
      System.out.println("Examples:");
      System.out.println("    xindice ad -c /db/test -f /tmp/xmldocument -n myxmldocument");
      System.out.println("    xindice dd -c /db/test -n myxmldocument");
      System.out.println("    xindice rd -c /db/test/ocs -f a:\\file.xml -n file.xml");
      System.out.println("    xindice xpath -c /db/test/ocs -q test");
      System.out.println("    xindice xpath -c /db/test -s a=http://somedomain.com/schema.xsd -q /a:foo");
      System.out.println("    xindice xupdate -c /db/test -f /path/to/xupdate.xml");
      System.out.println("    xindice xupdate -c /db/test -n document-to-update.xml -f /path/to/xupdate.xml");
      System.out.println();
      System.out.println("For more information, please read the Xindice - Tools Reference Guide");
      System.out.println();
  }

}
commands.xml.patch (text/x-patch, 964 B)
Index: config/commands.xml
===================================================================
RCS file: /home/cvspublic/xml-xindice/config/commands.xml,v
retrieving revision 1.7
diff -u -r1.7 commands.xml
--- config/commands.xml	8 Feb 2004 01:11:34 -0000	1.7
+++ config/commands.xml	6 Jun 2005 17:56:53 -0000
@@ -117,5 +117,15 @@
                class="org.apache.xindice.tools.command.ExportTree"
                helpclass="document"
                description="Exports a collection/document from a directory tree" />
+      <command switch="-h"
+               name="help"
+               class="org.apache.xindice.tools.command.HelpClass"
+               helpclass=""
+               description="Prints the help menu." />
+      <command switch="--help"
+               name="help"
+               class="org.apache.xindice.tools.command.HelpClass"
+               helpclass=""
+               description="Prints the help menu." />
    </user>
 </commands>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.