/*
* 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.
*
* CVS $Id: AddIndexer.java,v 1.10 2004/02/08 02:57:35 vgritsenko Exp $
*/
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.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import java.util.Hashtable;
/**
* AddIndexer.java is designed to let the user create an Indexer
* and insert it into a Collection.
*
* @version CVS $Revision: 1.10 $, $Date: 2004/02/08 02:57:35 $
*/
public class AddIndexer extends Command {
public boolean execute(Hashtable table) throws Exception {
Collection col = null;
try {
if (table.get(XMLTools.COLLECTION) != null) {
// Get a Collection reference to the collection
String colstring = normalizeCollectionURI((String) table.get(XMLTools.COLLECTION),
(String) table.get(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);
if (table.get(XMLTools.NAME_OF) != null && table.get(XMLTools.PATTERN) != null) {
Document doc = new DocumentImpl();
// Create the index element to hold attributes
Element idxEle = doc.createElement("index");
idxEle.setAttribute("class", XINDICE_VAL_INDEXER);
idxEle.setAttribute("name", (String) table.get(XMLTools.NAME_OF));
idxEle.setAttribute("pattern", (String) table.get(XMLTools.PATTERN));
// Setup optional index attributes
if (table.containsKey(XMLTools.TYPE)) {
String t = (String) table.get(XMLTools.TYPE);
if (t.equalsIgnoreCase("name")) {
idxEle.setAttribute("class", XINDICE_NAME_INDEXER);
} else {
idxEle.setAttribute("type", (String) table.get(XMLTools.TYPE));
}
}
if (table.containsKey(XMLTools.PAGE_SIZE)) {
idxEle.setAttribute("pagesize", (String) table.get(XMLTools.PAGE_SIZE));
}
if (table.containsKey(XMLTools.MAX_KEY_SIZE)) {
idxEle.setAttribute("maxkeysize", (String) table.get(XMLTools.MAX_KEY_SIZE));
}
// Add Element to the document
doc.appendChild(idxEle);
// If in verbose mode, show....
if (table.get(XMLTools.VERBOSE) == "true") {
String indexstr = TextWriter.toString(doc);
System.out.println("Index node element = ");
System.out.println("\t" + indexstr + "\n");
}
// Create the indexer for this collection manager
colman.createIndexer(doc);
System.out.println("CREATED : " + table.get(XMLTools.NAME_OF));
} else {
System.out.println("ERROR : Name and Pattern required");
}
} else {
System.out.println("ERROR : Collection and switch required");
}
} finally {
if (col != null) {
col.close();
}
}
return true;
}
}