Package org.apache.xindice.tools

Source Code of org.apache.xindice.tools.XMLTools

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
* $Id: XMLTools.java 593003 2007-11-08 03:12:23Z natalia $
*/

package org.apache.xindice.tools;

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.XindiceException;
import org.apache.xindice.xml.dom.DOMParser;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.XMLDBException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

/**
* XMLTools parses command line arguments and executes corresponding command
* to give user Xindice management flexibility within the current Database.
*
* @version $Revision: 593003 $, $Date: 2007-11-07 22:12:23 -0500 (Wed, 07 Nov 2007) $
*/
public class XMLTools {

    public static final String COLLECTION = "collection";
    public static final String FILER = "filer";
    public static final String EXTENSION = "extension";
    public static final String FILE_PATH = "filePath";
    public static final String ACTION = "action";
    public static final String NAME_OF = "nameOf";
    public static final String PATTERN = "pattern";
    public static final String QUERY = "query";
    public static final String VERBOSE = "verbose";
    public static final String HELP = "help";
    public static final String TYPE = "type";
    public static final String PAGE_SIZE = "pagesize";
    public static final String MAX_KEY_SIZE = "maxkeysize";
    public static final String LOCAL = "local";
    public static final String DB_CONFIG = "dbconfig";
    public static final String NAMESPACES = "namespaces";
    public static final String COMMAND_LIST = "__command_list__";
    public static final String PAGE_COUNT = "pagecount";

    private Config config;
    private boolean initialized;

    private List commandsList;

    public static void main(String[] args) {
        XMLTools tools = new XMLTools();
        try {
            tools.process(args);
        } catch (Exception e) {
            System.err.println(e.getMessage());
            if (tools.config.getBoolean(VERBOSE)) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Constructor for XMLTools, includes default variables for the
     * command line.
     */
    public XMLTools() {
        config = new Config();
    }

    /**
     * Constructor for XMLTools based on existing Config.
     */
    public XMLTools(Config config) {
        this.config = config;
    }

    /**
     * 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 = commands.getElementsByTagName("user");

        if (list.getLength() > 0) {
            // Retrieve the index of the first element (<user>)
            Element node = (Element) list.item(0);
            // get all command children from the user element
            list = node.getElementsByTagName("command");
        }

        List commandList = new ArrayList();
        for (int i = 0; i < list.getLength(); i++) {
            Element e = (Element) list.item(i);
            String switchName = e.getAttribute("switch");
            String fullName = e.getAttribute("name");
            String className = e.getAttribute("class");
            String descr = e.getAttribute("description");
            String helpClass = e.getAttribute("helpclass");
            commandList.add(new Action(className, descr, helpClass, switchName, fullName));
        }

        // Return the list generated
        commandsList = commandList;

        // Add the command list so that the HelpCommand can access it and
        // print specific help information.
        config.setActions(commandsList);
    }

    /**
     * Carries out necessary initialization of this class.
     */
    public void init() throws XindiceException, FileNotFoundException {
        if (!initialized) {
            parseCommandsList(readCommandsDocument());
            initialized = true;
        }
    }

    /**
     * Returns the <command> elements from the Commands Document this
     * tool can execute.
     */
    protected List getCommands() {
        return commandsList;
    }

    /**
     * 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 {
        try {
            parseArguments(args);
            execute();
        } catch (IllegalArgumentException e) {
            throw new XindiceException("ERROR : " + e.getMessage() + " Try -h for help.", e);
        } catch (NoSuchElementException e) {
            throw new NoSuchElementException("ERROR : " + e + " Switch found. Parameter missing. Try -h for help.");
        } catch (NullPointerException e) {
            throw new NullPointerException("ERROR : " + e + " Try -h for help.");
        } catch (Exception e) {
            throw new XindiceException("ERROR : " + e.getMessage() + " Try -h for help.", e);
        }
    }

    /**
     * Parses and validated the arguments of the command line. The arguments are
     * stored into the <tt>Config</tt> object.
     *
     * @exception IllegalArgumentException if an error is found
     */
    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 arguments found");
        }

        // Action should always be the second token, if not there show help
        config.setString(ACTION, at.nextToken());

        // Loop over remaining command line arguments, populating hashtable
        while (at.hasMoreTokens()) {
            String token = at.nextToken();

            if (token.equalsIgnoreCase("-h") || token.equalsIgnoreCase("--help")) {
                config.setBoolean(HELP, true);
            } else if (token.equalsIgnoreCase("-c") || token.equalsIgnoreCase("--collection")) {
                String colname = at.nextSwitchToken();
                if (!colname.startsWith("/") && !colname.startsWith("xmldb:xindice")) {
                    throw new IllegalArgumentException("The name of a collection must start with '/'");
                }

                if (colname.startsWith("xmldb:xindice-embed://")) {
                    config.setBoolean(LOCAL, true);
                }
                config.setString(COLLECTION, colname);
            } else if (token.equalsIgnoreCase("--filer")) {
                config.setString(FILER, at.nextSwitchToken());
            } else if (token.equalsIgnoreCase("-e") || token.equalsIgnoreCase("--extension")) {
                config.setString(EXTENSION, at.nextSwitchToken());
            } else if (token.equalsIgnoreCase("-f") || token.equalsIgnoreCase("--filepath")) {
                config.setString(FILE_PATH, at.nextSwitchToken());
            } else if (token.equalsIgnoreCase("-n") || token.equalsIgnoreCase("--nameOf")) {
                config.setString(NAME_OF, at.nextSwitchToken());
            } else if (token.equalsIgnoreCase("-p") || token.equalsIgnoreCase("--pattern")) {
                config.setString(PATTERN, at.nextSwitchToken());
            } else if (token.equalsIgnoreCase("-q") || token.equalsIgnoreCase("--query")) {
                config.setString(QUERY, at.nextSwitchToken());
            } else if (token.equalsIgnoreCase("-v") || token.equalsIgnoreCase("--verbose")) {
                config.setBoolean(VERBOSE, true);
            } else if (token.equalsIgnoreCase("-l") || token.equalsIgnoreCase("--localdb")) {
                config.setBoolean(LOCAL, true);
            } else if (token.equalsIgnoreCase("-d") || token.equalsIgnoreCase("--dbconfig")) {
                String configFile = at.nextSwitchToken();
                if (!new File(configFile).isAbsolute()) {
                    configFile = new File(System.getProperty("user.dir"), configFile).getAbsolutePath();
                }
                System.setProperty(Xindice.PROP_XINDICE_CONFIGURATION, configFile);
                config.setString(DB_CONFIG, configFile);
            } else if (token.equalsIgnoreCase("-s") || token.equalsIgnoreCase("--namespaces")) {
                config.setString(NAMESPACES, at.nextSwitchToken());
                // Index specific options
            } else if (token.equalsIgnoreCase("-t") || token.equalsIgnoreCase("--type")) {
                config.setString(TYPE, at.nextSwitchToken());
            } else if (token.equalsIgnoreCase("--pagesize")) {
                config.setString(PAGE_SIZE, at.nextSwitchToken());
            } else if (token.equalsIgnoreCase("--maxkeysize")) {
                config.setString(MAX_KEY_SIZE, at.nextSwitchToken());
            } else if (token.equalsIgnoreCase("--pagecount")) {
                config.setString(PAGE_COUNT, at.nextSwitchToken());
            } else {
                // todo: unrecognized switch
            }
        }
    }

    /**
     * This method is to carry out execution, after instance variables being setup by process( args )
     */
    public boolean execute() throws Exception {
        init();
        String action = config.getString(ACTION);

        // get command class name
        String commandClass = null;
        if (action != null) {
            // search for the tool Class associated with the given action
            List commands = getCommands();
            for (int i = 0; i < commands.size(); i++) {
                Action a = (Action) commands.get(i);
                if (action.equals(a.switchName) || action.equals(a.fullName)) {
                    commandClass = a.className;
                }
            }
        }

        if (commandClass == null) {
            throw new IllegalArgumentException("\"" + action +  "\" not recognized.");
        }

        Command command;
        try {
            command = (Command) Class.forName(commandClass).newInstance();
            if (config.getBoolean(HELP)) {
                // show command help
                command.usage();
            } else {
                // Register Xindice Database with XML:DB
                DatabaseImpl db = new DatabaseImpl();
                DatabaseManager.registerDatabase(db);

                // Execute command class
                command.execute(config);
            }

            return true;
        } catch (XMLDBException e) {
            System.err.println("XMLDB Exception " + e.errorCode + ": " + e.getMessage());
            if (config.getBoolean(VERBOSE)) {
                e.printStackTrace(System.err);
            }
            return false;
        } catch (Exception e) {
            System.err.println("ERROR : " + e.getMessage());
            if (config.getBoolean(VERBOSE)) {
                e.printStackTrace(System.err);
            }
            return false;
        } finally {
            // Close Database
            if (config.getBoolean(LOCAL)) {
                command = new org.apache.xindice.tools.command.Shutdown();
                command.execute(config);
            }
        }
    }

    public static class Config {
        private HashMap config;

        public Config() {
            config = new HashMap();

            // defaults for command switches
            setString(FILE_PATH, "");
            setString(EXTENSION, "");
            setString(QUERY, "");
            setBoolean(VERBOSE, false);
        }

        public String getString(String param) {
            return (String) config.get(param);
        }

        public void setString(String param, String value) {
            config.put(param, value);
        }

        public boolean getBoolean(String param) {
            return Boolean.TRUE.equals(config.get(param));
        }

        public void setBoolean(String param, boolean value) {
            config.put(param, Boolean.valueOf(value));
        }

        public List getActions() {
            return (List) config.get(COMMAND_LIST);
        }

        public void setActions(List value) {
            config.put(COMMAND_LIST, value);
        }
    }

    public static class Action {
        public final String helpclass;
        public final String className;
        public final String description;
        public final String switchName;
        public final String fullName;

        public Action(String className, String description, String helpclass, String switchName, String fullName) {
            this.helpclass = helpclass;
            this.className = className;
            this.description = description;
            this.switchName = switchName;
            this.fullName = fullName;
        }
    }
}
TOP

Related Classes of org.apache.xindice.tools.XMLTools

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.