[sip-comm-cvs] CVS update: /sip-communicator/src/net/java/sip/communicator/sip/simple/storage/

emcho at dev.java.net emcho at dev.java.net
Wed May 26 11:07:59 CEST 2004


User: emcho   
Date: 04/05/26 02:07:59

Modified:
 /sip-communicator/src/net/java/sip/communicator/sip/simple/storage/
  ContactListSerializer.java

Log:
 Ongoing work on simple

File Changes:

Directory: /sip-communicator/src/net/java/sip/communicator/sip/simple/storage/
==============================================================================

File [changed]: ContactListSerializer.java
Url: https://sip-communicator.dev.java.net/source/browse/sip-communicator/src/net/java/sip/communicator/sip/simple/storage/ContactListSerializer.java?r1=1.3&r2=1.4
Delta lines:  +121 -7
---------------------
--- ContactListSerializer.java	16 Apr 2004 08:52:26 -0000	1.3
+++ ContactListSerializer.java	26 May 2004 09:07:56 -0000	1.4
@@ -63,6 +63,10 @@
 import javax.xml.parsers.*;
 import net.java.sip.communicator.common.*;
 import net.java.sip.communicator.gui.config.xml.*;
+import net.java.sip.communicator.sip.*;
+import javax.xml.transform.dom.*;
+import javax.xml.transform.stream.*;
+import javax.xml.transform.*;
 
 /**
  * <p>Title: SIP Communicator</p>
@@ -77,9 +81,14 @@
 {
     private static final Console console = Console.getConsole(ContactListSerializer.class);
 
-    public static final String ALIAS      = "alias";
+    private static DocumentBuilderFactory factory = null;
+    private static DocumentBuilder        builder = null;
+
+
+    public static final String CONTACT_LIST = "contact-list";
     public static final String GROUP      = "group";
     public static final String CONTACT    = "contact";
+    public static final String ALIAS        = "alias";
     public static final String NAME       = "name";
     public static final String PRESENTITY = "presentity";
     public static final String URI        = "uri";
@@ -89,6 +98,14 @@
     {
     }
 
+    /**
+     * Load a Contact List from the location (file) specified by
+     * <code>resourceName</code>.
+     * @param resourceName The name of the file containing the contact list.
+     * @throws IOException if reading the contact list file fails.
+     * @return the contact list contained in the file specified by
+     * <code>resourceName</code>
+     */
     public static ContactGroup loadList(String resourceName) throws IOException
     {
         try{
@@ -206,5 +223,102 @@
 
         return null;
     }
+
+    /**
+     * Stores <code>contactList</code> to the location specified by
+     * <code>resourceName</code>.
+     * @param resourceName the name of the file where contactList is to be stored.
+     * @throws CommunciationsException if we fail while writing to the specified
+     * location.
+     */
+    public static void storeContactList(String       resourceName,
+                                        ContactGroup contactList)
+        throws CommunicationsException
+    {
+        DocumentBuilder builder = null;
+        try {
+            builder = getDocumentBuilder();
+        }
+        catch (ParserConfigurationException ex) {
+            throw new CommunicationsException("Failed to create pidf+xml document builder", ex);
+        }
+        Document document = builder.newDocument();
+
+
+        Element contactListRoot = document.createElement(CONTACT_LIST);
+        document.appendChild(contactListRoot);
+        appendContactGroupToXmlDocument(contactList, contactListRoot, document);
+
+        File file = new File(resourceName);
+        if (!file.exists())
+            try {
+                file.createNewFile();
+            }
+            catch (IOException ex) {
+                throw new CommunicationsException("Failed to create contact list file with name:" + resourceName, ex);
+            }
+
+        try {
+            DOMSource domSource = new DOMSource(document);
+            StreamResult streamResult = new StreamResult(file);
+            TransformerFactory tf = TransformerFactory.newInstance();
+            Transformer serializer = tf.newTransformer();
+            serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
+            serializer.transform(domSource, streamResult);
+        }
+        catch (Exception ex) {
+            throw new CommunicationsException("Failed to transfomr contact-list XMLdocument to a file!", ex);
+        }
+
+
+    }
+
+    private static void appendContactGroupToXmlDocument(ContactGroup    group,
+                                                        Element         root,
+                                                        Document        document)
+    {
+        for (int i = 0; i < group.getChildCount(); i++)
+        {
+            Contact currentChild = group.getChild(i);
+            if(currentChild instanceof ContactGroup)
+            {
+                //Create and init an element for the group
+                Element groupElement = document.createElement(GROUP);
+                groupElement.setAttribute(NAME, currentChild.getDisplayName());
+
+                //append the group to the xml tree
+                root.appendChild(groupElement);
+
+                //append group children to the xml tree
+                appendContactGroupToXmlDocument((ContactGroup)currentChild, groupElement, document);
+            }
+            else
+            {
+                //Creat and init the child element.
+                Element contactElement = document.createElement(CONTACT);
+                contactElement.setAttribute(ALIAS, currentChild.getDisplayName());
+                contactElement.setAttribute(PRESENTITY, currentChild.getPresenceUri().toString());
+
+                //Add the child to the xml tree
+                root.appendChild(contactElement);
+
+                /** @todo  save ContactURIs*/
+            }
+        }
+    }
+
+    private static DocumentBuilder getDocumentBuilder()
+        throws ParserConfigurationException
+    {
+        if (factory == null)
+            factory = DocumentBuilderFactory.newInstance();
+
+        if (builder == null)
+            builder = factory.newDocumentBuilder();
+
+        return builder;
+    }
+
 
 }




---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe at sip-communicator.dev.java.net
For additional commands, e-mail: cvs-help at sip-communicator.dev.java.net




More information about the commits mailing list