Author: xor
Date: 2008-11-11 21:43:23 +0000 (Tue, 11 Nov 2008)
New Revision: 23493

Modified:
   trunk/plugins/WoT/Identity.java
   trunk/plugins/WoT/OwnIdentity.java
Log:
Add functions to export an identity introduction to XML and import from XML.
I think that those should be in the Identity classes and not in the 
introduction package because introduction of a new identity is something very 
general and the introduction package is only the implementation of exchanging 
introductions.

Modified: trunk/plugins/WoT/Identity.java
===================================================================
--- trunk/plugins/WoT/Identity.java     2008-11-11 20:59:02 UTC (rev 23492)
+++ trunk/plugins/WoT/Identity.java     2008-11-11 21:43:23 UTC (rev 23493)
@@ -5,6 +5,8 @@
  */
 package plugins.WoT;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Date;
@@ -12,6 +14,13 @@
 import java.util.Iterator;
 import java.util.Map.Entry;
 
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
 import plugins.WoT.exceptions.DuplicateIdentityException;
 import plugins.WoT.exceptions.DuplicateScoreException;
 import plugins.WoT.exceptions.DuplicateTrustException;
@@ -207,7 +216,67 @@
                return uri.toString().substring(begin, end);
        }
 
+       /**
+        * Please commit after calling the function!
+        * @param db
+        * @param fetcher
+        * @param is
+        * @return
+        * @throws ParserConfigurationException
+        * @throws SAXException
+        * @throws IOException
+        * @throws InvalidParameterException
+        */
+       public static Identity importIntroductionFromXML(ObjectContainer db, 
IdentityFetcher fetcher, InputStream is) throws ParserConfigurationException, 
SAXException, IOException, InvalidParameterException {
+               IntroductionHandler introHandler = new IntroductionHandler();
+               SAXParserFactory.newInstance().newSAXParser().parse(is, 
introHandler);
+               
+               Identity id;
+               FreenetURI requestURI = introHandler.getRequestURI();
+               
+               try {
+                       id = Identity.getByURI(db, requestURI);
+               }
+               catch (UnknownIdentityException e) {
+                       id = new Identity(requestURI, null, false);
+                       db.store(id);
+                       fetcher.fetch(id);
+               }
+
+               return id;
+       }
        
+       public static class IntroductionHandler extends DefaultHandler {
+               private FreenetURI requestURI;
+
+               public IntroductionHandler() {
+                       super();
+               }
+
+               /**
+                * Called by SAXParser for each XML element.
+                */
+               public void startElement(String nameSpaceURI, String localName, 
String rawName, Attributes attrs) throws SAXException {
+                       String elt_name = rawName == null ? localName : rawName;
+
+                       try {
+                               if (elt_name.equals("Identity")) {
+                                       requestURI = new 
FreenetURI(attrs.getValue("value"));
+                               }                               
+                               else
+                                       Logger.error(this, "Unknown element in 
identity introduction: " + elt_name);
+                               
+                       } catch (Exception e1) {
+                               Logger.error(this, "Parsing error",e1);
+                       }
+               }
+
+               public FreenetURI getRequestURI() {
+                       return requestURI;
+               }
+       }
+       
+       
        /**
         * Gets the score of this identity in a trust tree.
         * Each [EMAIL PROTECTED] OwnIdentity} has its own trust tree.

Modified: trunk/plugins/WoT/OwnIdentity.java
===================================================================
--- trunk/plugins/WoT/OwnIdentity.java  2008-11-11 20:59:02 UTC (rev 23492)
+++ trunk/plugins/WoT/OwnIdentity.java  2008-11-11 21:43:23 UTC (rev 23493)
@@ -40,6 +40,7 @@
 import com.db4o.ext.DatabaseClosedException;
 import com.db4o.ext.Db4oIOException;
 import com.db4o.query.Query;
+import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
 
 import freenet.keys.FreenetURI;
 
@@ -245,6 +246,34 @@
                serializer.transform(domSource, resultStream);
        }
        
+       public void exportIntroductionToXML(OutputStream os) throws 
ParserConfigurationException, TransformerException {
+               StreamResult resultStream = new StreamResult(os);
+
+               // Create the XML document
+               DocumentBuilderFactory xmlFactory = 
DocumentBuilderFactory.newInstance();
+               DocumentBuilder xmlBuilder = xmlFactory.newDocumentBuilder();
+               DOMImplementation impl = xmlBuilder.getDOMImplementation();
+               Document xmlDoc = impl.createDocument(null, "WoT", null);
+               Element rootElement = xmlDoc.getDocumentElement();
+
+               // Create the content
+               Element introTag = xmlDoc.createElement("IdentityIntroduction");
+
+               Element identityTag = xmlDoc.createElement("Identity");
+               identityTag.setAttribute("value", getRequestURI().toString());
+               introTag.appendChild(identityTag);
+       
+               rootElement.appendChild(introTag);
+
+               DOMSource domSource = new DOMSource(xmlDoc);
+               TransformerFactory transformFactory = 
TransformerFactory.newInstance();
+               Transformer serializer = transformFactory.newTransformer();
+               
+               serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+               serializer.setOutputProperty(OutputKeys.INDENT,"yes");
+               serializer.transform(domSource, resultStream);
+       }
+       
        /**
         * Whether this OwnIdentity needs to be inserted or not.
         * We insert OwnIdentities when they have been modified AND at least 
once a week.

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

Reply via email to