You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by vg...@apache.org on 2006/11/12 05:21:36 UTC

svn commit: r473870 - in /xml/xindice/trunk/java/src/org/apache/xindice/tools: ArgTokenizer.java XMLTools.java command/HelpCommand.java

Author: vgritsenko
Date: Sat Nov 11 20:21:35 2006
New Revision: 473870

URL: http://svn.apache.org/viewvc?view=rev&rev=473870
Log:
fix failing unit test (XMLToolsTest).
while there, do some housekeeping.

Modified:
    xml/xindice/trunk/java/src/org/apache/xindice/tools/ArgTokenizer.java
    xml/xindice/trunk/java/src/org/apache/xindice/tools/XMLTools.java
    xml/xindice/trunk/java/src/org/apache/xindice/tools/command/HelpCommand.java   (contents, props changed)

Modified: xml/xindice/trunk/java/src/org/apache/xindice/tools/ArgTokenizer.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/tools/ArgTokenizer.java?view=diff&rev=473870&r1=473869&r2=473870
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/tools/ArgTokenizer.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/tools/ArgTokenizer.java Sat Nov 11 20:21:35 2006
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
- * CVS $Id$
+ * $Id$
  */
 
 package org.apache.xindice.tools;
@@ -24,11 +24,11 @@
  * ArgTokenizer performs the difficult task of parsing
  * command-line arguments from various sources.
  *
- * @version CVS $Revision$, $Date$
+ * @version $Revision$, $Date$
  */
 public final class ArgTokenizer {
 
-    private int idx = 0;
+    private int idx;
     private String[] args;
 
 
@@ -54,15 +54,17 @@
             sb.append(args[idx++]);
             while (hasMoreTokens()) {
                 String nt = args[idx];
-                if (!nt.startsWith("-")) {
-                    sb.append(' ' + args[idx++]);
-                }
-                else
+                if (nt.startsWith("-")) {
                     break;
+                }
+
+                sb.append(' ').append(args[idx++]);
             }
+
             return sb.toString();
-        } else
-            return "";
+        }
+
+        return "";
     }
 
     public String nextToken() {
@@ -73,4 +75,3 @@
         }
     }
 }
-

Modified: xml/xindice/trunk/java/src/org/apache/xindice/tools/XMLTools.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/tools/XMLTools.java?view=diff&rev=473870&r1=473869&r2=473870
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/tools/XMLTools.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/tools/XMLTools.java Sat Nov 11 20:21:35 2006
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
- * CVS $Id$
+ * $Id$
  */
 
 package org.apache.xindice.tools;
@@ -21,7 +21,6 @@
 import org.apache.xindice.client.xmldb.DatabaseImpl;
 import org.apache.xindice.server.Xindice;
 import org.apache.xindice.tools.command.Command;
-import org.apache.xindice.util.StringUtilities;
 import org.apache.xindice.util.XindiceException;
 import org.apache.xindice.xml.dom.DOMParser;
 
@@ -41,7 +40,7 @@
  * XMLAdmin is designed to take command line arguments and give
  * user Xindice management flexibility within the current Database.
  *
- * @version CVS $Revision$, $Date$
+ * @version $Revision$, $Date$
  */
 public class XMLTools {
 
@@ -69,41 +68,68 @@
     public static final String IMPL_CLASS = "implClass";
     public static final String COMMAND_LIST="__command_list__";
     public static final String PAGE_COUNT = "pagecount";
+
     private Hashtable table;
-    protected String location = null;
-    private boolean initialized = false;
-    private static boolean verbose = false;
+    private boolean initialized;
 
-    private Document commandsDocument = null;
-    protected NodeList commandsList = null;
+    //private Document commandsDocument;
+    private NodeList commandsList;
 
 
     public static void main(String[] args) {
+        XMLTools tools = new XMLTools();
         try {
-            new XMLTools().process(args);
+            tools.process(args);
         } catch (Exception e) {
-            System.out.println(e.getMessage());
-            if (verbose) {
-                e.printStackTrace(System.err);
+            System.err.println(e.getMessage());
+            if (tools.isVerbose()) {
+                e.printStackTrace();
             }
         }
     }
 
-    /** Constructor for XMLTools, includes default variables for the command line
+    /**
+     * Constructor for XMLTools, includes default variables for the
+     * command line.
      */
     public XMLTools() {
         table = new Hashtable();
         // defaults for command switches
         table.put(FILE_PATH, "");
         table.put(EXTENSION, "");
-        table.put(QUERY, "");
-        table.put(AUTO_KEY, "");
-        table.put(VERBOSE, "false");
+        table.put(QUERY,     "");
+        table.put(AUTO_KEY,  "");
+        table.put(VERBOSE,   "false");
+    }
+
+    /**
+     * @return true if operating in verbose mode
+     */
+    protected boolean isVerbose() {
+        return "true".equals(table.get(XMLTools.VERBOSE));
+    }
+
+    /**
+     * @return true if this class has admin access.
+     */
+    public boolean isAdmin() {
+        return false;
     }
 
-    protected void initCommandsList() {
+    /**
+     * Reads commands document from config directory.
+     */
+    private Document readCommandsDocument() throws XindiceException, FileNotFoundException {
+        // Absolute path to the commands.xml file, relative to $XINDICE_HOME
+        File xindiceHome = new File(System.getProperty(Xindice.PROP_XINDICE_HOME, "."));
+        File commandsFile = new File(xindiceHome, "config/commands.xml");
+
+        return DOMParser.toDocument(new FileInputStream(commandsFile));
+    }
+
+    private void parseCommandsList(Document commands) {
         // Get all user elements
-        NodeList list = getCommandsDocument().getElementsByTagName("user");
+        NodeList list = commands.getElementsByTagName("user");
 
         if (list.getLength() > 0) {
             // Retrieve the index of the first element (<user>)
@@ -114,47 +140,26 @@
 
         // Return the list generated
         commandsList = list;
-    }
 
-    /** Return true if this class has admin access
-     */
-    public boolean isAdmin() {
-        return false;
+        // Add the command list so that the HelpCommand can access it and
+        // print specific help information.
+        table.put(COMMAND_LIST, commandsList);
     }
 
     /**
      * Carries out necessary initialization of this class.
-     **/
+     */
     public void init() throws XindiceException, FileNotFoundException {
         if (!initialized) {
-            initCommandsDocument();
-            initCommandsList();
+            parseCommandsList(readCommandsDocument());
             initialized = true;
         }
     }
 
     /**
-     * Carries out the initialization of the Commands Document.
-     **/
-    protected void initCommandsDocument() throws XindiceException, FileNotFoundException {
-        // Absolute path to the commands.xml file, relative to $XINDICE_HOME
-        File xindiceHome = new File(System.getProperty(Xindice.PROP_XINDICE_HOME, "."));
-        File commandsFile = new File(xindiceHome, "config/commands.xml");
-
-        commandsDocument = DOMParser.toDocument(new FileInputStream(commandsFile));
-    }
-
-    /**
-     * Returns the Commands Document use for configuration.
-     **/
-    protected Document getCommandsDocument() {
-        return commandsDocument;
-    }
-
-    /**
      * Returns the <command> elements from the Commands Document this
      * tool can execute.
-     **/
+     */
     protected NodeList getCommands() {
         return commandsList;
     }
@@ -163,7 +168,7 @@
      * The Process function is designed for the implementation of the
      * command line tools, as well as, making the command line easier
      * to use.
-     **/
+     */
     public void process(String[] args) throws XindiceException, Exception {
         try {
             init();
@@ -186,16 +191,14 @@
      *
      * @exception IllegalArgumentException if an error is found
      */
-    protected void parseArguments(String[] args)
-            throws IllegalArgumentException {
+    protected void parseArguments(String[] args) throws IllegalArgumentException {
+
         // parsing arguments for the command tools
         ArgTokenizer at = new ArgTokenizer(args);
-
         if (!at.hasMoreTokens()) {
-            throw new IllegalArgumentException("No argument found");
+            throw new IllegalArgumentException("No arguments found");
         }
-        // add the command list so that the HelpCommand and print specific help information
-        table.put(COMMAND_LIST,commandsList);
+
         // Action should always be the second token, if not there show help
         table.put(ACTION, at.nextToken());
 
@@ -205,8 +208,7 @@
 
             if (token.equalsIgnoreCase("-c") || token.equalsIgnoreCase("--collection")) {
                 String colname = at.nextSwitchToken();
-                if (!colname.startsWith("/") &&
-                        !colname.startsWith("xmldb:xindice")) {
+                if (!colname.startsWith("/") && !colname.startsWith("xmldb:xindice")) {
                     throw new IllegalArgumentException("The name of a collection must start with '/'");
                 }
                 table.put(COLLECTION, colname);
@@ -253,7 +255,7 @@
             } else if (token.equalsIgnoreCase("--pagecount")) {
                 table.put(PAGE_COUNT, at.nextSwitchToken());
             }
-        } // End of while loop
+        }
     }
     
     /**
@@ -291,28 +293,25 @@
                 return true;
             } catch (XMLDBException e) {
                 System.err.println("XMLDB Exception " + e.errorCode + ": " + e.getMessage());
-                if (table.get(VERBOSE).equals("true")) {
+                if (isVerbose()) {
                     e.printStackTrace(System.err);
                 }
                 return false;
             } catch (Exception e) {
                 System.err.println("ERROR : " + e.getMessage());
-                if (table.get(VERBOSE).equals("true")) {
+                if (isVerbose()) {
                     e.printStackTrace(System.err);
                 }
                 return false;
-            }
-            finally {
-            	  // Close Database
+            } finally {
+                // Close Database
                 if ("true".equals(table.get(LOCAL))) {
                     command = new org.apache.xindice.tools.command.Shutdown();
                     command.execute(table);
                 }
             }
-        }
-        else 
-        {
-        	 throw new IllegalArgumentException("\"" + action +  "\" not recognized.");
+        } else {
+            throw new IllegalArgumentException("\"" + action +  "\" not recognized.");
         }
     }
     

Modified: xml/xindice/trunk/java/src/org/apache/xindice/tools/command/HelpCommand.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/tools/command/HelpCommand.java?view=diff&rev=473870&r1=473869&r2=473870
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/tools/command/HelpCommand.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/tools/command/HelpCommand.java Sat Nov 11 20:21:35 2006
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
- * @author <a href="mailto:byrne@apache.org">Todd Byrne</a>
+ * $Id$
  */
 package org.apache.xindice.tools.command;
 
@@ -25,6 +25,10 @@
 import org.w3c.dom.NodeList;
 import org.apache.xindice.tools.XMLTools;
 
+/**
+ *
+ * @author <a href="mailto:byrne@apache.org">Todd Byrne</a>
+ */
 public class HelpCommand extends Command {
 	
 	public boolean execute(Hashtable table) throws Exception {

Propchange: xml/xindice/trunk/java/src/org/apache/xindice/tools/command/HelpCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xml/xindice/trunk/java/src/org/apache/xindice/tools/command/HelpCommand.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision Author Date