Bookmarks.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.bookmarks;

  18. import org.jivesoftware.smack.util.XmlStringBuilder;
  19. import org.jivesoftware.smackx.iqprivate.packet.PrivateData;
  20. import org.jivesoftware.smackx.iqprivate.provider.PrivateDataProvider;
  21. import org.xmlpull.v1.XmlPullParser;
  22. import org.xmlpull.v1.XmlPullParserException;

  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.List;

  26. /**
  27.  * Bookmarks is used for storing and retrieving URLS and Conference rooms.
  28.  * Bookmark Storage (XEP-0048) defined a protocol for the storage of bookmarks to conference rooms and other entities
  29.  * in a Jabber user's account.
  30.  * See the following code sample for saving Bookmarks:
  31.  * <p/>
  32.  * <pre>
  33.  * XMPPConnection con = new XMPPTCPConnection("jabber.org");
  34.  * con.login("john", "doe");
  35.  * Bookmarks bookmarks = new Bookmarks();
  36.  * <p/>
  37.  * // Bookmark a URL
  38.  * BookmarkedURL url = new BookmarkedURL();
  39.  * url.setName("Google");
  40.  * url.setURL("http://www.jivesoftware.com");
  41.  * bookmarks.addURL(url);
  42.  * // Bookmark a Conference room.
  43.  * BookmarkedConference conference = new BookmarkedConference();
  44.  * conference.setName("My Favorite Room");
  45.  * conference.setAutoJoin("true");
  46.  * conference.setJID("dev@conference.jivesoftware.com");
  47.  * bookmarks.addConference(conference);
  48.  * // Save Bookmarks using PrivateDataManager.
  49.  * PrivateDataManager manager = new PrivateDataManager(con);
  50.  * manager.setPrivateData(bookmarks);
  51.  * <p/>
  52.  * <p/>
  53.  * LastActivity activity = LastActivity.getLastActivity(con, "xray@jabber.org");
  54.  * </pre>
  55.  *
  56.  * @author Derek DeMoro
  57.  */
  58. public class Bookmarks implements PrivateData {

  59.     public static final String NAMESPACE = "storage:bookmarks";
  60.     public static final String ELEMENT = "storage";

  61.     private List<BookmarkedURL> bookmarkedURLS;
  62.     private List<BookmarkedConference> bookmarkedConferences;

  63.     /**
  64.      * Required Empty Constructor to use Bookmarks.
  65.      */
  66.     public Bookmarks() {
  67.         bookmarkedURLS = new ArrayList<BookmarkedURL>();
  68.         bookmarkedConferences = new ArrayList<BookmarkedConference>();
  69.     }

  70.     /**
  71.      * Adds a BookmarkedURL.
  72.      *
  73.      * @param bookmarkedURL the bookmarked bookmarkedURL.
  74.      */
  75.     public void addBookmarkedURL(BookmarkedURL bookmarkedURL) {
  76.         bookmarkedURLS.add(bookmarkedURL);
  77.     }

  78.     /**
  79.      * Removes a bookmarked bookmarkedURL.
  80.      *
  81.      * @param bookmarkedURL the bookmarked bookmarkedURL to remove.
  82.      */
  83.     public void removeBookmarkedURL(BookmarkedURL bookmarkedURL) {
  84.         bookmarkedURLS.remove(bookmarkedURL);
  85.     }

  86.     /**
  87.      * Removes all BookmarkedURLs from user's bookmarks.
  88.      */
  89.     public void clearBookmarkedURLS() {
  90.         bookmarkedURLS.clear();
  91.     }

  92.     /**
  93.      * Add a BookmarkedConference to bookmarks.
  94.      *
  95.      * @param bookmarkedConference the conference to remove.
  96.      */
  97.     public void addBookmarkedConference(BookmarkedConference bookmarkedConference) {
  98.         bookmarkedConferences.add(bookmarkedConference);
  99.     }

  100.     /**
  101.      * Removes a BookmarkedConference.
  102.      *
  103.      * @param bookmarkedConference the BookmarkedConference to remove.
  104.      */
  105.     public void removeBookmarkedConference(BookmarkedConference bookmarkedConference) {
  106.         bookmarkedConferences.remove(bookmarkedConference);
  107.     }

  108.     /**
  109.      * Removes all BookmarkedConferences from Bookmarks.
  110.      */
  111.     public void clearBookmarkedConferences() {
  112.         bookmarkedConferences.clear();
  113.     }

  114.     /**
  115.      * Returns a Collection of all Bookmarked URLs for this user.
  116.      *
  117.      * @return a collection of all Bookmarked URLs.
  118.      */
  119.     public List<BookmarkedURL> getBookmarkedURLS() {
  120.         return bookmarkedURLS;
  121.     }

  122.     /**
  123.      * Returns a Collection of all Bookmarked Conference for this user.
  124.      *
  125.      * @return a collection of all Bookmarked Conferences.
  126.      */
  127.     public List<BookmarkedConference> getBookmarkedConferences() {
  128.         return bookmarkedConferences;
  129.     }


  130.     /**
  131.      * Returns the root element name.
  132.      *
  133.      * @return the element name.
  134.      */
  135.     public String getElementName() {
  136.         return ELEMENT;
  137.     }

  138.     /**
  139.      * Returns the root element XML namespace.
  140.      *
  141.      * @return the namespace.
  142.      */
  143.     public String getNamespace() {
  144.         return NAMESPACE;
  145.     }

  146.     /**
  147.      * Returns the XML representation of the PrivateData.
  148.      *
  149.      * @return the private data as XML.
  150.      */
  151.     @Override
  152.     public XmlStringBuilder toXML() {
  153.         XmlStringBuilder buf = new XmlStringBuilder();
  154.         buf.halfOpenElement(ELEMENT).xmlnsAttribute(NAMESPACE).rightAngleBracket();

  155.         for (BookmarkedURL urlStorage : getBookmarkedURLS()) {
  156.             if(urlStorage.isShared()) {
  157.                 continue;
  158.             }
  159.             buf.halfOpenElement("url").attribute("name", urlStorage.getName()).attribute("url", urlStorage.getURL());
  160.             buf.condAttribute(urlStorage.isRss(), "rss", "true");
  161.             buf.closeEmptyElement();
  162.         }

  163.         // Add Conference additions
  164.         for (BookmarkedConference conference : getBookmarkedConferences()) {
  165.             if(conference.isShared()) {
  166.                 continue;
  167.             }
  168.             buf.halfOpenElement("conference");
  169.             buf.attribute("name", conference.getName());
  170.             buf.attribute("autojoin", Boolean.toString(conference.isAutoJoin()));
  171.             buf.attribute("jid", conference.getJid());
  172.             buf.rightAngleBracket();

  173.             buf.optElement("nick", conference.getNickname());
  174.             buf.optElement("password", conference.getPassword());

  175.             buf.closeElement("conference");
  176.         }

  177.         buf.closeElement(ELEMENT);
  178.         return buf;
  179.     }

  180.     /**
  181.      * The IQ Provider for BookmarkStorage.
  182.      *
  183.      * @author Derek DeMoro
  184.      */
  185.     public static class Provider implements PrivateDataProvider {

  186.         /**
  187.          * Empty Constructor for PrivateDataProvider.
  188.          */
  189.         public Provider() {
  190.             super();
  191.         }

  192.         public PrivateData parsePrivateData(XmlPullParser parser) throws XmlPullParserException, IOException {
  193.             Bookmarks storage = new Bookmarks();

  194.             boolean done = false;
  195.             while (!done) {
  196.                 int eventType = parser.next();
  197.                 if (eventType == XmlPullParser.START_TAG && "url".equals(parser.getName())) {
  198.                     final BookmarkedURL urlStorage = getURLStorage(parser);
  199.                     if (urlStorage != null) {
  200.                         storage.addBookmarkedURL(urlStorage);
  201.                     }
  202.                 }
  203.                 else if (eventType == XmlPullParser.START_TAG &&
  204.                         "conference".equals(parser.getName()))
  205.                 {
  206.                     final BookmarkedConference conference = getConferenceStorage(parser);
  207.                     storage.addBookmarkedConference(conference);
  208.                 }
  209.                 else if (eventType == XmlPullParser.END_TAG && "storage".equals(parser.getName()))
  210.                 {
  211.                     done = true;
  212.                 }
  213.             }


  214.             return storage;
  215.         }
  216.     }

  217.     private static BookmarkedURL getURLStorage(XmlPullParser parser) throws IOException, XmlPullParserException {
  218.         String name = parser.getAttributeValue("", "name");
  219.         String url = parser.getAttributeValue("", "url");
  220.         String rssString = parser.getAttributeValue("", "rss");
  221.         boolean rss = rssString != null && "true".equals(rssString);

  222.         BookmarkedURL urlStore = new BookmarkedURL(url, name, rss);
  223.         boolean done = false;
  224.         while (!done) {
  225.             int eventType = parser.next();
  226.             if(eventType == XmlPullParser.START_TAG
  227.                         && "shared_bookmark".equals(parser.getName())) {
  228.                     urlStore.setShared(true);
  229.             }
  230.             else if (eventType == XmlPullParser.END_TAG && "url".equals(parser.getName())) {
  231.                 done = true;
  232.             }
  233.         }
  234.         return urlStore;
  235.     }

  236.     private static BookmarkedConference getConferenceStorage(XmlPullParser parser) throws XmlPullParserException, IOException {
  237.         String name = parser.getAttributeValue("", "name");
  238.         String autojoin = parser.getAttributeValue("", "autojoin");
  239.         String jid = parser.getAttributeValue("", "jid");

  240.         BookmarkedConference conf = new BookmarkedConference(jid);
  241.         conf.setName(name);
  242.         conf.setAutoJoin(Boolean.valueOf(autojoin).booleanValue());

  243.         // Check for nickname
  244.         boolean done = false;
  245.         while (!done) {
  246.             int eventType = parser.next();
  247.             if (eventType == XmlPullParser.START_TAG && "nick".equals(parser.getName())) {
  248.                 conf.setNickname(parser.nextText());
  249.             }
  250.             else if (eventType == XmlPullParser.START_TAG && "password".equals(parser.getName())) {
  251.                 conf.setPassword(parser.nextText());
  252.             }
  253.             else if(eventType == XmlPullParser.START_TAG
  254.                         && "shared_bookmark".equals(parser.getName())) {
  255.                     conf.setShared(true);
  256.             }
  257.             else if (eventType == XmlPullParser.END_TAG && "conference".equals(parser.getName())) {
  258.                 done = true;
  259.             }
  260.         }


  261.         return conf;
  262.     }
  263. }