Package org.apache.xindice.tools.command

Source Code of org.apache.xindice.tools.command.AddIndexer

/*
* 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: AddIndexer.java 593399 2007-11-09 02:27:03Z natalia $
*/

package org.apache.xindice.tools.command;

import org.apache.xindice.client.xmldb.services.CollectionManager;
import org.apache.xindice.tools.XMLTools;
import org.apache.xindice.xml.TextWriter;
import org.apache.xindice.xml.dom.DocumentImpl;
import org.apache.xindice.xml.dom.DOMParser;
import org.apache.xindice.util.XindiceException;

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

import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;

/**
* AddIndexer.java is designed to let the user create an Indexer
* and insert it into a Collection.
*
* @version $Revision: 593399 $, $Date: 2007-11-08 21:27:03 -0500 (Thu, 08 Nov 2007) $
*/
public class AddIndexer extends Command {

    public boolean execute(XMLTools.Config table) throws Exception {

        if (table.getString(XMLTools.COLLECTION) == null) {
            System.out.println("ERROR : Collection and switch required");
            return false;
        }

        if ("".equals(table.getString(XMLTools.FILE_PATH)) &&
            (table.getString(XMLTools.NAME_OF) == null || table.getString(XMLTools.PATTERN) == null)) {
            System.out.println("ERROR : Name and Pattern required or File path required");
            return false;
        }

        Document config;
        if (!"".equals(table.getString(XMLTools.FILE_PATH))) {
            // configuration was passed in a file
            config = readConfig(table);
        } else {
            // build configuration from parameters
            config = buildConfig(table);
        }

        Collection col = null;
        try {
            // Get a Collection reference to the collection
            String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
                                                      table.getBoolean(XMLTools.LOCAL));

            col = DatabaseManager.getCollection(colstring);
            if (col == null) {
                System.out.println("ERROR : Collection not found!");
                return false;
            }

            // Create a collection manager instance for the collection
            CollectionManager colman = (CollectionManager) col.getService("CollectionManager", XMLDBAPIVERSION);

            // Create the indexer for this collection manager
            colman.createIndexer(config);

            System.out.println("CREATED : " + config.getDocumentElement().getAttributeNode("name").getNodeValue());
        } finally {
            if (col != null) {
                col.close();
            }
        }

        return true;
    }

    private Document readConfig(XMLTools.Config table) throws Exception {
        InputStream fis = null;
        try {
            File file = new File(table.getString(XMLTools.FILE_PATH));
            fis = new FileInputStream(file);

            return DOMParser.toDocument(fis);
        } catch (XindiceException e) {
            throw new Exception("Indexer configuration could not be parsed", e);
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }

    private Document buildConfig(XMLTools.Config table) throws Exception {
        Document config = new DocumentImpl();

        // Create the index element to hold attributes
        Element idxEle = config.createElement("index");
        idxEle.setAttribute("class", XINDICE_VAL_INDEXER);
        idxEle.setAttribute("name", table.getString(XMLTools.NAME_OF));

        // Setup optional index attributes
        String type = table.getString(XMLTools.TYPE);
        if (type != null) {
            if (type.equalsIgnoreCase("name")) {
                idxEle.setAttribute("class", XINDICE_NAME_INDEXER);
            } else if (type.equalsIgnoreCase("text")) {
                idxEle.setAttribute("class", XINDICE_TEXT_INDEXER);
            } else {
                idxEle.setAttribute("type", type);
            }
        }

        // LuceneIndexer configuration is different
        if (idxEle.getAttribute("class").equals(XINDICE_TEXT_INDEXER)) {
            addPatterns(config, idxEle, table.getString(XMLTools.PATTERN));
        } else {
            idxEle.setAttribute("pattern", table.getString(XMLTools.PATTERN));

            if (table.getString(XMLTools.PAGE_SIZE) != null) {
                idxEle.setAttribute(XMLTools.PAGE_SIZE, table.getString(XMLTools.PAGE_SIZE));
            }
            if (table.getString(XMLTools.MAX_KEY_SIZE) != null) {
                idxEle.setAttribute(XMLTools.MAX_KEY_SIZE, table.getString(XMLTools.MAX_KEY_SIZE));
            }
            if (table.getString(XMLTools.PAGE_COUNT) != null) {
                idxEle.setAttribute(XMLTools.PAGE_COUNT, table.getString(XMLTools.PAGE_COUNT));
            }
        }

        // Add Element to the document
        config.appendChild(idxEle);

        // If in verbose mode, show....
        if (table.getBoolean(XMLTools.VERBOSE)) {
            String indexstr = TextWriter.toString(config);
            System.out.println("Index node element = ");
            System.out.println("\t" + indexstr + "\n");
        }

        return config;
    }

    private void addPatterns(Document doc, Element idxEle, String conf) {
        String[] patterns = conf.split(";");

        for (int i = 0; i < patterns.length; i++) {
            String[] st = patterns[i].split("=");
            if (st.length != 2) {
                System.out.println("ERROR : mismatched patterns and aliases in " + conf);
            }

            Element pattern = doc.createElement("pattern");
            pattern.setAttribute("pattern", st[0]);
            pattern.setAttribute("alias", st[1]);

            idxEle.appendChild(pattern);
        }
    }

    public void usage() {
        System.out.println("Format: xindice ai -c <context> [-l [-d <path>]] [-v] [parameters...]");
        System.out.println();
        System.out.println("Creates an index for a collection based on given pattern");
        System.out.println();
        System.out.println("Command-specific switches:");
        System.out.println("    -n|--nameOf <name>");
        System.out.println("                 Name for the indexer to be created, must be present");
        System.out.println("                 unless indexer configuration is specified");
        System.out.println("    -p|--pattern <pattern>");
        System.out.println("                 Index pattern, must be present unless indexer configuration");
        System.out.println("                 is specified. It is either single pattern for NameIndexer");
        System.out.println("                 and ValueIndexer, or semicolon delimited list of patterns for");
        System.out.println("                 LuceneIndexer in the form pattern=alias");
        System.out.println("    -f|--filepath <file>");
        System.out.println("                 Name of the file that holds indexer configuration. If");
        System.out.println("                 specified, the rest of command-specific parameters is");
        System.out.println("                 ignored");
        System.out.println("    -t|--type <type>");
        System.out.println("                 Specify the data type in collection index. Type can be");
        System.out.println("                 one of the following:");
        System.out.println("                 string  (ValueIndexer)");
        System.out.println("                 trimmed (ValueIndexer)");
        System.out.println("                 short   (ValueIndexer)");
        System.out.println("                 int     (ValueIndexer)");
        System.out.println("                 long    (ValueIndexer)");
        System.out.println("                 float   (ValueIndexer)");
        System.out.println("                 double  (ValueIndexer)");
        System.out.println("                 byte    (ValueIndexer)");
        System.out.println("                 char    (ValueIndexer)");
        System.out.println("                 boolean (ValueIndexer)");
        System.out.println("                 name    (NameIndexer)");
        System.out.println("                 text    (LuceneIndexer)");
        System.out.println("                 (default: string/trimmed)");
        System.out.println("    --pagesize <number>");
        System.out.println("                 Page size for file pages (default: 4096). Does not apply to");
        System.out.println("                 LuceneIndexer");
        System.out.println("    --maxkeysize <number>");
        System.out.println("                 The maximum size for file keys (default: 0=none). Does not");
        System.out.println("                 apply to LuceneIndexer");
        System.out.println("    --pagecount <nuymber>");
        System.out.println("                 Number of pages in the primary storage (default: 1024). Does");
        System.out.println("                 not apply to LuceneIndexer");
        System.out.println();
    }
}
TOP

Related Classes of org.apache.xindice.tools.command.AddIndexer

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.