Author: j16sdiz
Date: 2009-02-13 06:47:09 +0000 (Fri, 13 Feb 2009)
New Revision: 25601

Modified:
   trunk/plugins/XMLLibrarian/XMLLibrarian.java
Log:
indent and formatting

Modified: trunk/plugins/XMLLibrarian/XMLLibrarian.java
===================================================================
--- trunk/plugins/XMLLibrarian/XMLLibrarian.java        2009-02-13 06:46:58 UTC 
(rev 25600)
+++ trunk/plugins/XMLLibrarian/XMLLibrarian.java        2009-02-13 06:47:09 UTC 
(rev 25601)
@@ -28,22 +28,22 @@
 import freenet.support.api.Bucket;
 import freenet.support.api.HTTPRequest;
 import freenet.support.io.FileBucket;
+
 /**
- * XMLLibrarian is a modified version of the old librarian.
- * It uses the Xml index files for searching. 
- * In addition to searching in a single index, XMLLibrarian allows searching 
in multiple 
- * indices at the same time. 
- * Folders containing set of indices can be created and any search on a folder 
searches the string in 
- * all the indices in the folder.
+ * XMLLibrarian is a modified version of the old librarian. It uses the Xml 
index files for
+ * searching. In addition to searching in a single index, XMLLibrarian allows 
searching in multiple
+ * indices at the same time. Folders containing set of indices can be created 
and any search on a
+ * folder searches the string in all the indices in the folder.
  * 
- * The current configuration can be stored in an external file and reused 
later. The default file for the same 
- * is XMLLibrarian.xml.
+ * The current configuration can be stored in an external file and reused 
later. The default file
+ * for the same is XMLLibrarian.xml.
  * 
- *The index list of a particular folder can be saved in an external file and 
likewise imported from 
- *an existing file.
- *XMLLibrarian assumes that the index to be used is present at 
DEFAULT_INDEX_SITE/index.xml .
+ *The index list of a particular folder can be saved in an external file and 
likewise imported from
+ * an existing file. XMLLibrarian assumes that the index to be used is present 
at
+ * DEFAULT_INDEX_SITE/index.xml .
+ * 
  * @author swatigoyal
- *
+ * 
  */
 public class XMLLibrarian implements FredPlugin, FredPluginHTTP, 
FredPluginVersioned, FredPluginThreadless {
        /**
@@ -51,8 +51,8 @@
         */
        public static final String DEFAULT_INDEX_SITE = 
"u...@5hh~39ftja7a9~vxwtbki~prudtujzuruddg0xfn3ka,GDgRGt5f6xqbmo-WraQtU54x4H~871Sho9Hz6hC-0RA,AQACAAE/Search/19/";
        /*
-        * Current configuration gets saved by default in the configfile.
-        * To Save the current configuration use "Save Configuration"
+        * Current configuration gets saved by default in the configfile. To 
Save the current
+        * configuration use "Save Configuration"
         */
        private static int version = 20;
        private static final String plugName = "XMLLibrarian " + version;
@@ -60,23 +60,23 @@
        public String getVersion() {
                return version + " r" + Version.getSvnRevision();
        }
-       
+
        private static final String DEFAULT_FILE = "index.xml";
        private PluginRespirator pr;
 
        public void terminate() {
-       
+
        }
+
        public String handleHTTPGet(HTTPRequest request) throws 
PluginHTTPException {
                String search = request.getParam("search");
                String indexuri = request.isParameterSet("index") ? 
request.getParam("index") : DEFAULT_INDEX_SITE;
 
-               
                return handleInner(request.getPath(), search, indexuri);
        }
 
        private void appendDefaultPageStart(StringBuilder out) {
-               
+
                out.append("<HTML><HEAD><TITLE>" + plugName + "</TITLE>");
                out.append("</HEAD><BODY>\n");
                out.append("<CENTER><H1>" + plugName + 
"</H1><BR/><BR/><BR/>\n");
@@ -86,14 +86,14 @@
                out.append("</CENTER></BODY></HTML>");
        }
 
-
        /**
         * appendDefaultPostFields generates the main interface to the 
XMLLibrarian
+        * 
         * @param out
         * @param search
         * @param index
         */
-       
+
        private void appendDefaultPostFields(StringBuilder out, String search, 
String index) {
                search = HTMLEncoder.encode(search);
                index = HTMLEncoder.encode(index);
@@ -109,9 +109,8 @@
                out.append("</p></form>");
        }
 
-
        /**
-        * Generates the interface to the XMLLibrarian and takes apropos action 
to an event. 
+        * Generates the interface to the XMLLibrarian and takes apropos action 
to an event.
         * 
         * @param request
         */
@@ -119,35 +118,39 @@
                String search = request.getPartAsString("search", 80);
                String indexuri = request.isPartSet("index") ? 
request.getPartAsString("index", 200) : DEFAULT_INDEX_SITE;
 
-               
                return handleInner(request.getPath(), search, indexuri);
        }
-       
+
        private String handleInner(String path, String search, String indexuri) 
{
                StringBuilder out = new StringBuilder();
 
-               if (!indexuri.endsWith("/")) indexuri += "/";
+               if (!indexuri.endsWith("/"))
+                       indexuri += "/";
 
                appendDefaultPageStart(out);
                appendDefaultPostFields(out, search, indexuri);
                appendDefaultPageEnd(out);
-               
-                       try{
-                               if(indexuri.equals("")) out.append("Specify a 
valid index \n");
+
+               try {
+                       if (indexuri.equals(""))
+                               out.append("Specify a valid index \n");
                        else
                                searchStr(out, search, indexuri);
+               } catch (Exception e) {
+                       Logger.error(this,
+                               "Searching for the word " + search + " in index 
" + indexuri + " failed " + e.toString(), e);
                }
-                       catch(Exception e){
-                               Logger.error(this, "Searching for the word 
"+search+" in index "+indexuri+" failed "+e.toString(), e);
-                       }
 
                return out.toString();
        }
-       
+
        /**
-        * Searches for the string in the specified index. In case of a folder 
searches in all included indices
+        * Searches for the string in the specified index. In case of a folder 
searches in all included
+        * indices
+        * 
         * @param out
-        * @param search - string to be searched
+        * @param search
+        *            - string to be searched
         * @param indexuri
         * @param stylesheet
         */
@@ -159,23 +162,25 @@
                }
                String searchWord = null;
                try {
-                       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");
+                       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: 
"+HTMLEncoder.encode(indexuri)+"</p>");
-                       
+                       out.append("<p>Index Site: " + 
HTMLEncoder.encode(indexuri) + "</p>");
+
                        String[] searchWords = search.split("[^\\p{L}\\{N}]+");
                        // Return results in order.
                        LinkedHashSet<URIWrapper> hs = null;
                        /*
-                        * search for each string in the search list
-                        * only the common results to all words are returned as 
final result 
-                        * 
-                       */
-                       try{
+                        * search for each string in the search list only the 
common results to all words are
+                        * returned as final result
+                        */
+                       try {
                                for (String s : searchWords) {
                                        searchWord = s;
                                        if (searchWord.length() < 3)
-                                               continue;               // 
xmlspider don't include words length < 3, have to fix this
+                                               continue; // xmlspider don't 
include words length < 3, have to fix this
 
                                        Vector<URIWrapper> keyuris = 
searchWord(indexuri, searchWord);
                                        if (hs == null)
@@ -190,63 +195,72 @@
                                String uri = getSubIndex(indexuri, searchWord);
                                String href = "";
                                String endHref = "";
-                               if(uri != null) {
+                               if (uri != null) {
                                        String encoded = 
HTMLEncoder.encode(uri);
-                                       href="<a href=\"/" + encoded+"\">";
+                                       href = "<a href=\"/" + encoded + "\">";
                                        endHref = "</a>";
                                }
-                               out.append("<p>Could not fetch 
"+href+"sub-index"+endHref+" for "+HTMLEncoder.encode(search)+" : 
"+e.getMessage()+"</p>\n");
-                               Logger.normal(this, "<p>Could not fetch 
sub-index for "+HTMLEncoder.encode(search)+" in 
"+HTMLEncoder.encode(indexuri)+" : "+e.toString()+"</p>\n", e);
-                       } catch(Exception e) {
-                               out.append("<p>Could not complete search for 
"+HTMLEncoder.encode(search) +" : "+e.toString()+"</p>\n");
+                               out.append("<p>Could not fetch " + href + 
"sub-index" + endHref + " for " + HTMLEncoder.encode(search)
+                                       + " : " + e.getMessage() + "</p>\n");
+                               Logger.normal(this, "<p>Could not fetch 
sub-index for " + HTMLEncoder.encode(search) + " in "
+                                       + HTMLEncoder.encode(indexuri) + " : " 
+ e.toString() + "</p>\n", e);
+                       } catch (Exception e) {
+                               out.append("<p>Could not complete search for " 
+ HTMLEncoder.encode(search) + " : " + e.toString()
+                                       + "</p>\n");
                                out.append(String.valueOf(e.getStackTrace()));
-                               Logger.error(this, "Could not complete search 
for "+search +"in "+indexuri+e.toString(), e);
+                               Logger.error(this, "Could not complete search 
for " + search + "in " + indexuri + e.toString(), e);
                        }
                        // Output results
                        int results = 0;
                        out.append("<table class=\"librarian-results\"><tr>\n");
                        Iterator<URIWrapper> it = hs.iterator();
-                       try{
+                       try {
                                while (it.hasNext()) {
                                        URIWrapper o = it.next();
                                        String showurl = o.URI;
                                        String showtitle = o.descr;
-                                       if(showtitle.trim().length() == 0)
+                                       if (showtitle.trim().length() == 0)
                                                showtitle = "not available";
-                                       if(showtitle.equals("not available")) 
showtitle = showurl;
+                                       if (showtitle.equals("not available"))
+                                               showtitle = showurl;
                                        String description = 
HTMLEncoder.encode(o.descr);
-                                       if(!description.equals("not 
available")){
-                                               
description=description.replaceAll("(\n|&lt;(b|B)(r|R)&gt;)", "<br>");
-                                               
description=description.replaceAll("  ", "&nbsp; ");
-                                               
description=description.replaceAll("&lt;/?[a-zA-Z].*/?&gt;", "");       
+                                       if (!description.equals("not 
available")) {
+                                               description = 
description.replaceAll("(\n|&lt;(b|B)(r|R)&gt;)", "<br>");
+                                               description = 
description.replaceAll("  ", "&nbsp; ");
+                                               description = 
description.replaceAll("&lt;/?[a-zA-Z].*/?&gt;", "");
                                        }
                                        showurl = HTMLEncoder.encode(showurl);
                                        if (showurl.length() > 60)
-                                               showurl = 
showurl.substring(0,15) + "&hellip;" +  showurl.replaceFirst("[^/]*/", "/");
-                                       String realurl = 
(o.URI.startsWith("/")?"":"/") + o.URI;
+                                               showurl = showurl.substring(0, 
15) + "&hellip;" + 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(showtitle).append("</A>\n");
+                                       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(showtitle).append("</A>\n");
                                        out.append("</td></tr><tr><td 
align=left class=\"librarian-result-summary\">\n");
                                        out.append("</td></tr></table>\n");
                                        results++;
                                }
+                       } catch (Exception e) {
+                               out.append("Could not display results for " + 
search + e.toString());
+                               Logger.error(this, "Could not display search 
results for " + search + e.toString(), e);
                        }
-                       catch(Exception e){
-                               out.append("Could not display results for 
"+search+e.toString());
-                               Logger.error(this, "Could not display search 
results for "+search+e.toString(), e);
-                       }
                        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");
+                       out
+                               .append(
+                                       "<p><span 
class=\"librarian-summary-found-text\">Found: </span><span 
class=\"librarian-summary-found-number\">")
+                               .append(results).append(" 
results</span></p>\n");
                } catch (Exception e) {
-                       Logger.error(this, "Could not complete search for 
"+search +" in "+indexuri+e.toString(), e);
+                       Logger.error(this, "Could not complete search for " + 
search + " in " + indexuri + e.toString(), e);
                        e.printStackTrace();
                }
        }
-       
+
        private String getSubIndex(String indexuri, String word) {
-               if(word == null) return null;
-               
+               if (word == null)
+                       return null;
+
                try {
                        String subindex = getSubindex(indexuri, word);
                        File file = new File(indexuri + "index_" + subindex + 
".xml");
@@ -261,7 +275,7 @@
                        return null;
                }
        }
-       
+
        /**
         * Search for a word
         */
@@ -294,19 +308,22 @@
                                        throw e;
                        }
                }
-               
+
                return res.asBucket();
        }
-       
+
        /**
-        * Parses through the main index file(index.xml) looking for the 
subindex containing the entry for the search string.
-        * @param str  word to be searched
+        * Parses through the main index file(index.xml) looking for the 
subindex containing the entry
+        * for the search string.
+        * 
+        * @param str
+        *            word to be searched
         * @return
         * @throws Exception
         */
        private String getSubindex(String indexuri, String str) throws 
Exception {
                Bucket bucket = fetchBucket(indexuri + DEFAULT_FILE);
-               
+
                SAXParserFactory factory = SAXParserFactory.newInstance();
                try {
                        SAXParser saxParser = factory.newSAXParser();
@@ -314,7 +331,7 @@
                        LibrarianHandler lib = new LibrarianHandler(str, new 
Vector<URIWrapper>());
                        saxParser.parse(is, lib);
                        is.close();
-                       
+
                        return lib.getPrefixMatch();
                } catch (Throwable err) {
                        err.printStackTrace();
@@ -327,16 +344,18 @@
 
        /**
         * Searches through the chosen subindex for the files containing the 
searc word
-        * @param str search string
+        * 
+        * @param str
+        *            search string
         * @subIndex subIndex containing the word
         */
        private Vector<URIWrapper> getEntry(String str, String indexuri, String 
subIndex) throws Exception {
                //search for the word in the given subIndex
                Vector<URIWrapper> fileuris = new Vector<URIWrapper>();
-               
-                               try {
+
+               try {
                        Bucket bucket = fetchBucket(indexuri + "index_" + 
subIndex + ".xml");
-       
+
                        SAXParserFactory factory = 
SAXParserFactory.newInstance();
                        try {
                                SAXParser saxParser = factory.newSAXParser();
@@ -344,13 +363,12 @@
                                saxParser.parse(is, new LibrarianHandler(str, 
fileuris));
                                is.close();
                        } catch (Throwable err) {
-                               err.printStackTrace ();
-                               throw new Exception("Could not parse XML: 
"+err.toString());
+                               err.printStackTrace();
+                               throw new Exception("Could not parse XML: " + 
err.toString());
                        } finally {
                                bucket.free();
                        }
-               }
-               catch(Exception e){
+               } catch (Exception e) {
                        Logger.error(this, indexuri + "index_" + subIndex + 
".xml could not be opened " + e.toString(), e);
                        throw e;
                }
@@ -372,13 +390,13 @@
                                else
                                        buf.append((char) ('a' + (halfbyte - 
10)));
                                halfbyte = data[i] & 0x0F;
-                       } while(two_halfs++ < 1);
+                       } while (two_halfs++ < 1);
                }
                return buf.toString();
        }
 
        //this function will return the String representation of the MD5 hash 
for the input string 
-       public static String MD5(String text) throws NoSuchAlgorithmException, 
UnsupportedEncodingException  {
+       public static String MD5(String text) throws NoSuchAlgorithmException, 
UnsupportedEncodingException {
                MessageDigest md;
                md = MessageDigest.getInstance("MD5");
                byte[] b = text.getBytes("UTF-8");

_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to