Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 224   Methods: 5
NCLOC: 160   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AddIndexer.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: AddIndexer.java 765794 2009-04-16 22:32:28Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.tools.command;
 21   
 22    import org.apache.xindice.client.xmldb.services.CollectionManager;
 23    import org.apache.xindice.tools.XMLTools;
 24    import org.apache.xindice.xml.TextWriter;
 25    import org.apache.xindice.xml.dom.DocumentImpl;
 26    import org.apache.xindice.xml.dom.DOMParser;
 27    import org.apache.xindice.util.XindiceException;
 28   
 29    import org.w3c.dom.Document;
 30    import org.w3c.dom.Element;
 31    import org.xmldb.api.DatabaseManager;
 32    import org.xmldb.api.base.Collection;
 33   
 34    import java.io.File;
 35    import java.io.InputStream;
 36    import java.io.FileInputStream;
 37   
 38    /**
 39    * AddIndexer.java is designed to let the user create an Indexer
 40    * and insert it into a Collection.
 41    *
 42    * @version $Revision: 765794 $, $Date: 2009-04-16 22:32:28 +0000 (Thu, 16 Apr 2009) $
 43    */
 44    public class AddIndexer extends Command {
 45   
 46  0 public boolean execute(XMLTools.Config table) throws Exception {
 47   
 48  0 if (table.getString(XMLTools.COLLECTION) == null) {
 49  0 System.out.println("ERROR : Collection and switch required");
 50  0 return false;
 51    }
 52   
 53  0 if ("".equals(table.getString(XMLTools.FILE_PATH)) &&
 54    (table.getString(XMLTools.NAME_OF) == null || table.getString(XMLTools.PATTERN) == null)) {
 55  0 System.out.println("ERROR : Name and Pattern required or File path required");
 56  0 return false;
 57    }
 58   
 59  0 Document config;
 60  0 if (!"".equals(table.getString(XMLTools.FILE_PATH))) {
 61    // configuration was passed in a file
 62  0 config = readConfig(table);
 63    } else {
 64    // build configuration from parameters
 65  0 config = buildConfig(table);
 66    }
 67   
 68  0 Collection col = null;
 69  0 try {
 70    // Get a Collection reference to the collection
 71  0 String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
 72    table.getBoolean(XMLTools.LOCAL));
 73   
 74  0 col = DatabaseManager.getCollection(colstring);
 75  0 if (col == null) {
 76  0 System.out.println("ERROR : Collection not found!");
 77  0 return false;
 78    }
 79   
 80    // Create a collection manager instance for the collection
 81  0 CollectionManager colman = (CollectionManager) col.getService("CollectionManager", XMLDBAPIVERSION);
 82   
 83    // Create the indexer for this collection manager
 84  0 colman.createIndexer(config);
 85   
 86  0 System.out.println("CREATED : " + config.getDocumentElement().getAttributeNode("name").getNodeValue());
 87    } finally {
 88  0 if (col != null) {
 89  0 col.close();
 90    }
 91    }
 92   
 93  0 return true;
 94    }
 95   
 96  0 private Document readConfig(XMLTools.Config table) throws Exception {
 97  0 InputStream fis = null;
 98  0 try {
 99  0 File file = new File(table.getString(XMLTools.FILE_PATH));
 100  0 fis = new FileInputStream(file);
 101   
 102  0 return DOMParser.toDocument(fis);
 103    } catch (XindiceException e) {
 104  0 throw new Exception("Indexer configuration could not be parsed", e);
 105    } finally {
 106  0 if (fis != null) {
 107  0 fis.close();
 108    }
 109    }
 110    }
 111   
 112  0 private Document buildConfig(XMLTools.Config table) throws Exception {
 113  0 Document config = new DocumentImpl();
 114   
 115    // Create the index element to hold attributes
 116  0 Element idxEle = config.createElement("index");
 117  0 idxEle.setAttribute("class", XINDICE_VAL_INDEXER);
 118  0 idxEle.setAttribute("name", table.getString(XMLTools.NAME_OF));
 119   
 120    // Setup optional index attributes
 121  0 String type = table.getString(XMLTools.TYPE);
 122  0 if (type != null) {
 123  0 if (type.equalsIgnoreCase("name")) {
 124  0 idxEle.setAttribute("class", XINDICE_NAME_INDEXER);
 125  0 } else if (type.equalsIgnoreCase("text")) {
 126  0 idxEle.setAttribute("class", XINDICE_TEXT_INDEXER);
 127    } else {
 128  0 idxEle.setAttribute("type", type);
 129    }
 130    }
 131   
 132    // LuceneIndexer configuration is different
 133  0 if (idxEle.getAttribute("class").equals(XINDICE_TEXT_INDEXER)) {
 134  0 addPatterns(config, idxEle, table.getString(XMLTools.PATTERN));
 135    } else {
 136  0 idxEle.setAttribute("pattern", table.getString(XMLTools.PATTERN));
 137   
 138  0 if (table.getString(XMLTools.PAGE_SIZE) != null) {
 139  0 idxEle.setAttribute(XMLTools.PAGE_SIZE, table.getString(XMLTools.PAGE_SIZE));
 140    }
 141  0 if (table.getString(XMLTools.MAX_KEY_SIZE) != null) {
 142  0 idxEle.setAttribute(XMLTools.MAX_KEY_SIZE, table.getString(XMLTools.MAX_KEY_SIZE));
 143    }
 144  0 if (table.getString(XMLTools.PAGE_COUNT) != null) {
 145  0 idxEle.setAttribute(XMLTools.PAGE_COUNT, table.getString(XMLTools.PAGE_COUNT));
 146    }
 147    }
 148   
 149    // Add Element to the document
 150  0 config.appendChild(idxEle);
 151   
 152    // If in verbose mode, show....
 153  0 if (table.getBoolean(XMLTools.VERBOSE)) {
 154  0 String indexstr = TextWriter.toString(config);
 155  0 System.out.println("Index node element = ");
 156  0 System.out.println("\t" + indexstr + "\n");
 157    }
 158   
 159  0 return config;
 160    }
 161   
 162  0 private void addPatterns(Document doc, Element idxEle, String conf) {
 163  0 String[] patterns = conf.split(";");
 164   
 165  0 for (int i = 0; i < patterns.length; i++) {
 166  0 String[] st = patterns[i].split("=");
 167  0 if (st.length != 2) {
 168  0 System.out.println("ERROR : mismatched patterns and aliases in " + conf);
 169    }
 170   
 171  0 Element pattern = doc.createElement("pattern");
 172  0 pattern.setAttribute("pattern", st[0]);
 173  0 pattern.setAttribute("alias", st[1]);
 174   
 175  0 idxEle.appendChild(pattern);
 176    }
 177    }
 178   
 179  0 public void usage() {
 180  0 System.out.println("Format: xindice ai -c <context> [-l [-d <path>]] [-v] [parameters...]");
 181  0 System.out.println();
 182  0 System.out.println("Creates an index for a collection based on given pattern");
 183  0 System.out.println();
 184  0 System.out.println("Command-specific switches:");
 185  0 System.out.println(" -n|--nameOf <name>");
 186  0 System.out.println(" Name for the indexer to be created, must be present");
 187  0 System.out.println(" unless indexer configuration is specified");
 188  0 System.out.println(" -p|--pattern <pattern>");
 189  0 System.out.println(" Index pattern, must be present unless indexer configuration");
 190  0 System.out.println(" is specified. It is either single pattern for NameIndexer");
 191  0 System.out.println(" and ValueIndexer, or semicolon delimited list of patterns for");
 192  0 System.out.println(" LuceneIndexer in the form pattern=alias");
 193  0 System.out.println(" -f|--filepath <file>");
 194  0 System.out.println(" Name of the file that holds indexer configuration. If");
 195  0 System.out.println(" specified, the rest of command-specific parameters is");
 196  0 System.out.println(" ignored");
 197  0 System.out.println(" -t|--type <type>");
 198  0 System.out.println(" Specify the data type in collection index. Type can be");
 199  0 System.out.println(" one of the following:");
 200  0 System.out.println(" string (ValueIndexer)");
 201  0 System.out.println(" trimmed (ValueIndexer)");
 202  0 System.out.println(" short (ValueIndexer)");
 203  0 System.out.println(" int (ValueIndexer)");
 204  0 System.out.println(" long (ValueIndexer)");
 205  0 System.out.println(" float (ValueIndexer)");
 206  0 System.out.println(" double (ValueIndexer)");
 207  0 System.out.println(" byte (ValueIndexer)");
 208  0 System.out.println(" char (ValueIndexer)");
 209  0 System.out.println(" boolean (ValueIndexer)");
 210  0 System.out.println(" name (NameIndexer)");
 211  0 System.out.println(" text (LuceneIndexer)");
 212  0 System.out.println(" (default: string/trimmed)");
 213  0 System.out.println(" --pagesize <number>");
 214  0 System.out.println(" Page size for file pages (default: 4096). Does not apply to");
 215  0 System.out.println(" LuceneIndexer");
 216  0 System.out.println(" --maxkeysize <number>");
 217  0 System.out.println(" The maximum size for file keys (default: 0=none). Does not");
 218  0 System.out.println(" apply to LuceneIndexer");
 219  0 System.out.println(" --pagecount <number>");
 220  0 System.out.println(" Number of pages in the primary storage (default: 1024). Does");
 221  0 System.out.println(" not apply to LuceneIndexer");
 222  0 System.out.println();
 223    }
 224    }