Author: natalia
Date: Wed Aug 29 19:58:34 2007
New Revision: 571022

URL: http://svn.apache.org/viewvc?rev=571022&view=rev
Log:
Add LuceneIndexer to list of indexers managed by command-line tools
Indexers can be created by supplying file with index configuration

Modified:
    xml/xindice/trunk/java/src/org/apache/xindice/tools/command/AddIndexer.java
    xml/xindice/trunk/java/src/org/apache/xindice/tools/command/Command.java

Modified: 
xml/xindice/trunk/java/src/org/apache/xindice/tools/command/AddIndexer.java
URL: 
http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/tools/command/AddIndexer.java?rev=571022&r1=571021&r2=571022&view=diff
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/tools/command/AddIndexer.java 
(original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/tools/command/AddIndexer.java 
Wed Aug 29 19:58:34 2007
@@ -23,6 +23,8 @@
 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;
@@ -30,6 +32,9 @@
 import org.xmldb.api.base.Collection;
 
 import java.util.Hashtable;
+import java.io.File;
+import java.io.InputStream;
+import java.io.FileInputStream;
 
 /**
  * AddIndexer.java is designed to let the user create an Indexer
@@ -41,78 +46,45 @@
 
     public boolean execute(Hashtable table) throws Exception {
 
-        Collection col = null;
-        try {
+        if (table.get(XMLTools.COLLECTION) == null) {
+            System.out.println("ERROR : Collection and switch required");
+            return false;
+        }
 
-            if (table.get(XMLTools.COLLECTION) != null) {
+        if ("".equals(table.get(XMLTools.FILE_PATH)) &&
+            (table.get(XMLTools.NAME_OF) == null || 
table.get(XMLTools.PATTERN) == null)) {
+            System.out.println("ERROR : Name and Pattern required or File path 
required");
+            return false;
+        }
 
-                // 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(XMLTools.PAGE_SIZE, (String) 
table.get(XMLTools.PAGE_SIZE));
-                    }
-                    if (table.containsKey(XMLTools.MAX_KEY_SIZE)) {
-                        idxEle.setAttribute(XMLTools.MAX_KEY_SIZE, (String) 
table.get(XMLTools.MAX_KEY_SIZE));
-                    }
-                    if (table.containsKey(XMLTools.PAGE_COUNT)) {
-                       idxEle.setAttribute(XMLTools.PAGE_COUNT, (String) 
table.get(XMLTools.PAGE_COUNT));
-                    }
-
-
-                    // Add Element to the document
-                    doc.appendChild(idxEle);
-
-                    // If in verbose mode, show....
-                    if ("true".equals(table.get(XMLTools.VERBOSE))) {
-                        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");
-                }
+        Document config;
+        if (!"".equals(table.get(XMLTools.FILE_PATH))) {
+            // configuration was passed in a file
+            config = readConfig(table);
+        } else {
+            // build configuration from parameters
+            config = buildConfig(table);
+        }
 
-            } else {
-                System.out.println("ERROR : Collection and switch required");
+        Collection col = null;
+        try {
+            // 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);
+
+            // 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();
@@ -120,5 +92,88 @@
         }
 
         return true;
+    }
+
+    private Document readConfig(Hashtable table) throws Exception {
+        InputStream fis = null;
+        try {
+            File file = new File((String) table.get(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(Hashtable 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", (String) table.get(XMLTools.NAME_OF));
+
+        // Setup optional index attributes
+        if (table.containsKey(XMLTools.TYPE)) {
+            String type = (String) table.get(XMLTools.TYPE);
+            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, (String) table.get(XMLTools.PATTERN));
+        } else {
+            idxEle.setAttribute("pattern", (String) 
table.get(XMLTools.PATTERN));
+
+            if (table.containsKey(XMLTools.PAGE_SIZE)) {
+                idxEle.setAttribute(XMLTools.PAGE_SIZE, (String) 
table.get(XMLTools.PAGE_SIZE));
+            }
+            if (table.containsKey(XMLTools.MAX_KEY_SIZE)) {
+                idxEle.setAttribute(XMLTools.MAX_KEY_SIZE, (String) 
table.get(XMLTools.MAX_KEY_SIZE));
+            }
+            if (table.containsKey(XMLTools.PAGE_COUNT)) {
+                idxEle.setAttribute(XMLTools.PAGE_COUNT, (String) 
table.get(XMLTools.PAGE_COUNT));
+            }
+        }
+
+        // Add Element to the document
+        config.appendChild(idxEle);
+
+        // If in verbose mode, show....
+        if ("true".equals(table.get(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);
+        }
     }
 }

Modified: 
xml/xindice/trunk/java/src/org/apache/xindice/tools/command/Command.java
URL: 
http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/tools/command/Command.java?rev=571022&r1=571021&r2=571022&view=diff
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/tools/command/Command.java 
(original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/tools/command/Command.java 
Wed Aug 29 19:58:34 2007
@@ -39,6 +39,7 @@
     // Class name of the Standard Xindice Indexer
     public static final String XINDICE_VAL_INDEXER = 
"org.apache.xindice.core.indexer.ValueIndexer";
     public static final String XINDICE_NAME_INDEXER = 
"org.apache.xindice.core.indexer.NameIndexer";
+    public static final String XINDICE_TEXT_INDEXER = 
"org.apache.xindice.core.indexer.LuceneIndexer";
 
     //public boolean execute(Hashtable table, Database db)
     public abstract boolean execute(Hashtable table) throws Exception;


Reply via email to