Author: swatig0
Date: 2007-07-16 20:15:13 +0000 (Mon, 16 Jul 2007)
New Revision: 14158
Modified:
trunk/plugins/XMLLibrarian/XMLLibrarian.java
Log:
Librarian with options for multiple indices
Modified: trunk/plugins/XMLLibrarian/XMLLibrarian.java
===================================================================
--- trunk/plugins/XMLLibrarian/XMLLibrarian.java 2007-07-16 13:54:21 UTC
(rev 14157)
+++ trunk/plugins/XMLLibrarian/XMLLibrarian.java 2007-07-16 20:15:13 UTC
(rev 14158)
@@ -1,5 +1,7 @@
package plugins.XMLLibrarian;
+import java.io.BufferedReader;
+import java.io.FileReader;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
@@ -63,6 +65,12 @@
private FileWriter output;
private String prefix_match;
private int prefix;
+ /**
+ * indexList contains the index folders
+ * each folder has a name and a list of indices added to that folder
+ */
+ private HashMap indexList = new HashMap();
+
public void terminate() {
goon = false;
}
@@ -103,11 +111,43 @@
private void appendDefaultPostFields(StringBuffer out, String search,
String index) {
search = HTMLEncoder.encode(search);
index = HTMLEncoder.encode(index);
- out.append("Search for:<br/>");
+ out.append("Search in the index or folder:<br/>");
out.append("<form method=\"GET\"><input type=\"text\"
value=\"").append(search).append("\" name=\"search\" size=80/><br/><br/>");
- out.append("Using the index site(remember to give the site
without index.xml):<br/>");
- out.append("<input type=text name=\"index\"
value=\"").append(index).append("\" size=80/>");
- out.append("<input type=submit value=\"Find!\"/></form>\n");
+ out.append("<input type=\"radio\" name=\"choice\"
value=\"index\">Index<br/>");
+ out.append("Using the index site(remember to give the site without
index.xml):<br/>");
+ out.append("<input type=\"text\" name=\"index\"
value=\"").append(index).append("\" size=80/><br/>");
+
+ out.append("<select name=\"folderList\">");
+
+ // indexList.put("word", "dkfhdj");
+ // indexList.put("try2", "value2");
+
+ String[] words = (String[]) indexList.keySet().toArray(new
String[indexList.size()]);
+
+ for(int i =0;i<words.length;i++)
+ {
+ out.append("<option
value=\"").append(words[i]).append("\">").append(words[i]).append("</option");
+ }
+ out.append("<br/><input type=\"radio\" name=\"choice\"
value=\"folder\"> Folder<br/>");
+ out.append("<input type=submit name=\"addToFolder\" value=\"Add to
folder\" />");
+ out.append("<input type=submit name=\"newFolder\" value=\"New Folder\"
/>");
+// out.append("<select name=\"folderList\">");
+//
+// // indexList.put(word, "dkfhdj");
+//
+// words = (String[]) indexList.keySet().toArray(new
String[indexList.size()]);
+//
+// for(int i =0;i<words.length;i++)
+// {
+// out.append("<option
value=\"").append(words[i]).append("\">").append(words[i]).append("</option");
+// }
+ out.append("<input type=submit name=\"List\" value=\"List\" />");
+ out.append("<input type=submit name=\"help\" value=\"Help!\" />");
+ out.append("<input type=submit name = \"find\" value=\"Find!\"/>\n");
+ out.append("<form><input type=\"file\" name=\"datafile\" /> ");
+ out.append("<input type=submit name=\"Import\" value=\"Import
From File\"/> ");
+ out.append("<form><input type=\"text\" name=\"datafile2\" /> ");
+ out.append("<input type=submit name=\"Export\" value=\"Export
To File\"/> </form>");
// index - key to index
// search - text to search for
}
@@ -147,12 +187,12 @@
public String handleHTTPGet(HTTPRequest request) throws
PluginHTTPException {
StringBuffer out = new StringBuffer();
-
- //int page = request.getIntParam("page", 1);
- String indexuri = request.getParam("index", DEFAULT_INDEX_SITE);
String search = request.getParam("search");
String stylesheet = request.getParam("stylesheet", null);
+ //int page = request.getIntParam("page", 1);
+
+ String choice = request.getParam("choice");
if(stylesheet != null) {
FilterCallback cb =
pr.makeFilterCallback(request.getPath());
try {
@@ -161,119 +201,285 @@
return "Invalid stylesheet: "+e.getMessage();
}
}
+ String indexuri = request.getParam("index", DEFAULT_INDEX_SITE);
+ appendDefaultPageStart(out, stylesheet);
+ //appendDefaultPostFields(out);
+ appendDefaultPostFields(out, search, indexuri);
+ appendDefaultPageEnd(out);
+ if(choice.equals("folder")){
+ if((request.getParam("find")).equals("Find!"))
+ {
+ String folder =
request.getParam("folderList");
+ String[] indices = (String
[])indexList.get(folder);
+ for(int i =0;i<indices.length;i++)
+
searchStr(out,search,indices[i],stylesheet);
+ }
+ // return out.toString();
+ }
+ else if((request.getParam("newFolder")).equals("New Folder")){
+
+ // String[] indices = new String[];
+
+ out.append("<p>Name of the new Folder<br/>");
+ out.append("<form><input type=\"text\"
name=\"newfolder\" size=80/> ");
+ out.append("<input type=submit value=\"Add\"
name=\"addNew\" />");
+ }
+ else if((request.getParam("addNew")).equals("Add"))
+ {
+ String newFolder = request.getParam("newfolder");
+ // out.append("<p>new foler to be added "+newFolder+"</p>");
+ indexList.put(newFolder, new String[]{new String()});
+ out.append("New folder "+newFolder+" added. Kindly refresh the
page<br/> ");
+// appendDefaultPageStart(out, stylesheet);
+// //appendDefaultPostFields(out);
+// appendDefaultPostFields(out, search, indexuri);
+// appendDefaultPageEnd(out);
+ return out.toString();
+ }
+ else if((request.getParam("help")).equals("Help!"))
+ {
+ out.append("<h3>Find</h3>");
+ out.append("<p>Search for the queried word in either an
index site or a selected folder of indices <br/>");
+ out.append("If searching in a folder of indices, select
the appropriate folder and check the button for folder<br/>");
+ out.append("<h3>Add to folder</h3>");
+ out.append("<p>Add the current index site to selected
folder<br/>");
+ out.append("<h3>New folder</h3>");
+ out.append("<p>Create a new folder. To see the added
folder refresh the page<br/>");
+ out.append("<h3>List</h3>");
+ out.append("<p>List the indices in the current
folder<br/>");
+
+ }
+ else if((request.getParam("addToFolder")).equals("Add to
folder"))
+ {
+ String folder = request.getParam("folderList");
+ indexuri =
request.getParam("index",DEFAULT_INDEX_SITE);
+ String[] old = (String []) indexList.get(folder);
+
+ String firstIndex = old[0];
+ String[] indices;
+ if(firstIndex == null || firstIndex.length() == 0)
+ {
+ indices = new String[]{indexuri};
+ }
+ else{
+ indices = new String[old.length+1];
+ System.arraycopy(old, 0, indices, 0, old.length);
+ //out.append("length of new "+indices.length);
+ indices[old.length] = indexuri;}
+ // out.append("length of new "+indices.length);
+ out.append("index site "+indexuri+" added to "+folder);
+ indexList.put(folder, indices);
+
+ }
+ else if((request.getParam("List")).equals("List"))
+ {
+ String folder = request.getParam("folderList");
+ String[] indices = (String[]) indexList.get(folder);
+ for(int i = 0;i<indices.length;i++)
+ {
+ out.append("<p>\n<table class=\"librarian-result\"
width=\"100%\" border=1><tr><td align=center bgcolor=\"#D0D0D0\"
class=\"librarian-result-url\">\n");
+ out.append(" <A
HREF=\"").append(indices[i]).append("\">").append(indices[i]).append("</A>");
+ out.append("</td></tr><tr><td align=left
class=\"librarian-result-summary\">\n");
+
//out.append("<tt>").append(description).append("</tt>\n");
+ out.append("</td></tr></table>\n");
+ }
+
+ }
+// else if((request.getParam("import")).equals("Import from
File")){
+// // String folder = request.getParam("folderList");
+// out.append("<form><input type=\"file\"
name=\"datafile\" /> ");
+// out.append("<input type=submit name=\"Import\"
value=\"Import\" /></form>");
+// }
+// else if((request.getParam("export")).equals("Export to File")){
+//// // String folder = request.getParam("folderList");
+//// out.append("Export Folder :<select
name=\"exportFolder\">");
+////
+//// // indexList.put("word", "dkfhdj");
+//// // indexList.put("try2", "value2");
+////
+//// String[] words = (String[])
indexList.keySet().toArray(new String[indexList.size()]);
+////
+//// for(int i =0;i<words.length;i++)
+//// {
+//// out.append("<option
value=\"").append(words[i]).append("\">").append(words[i]).append("</option");
+//// }
+//// out.append("<form><input type=\"text\"
name=\"datafile2\" /> ");
+//// out.append("<input type=submit name=\"Export\"
value=\"Export\" /></form>");
+// }
+ else if(choice.equals("index"))
+ {
+ searchStr(out,search,indexuri,stylesheet);
+ // return out.toString();
+ }
+ else if((request.getParam("Import")).equals("Import From
File")){
+ String folder = request.getParam("folderList");
+ String file = request.getParam("datafile");
+ Vector indices=new Vector();
+ try{
+ BufferedReader inp = new BufferedReader(new
FileReader(file));
+ String index = inp.readLine();
+ if(index != null) indices.add(index);
+
+ while(index != null){
+ indices.add(index);
+ out.append("index :"+index);
+ index = inp.readLine();
+
+ }
+ String[] old = (String []) indexList.get(folder);
+ String[] finalIndex = new String[old.length +
indices.size()];
+ System.arraycopy(old, 0, finalIndex, 0, old.length);
+ for(int i = 0;i<indices.size();i++){
+ finalIndex[old.length + i] = (String)
indices.elementAt(i);
+ }
+ //System.arraycopy(indices, 0, finalIndex, old.length,
indices.length);
+// indices = new String[old.length+1];
+// System.arraycopy(old, 0, indices, 0, old.length);
+// //out.append("length of new "+indices.length);
+// indices[old.length] = indexuri;}
+// // out.append("length of new "+indices.length);
+// out.append("index site "+indexuri+" added to "+folder);
+ indexList.put(folder, finalIndex);
+ inp.close();
+ }
+ catch(Exception e){
+
+ }
+ }
+ else if((request.getParam("Export")).equals("Export To File")){
+
+ String folder = request.getParam("folderList");
+ String file = request.getParam("datafile2");
+ try{
+ FileWriter outp = new FileWriter(file,true);
+
+ String[] indices = ((String [])indexList.get(folder));
+ for(int i = 0;i<indices.length;i++){
+ outp.write(indices[i]+"\n");
+ }
+ outp.close();
+ }
+ catch(Exception e){
+
+ }
+ return out.toString();
+ }
+ return out.toString();
+ }
+ private void searchStr(StringBuffer out,String search,String
indexuri,String stylesheet){
if (search.equals("")) {
- appendDefaultPageStart(out, stylesheet);
- //appendDefaultPostFields(out);
- appendDefaultPostFields(out, search, indexuri);
- appendDefaultPageEnd(out);
- return out.toString();
+// appendDefaultPageStart(out, stylesheet);
+// //appendDefaultPostFields(out);
+// appendDefaultPostFields(out, search, indexuri);
+// appendDefaultPageEnd(out);
+ return;
}
+ try {
- try {
-
- appendDefaultPageStart(out, stylesheet);
- appendDefaultPostFields(out, search, indexuri);
+// appendDefaultPageStart(out, stylesheet);
+// appendDefaultPostFields(out, search, indexuri);
- out.append("<p><span
class=\"librarian-searching-for-header\">Searching for yoohoo: </span><span
class=\"librarian-searching-for-target\">").append(HTMLEncoder.encode(search)).append("</span></p>\n");
- // Get search result
- String searchWords[] = search.split(" ");
-
- // Return results in order.
- LinkedHashSet hs = new LinkedHashSet();
- //hs will contain the final results
- //first we will check for the first word
- Vector keyuris;
- for(int i = 0;i<searchWords.length;i++){
- keyuris = getIndex(searchWords[i]);
- if(i == 0){
- synchronized(hs){
- hs.clear();
- if (keyuris != null) {
- Iterator it = keyuris.iterator();
- while (it.hasNext())
+ out.append("<p><span
class=\"librarian-searching-for-header\">Searching: </span><span
class=\"librarian-searching-for-target\">").append(HTMLEncoder.encode(search)).append("</span></p>\n");
+ // Get search result
+ out.append("<p>Index Site: "+indexuri+"</p>");
+ String searchWords[] = search.split(" ");
+
+ // Return results in order.
+ LinkedHashSet hs = new LinkedHashSet();
+ //hs will contain the final results
+ //first we will check for the first word
+ Vector keyuris;
+ for(int i = 0;i<searchWords.length;i++){
+ keyuris = getIndex(searchWords[i]);
+ if(i == 0){
+ synchronized(hs){
+ hs.clear();
+ if (keyuris != null) {
+ Iterator it = keyuris.iterator();
+ while (it.hasNext())
+ {
+ hs.add(it.next());
+ }
+ }
+ }
+ }
+ else{
+ try{
+ FileWriter output3 = new
FileWriter("logfile_geturi",true);
+ output3.write("inside keyuris elimination
"+keyuris.size());
+ synchronized(hs){
+ if(keyuris.size() > 0){
+
+ Iterator it = hs.iterator();
+ while(it.hasNext())
{
- hs.add(it.next());
+ URIWrapper uri = (URIWrapper) it.next();
+ //output.write("\nhs values "+uri.URI);
+ if(!Contains(uri.URI,keyuris))
it.remove();
+// if(!((uri.URI).equals(((URIWrapper)
(keyuris.elementAt(0))).URI))) {
+// //output.write("\ndoesn't match
\n "+keyuris.elementAt())
+// it.remove();
+// }
}
- }
+
}
+ if(keyuris.size() == 0) hs.clear();}
+
+ output3.close();
}
- else{
- try{
- FileWriter output3 = new
FileWriter("logfile_geturi",true);
- output3.write("inside keyuris
elimination "+keyuris.size());
- synchronized(hs){
- if(keyuris.size() > 0){
+ catch(Exception e){
+ e.getMessage();
- Iterator it = hs.iterator();
- while(it.hasNext())
- {
- URIWrapper uri = (URIWrapper)
it.next();
- //output.write("\nhs values
"+uri.URI);
- if(!Contains(uri.URI,keyuris))
it.remove();
-//
if(!((uri.URI).equals(((URIWrapper) (keyuris.elementAt(0))).URI))) {
-//
//output.write("\ndoesn't match \n "+keyuris.elementAt())
-// it.remove();
-// }
- }
- }
- if(keyuris.size() == 0) hs.clear();}
-
- output3.close();
- }
- catch(Exception e){
- e.getMessage();
-
-
- }
-
}
-
- }
- // Output results
- int results = 0;
- out.append("<table class=\"librarian-results\"><tr>\n");
- Iterator it = hs.iterator();
- while (it.hasNext()) {
+ }
+
- URIWrapper o = (URIWrapper)it.next();
- String showurl = o.URI;
-
- String description =
HTMLEncoder.encode(o.descr);
- if(!description.equals("not available")){
-
description=description.replaceAll("(\n|<(b|B)(r|R)>)", "<br>");
- description=description.replaceAll("
", " ");
-
description=description.replaceAll("</?[a-zA-Z].*/?>", "");
- }
-
- showurl = HTMLEncoder.encode(showurl);
- if (showurl.length() > 60)
- showurl = showurl.substring(0,15) +
"…" +
- showurl.replaceFirst("[^/]*/", "/");
- String realurl = (o.URI.startsWith("/")?"":"/")
+ o.URI;
- realurl = HTMLEncoder.encode(realurl);
- out.append("<p>\n<table
class=\"librarian-result\" width=\"100%\" border=1><tr><td align=center
bgcolor=\"#D0D0D0\" class=\"librarian-result-url\">\n");
- out.append(" <A
HREF=\"").append(realurl).append("\"
title=\"").append(o.URI).append("\">").append(showurl).append("</A>\n");
- out.append("</td></tr><tr><td align=left
class=\"librarian-result-summary\">\n");
-
//out.append("<tt>").append(description).append("</tt>\n");
- out.append("</td></tr></table>\n");
- results++;
}
- out.append("</tr><table>\n");
- out.append("<p><span class=\"librarian-summary-found-text\">Found:
</span><span
class=\"librarian-summary-found-number\">").append(results).append("
results</span></p>\n");
+ // Output results
+ int results = 0;
+ out.append("<table class=\"librarian-results\"><tr>\n");
+ Iterator it = hs.iterator();
+ while (it.hasNext()) {
-
- appendDefaultPageEnd(out);
+ URIWrapper o = (URIWrapper)it.next();
+ String showurl = o.URI;
- return out.toString();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return e.toString();
+ String description = HTMLEncoder.encode(o.descr);
+ if(!description.equals("not available")){
+
description=description.replaceAll("(\n|<(b|B)(r|R)>)", "<br>");
+ description=description.replaceAll(" ",
" ");
+
description=description.replaceAll("</?[a-zA-Z].*/?>", "");
+ }
+
+ showurl = HTMLEncoder.encode(showurl);
+ if (showurl.length() > 60)
+ showurl = showurl.substring(0,15) + "…"
+
+ showurl.replaceFirst("[^/]*/", "/");
+ String realurl = (o.URI.startsWith("/")?"":"/") + o.URI;
+ realurl = HTMLEncoder.encode(realurl);
+ out.append("<p>\n<table class=\"librarian-result\"
width=\"100%\" border=1><tr><td align=center bgcolor=\"#D0D0D0\"
class=\"librarian-result-url\">\n");
+ out.append(" <A HREF=\"").append(realurl).append("\"
title=\"").append(o.URI).append("\">").append(showurl).append("</A>\n");
+ out.append("</td></tr><tr><td align=left
class=\"librarian-result-summary\">\n");
+
//out.append("<tt>").append(description).append("</tt>\n");
+ out.append("</td></tr></table>\n");
+ results++;
}
+ out.append("</tr><table>\n");
+ out.append("<p><span class=\"librarian-summary-found-text\">Found:
</span><span
class=\"librarian-summary-found-number\">").append(results).append("
results</span></p>\n");
+
+
+ // appendDefaultPageEnd(out);
+
+ //return out.toString();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ // return e.toString();
}
+ }
private boolean Contains(String str, Vector keyuris){
if(keyuris.size() > 0){
//to search if the string is present in the vector