Author: j16sdiz
Date: 2008-12-23 08:08:44 +0000 (Tue, 23 Dec 2008)
New Revision: 24759

Added:
   trunk/plugins/XMLSpider/web/
   trunk/plugins/XMLSpider/web/MainPage.java
   trunk/plugins/XMLSpider/web/WebInterface.java
   trunk/plugins/XMLSpider/web/WebPage.java
Modified:
   trunk/plugins/XMLSpider/Config.java
   trunk/plugins/XMLSpider/IndexWriter.java
   trunk/plugins/XMLSpider/Page.java
   trunk/plugins/XMLSpider/Status.java
   trunk/plugins/XMLSpider/XMLSpider.java
Log:
refactor out web interface

Modified: trunk/plugins/XMLSpider/Config.java
===================================================================
--- trunk/plugins/XMLSpider/Config.java 2008-12-23 08:08:26 UTC (rev 24758)
+++ trunk/plugins/XMLSpider/Config.java 2008-12-23 08:08:44 UTC (rev 24759)
@@ -6,7 +6,7 @@
 import freenet.node.RequestStarter;
 import freenet.support.Logger;
 
-class Config implements Cloneable {
+public class Config implements Cloneable {
        /**
         * Directory where the generated indices are stored
         */

Modified: trunk/plugins/XMLSpider/IndexWriter.java
===================================================================
--- trunk/plugins/XMLSpider/IndexWriter.java    2008-12-23 08:08:26 UTC (rev 
24758)
+++ trunk/plugins/XMLSpider/IndexWriter.java    2008-12-23 08:08:44 UTC (rev 
24759)
@@ -33,9 +33,9 @@
 /**
  * Write index to disk file
  */
-class IndexWriter {
+public class IndexWriter {
        //- Writing Index
-       long tProducedIndex;
+       public long tProducedIndex;
        private Vector<String> indices;
        private int match;
        private long time_taken;

Modified: trunk/plugins/XMLSpider/Page.java
===================================================================
--- trunk/plugins/XMLSpider/Page.java   2008-12-23 08:08:26 UTC (rev 24758)
+++ trunk/plugins/XMLSpider/Page.java   2008-12-23 08:08:44 UTC (rev 24759)
@@ -3,19 +3,19 @@
  */
 package plugins.XMLSpider;
 
-class Page {
+public class Page {
        /** Page Id */
-       long id;
+       public long id;
        /** URI of the page */
-       String uri;
+       public String uri;
        /** Title */
-       String pageTitle;
+       public String pageTitle;
        /** Status */
-       Status status;
+       public Status status;
        /** Last Change Time */
-       long lastChange;
+       public long lastChange;
        /** Comment, for debugging */
-       String comment;
+       public String comment;
 
        public Page() {}        // for db4o callConstructors(true)
 

Modified: trunk/plugins/XMLSpider/Status.java
===================================================================
--- trunk/plugins/XMLSpider/Status.java 2008-12-23 08:08:26 UTC (rev 24758)
+++ trunk/plugins/XMLSpider/Status.java 2008-12-23 08:08:44 UTC (rev 24759)
@@ -3,7 +3,7 @@
  */
 package plugins.XMLSpider;
 
-enum Status {
+public enum Status {
        /** For simplicity, running is also mark as QUEUED */
        QUEUED, SUCCEEDED, FAILED
 }
\ No newline at end of file

Modified: trunk/plugins/XMLSpider/XMLSpider.java
===================================================================
--- trunk/plugins/XMLSpider/XMLSpider.java      2008-12-23 08:08:26 UTC (rev 
24758)
+++ trunk/plugins/XMLSpider/XMLSpider.java      2008-12-23 08:08:44 UTC (rev 
24759)
@@ -10,7 +10,6 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -23,6 +22,8 @@
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
+import plugins.XMLSpider.web.WebInterface;
+
 import com.db4o.Db4o;
 import com.db4o.ObjectContainer;
 import com.db4o.ObjectSet;
@@ -57,7 +58,6 @@
 import freenet.pluginmanager.FredPluginVersioned;
 import freenet.pluginmanager.PluginHTTPException;
 import freenet.pluginmanager.PluginRespirator;
-import freenet.support.HTMLNode;
 import freenet.support.Logger;
 import freenet.support.api.Bucket;
 import freenet.support.api.HTTPRequest;
@@ -93,20 +93,21 @@
        /**
         * Lists the allowed mime types of the fetched page. 
         */
-       public Set<String> allowedMIMETypes;    
+       protected Set<String> allowedMIMETypes; 
        
-       private static int version = 33;
-       private static final String pluginName = "XML spider " + version;
+       static int version = 33;
+       public static final String pluginName = "XML spider " + version;
 
        public String getVersion() {
                return version + " r" + Version.getSvnRevision();
        }
-
-       private NodeClientCore core;
+       
        private FetchContext ctx;
        private boolean stopped = true;
 
-       private PageMaker pageMaker;
+       private NodeClientCore core;
+       private PageMaker pageMaker;    
+       private PluginRespirator pr;
        
        /**
         * Adds the found uri to the list of to-be-retrieved uris. <p>Every usk 
uri added as ssk.
@@ -147,7 +148,7 @@
 
        protected List<Page> queuedRequestCache = new ArrayList<Page>();
 
-       private void startSomeRequests() {
+       public void startSomeRequests() {
                FreenetURI[] initialURIs = core.getBookmarkURIs();
                for (int i = 0; i < initialURIs.length; i++)
                        queueURI(initialURIs[i], "bookmark", false);
@@ -301,7 +302,7 @@
                }
        }
 
-       private synchronized void scheduleMakeIndex() {
+       public synchronized void scheduleMakeIndex() {
                if (writeIndexScheduled || writingIndex)
                        return;
 
@@ -355,7 +356,7 @@
 
        // this is java.util.concurrent.Executor, not freenet.support.Executor
        // always run with one thread --> more thread cause contention and 
slower!
-       protected ThreadPoolExecutor callbackExecutor = new ThreadPoolExecutor( 
//
+       public ThreadPoolExecutor callbackExecutor = new ThreadPoolExecutor( //
                1, 1, 600, TimeUnit.SECONDS, //
                new PriorityBlockingQueue<Runnable>(5, new 
CallbackPrioritizer()), //
                new ThreadFactory() {
@@ -472,6 +473,10 @@
 
        protected IndexWriter indexWriter;
        
+       public IndexWriter getIndexWriter() {
+               return indexWriter;
+       }
+       
        public void terminate(){
                synchronized (this) {
                        stopped = true;
@@ -493,11 +498,9 @@
 
        public synchronized void runPlugin(PluginRespirator pr) {
                this.core = pr.getNode().clientCore;
+               this.pr = pr;
+               pageMaker = pr.getPageMaker();
 
-               this.pageMaker = pr.getPageMaker();
-               
pageMaker.addNavigationLink("/plugins/plugins.XMLSpider.XMLSpider", "Home", 
"Home page", false, null);
-               pageMaker.addNavigationLink("/plugins/", "Plugins page", "Back 
to Plugins page", false, null);
-
                /* Initialize Fetch Context */
                this.ctx = pr.getHLSimpleClient().getFetchContext();
                ctx.maxSplitfileBlockRetries = 1;
@@ -538,6 +541,7 @@
                }
                
                indexWriter = new IndexWriter(this);
+               webInterface = new WebInterface(this);
 
                pr.getNode().executor.execute(new Runnable() {
                        public void run() {
@@ -549,202 +553,16 @@
                }, "Spider Plugin Starter");
        }
 
-       static class PageStatus {
-               long count;
-               List<Page> pages;
-
-               PageStatus(long count, List<Page> pages) {
-                       this.count = count;
-                       this.pages = pages;
-               }
-       }
-
-       private PageStatus getPageStatus(Status status) {
-               Query query = db.query();
-               query.constrain(Page.class);
-               query.descend("status").constrain(status);
-               query.descend("lastChange").orderDescending();
-               
-               @SuppressWarnings("unchecked")
-               ObjectSet<Page> set = query.execute();
-               List<Page> pages = new ArrayList<Page>();
-               while (set.hasNext() && pages.size() < 
config.getMaxShownURIs()) {
-                       pages.add(set.next());
-               }
-
-               return new PageStatus(set.size(), pages);
-       }
-
-       private void listPages(PageStatus pageStatus, HTMLNode parent) {
-               if (pageStatus.pages.isEmpty()) {
-                       HTMLNode list = parent.addChild("#", "NO URI");
-               } else {
-                       HTMLNode list = parent.addChild("ol", "style", 
"overflow: auto; white-space: nowrap;");
-
-                       for (Page page : pageStatus.pages) {
-                               HTMLNode litem = list.addChild("li", "title", 
page.comment);
-                               litem.addChild("a", "href", "/freenet:" + 
page.uri, page.uri);
-                       }
-               }
-       }
-
-       /**
-        * Interface to the Spider data
-        */
+       private WebInterface webInterface;
+       
        public String handleHTTPGet(HTTPRequest request) throws 
PluginHTTPException {
-               HTMLNode pageNode = pageMaker.getPageNode(pluginName, null);
-               HTMLNode contentNode = pageMaker.getContentNode(pageNode);
-
-               return generateHTML(request, pageNode, contentNode);
+               return webInterface.handleHTTPGet(request);
        }
 
        public String handleHTTPPost(HTTPRequest request) throws 
PluginHTTPException {
-               HTMLNode pageNode = pageMaker.getPageNode(pluginName, null);
-               HTMLNode contentNode = pageMaker.getContentNode(pageNode);
-
-               // Create Index
-               if (request.isPartSet("createIndex")) {
-                       synchronized (this) {
-                               scheduleMakeIndex();
-
-                               HTMLNode infobox = 
pageMaker.getInfobox("infobox infobox-success", "Scheduled Creating Index");
-                               infobox.addChild("#", "Index will start create 
soon.");
-                               contentNode.addChild(infobox);
-                       }
-               }
-
-               // Queue URI
-               String addURI = request.getPartAsString("addURI", 512);
-               if (addURI != null && addURI.length() != 0) {
-                       try {
-                               FreenetURI uri = new FreenetURI(addURI);
-                               queueURI(uri, "manually", true);
-
-                               HTMLNode infobox = 
pageMaker.getInfobox("infobox infobox-success", "URI Added");
-                               infobox.addChild("#", "Added " + uri);
-                               contentNode.addChild(infobox);
-                       } catch (Exception e) {
-                               HTMLNode infobox = 
pageMaker.getInfobox("infobox infobox-error", "Error adding URI");
-                               infobox.addChild("#", e.getMessage());
-                               contentNode.addChild(infobox);
-                               Logger.normal(this, "Manual added URI cause 
exception", e);
-                       }
-                       startSomeRequests();
-               }
-               
-               
-
-               return generateHTML(request, pageNode, contentNode);
+               return webInterface.handleHTTPPost(request);
        }
 
-       private String generateHTML(HTTPRequest request, HTMLNode pageNode, 
HTMLNode contentNode) {
-               HTMLNode overviewTable = contentNode.addChild("table", "class", 
"column");
-               HTMLNode overviewTableRow = overviewTable.addChild("tr");
-
-               PageStatus queuedStatus = getPageStatus(Status.QUEUED);
-               PageStatus succeededStatus = getPageStatus(Status.SUCCEEDED);
-               PageStatus failedStatus = getPageStatus(Status.FAILED);
-
-               // Column 1
-               HTMLNode nextTableCell = overviewTableRow.addChild("td", 
"class", "first");
-               HTMLNode statusBox = pageMaker.getInfobox("Spider Status");
-               HTMLNode statusContent = pageMaker.getContentNode(statusBox);
-               statusContent.addChild("#", "Running Request: " + 
runningFetch.size() + "/" + config.getMaxParallelRequests());
-               statusContent.addChild("br");
-               statusContent.addChild("#", "Queued: " + queuedStatus.count);
-               statusContent.addChild("br");
-               statusContent.addChild("#", "Succeeded: " + 
succeededStatus.count);
-               statusContent.addChild("br");
-               statusContent.addChild("#", "Failed: " + failedStatus.count);
-               statusContent.addChild("br");
-               statusContent.addChild("br");
-               statusContent.addChild("#", "Queued Event: " + 
callbackExecutor.getQueue().size());
-               statusContent.addChild("br");
-               statusContent.addChild("#", "Index Writer: ");
-               synchronized (this) {
-                       if (writingIndex)
-                               statusContent.addChild("span", "style", "color: 
red; font-weight: bold;", "RUNNING");
-                       else if (writeIndexScheduled)
-                               statusContent.addChild("span", "style", "color: 
blue; font-weight: bold;", "SCHEDULED");
-                       else
-                               statusContent.addChild("span", "style", "color: 
green; font-weight: bold;", "IDLE");
-               }
-               statusContent.addChild("br");
-               statusContent.addChild("#", "Last Written: "
-                               + (indexWriter.tProducedIndex == 0 ? "NEVER" : 
new Date(indexWriter.tProducedIndex).toString()));
-               nextTableCell.addChild(statusBox);
-
-               // Column 2
-               nextTableCell = overviewTableRow.addChild("td", "class", 
"second");
-               HTMLNode mainBox = pageMaker.getInfobox("Main");
-               HTMLNode mainContent = pageMaker.getContentNode(mainBox);
-               HTMLNode addForm = mainContent.addChild("form", //
-                               new String[] { "action", "method" }, //
-                       new String[] { "plugins.XMLSpider.XMLSpider", "post" });
-               addForm.addChild("label", "for", "addURI", "Add URI:");
-               addForm.addChild("input", new String[] { "name", "style" }, new 
String[] { "addURI", "width: 20em;" });
-               addForm.addChild("input", //
-                               new String[] { "name", "type", "value" },//
-                       new String[] { "formPassword", "hidden", 
core.formPassword });
-               addForm.addChild("input", "type", "submit");
-               nextTableCell.addChild(mainBox);
-
-               HTMLNode indexBox = pageMaker.getInfobox("Create Index");
-               HTMLNode indexContent = pageMaker.getContentNode(indexBox);
-               HTMLNode indexForm = indexContent.addChild("form", //
-                               new String[] { "action", "method" }, //
-                       new String[] { "plugins.XMLSpider.XMLSpider", "post" });
-               indexForm.addChild("input", //
-                               new String[] { "name", "type", "value" },//
-                       new String[] { "formPassword", "hidden", 
core.formPassword });
-               indexForm.addChild("input", //
-                               new String[] { "name", "type", "value" },//
-                       new String[] { "createIndex", "hidden", "createIndex" 
});
-               indexForm.addChild("input", //
-                               new String[] { "type", "value" }, //
-                       new String[] { "submit", "Create Index Now" });
-               nextTableCell.addChild(indexBox);
-
-               HTMLNode runningBox = pageMaker.getInfobox("Running URI");
-               runningBox.addAttribute("style", "right: 0;");
-               HTMLNode runningContent = pageMaker.getContentNode(runningBox);
-               synchronized (runningFetch) {
-                       if (runningFetch.isEmpty()) {
-                               HTMLNode list = runningContent.addChild("#", 
"NO URI");
-                       } else {
-                               HTMLNode list = runningContent.addChild("ol", 
"style", "overflow: auto; white-space: nowrap;");
-
-                               Iterator<Page> pi = 
runningFetch.keySet().iterator();
-                               for (int i = 0; i < config.getMaxShownURIs() && 
pi.hasNext(); i++) {
-                                       Page page = pi.next();
-                                       HTMLNode litem = list.addChild("li", 
"title", page.comment);
-                                       litem.addChild("a", "href", "/freenet:" 
+ page.uri, page.uri);
-                               }
-                       }
-               }
-               contentNode.addChild(runningBox);
-
-               HTMLNode queuedBox = pageMaker.getInfobox("Queued URI");
-               queuedBox.addAttribute("style", "right: 0; overflow: auto;");
-               HTMLNode queuedContent = pageMaker.getContentNode(queuedBox);
-               listPages(queuedStatus, queuedContent);
-               contentNode.addChild(queuedBox);
-
-               HTMLNode succeededBox = pageMaker.getInfobox("Succeeded URI");
-               succeededBox.addAttribute("style", "right: 0;");
-               HTMLNode succeededContent = 
pageMaker.getContentNode(succeededBox);
-               listPages(succeededStatus, succeededContent);
-               contentNode.addChild(succeededBox);
-
-               HTMLNode failedBox = pageMaker.getInfobox("Failed URI");
-               failedBox.addAttribute("style", "right: 0;");
-               HTMLNode failedContent = pageMaker.getContentNode(failedBox);
-               listPages(failedStatus, failedContent);
-               contentNode.addChild(failedBox);
-
-               return pageNode.generate();
-       }
-
        /**
         * creates the callback object for each page.
         *<p>Used to create inlinks and outlinks for each page separately.
@@ -752,7 +570,7 @@
         *
         */
        public class PageCallBack implements FoundURICallback{
-               final Page page;
+               protected final Page page;
 
                PageCallBack(Page page) {
                        this.page = page;
@@ -767,7 +585,7 @@
                        queueURI(uri, "Added from " + page.uri, false);
                }
 
-               Integer lastPosition = null;
+               protected Integer lastPosition = null;
 
                public void onText(String s, String type, URI baseURI){
                        Logger.debug(this, "onText on " + page.id + " (" + 
baseURI + ")");
@@ -942,6 +760,10 @@
 
                return oc;
        }
+       
+       public ObjectContainer getDB() {
+               return db;
+       }
 
        protected Page getPageByURI(FreenetURI uri) {
                Query query = db.query();
@@ -1028,4 +850,26 @@
        public void setLanguage(LANGUAGE newLanguage) {
                language = newLanguage;
        }
+
+       public PageMaker getPageMaker() {
+               return pageMaker;
+       }
+       
+       public List<Page> getRunningFetch() {
+               synchronized (runningFetch) {
+                       return new ArrayList<Page>(runningFetch.keySet());
+               }
+       }
+
+       public boolean isWriteIndexScheduled() {
+               return writeIndexScheduled;
+       }
+
+       public boolean isWritingIndex() {
+               return writingIndex;
+       }
+
+       public PluginRespirator getPluginRespirator() {
+               return pr;
+       }
 }

Added: trunk/plugins/XMLSpider/web/MainPage.java
===================================================================
--- trunk/plugins/XMLSpider/web/MainPage.java                           (rev 0)
+++ trunk/plugins/XMLSpider/web/MainPage.java   2008-12-23 08:08:44 UTC (rev 
24759)
@@ -0,0 +1,219 @@
+/**
+ * Main page
+ * @author j16sdiz (1024D/75494252)
+ */
+package plugins.XMLSpider.web;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+
+import plugins.XMLSpider.Page;
+import plugins.XMLSpider.Status;
+import plugins.XMLSpider.XMLSpider;
+
+import com.db4o.ObjectSet;
+import com.db4o.query.Query;
+
+import freenet.clients.http.PageMaker;
+import freenet.keys.FreenetURI;
+import freenet.pluginmanager.PluginRespirator;
+import freenet.support.HTMLNode;
+import freenet.support.Logger;
+import freenet.support.api.HTTPRequest;
+
+class MainPage implements WebPage {
+       static class PageStatus {
+               long count;
+               List<Page> pages;
+
+               PageStatus(long count, List<Page> pages) {
+                       this.count = count;
+                       this.pages = pages;
+               }
+       }
+
+       private final XMLSpider xmlSpider;
+       private final PageMaker pageMaker;
+       private final PluginRespirator pr;
+
+       MainPage(XMLSpider xmlSpider) {
+               this.xmlSpider = xmlSpider;
+               pageMaker = xmlSpider.getPageMaker();
+               pr = xmlSpider.getPluginRespirator();
+       }
+
+       /*
+        * (non-Javadoc)
+        * 
+        * @see 
plugins.XMLSpider.WebPage#processPostRequest(freenet.support.api.HTTPRequest,
+        * freenet.support.HTMLNode)
+        */
+       public void processPostRequest(HTTPRequest request, HTMLNode 
contentNode) {
+               // Create Index
+               if (request.isPartSet("createIndex")) {
+                       synchronized (this) {
+                               xmlSpider.scheduleMakeIndex();
+
+                               HTMLNode infobox = 
pageMaker.getInfobox("infobox infobox-success", "Scheduled Creating Index");
+                               infobox.addChild("#", "Index will start create 
soon.");
+                               contentNode.addChild(infobox);
+                       }
+               }
+
+               // Queue URI
+               String addURI = request.getPartAsString("addURI", 512);
+               if (addURI != null && addURI.length() != 0) {
+                       try {
+                               FreenetURI uri = new FreenetURI(addURI);
+                               xmlSpider.queueURI(uri, "manually", true);
+
+                               HTMLNode infobox = 
pageMaker.getInfobox("infobox infobox-success", "URI Added");
+                               infobox.addChild("#", "Added " + uri);
+                               contentNode.addChild(infobox);
+                       } catch (Exception e) {
+                               HTMLNode infobox = 
pageMaker.getInfobox("infobox infobox-error", "Error adding URI");
+                               infobox.addChild("#", e.getMessage());
+                               contentNode.addChild(infobox);
+                               Logger.normal(this, "Manual added URI cause 
exception", e);
+                       }
+                       xmlSpider.startSomeRequests();
+               }
+       }
+
+       /*
+        * (non-Javadoc)
+        * 
+        * @see 
plugins.XMLSpider.WebPage#writeContent(freenet.support.api.HTTPRequest,
+        * freenet.support.HTMLNode)
+        */
+       public void writeContent(HTTPRequest request, HTMLNode contentNode) {
+               HTMLNode overviewTable = contentNode.addChild("table", "class", 
"column");
+               HTMLNode overviewTableRow = overviewTable.addChild("tr");
+
+               PageStatus queuedStatus = getPageStatus(Status.QUEUED);
+               PageStatus succeededStatus = getPageStatus(Status.SUCCEEDED);
+               PageStatus failedStatus = getPageStatus(Status.FAILED);
+
+               List<Page> runningFetch = xmlSpider.getRunningFetch();
+
+               // Column 1
+               HTMLNode nextTableCell = overviewTableRow.addChild("td", 
"class", "first");
+               HTMLNode statusBox = pageMaker.getInfobox("Spider Status");
+               HTMLNode statusContent = pageMaker.getContentNode(statusBox);
+               statusContent.addChild("#", "Running Request: " + 
runningFetch.size() + "/"
+                       + xmlSpider.getConfig().getMaxParallelRequests());
+               statusContent.addChild("br");
+               statusContent.addChild("#", "Queued: " + queuedStatus.count);
+               statusContent.addChild("br");
+               statusContent.addChild("#", "Succeeded: " + 
succeededStatus.count);
+               statusContent.addChild("br");
+               statusContent.addChild("#", "Failed: " + failedStatus.count);
+               statusContent.addChild("br");
+               statusContent.addChild("br");
+               statusContent.addChild("#", "Queued Event: " + 
xmlSpider.callbackExecutor.getQueue().size());
+               statusContent.addChild("br");
+               statusContent.addChild("#", "Index Writer: ");
+               synchronized (this) {
+                       if (xmlSpider.isWritingIndex())
+                               statusContent.addChild("span", "style", "color: 
red; font-weight: bold;", "RUNNING");
+                       else if (xmlSpider.isWriteIndexScheduled())
+                               statusContent.addChild("span", "style", "color: 
blue; font-weight: bold;", "SCHEDULED");
+                       else
+                               statusContent.addChild("span", "style", "color: 
green; font-weight: bold;", "IDLE");
+               }
+               statusContent.addChild("br");
+               statusContent.addChild("#", "Last Written: "
+                       + (xmlSpider.getIndexWriter().tProducedIndex == 0 ? 
"NEVER" : new Date(
+                               
xmlSpider.getIndexWriter().tProducedIndex).toString()));
+               nextTableCell.addChild(statusBox);
+
+               // Column 2
+               nextTableCell = overviewTableRow.addChild("td", "class", 
"second");
+               HTMLNode mainBox = pageMaker.getInfobox("Main");
+               HTMLNode mainContent = pageMaker.getContentNode(mainBox);
+               HTMLNode addForm = pr.addFormChild(mainContent, 
"plugins.XMLSpider.XMLSpider", "addForm");
+               addForm.addChild("label", "for", "addURI", "Add URI:");
+               addForm.addChild("input", new String[] { "name", "style" }, new 
String[] { "addURI", "width: 20em;" });
+               addForm.addChild("input", "type", "submit");
+               nextTableCell.addChild(mainBox);
+
+               HTMLNode indexBox = pageMaker.getInfobox("Create Index");
+               HTMLNode indexContent = pageMaker.getContentNode(indexBox);
+               HTMLNode indexForm = pr.addFormChild(indexContent, 
"plugins.XMLSpider.XMLSpider", "indexForm");
+               indexForm.addChild("input", //
+                       new String[] { "name", "type", "value" },//
+                       new String[] { "createIndex", "hidden", "createIndex" 
});
+               indexForm.addChild("input", //
+                       new String[] { "type", "value" }, //
+                       new String[] { "submit", "Create Index Now" });
+               nextTableCell.addChild(indexBox);
+
+               HTMLNode runningBox = pageMaker.getInfobox("Running URI");
+               runningBox.addAttribute("style", "right: 0;");
+               HTMLNode runningContent = pageMaker.getContentNode(runningBox);
+
+               if (runningFetch.isEmpty()) {
+                       runningContent.addChild("#", "NO URI");
+               } else {
+                       HTMLNode list = runningContent.addChild("ol", "style", 
"overflow: auto; white-space: nowrap;");
+
+                       Iterator<Page> pi = runningFetch.iterator();
+                       for (int i = 0; i < 
xmlSpider.getConfig().getMaxShownURIs() && pi.hasNext(); i++) {
+                               Page page = pi.next();
+                               HTMLNode litem = list.addChild("li", "title", 
page.comment);
+                               litem.addChild("a", "href", "/freenet:" + 
page.uri, page.uri);
+                       }
+               }
+               contentNode.addChild(runningBox);
+
+               HTMLNode queuedBox = pageMaker.getInfobox("Queued URI");
+               queuedBox.addAttribute("style", "right: 0; overflow: auto;");
+               HTMLNode queuedContent = pageMaker.getContentNode(queuedBox);
+               listPages(queuedStatus, queuedContent);
+               contentNode.addChild(queuedBox);
+
+               HTMLNode succeededBox = pageMaker.getInfobox("Succeeded URI");
+               succeededBox.addAttribute("style", "right: 0;");
+               HTMLNode succeededContent = 
pageMaker.getContentNode(succeededBox);
+               listPages(succeededStatus, succeededContent);
+               contentNode.addChild(succeededBox);
+
+               HTMLNode failedBox = pageMaker.getInfobox("Failed URI");
+               failedBox.addAttribute("style", "right: 0;");
+               HTMLNode failedContent = pageMaker.getContentNode(failedBox);
+               listPages(failedStatus, failedContent);
+               contentNode.addChild(failedBox);
+       }
+
+       //-- Utilities
+       private PageStatus getPageStatus(Status status) {
+               Query query = xmlSpider.getDB().query();
+               query.constrain(Page.class);
+               query.descend("status").constrain(status);
+               query.descend("lastChange").orderDescending();
+
+               @SuppressWarnings("unchecked")
+               ObjectSet<Page> set = query.execute();
+               List<Page> pages = new ArrayList<Page>();
+               while (set.hasNext() && pages.size() < 
xmlSpider.getConfig().getMaxShownURIs()) {
+                       pages.add(set.next());
+               }
+
+               return new PageStatus(set.size(), pages);
+       }
+
+       private void listPages(PageStatus pageStatus, HTMLNode parent) {
+               if (pageStatus.pages.isEmpty()) {
+                       parent.addChild("#", "NO URI");
+               } else {
+                       HTMLNode list = parent.addChild("ol", "style", 
"overflow: auto; white-space: nowrap;");
+
+                       for (Page page : pageStatus.pages) {
+                               HTMLNode litem = list.addChild("li", "title", 
page.comment);
+                               litem.addChild("a", "href", "/freenet:" + 
page.uri, page.uri);
+                       }
+               }
+       }
+}
\ No newline at end of file

Added: trunk/plugins/XMLSpider/web/WebInterface.java
===================================================================
--- trunk/plugins/XMLSpider/web/WebInterface.java                               
(rev 0)
+++ trunk/plugins/XMLSpider/web/WebInterface.java       2008-12-23 08:08:44 UTC 
(rev 24759)
@@ -0,0 +1,60 @@
+/**
+ * Web reuqest handlers
+ * 
+ * @author j16sdiz (1024D/75494252)
+ */
+package plugins.XMLSpider.web;
+
+import plugins.XMLSpider.XMLSpider;
+import freenet.clients.http.PageMaker;
+import freenet.pluginmanager.PluginHTTPException;
+import freenet.support.HTMLNode;
+import freenet.support.api.HTTPRequest;
+
+public class WebInterface {
+       private final XMLSpider xmlSpider;
+       private PageMaker pageMaker;
+
+       /**
+        * @param spider
+        */
+       public WebInterface(XMLSpider spider) {
+               xmlSpider = spider;
+
+               pageMaker = xmlSpider.getPageMaker();
+               
pageMaker.addNavigationLink("/plugins/plugins.XMLSpider.XMLSpider", //
+                       "Home", "Home page", false, null);
+               
//pageMaker.addNavigationLink("/plugins/plugins.XMLSpider.XMLSpider?ConfigPage",
 //
+               //        "Config", "Configuration", false, null);
+               pageMaker.addNavigationLink("/plugins/", "Plugins page", "Back 
to Plugins page", false, null);
+       }
+
+       /**
+        * Interface to the Spider data
+        */
+       public String handleHTTPGet(HTTPRequest request) throws 
PluginHTTPException {
+               HTMLNode pageNode = pageMaker.getPageNode(XMLSpider.pluginName, 
null);
+               HTMLNode contentNode = pageMaker.getContentNode(pageNode);
+
+               WebPage page = getPageObject(request);
+               page.writeContent(request, contentNode);
+
+               return pageNode.generate();
+       }
+
+       public String handleHTTPPost(HTTPRequest request) throws 
PluginHTTPException {
+               HTMLNode pageNode = pageMaker.getPageNode(XMLSpider.pluginName, 
null);
+               HTMLNode contentNode = pageMaker.getContentNode(pageNode);
+
+               WebPage page = getPageObject(request);
+
+               page.processPostRequest(request, contentNode);
+               page.writeContent(request, contentNode);
+
+               return pageNode.generate();
+       }
+
+       public WebPage getPageObject(HTTPRequest request) {
+               return new MainPage(xmlSpider);
+       }
+}
\ No newline at end of file

Added: trunk/plugins/XMLSpider/web/WebPage.java
===================================================================
--- trunk/plugins/XMLSpider/web/WebPage.java                            (rev 0)
+++ trunk/plugins/XMLSpider/web/WebPage.java    2008-12-23 08:08:44 UTC (rev 
24759)
@@ -0,0 +1,17 @@
+package plugins.XMLSpider.web;
+
+import freenet.support.HTMLNode;
+import freenet.support.api.HTTPRequest;
+
+/**
+ * Interface for all web pages
+ * 
+ * @author j16sdiz (1024D/75494252)
+ */
+interface WebPage {
+
+       public abstract void processPostRequest(HTTPRequest request, HTMLNode 
contentNode);
+
+       public abstract void writeContent(HTTPRequest request, HTMLNode 
contentNode);
+
+}
\ No newline at end of file

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

Reply via email to