Author: jflesch
Date: 2007-08-03 17:40:46 +0000 (Fri, 03 Aug 2007)
New Revision: 14475

Added:
   trunk/apps/Thaw/src/thaw/plugins/index/FileContainer.java
   trunk/apps/Thaw/src/thaw/plugins/index/IndexContainer.java
   trunk/apps/Thaw/src/thaw/plugins/index/IndexParser.java
   trunk/apps/Thaw/src/thaw/plugins/index/LinkContainer.java
Modified:
   trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java
   trunk/apps/Thaw/src/thaw/plugins/TrayIcon.java
   trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java
   trunk/apps/Thaw/src/thaw/plugins/index/File.java
   trunk/apps/Thaw/src/thaw/plugins/index/Index.java
   trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java
   trunk/apps/Thaw/src/thaw/plugins/index/Link.java
Log:
Separate the index parsing from the index storage

Modified: trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java
===================================================================
--- trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java     2007-08-03 03:39:36 UTC 
(rev 14474)
+++ trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java     2007-08-03 17:40:46 UTC 
(rev 14475)
@@ -372,8 +372,7 @@

                /* SECURITY */
                if(rawBytesWaiting > 0) {
-                       Logger.error(this, "RAW BYTES STILL WAITING ON SOCKET. 
THIS IS ABNORMAL.");
-                       Logger.error(this, "Will drop them.");
+                       Logger.warning(this, "RAW BYTES STILL WAITING ON 
SOCKET. THIS IS ABNORMAL. : Will drop them.");

                        while(rawBytesWaiting > 0) {
                                int to_read = 1024;

Modified: trunk/apps/Thaw/src/thaw/plugins/TrayIcon.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/TrayIcon.java      2007-08-03 03:39:36 UTC 
(rev 14474)
+++ trunk/apps/Thaw/src/thaw/plugins/TrayIcon.java      2007-08-03 17:40:46 UTC 
(rev 14475)
@@ -149,8 +149,7 @@


        public String getNameForUser() {
-               return I18n.getMessage("thaw.plugin.trayIcon.pluginName")
-                       + " "+I18n.getMessage("thaw.common.unstable.linux");
+               return I18n.getMessage("thaw.plugin.trayIcon.pluginName");
        }

        public javax.swing.ImageIcon getIcon() {

Modified: trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java 2007-08-03 
03:39:36 UTC (rev 14474)
+++ trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java 2007-08-03 
17:40:46 UTC (rev 14475)
@@ -527,7 +527,7 @@
                 * if null, then not in an index
                 * else all the tag found will be sent to this handler
                 */
-               private Index.IndexHandler indexHandler = null;
+               private IndexParser.IndexHandler indexHandler = null;


                /**
@@ -575,7 +575,7 @@
                                if (index != null) {
                                        
index.rename(attrs.getValue("displayName"));

-                                       indexHandler = index.getIndexHandler();
+                                       indexHandler = new 
IndexParser(index).getIndexHandler();

                                        indexHandler.startDocument();
                                }

Modified: trunk/apps/Thaw/src/thaw/plugins/index/File.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/File.java    2007-08-03 03:39:36 UTC 
(rev 14474)
+++ trunk/apps/Thaw/src/thaw/plugins/index/File.java    2007-08-03 17:40:46 UTC 
(rev 14475)
@@ -26,7 +26,7 @@
 import thaw.plugins.Hsqldb;


-public class File implements Observer {
+public class File implements Observer, FileContainer {
        private Hsqldb db = null;
        private int id = -1; /* -1 = undefined */

@@ -175,6 +175,11 @@
                return res;
        }

+
+       public String getMime() {
+               return mime;
+       }
+
        public long getSize() {
                return size;
        }

Added: trunk/apps/Thaw/src/thaw/plugins/index/FileContainer.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/FileContainer.java                   
        (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/index/FileContainer.java   2007-08-03 
17:40:46 UTC (rev 14475)
@@ -0,0 +1,19 @@
+package thaw.plugins.index;
+
+public interface FileContainer {
+
+       public String getFilename();
+
+       /**
+        * @return if the result is not a valid key, it won't be added to the 
index
+        */
+       public String getPublicKey();
+
+       public long getSize();
+
+       /**
+        * @return null if null, the mime type will be guessed from the filename
+        */
+       public String getMime();
+
+}

Modified: trunk/apps/Thaw/src/thaw/plugins/index/Index.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/Index.java   2007-08-03 03:39:36 UTC 
(rev 14474)
+++ trunk/apps/Thaw/src/thaw/plugins/index/Index.java   2007-08-03 17:40:46 UTC 
(rev 14475)
@@ -22,40 +22,11 @@

 import javax.swing.tree.TreePath;

-
-/* DOM */
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-
-import org.w3c.dom.DOMImplementation;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Text;



-/* SAX */
-
-import org.xml.sax.*;
-import org.xml.sax.helpers.LocatorImpl;
-
-import java.io.IOException;
-
-import org.xml.sax.helpers.XMLReaderFactory;
-import org.xml.sax.helpers.DefaultHandler;
-
-import javax.xml.parsers.SAXParserFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
-
 import thaw.core.Main;

 import thaw.core.I18n;
@@ -76,7 +47,11 @@



-public class Index extends Observable implements MutableTreeNode, 
FileAndLinkList, IndexTreeNode, Observer {
+public class Index extends Observable implements MutableTreeNode,
+                                                IndexTreeNode,
+                                                Observer,
+                                                IndexContainer {
+
        private final static long MAX_SIZE = 5242880; /* 5MB */


@@ -105,8 +80,6 @@

        private Config config;

-       public final static String DATE_FORMAT = "yyyyMMdd";
-
        /**
         * @deprecated Just don't use it !
         */
@@ -439,7 +412,7 @@
        }


-       public boolean getPublishPrivateKey() {
+       public boolean publishPrivateKey() {
                try {

                PreparedStatement st
@@ -669,7 +642,7 @@


                /* Let's hope that users are not stupid
-                * and won't insert 5 revisions at once. */
+                * and won't insert too much revisions at once. */
                /*
                if (indexBrowser != null && indexBrowser.getIndexTree() != null
                    && indexBrowser.getIndexTree().isIndexUpdating(this)) {
@@ -696,7 +669,9 @@

                Logger.info(this, "Generating index ...");

-               if (!generateXML(targetFile.getAbsolutePath()))
+               IndexParser parser = new IndexParser(this);
+
+               if (!parser.generateXML(targetFile.getAbsolutePath()))
                        return 0;

                FCPClientPut put;
@@ -894,8 +869,10 @@
                                String path = get.getPath();

                                if (path != null) {
-                                       loadXML(path);
+                                       IndexParser parser = new 
IndexParser(this);

+                                       parser.loadXML(path);
+
                                        boolean loadComm = true;

                                        if (config != null && 
config.getValue("indexFetchComments") != null)
@@ -993,8 +970,88 @@
        }


+       public void purgeIndex() {
+               purgeFileList();
+               purgeLinkList();
+               purgeCommentKeys();
+       }
+
+
+
+       public void setInsertionDate(java.util.Date date) {
+               try {
+                       synchronized(db.dbLock) {
+                               java.sql.Date dateSql = null;
+                               dateSql = new java.sql.Date(date.getTime());
+
+                               PreparedStatement st =
+                                       
db.getConnection().prepareStatement("UPDATE indexes "+
+                                                                           
"SET insertionDate = ? "+
+                                                                           
"WHERE id = ?");
+                               st.setDate(1, dateSql);
+                               st.setInt(2, id);
+
+                               st.execute();
+                       }
+               } catch(SQLException e) {
+                       Logger.error(this, "Error while updating index 
insertion date: "+e.toString());
+               }
+       }
+
+
+
+       ////// Comments black list //////
+       public Vector getCommentBlacklistedRev() {
+               Vector v = new Vector();
+
+               try {
+                       synchronized(db.dbLock) {
+                               PreparedStatement st;
+
+                               st = 
db.getConnection().prepareStatement("SELECT rev "+
+                                                                        "FROM 
indexCommentBlackList "+
+                                                                        "WHERE 
indexId = ?");
+                               st.setInt(1, id);
+
+                               ResultSet set = st.executeQuery();
+
+                               while (set.next()) {
+                                       v.add(new Integer(set.getInt("rev")));
+                               }
+                       }
+               } catch(SQLException e) {
+                       Logger.error(this, "Unable to get comment black list  
because: "+e.toString());
+               }
+
+               return v;
+       }
+
+
+       public void addBlackListedRev(int rev) {
+               try {
+                       synchronized(db.dbLock) {
+                               PreparedStatement st;
+
+                               st = 
db.getConnection().prepareStatement("INSERT into indexCommentBlackList (rev, 
indexId) VALUES (?, ?)");
+                               st.setInt(1, rev);
+                               st.setInt(2, id);
+
+                               st.execute();
+
+                       }
+               } catch(SQLException e) {
+                       Logger.error(this, "Error while adding element to the 
blackList: "+e.toString());
+               }
+
+       }
+
+
        ////// FILE LIST ////////

+       public Vector getFileList() {
+               return getFileList(null, false);
+       }
+
        public Vector getFileList(String columnToSort, boolean asc) {
                synchronized(db.dbLock) {

@@ -1041,8 +1098,41 @@
        }


+       public void addFile(String key, long size, String mime) {
+               try {
+                       synchronized(db.dbLock) {
+                               PreparedStatement st;
+
+                               st = 
db.getConnection().prepareStatement("INSERT INTO files "
+                                                                        + 
"(filename, publicKey, localPath, mime, size, category, indexParent) "
+                                                                        + 
"VALUES (?, ?, NULL, ?, ?, NULL, ?)");
+
+
+                               String filename = 
FreenetURIHelper.getFilenameFromKey(key);
+                               if (filename == null)
+                                       filename = key;
+
+                               st.setString(1, filename);
+                               st.setString(2, key);
+                               st.setString(3, mime);
+                               st.setLong(4, size);
+                               st.setInt(5, id);
+
+                               st.execute();
+                       }
+               } catch(SQLException e) {
+                       Logger.error(this, "Error while adding file to index 
'"+toString()+"' : "+e.toString());
+               }
+       }
+
+
+
        //// LINKS ////

+       public Vector getLinkList() {
+               return getLinkList(null, false);
+       }
+
        public Vector getLinkList(String columnToSort, boolean asc) {
                synchronized(db.dbLock) {

@@ -1051,7 +1141,8 @@

                                PreparedStatement st;

-                               st = 
db.getConnection().prepareStatement("SELECT id, publicKey, blackListed FROM 
links WHERE indexParent = ?");
+                               st = 
db.getConnection().prepareStatement("SELECT id, publicKey, blackListed "+
+                                                                        "FROM 
links WHERE indexParent = ?");

                                st.setInt(1, id);

@@ -1074,704 +1165,89 @@
                return null;
        }

-       //// XML ////
-       public boolean generateXML(String path) {
-               try {
-                       generateXML(new FileOutputStream(new File(path)));
-                       return true;
-               } catch(java.io.FileNotFoundException e) {
-                       Logger.error(this, "File not found exception ?!");
-               }
-               return false;
-       }

-       public void generateXML(final OutputStream out) {
-               StreamResult streamResult;
+       public static String getNameFromKey(final String key) {
+               String name = null;

-               streamResult = new StreamResult(out);
+               name = FreenetURIHelper.getFilenameFromKey(key);

-               Document xmlDoc;
+               if (name == null)
+                       return null;

-               final DocumentBuilderFactory xmlFactory = 
DocumentBuilderFactory.newInstance();
-               DocumentBuilder xmlBuilder;
+               /* quick and dirty */
+               name = name.replaceAll(".xml", "");
+               name = name.replaceAll(".frdx", "");

-               try {
-                       xmlBuilder = xmlFactory.newDocumentBuilder();
-               } catch(final javax.xml.parsers.ParserConfigurationException e) 
{
-                       Logger.error(this, "Unable to generate the index 
because : "+e.toString());
-                       return;
-               }
+               return name;
+       }

-               final DOMImplementation impl = 
xmlBuilder.getDOMImplementation();

-               xmlDoc = impl.createDocument(null, "index", null);
-
-               final Element rootEl = xmlDoc.getDocumentElement();
-
-               fillInRootElement(rootEl, xmlDoc);
-
-               /* Serialization */
-               final DOMSource domSource = new DOMSource(xmlDoc);
-               final TransformerFactory transformFactory = 
TransformerFactory.newInstance();
-
-               Transformer serializer;
-
+       public void addLink(String key) {
                try {
-                       serializer = transformFactory.newTransformer();
-               } catch(final 
javax.xml.transform.TransformerConfigurationException e) {
-                       Logger.error(this, "Unable to save index because: 
"+e.toString());
-                       return;
-               }
+                       if (key == null) /* it was the beginning of the index */
+                               return;

-               serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
-               serializer.setOutputProperty(OutputKeys.INDENT,"yes");
+                       key = key.trim();

-               /* final step */
-               try {
-                       serializer.transform(domSource, streamResult);
-               } catch(final javax.xml.transform.TransformerException e) {
-                       Logger.error(this, "Unable to save index because: 
"+e.toString());
-                       return;
-               }
-       }

-       public boolean fillInRootElement(Element rootEl, Document xmlDoc) {
-               rootEl.appendChild(getXMLHeader(xmlDoc));
-               rootEl.appendChild(getXMLLinks(xmlDoc));
-               rootEl.appendChild(getXMLFileList(xmlDoc));
+                       boolean blackListed = (BlackList.isBlackListed(db, key) 
>= 0);

-               if (canHaveComments())
-                       rootEl.appendChild(getXMLCommentInfos(xmlDoc));
-
-               return true;
-       }
-
-       public Element getXMLHeader(final Document xmlDoc) {
-               final Element header = xmlDoc.createElement("header");
-
-               final Element title = xmlDoc.createElement("title");
-               final Text titleText = xmlDoc.createTextNode(toString(false));
-               title.appendChild(titleText);
-
-               header.appendChild(title);
-
-
-               final Element thawVersion = xmlDoc.createElement("client");
-               final Text versionText = xmlDoc.createTextNode("Thaw 
"+Main.VERSION);
-               thawVersion.appendChild(versionText);
-
-               header.appendChild(thawVersion);
-
-               if (getPublishPrivateKey() && getPrivateKey() != null) {
-                       final Element privateKeyEl = 
xmlDoc.createElement("privateKey");
-                       final Text privateKeyText = 
xmlDoc.createTextNode(getPrivateKey());
-                       privateKeyEl.appendChild(privateKeyText);
-
-                       header.appendChild(privateKeyEl);
-               }
-
-               /* insertion date */
-               String dateStr;
-
-               java.text.SimpleDateFormat sdf =
-                       new java.text.SimpleDateFormat(DATE_FORMAT);
-               Calendar c1 = Calendar.getInstance(); // today
-               dateStr = sdf.format(c1.getTime());
-
-               final Element date = xmlDoc.createElement("date");
-               final Text dateText = xmlDoc.createTextNode(dateStr);
-               date.appendChild(dateText);
-
-               header.appendChild(date);
-
-               /* TODO : Author */
-
-               return header;
-       }
-
-
-       /**
-        * must be in a synchronized(db.dbLock)
-        */
-       private String findTheLatestKey(String linkKey) throws SQLException {
-               PreparedStatement st = 
db.getConnection().prepareStatement("SELECT publicKey FROM indexes "+
-                                                                          
"WHERE publicKey LIKE ?");
-               st.setString(1, FreenetURIHelper.getComparablePart(linkKey));
-
-               ResultSet set = st.executeQuery();
-
-               while (set.next()) {
-                       /* we will assume that we have *always* the latest 
version of the index */
-
-                       String oKey = set.getString("publicKey");
-                       if (FreenetURIHelper.compareKeys(oKey, linkKey))
-                               return oKey;
-               }
-
-               return linkKey;
-       }
-
-
-       public Element getXMLLinks(final Document xmlDoc) {
-               final Element links = xmlDoc.createElement("indexes");
-               synchronized(db.dbLock) {
-                       try {
+                       synchronized(db.dbLock) {
                                PreparedStatement st;

-                               st = 
db.getConnection().prepareStatement("SELECT publicKey FROM links WHERE 
indexParent = ?");
-                               st.setInt(1, id);
+                               st = 
db.getConnection().prepareStatement("INSERT INTO links "+
+                                                                        
"(publicKey, mark, comment, "+
+                                                                        
"indexParent, indexTarget, blackListed) "+
+                                                                        
"VALUES (?, 0, ?, ?, NULL, ?)");
+                               st.setString(1, key);
+                               st.setString(2, "No comment"); /* comment not 
used at the moment */
+                               st.setInt(3, id);
+                               st.setBoolean(4, blackListed);

-                               ResultSet set = st.executeQuery();
-
-                               while (set.next()) {
-                                       String key = 
findTheLatestKey(set.getString("publicKey"));
-
-                                       final Element xmlLink = 
xmlDoc.createElement("index");
-
-                                       xmlLink.setAttribute("key", key);
-
-                                       if (xmlLink != null)
-                                               links.appendChild(xmlLink);
-                                       else
-                                               Logger.warning(this, "Unable to 
get XML for a link => Gruick da link");
-
-                               }
-                       } catch(SQLException e) {
-                               Logger.error(this, "Error while getting link 
list for XML : "+e.toString());
+                               st.execute();
                        }
+               } catch(SQLException e) {
+                       Logger.error(this, "Error while adding link to index 
'"+toString()+"' : "+e.toString());
                }
-
-               return links;
        }

-       public Element getXMLFileList(final Document xmlDoc) {
-               final Element files = xmlDoc.createElement("files");

+
+       public String findTheLatestKey(String linkKey) {
                synchronized(db.dbLock) {
                        try {
                                PreparedStatement st;

-                               st = 
db.getConnection().prepareStatement("SELECT id, filename, publicKey, size, mime 
"+
-                                                                        "FROM 
files "+
-                                                                        "WHERE 
indexParent = ?");
+                               st = 
db.getConnection().prepareStatement("SELECT publicKey, revision "+
+                                                                        "FROM 
indexes "+
+                                                                        "WHERE 
publicKey LIKE ?");

-                               st.setInt(1, id);
+                               st.setString(1, 
FreenetURIHelper.getComparablePart(linkKey));

                                ResultSet set = st.executeQuery();

-                               while(set.next()) {
-                                       String pubKey = 
set.getString("publicKey");
+                               while (set.next()) {
+                                       /* we will assume that we have *always* 
the latest version of the index */

-                                       if (pubKey == null)
-                                               continue;
-
-                                       pubKey = pubKey.trim();
-
-                                       if (!FreenetURIHelper.isAKey(pubKey)) {
-                                               Logger.notice(this, "One of the 
file key wasn't generated => not added");
-                                               continue;
+                                       String oKey = 
set.getString("publicKey");
+                                       if (FreenetURIHelper.compareKeys(oKey, 
linkKey)) {
+                                               String key = 
FreenetURIHelper.changeUSKRevision(oKey,
+                                                                               
                set.getInt("revision"),
+                                                                               
                0);
+                                               return key;
                                        }
-
-                                       final Element xmlFile = 
xmlDoc.createElement("file");
-
-                                       //xmlFile.setAttribute("id", 
set.getString("id"));
-                                       xmlFile.setAttribute("key", pubKey);
-                                       xmlFile.setAttribute("size", 
set.getString("size"));
-                                       if (set.getString("mime") == null)
-                                               xmlFile.setAttribute("mime", 
DefaultMIMETypes.guessMIMEType(set.getString("filename")));
-                                       else
-                                               xmlFile.setAttribute("mime", 
set.getString("mime"));
-
-                                       if(xmlFile != null)
-                                               files.appendChild(xmlFile);
-                                       else
-                                               Logger.warning(this, "Public 
key wasn't generated ! Not added to the index !");
                                }
-
                        } catch(SQLException e) {
-                               Logger.error(this, "Error while getting file 
list for XML : "+e.toString());
+                               Logger.error(this, "Can't find the latest key 
of a link because : "+e.toString());
                        }
                }

-               return files;
+               return linkKey;
        }



-       public Element getXMLCommentInfos(final Document xmlDoc) {
-               final Element infos = xmlDoc.createElement("comments");

-               infos.setAttribute("publicKey", getCommentPublicKey());
-               infos.setAttribute("privateKey", getCommentPrivateKey());
-
-               try {
-                       synchronized(db.dbLock) {
-                               PreparedStatement st;
-
-                               st = 
db.getConnection().prepareStatement("SELECT rev FROM indexCommentBlackList 
WHERE indexId = ?");
-                               st.setInt(1, id);
-
-                               ResultSet set = st.executeQuery();
-
-                               while (set.next()) {
-                                       Element bl = 
xmlDoc.createElement("blackListed");
-                                       bl.setAttribute("rev", 
set.getString("rev"));
-
-                                       infos.appendChild(bl);
-                               }
-                       }
-               } catch(SQLException e) {
-                       Logger.error(this, "Unable to get comment black list  
because: "+e.toString());
-               }
-
-               return infos;
-       }
-
-
-
-
-       /*********** INDEX LOADING **************/
-
-
-       public void loadXML(final String filePath) {
-               loadXML(filePath, true);
-       }
-
-
-       /**
-        * @param clean if set to false, will do a merge
-        */
-       public void loadXML(final String filePath, boolean clean) {
-               try {
-                       loadXML(new FileInputStream(filePath), clean);
-               } catch(final java.io.FileNotFoundException e) {
-                       Logger.error(this, "Unable to load XML: 
FileNotFoundException ('"+filePath+"') ! : "+e.toString());
-               }
-       }
-
-
-       public class IndexHandler extends DefaultHandler {
-               private Locator locator = null;
-
-               public IndexHandler() {
-
-               }
-
-               /**
-                * @see 
org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
-                */
-               public void setDocumentLocator(Locator value) {
-                       locator =  value;
-               }
-
-               /**
-                * Called when parsing is started
-                * @see org.xml.sax.ContentHandler#startDocument()
-                */
-               public void startDocument() throws SAXException {
-                       try {
-                               PreparedStatement st;
-
-                               st = 
db.getConnection().prepareStatement("DELETE FROM files WHERE indexParent = ? 
AND dontDelete = FALSE");
-                               st.setInt(1, id);
-                               st.execute();
-
-                               st = 
db.getConnection().prepareStatement("DELETE FROM links WHERE indexParent = ? 
AND dontDelete = FALSE");
-                               st.setInt(1, id);
-                               st.execute();
-
-                               st = 
db.getConnection().prepareStatement("DELETE FROM indexCommentBlackList WHERE 
indexId = ?");
-                               st.setInt(1, id);
-                               st.execute();
-
-                       } catch(SQLException e) {
-                               Logger.error(this, "Hm, failure while starting 
to parse the index: "+e.toString());
-                               throw new SAXException("SQLException ; have a 
nice day.");
-                       }
-               }
-
-               /**
-                * Called when starting to parse in a specific name space
-                * @param prefix name space prefix
-                * @param URI name space URI
-                * @see 
org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, 
java.lang.String)
-                */
-               public void startPrefixMapping(String prefix, String URI) 
throws SAXException {
-                       /* \_o< */
-               }
-
-               /**
-                * @param prefix name space prefix
-                * @see 
org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
-                */
-               public void endPrefixMapping(String prefix) throws SAXException 
{
-                       /* \_o< */
-               }
-
-
-
-               private boolean ownerTag = false;
-               private boolean privateKeyTag = false;
-               private boolean dateTag = false;
-               private boolean commentsTag = false;
-
-               private boolean hasCommentTag = false;
-
-               private String dateStr = null;
-
-               private PreparedStatement insertFileSt = null;
-               private PreparedStatement insertLinkSt = null;
-
-               /**
-                * Called when the parsed find an opening tag
-                * @param localName local tag name
-                * @param rawName rawName (the one used here)
-                * @see 
org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, 
java.lang.String, org.xml.sax.Attributes)
-                */
-               public void startElement(String nameSpaceURI, String localName,
-                                        String rawName, Attributes attrs) 
throws SAXException {
-                       if (rawName == null) {
-                               rawName = localName;
-                       }
-
-                       if (rawName == null)
-                               return;
-
-                       ownerTag = false;
-                       privateKeyTag = false;
-
-                       /* TODO : <title></title> */
-
-                       if ("owner".equals(rawName)) {
-                               ownerTag = true;
-                               return;
-                       }
-
-                       if ("privateKey".equals(rawName)) {
-                               privateKeyTag = true;
-                               return;
-                       }
-
-                       if ("date".equals(rawName)) {
-                               dateTag = true;
-                               return;
-                       }
-
-                       if ("index".equals(rawName)) { /* links */
-
-                               int nextId;
-
-                               try {
-                                       String key = attrs.getValue("key");
-
-                                       if (key == null) /* it was the 
beginning of the index */
-                                               return;
-
-                                       key = key.trim();
-
-
-                                       boolean blackListed = 
(BlackList.isBlackListed(db, key) >= 0);
-
-
-                                       if (insertLinkSt == null)
-                                               insertLinkSt = 
db.getConnection().prepareStatement("INSERT INTO links "
-                                                                               
                   + "(publicKey, mark, comment, indexParent, indexTarget, 
blackListed) "
-                                                                               
                   + "VALUES (?, 0, ?, ?, NULL, ?)");
-
-                                       insertLinkSt.setString(1, key);
-                                       insertLinkSt.setString(2, "No 
comment"); /* comment not used at the moment */
-                                       insertLinkSt.setInt(3, id);
-                                       insertLinkSt.setBoolean(4, blackListed);
-
-                                       insertLinkSt.execute();
-                               } catch(SQLException e) {
-                                       Logger.error(this, "Error while adding 
link to index '"+toString()+"' : "+e.toString());
-                               }
-
-                               return;
-                       }
-
-                       if ("file".equals(rawName)) {
-                               int nextId;
-
-                               try {
-                                       if (insertFileSt == null)
-                                               insertFileSt =
-                                                       
db.getConnection().prepareStatement("INSERT INTO files "
-                                                                               
            + "(filename, publicKey, localPath, mime, size, category, 
indexParent) "
-                                                                               
            + "VALUES (?, ?, NULL, ?, ?, NULL, ?)");
-
-                                       String key = attrs.getValue("key");
-                                       String filename = 
FreenetURIHelper.getFilenameFromKey(key);
-                                       if (filename == null)
-                                               filename = key;
-                                       String mime = attrs.getValue("mime");
-                                       long size = 
Long.parseLong(attrs.getValue("size"));
-
-                                       insertFileSt.setString(1, filename);
-                                       insertFileSt.setString(2, key);
-                                       insertFileSt.setString(3, mime);
-                                       insertFileSt.setLong(4, size);
-                                       insertFileSt.setInt(5, id);
-
-                                       synchronized(db.dbLock) {
-                                               insertFileSt.execute();
-                                       }
-                               } catch(SQLException e) {
-                                       Logger.error(this, "Error while adding 
file to index '"+toString()+"' : "+e.toString());
-                               }
-
-                               return;
-
-                       }
-
-
-                       if ("comments".equals(rawName)) {
-                               String pub = attrs.getValue("publicKey");
-                               String priv = attrs.getValue("privateKey");
-
-                               if (pub != null && priv != null) {
-                                       hasCommentTag = true;
-                                       commentsTag = true;
-                                       Logger.debug(this, "Comment allowed in 
this index");
-                                       setCommentKeys(pub, priv);
-                               }
-                       }
-
-
-                       if ("blackListed".equals(rawName)) {
-                               int blRev;
-
-                               try {
-                                       blRev = 
Integer.parseInt(attrs.getValue("rev"));
-                               } catch(Exception e) {
-                                       /* quick and dirty */
-                                       return;
-                               }
-
-                               Logger.notice(this, "BlackListing rev 
'"+Integer.toString(rev)+"'");
-
-                               try {
-                                       synchronized(db.dbLock) {
-                                               PreparedStatement st;
-
-                                               st = 
db.getConnection().prepareStatement("INSERT into indexCommentBlackList (rev, 
indexId) VALUES (?, ?)");
-                                               st.setInt(1, blRev);
-                                               st.setInt(2, id);
-
-                                               st.execute();
-
-                                       }
-                               } catch(SQLException e) {
-                                       Logger.error(this, "Error while adding 
element to the blackList: "+e.toString());
-                               }
-                       }
-
-
-
-                       /* ignore unknown tags */
-
-                       /* et paf ! Ca fait des Chocapics(tm)(r)(c)(m)(dtc) ! */
-               }
-
-               /**
-                * Called when a closing tag is met
-                * @see org.xml.sax.ContentHandler#endElement(java.lang.String, 
java.lang.String, java.lang.String)
-                */
-               public void endElement(String nameSpaceURI, String localName,
-                                      String rawName) throws SAXException {
-                       if (rawName == null) {
-                               rawName = localName;
-                       }
-
-                       if (rawName == null)
-                               return;
-
-                       if ("owner".equals(rawName)) {
-                               ownerTag = false;
-                               return;
-                       }
-
-                       if ("privateKey".equals(rawName)) {
-                               privateKeyTag = false;
-                               return;
-                       }
-
-                       if ("date".equals(rawName)) {
-                               dateTag = false;
-                               return;
-                       }
-
-                       if ("header".equals(rawName)) {
-                               try {
-                                       synchronized(db.dbLock) {
-                                               java.sql.Date dateSql = null;
-
-                                               if (dateStr != null) {
-                                                       
java.text.SimpleDateFormat sdf =
-                                                               new 
java.text.SimpleDateFormat(DATE_FORMAT);
-                                                       java.util.Date dateUtil 
= sdf.parse(dateStr, new java.text.ParsePosition(0));
-                                                       dateSql = new 
java.sql.Date(dateUtil.getTime());
-                                               }
-
-                                               PreparedStatement st =
-                                                       
db.getConnection().prepareStatement("UPDATE indexes "+
-                                                                               
            "SET insertionDate = ? "+
-                                                                               
            "WHERE id = ?");
-                                               st.setDate(1, dateSql);
-                                               st.setInt(2, id);
-
-                                               st.execute();
-                                       }
-                               } catch(SQLException e) {
-                                       Logger.error(this, "Error while 
updating index insertion date: "+e.toString());
-                               }
-                       }
-
-                       if ("comments".equals(rawName)) {
-                               commentsTag = false;
-                               return;
-                       }
-               }
-
-
-               /**
-                * Called when a text between two tag is met
-                * @param ch text
-                * @param start position
-                * @param end position
-                * @see org.xml.sax.ContentHandler#characters(char[], int, int)
-                */
-               public void characters(char[] ch, int start, int end) throws 
SAXException {
-                       String txt = new String(ch, start, end);
-
-                       if (ownerTag) {
-                               /* \_o< ==> TODO */
-
-                               return;
-                       }
-
-                       if (dateTag) {
-                               dateStr = txt;
-                       }
-
-                       if (privateKeyTag) {
-                               if (privateKey == null || 
privateKey.trim().equals(txt.trim())) {
-                                       /**
-                                        * the private key was published, we 
will have to do the same later
-                                        */
-                                       setPublishPrivateKey(true);
-                               } else {
-                                       /**
-                                        * the provided key doesn't match with 
the one we have,
-                                        * we won't publish it anymore
-                                        */
-                                       Logger.warning(this, "A private key was 
provided, but didn't match with the one we have ; ignored.");
-
-                               }
-
-                               if (privateKey == null) {
-                                       String newPrivate = txt.trim();
-
-                                       /* check that nobody is trying to 
inject some FCP commands
-                                        * through the private key
-                                        */
-                                       if (!newPrivate.startsWith("SSK@")
-                                           || newPrivate.indexOf('\n') >= 0) {
-                                               Logger.warning(this, "Invalid 
private key");
-                                               return;
-                                       }
-
-                                       setPrivateKey(newPrivate);
-                               }
-                               return;
-                       }
-
-
-
-
-                       /* ignore unkwown stuffs */
-
-               }
-
-               public void ignorableWhitespace(char[] ch, int start, int end) 
throws SAXException {
-
-               }
-
-               public void processingInstruction(String target, String data) 
throws SAXException {
-
-               }
-
-               /**
-                * @see 
org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
-                */
-               public void skippedEntity(String arg0) throws SAXException {
-
-               }
-
-
-               /**
-                * Called when parsing is finished
-                * @see org.xml.sax.ContentHandler#endDocument()
-                */
-               public void endDocument() throws SAXException {
-                       if (!hasCommentTag) {
-                               Logger.debug(this, "No comment allowed in this 
index");
-                               purgeCommentKeys();
-                       }
-               }
-       }
-
-
-       /**
-        * see import functionnality
-        */
-       public IndexHandler getIndexHandler() {
-               return new IndexHandler();
-       }
-
-
-       public synchronized void loadXML(final java.io.InputStream input, 
boolean clean) {
-               IndexHandler handler = new IndexHandler();
-
-               synchronized(db.dbLock) {
-                       try {
-                               // Use the default (non-validating) parser
-                               SAXParserFactory factory = 
SAXParserFactory.newInstance();
-
-                               // Parse the input
-                               SAXParser saxParser = factory.newSAXParser();
-                               Logger.info(this, "Parsing index ...");
-                               saxParser.parse(input, handler );
-                               Logger.info(this, "Parsing done");
-                       } catch(javax.xml.parsers.ParserConfigurationException 
e) {
-                               Logger.error(this, "Error (1) while parsing 
index: "+e.toString());
-                       } catch(org.xml.sax.SAXException e) {
-                               Logger.error(this, "Error (2) while parsing 
index: "+e.toString());
-                       } catch(java.io.IOException e) {
-                               Logger.error(this, "Error (3) while parsing 
index: "+e.toString());
-                       }
-               }
-       }
-
-
-
-       public static String getNameFromKey(final String key) {
-               String name = null;
-
-               name = FreenetURIHelper.getFilenameFromKey(key);
-
-               if (name == null)
-                       return null;
-
-               /* quick and dirty */
-               name = name.replaceAll(".xml", "");
-               name = name.replaceAll(".frdx", "");
-
-               return name;
-       }
-
-
        public boolean isModifiable() {
                if (getPrivateKey() != null)
                        return true;
@@ -1931,7 +1407,7 @@
                        e.setAttribute("privateKey", getPrivateKey());

                if (withContent) {
-                       fillInRootElement(e, xmlDoc);
+                       new IndexParser(this).fillInRootElement(e, xmlDoc);
                }

                return e;
@@ -2302,4 +1778,9 @@

                return null;
        }
+
+
+       public String getClientVersion() {
+               return ("Thaw "+Main.VERSION);
+       }
 }

Added: trunk/apps/Thaw/src/thaw/plugins/index/IndexContainer.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexContainer.java                  
        (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexContainer.java  2007-08-03 
17:40:46 UTC (rev 14475)
@@ -0,0 +1,73 @@
+package thaw.plugins.index;
+
+import java.util.Vector;
+
+
+/**
+ * This is what you need to implements to use IndexParser.
+ */
+public interface IndexContainer extends FileAndLinkList {
+
+       /**
+        * @return a vector of FileContainer
+        */
+       public Vector getFileList();
+
+       /**
+        * @return a vector of LinkContainer
+        */
+       public Vector getLinkList();
+
+
+       /**
+        * @return true if the private key must be published
+        */
+       public boolean publishPrivateKey();
+
+       /**
+        * @return the private key of the index (Expected: "SSK@[...]/")
+        */
+       public String getPrivateKey();
+
+
+       /**
+        * Used to update the links. The simplest way to find the correct
+        * is to look at your index list.
+        */
+       public String findTheLatestKey(String originalKey);
+
+
+       /**
+        * @param toGUI if set to false, must *just* return the index name
+        */
+       public String toString(boolean toGUI);
+
+       /**
+        * @return for example "Thaw 0.8 r4242"
+        */
+       public String getClientVersion();
+
+       public boolean canHaveComments();
+       public String getCommentPublicKey();
+       public String getCommentPrivateKey();
+
+       /**
+        * @return Vector of Integer
+        */
+       public Vector getCommentBlacklistedRev();
+
+       /**
+        * delete all the files / links / comment blacklisted revs / comments 
keys
+        */
+       public void purgeIndex();
+
+       public void setInsertionDate(java.util.Date date);
+
+       public void addFile(String publicKey, long size, String mime);
+       public void addLink(String publicKey);
+       public void setCommentKeys(String publicKey, String privateKey);
+       public void addBlackListedRev(int rev);
+
+       public void setPublishPrivateKey(boolean publish);
+       public void setPrivateKey(String key);
+}

Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java   
2007-08-03 03:39:36 UTC (rev 14474)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java   
2007-08-03 17:40:46 UTC (rev 14475)
@@ -536,7 +536,7 @@

                        final KeyAsker asker = KeyAsker.askKeys(index, 
getQueueManager(),
                                                                true, 
index.getPublicKey(),
-                                                               
index.getPrivateKey(), index.getPublishPrivateKey(),
+                                                               
index.getPrivateKey(), index.publishPrivateKey(),
                                                                
index.canHaveComments(), true, getIndexBrowserPanel());

                        if (asker == null) {
@@ -1020,7 +1020,7 @@
                                return;
                        }

-                       ((Index)getTarget()).generateXML(out);
+                       new IndexParser(((Index)getTarget())).generateXML(out);
                }
        }

@@ -1049,7 +1049,7 @@
                        if (newFile == null)
                                return;

-                       ((Index)getTarget()).loadXML(newFile.getPath());
+                       new 
IndexParser(((Index)getTarget())).loadXML(newFile.getPath());
                }
        }


Added: trunk/apps/Thaw/src/thaw/plugins/index/IndexParser.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexParser.java                     
        (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexParser.java     2007-08-03 
17:40:46 UTC (rev 14475)
@@ -0,0 +1,579 @@
+package thaw.plugins.index;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.Vector;
+import java.util.Iterator;
+
+/* DOM */
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+
+
+/* SAX */
+
+import org.xml.sax.*;
+import org.xml.sax.helpers.LocatorImpl;
+
+import java.io.IOException;
+
+import org.xml.sax.helpers.XMLReaderFactory;
+import org.xml.sax.helpers.DefaultHandler;
+
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+
+
+
+import thaw.core.Main;
+import thaw.core.Logger;
+import thaw.plugins.Hsqldb;
+import thaw.fcp.FreenetURIHelper;
+import thaw.plugins.insertPlugin.DefaultMIMETypes;
+
+
+
+public class IndexParser {
+       public final static String DATE_FORMAT = "yyyyMMdd";
+
+       private IndexContainer index;
+
+       public IndexParser(IndexContainer index) {
+               this.index = index;
+       }
+
+       public boolean generateXML(String path) {
+               try {
+                       generateXML(new FileOutputStream(new File(path)));
+                       return true;
+               } catch(java.io.FileNotFoundException e) {
+                       Logger.error(this, "File not found exception ?!");
+               }
+               return false;
+       }
+
+       public void generateXML(final OutputStream out) {
+               StreamResult streamResult;
+
+               streamResult = new StreamResult(out);
+
+               Document xmlDoc;
+
+               final DocumentBuilderFactory xmlFactory = 
DocumentBuilderFactory.newInstance();
+               DocumentBuilder xmlBuilder;
+
+               try {
+                       xmlBuilder = xmlFactory.newDocumentBuilder();
+               } catch(final javax.xml.parsers.ParserConfigurationException e) 
{
+                       Logger.error(this, "Unable to generate the index 
because : "+e.toString());
+                       return;
+               }
+
+               final DOMImplementation impl = 
xmlBuilder.getDOMImplementation();
+
+               xmlDoc = impl.createDocument(null, "index", null);
+
+               final Element rootEl = xmlDoc.getDocumentElement();
+
+               /**** DOM Tree generation ****/
+               fillInRootElement(rootEl, xmlDoc);
+
+
+               /* Serialization */
+               final DOMSource domSource = new DOMSource(xmlDoc);
+               final TransformerFactory transformFactory = 
TransformerFactory.newInstance();
+
+               Transformer serializer;
+
+               try {
+                       serializer = transformFactory.newTransformer();
+               } catch(final 
javax.xml.transform.TransformerConfigurationException e) {
+                       Logger.error(this, "Unable to save index because: 
"+e.toString());
+                       return;
+               }
+
+               serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
+               serializer.setOutputProperty(OutputKeys.INDENT,"yes");
+
+               /* final step */
+               try {
+                       serializer.transform(domSource, streamResult);
+               } catch(final javax.xml.transform.TransformerException e) {
+                       Logger.error(this, "Unable to save index because: 
"+e.toString());
+                       return;
+               }
+       }
+
+
+
+       public boolean fillInRootElement(Element rootEl, Document xmlDoc) {
+               rootEl.appendChild(getXMLHeader(xmlDoc));
+               rootEl.appendChild(getXMLLinks(xmlDoc));
+               rootEl.appendChild(getXMLFileList(xmlDoc));
+
+               if (index.canHaveComments())
+                       rootEl.appendChild(getXMLCommentInfos(xmlDoc));
+
+               return true;
+       }
+
+
+       public Element getXMLHeader(final Document xmlDoc) {
+               final Element header = xmlDoc.createElement("header");
+
+               final Element title = xmlDoc.createElement("title");
+               final Text titleText = 
xmlDoc.createTextNode(index.toString(false));
+               title.appendChild(titleText);
+
+               header.appendChild(title);
+
+
+               final Element clientVersion = xmlDoc.createElement("client");
+               final Text versionText = 
xmlDoc.createTextNode(index.getClientVersion());
+               clientVersion.appendChild(versionText);
+
+               header.appendChild(clientVersion);
+
+               if (index.publishPrivateKey() && index.getPrivateKey() != null) 
{
+                       final Element privateKeyEl = 
xmlDoc.createElement("privateKey");
+                       final Text privateKeyText = 
xmlDoc.createTextNode(index.getPrivateKey());
+                       privateKeyEl.appendChild(privateKeyText);
+
+                       header.appendChild(privateKeyEl);
+               }
+
+               /* insertion date */
+               String dateStr;
+
+               java.text.SimpleDateFormat sdf =
+                       new java.text.SimpleDateFormat(DATE_FORMAT);
+               sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
+               dateStr = sdf.format(new java.util.Date());
+
+               final Element date = xmlDoc.createElement("date");
+               final Text dateText = xmlDoc.createTextNode(dateStr);
+               date.appendChild(dateText);
+
+               header.appendChild(date);
+
+               /* TODO : Author */
+
+               return header;
+       }
+
+
+       public Element getXMLLinks(final Document xmlDoc) {
+               final Element linksEl = xmlDoc.createElement("indexes");
+
+               Vector links = index.getLinkList();
+
+               for(Iterator it = links.iterator(); it.hasNext() ; ) {
+                       LinkContainer link = (LinkContainer)it.next();
+
+                       String key = 
index.findTheLatestKey(link.getPublicKey());
+
+                       final Element xmlLink = xmlDoc.createElement("link");
+
+                       xmlLink.setAttribute("key", key);
+
+                       linksEl.appendChild(xmlLink);
+               }
+
+               return linksEl;
+       }
+
+
+       public Element getXMLFileList(final Document xmlDoc) {
+               final Element filesEl = xmlDoc.createElement("files");
+
+               Vector files = index.getFileList();
+
+               for(Iterator it = files.iterator(); it.hasNext();) {
+
+                       FileContainer file = (FileContainer)it.next();
+
+                       String pubKey = file.getPublicKey();
+
+                       if (pubKey == null)
+                               continue;
+
+                       pubKey = pubKey.trim();
+
+                       if (!FreenetURIHelper.isAKey(pubKey)) {
+                               Logger.notice(this, "One of the file key wasn't 
generated => not added");
+                               continue;
+                       }
+
+                       final Element xmlFile = xmlDoc.createElement("file");
+
+                       //xmlFile.setAttribute("id", set.getString("id"));
+                       xmlFile.setAttribute("key", pubKey);
+                       xmlFile.setAttribute("size", 
Long.toString(file.getSize()));
+                       if (file.getMime() == null)
+                               xmlFile.setAttribute("mime", 
DefaultMIMETypes.guessMIMEType(file.getFilename()));
+                       else
+                               xmlFile.setAttribute("mime", file.getMime());
+
+
+                       filesEl.appendChild(xmlFile);
+               }
+
+               return filesEl;
+       }
+
+
+       public Element getXMLCommentInfos(final Document xmlDoc) {
+               final Element infos = xmlDoc.createElement("comments");
+
+               infos.setAttribute("publicKey", index.getCommentPublicKey());
+
+               if (index.getCommentPrivateKey() != null)
+                       infos.setAttribute("privateKey", 
index.getCommentPrivateKey());
+
+               for (Iterator it = index.getCommentBlacklistedRev().iterator();
+                    it.hasNext() ;) {
+
+                       Integer rev = (Integer)it.next();
+                       Element bl = xmlDoc.createElement("blackListed");
+                       bl.setAttribute("rev", rev.toString());
+                       infos.appendChild(bl);
+
+               }
+
+               return infos;
+       }
+
+
+       /*********** INDEX LOADING **************/
+
+
+       public void loadXML(final String filePath) {
+               loadXML(filePath, true);
+       }
+
+
+       /**
+        * @param clean if set to false, will do a merge
+        */
+       public void loadXML(final String filePath, boolean clean) {
+               try {
+                       loadXML(new FileInputStream(filePath), clean);
+               } catch(final java.io.FileNotFoundException e) {
+                       Logger.error(this, "Unable to load XML: 
FileNotFoundException ('"+filePath+"') ! : "+e.toString());
+               }
+       }
+
+
+       public class IndexHandler extends DefaultHandler {
+               private Locator locator = null;
+
+               public IndexHandler() {
+
+               }
+
+               /**
+                * @see 
org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
+                */
+               public void setDocumentLocator(Locator value) {
+                       locator =  value;
+               }
+
+               /**
+                * Called when parsing is started
+                * @see org.xml.sax.ContentHandler#startDocument()
+                */
+               public void startDocument() throws SAXException {
+
+               }
+
+               /**
+                * Called when starting to parse in a specific name space
+                * @param prefix name space prefix
+                * @param URI name space URI
+                * @see 
org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, 
java.lang.String)
+                */
+               public void startPrefixMapping(String prefix, String URI) 
throws SAXException {
+                       /* \_o< */
+               }
+
+               /**
+                * @param prefix name space prefix
+                * @see 
org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
+                */
+               public void endPrefixMapping(String prefix) throws SAXException 
{
+                       /* \_o< */
+               }
+
+
+
+               private boolean ownerTag = false;
+               private boolean privateKeyTag = false;
+               private boolean dateTag = false;
+               private boolean commentsTag = false;
+
+               private boolean hasCommentTag = false;
+
+               private String dateStr = null;
+
+
+               /**
+                * Called when the parsed find an opening tag
+                * @param localName local tag name
+                * @param rawName rawName (the one used here)
+                * @see 
org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, 
java.lang.String, org.xml.sax.Attributes)
+                */
+               public void startElement(String nameSpaceURI, String localName,
+                                        String rawName, Attributes attrs) 
throws SAXException {
+                       if (rawName == null) {
+                               rawName = localName;
+                       }
+
+                       if (rawName == null)
+                               return;
+
+                       ownerTag = false;
+                       privateKeyTag = false;
+
+                       /* TODO : <title></title> */
+
+                       if ("owner".equals(rawName)) {
+                               ownerTag = true;
+                               return;
+                       }
+
+                       if ("privateKey".equals(rawName)) {
+                               privateKeyTag = true;
+                               return;
+                       }
+
+                       if ("date".equals(rawName)) {
+                               dateTag = true;
+                               return;
+                       }
+
+                       if ("link".equals(rawName)
+                           || "index".equals(rawName)) { /* links */
+
+                               index.addLink(attrs.getValue("key"));
+
+                               return;
+                       }
+
+                       if ("file".equals(rawName)) {
+
+                               index.addFile(attrs.getValue("key"),
+                                             
Long.parseLong(attrs.getValue("size")),
+                                             attrs.getValue("mime"));
+
+                       }
+
+
+                       if ("comments".equals(rawName)) {
+                               String pub = attrs.getValue("publicKey");
+                               String priv = attrs.getValue("privateKey");
+
+                               if (pub != null) {
+                                       hasCommentTag = true;
+                                       commentsTag = true;
+                                       Logger.debug(this, "Comment allowed in 
this index");
+                                       index.setCommentKeys(pub, priv);
+                               }
+                       }
+
+
+                       if ("blackListed".equals(rawName)) {
+                               int blRev;
+
+                               blRev = Integer.parseInt(attrs.getValue("rev"));
+
+                               Logger.notice(this, "BlackListing rev 
'"+Integer.toString(blRev)+"'");
+
+                               index.addBlackListedRev(blRev);
+                       }
+
+
+
+                       /* ignore unknown tags */
+
+                       /* et paf ! Ca fait des Chocapics(tm)(r)(c)(m)(dtc) ! */
+               }
+
+               /**
+                * Called when a closing tag is met
+                * @see org.xml.sax.ContentHandler#endElement(java.lang.String, 
java.lang.String, java.lang.String)
+                */
+               public void endElement(String nameSpaceURI, String localName,
+                                      String rawName) throws SAXException {
+                       if (rawName == null) {
+                               rawName = localName;
+                       }
+
+                       if (rawName == null)
+                               return;
+
+                       if ("owner".equals(rawName)) {
+                               ownerTag = false;
+                               return;
+                       }
+
+                       if ("privateKey".equals(rawName)) {
+                               privateKeyTag = false;
+                               return;
+                       }
+
+                       if ("date".equals(rawName)) {
+                               dateTag = false;
+                               return;
+                       }
+
+                       if ("header".equals(rawName)) {
+                               if (dateStr != null) {
+                                       java.text.SimpleDateFormat sdf =
+                                               new 
java.text.SimpleDateFormat(DATE_FORMAT);
+                                       java.util.Date dateUtil = 
sdf.parse(dateStr, new java.text.ParsePosition(0));
+
+                                       index.setInsertionDate(dateUtil);
+                               }
+                       }
+
+                       if ("comments".equals(rawName)) {
+                               commentsTag = false;
+                               return;
+                       }
+               }
+
+
+               /**
+                * Called when a text between two tag is met
+                * @param ch text
+                * @param start position
+                * @param end position
+                * @see org.xml.sax.ContentHandler#characters(char[], int, int)
+                */
+               public void characters(char[] ch, int start, int end) throws 
SAXException {
+                       String txt = new String(ch, start, end);
+
+                       if (ownerTag) {
+                               /* \_o< ==> TODO */
+
+                               return;
+                       }
+
+                       if (dateTag) {
+                               dateStr = txt;
+                       }
+
+                       if (privateKeyTag) {
+                               if (index.getPrivateKey() == null
+                                   || 
index.getPrivateKey().trim().equals(txt.trim())) {
+                                       /**
+                                        * the private key was published, we 
will have to do the same later
+                                        */
+                                       index.setPublishPrivateKey(true);
+                               } else {
+                                       /**
+                                        * the provided key doesn't match with 
the one we have,
+                                        * we won't publish it anymore
+                                        */
+                                       Logger.warning(this, "A private key was 
provided, but didn't match with the one we have ; ignored.");
+                               }
+
+                               if (index.getPrivateKey() == null) {
+                                       String newPrivate = txt.trim();
+
+                                       /* check that nobody is trying to 
inject some FCP commands
+                                        * through the private key
+                                        */
+                                       if (!FreenetURIHelper.isAKey(newPrivate)
+                                           || newPrivate.indexOf('\n') >= 0) {
+                                               Logger.warning(this, "Invalid 
private key");
+                                               return;
+                                       }
+
+                                       index.setPrivateKey(newPrivate);
+                               }
+                               return;
+                       }
+
+
+
+
+                       /* ignore unkwown stuffs */
+
+               }
+
+               public void ignorableWhitespace(char[] ch, int start, int end) 
throws SAXException {
+
+               }
+
+               public void processingInstruction(String target, String data) 
throws SAXException {
+
+               }
+
+               /**
+                * @see 
org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
+                */
+               public void skippedEntity(String arg0) throws SAXException {
+
+               }
+
+
+               /**
+                * Called when parsing is finished
+                * @see org.xml.sax.ContentHandler#endDocument()
+                */
+               public void endDocument() throws SAXException {
+                       if (!hasCommentTag) {
+                               Logger.debug(this, "No comment allowed in this 
index");
+                       }
+               }
+       }
+
+
+       /**
+        * see import functionnality
+        */
+       public IndexHandler getIndexHandler() {
+               return new IndexHandler();
+       }
+
+
+       public synchronized void loadXML(final java.io.InputStream input, 
boolean clean) {
+               IndexHandler handler = new IndexHandler();
+
+               try {
+                       // Use the default (non-validating) parser
+                       SAXParserFactory factory = 
SAXParserFactory.newInstance();
+
+                       // Parse the input
+                       SAXParser saxParser = factory.newSAXParser();
+                       Logger.info(this, "Parsing index ...");
+                       saxParser.parse(input, handler );
+                       Logger.info(this, "Parsing done");
+               } catch(javax.xml.parsers.ParserConfigurationException e) {
+                       Logger.error(this, "Error (1) while parsing index: 
"+e.toString());
+               } catch(org.xml.sax.SAXException e) {
+                       Logger.error(this, "Error (2) while parsing index: 
"+e.toString());
+               } catch(java.io.IOException e) {
+                       Logger.error(this, "Error (3) while parsing index: 
"+e.toString());
+               }
+       }
+
+}
+

Modified: trunk/apps/Thaw/src/thaw/plugins/index/Link.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/Link.java    2007-08-03 03:39:36 UTC 
(rev 14474)
+++ trunk/apps/Thaw/src/thaw/plugins/index/Link.java    2007-08-03 17:40:46 UTC 
(rev 14475)
@@ -11,7 +11,7 @@
 import thaw.core.Logger;
 import thaw.plugins.Hsqldb;

-public class Link extends java.util.Observable implements Comparable {
+public class Link extends java.util.Observable implements Comparable, 
LinkContainer {
        private int id;
        private final Hsqldb db;


Added: trunk/apps/Thaw/src/thaw/plugins/index/LinkContainer.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/LinkContainer.java                   
        (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/index/LinkContainer.java   2007-08-03 
17:40:46 UTC (rev 14475)
@@ -0,0 +1,7 @@
+package thaw.plugins.index;
+
+public interface LinkContainer {
+
+       public String getPublicKey();
+
+}


Reply via email to