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 java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.List;

  21. import org.jivesoftware.smack.util.ParserUtils;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;
  23. import org.jivesoftware.smack.xml.XmlPullParser;
  24. import org.jivesoftware.smack.xml.XmlPullParserException;

  25. import org.jivesoftware.smackx.iqprivate.packet.PrivateData;
  26. import org.jivesoftware.smackx.iqprivate.provider.PrivateDataProvider;

  27. import org.jxmpp.jid.EntityBareJid;
  28. import org.jxmpp.jid.parts.Resourcepart;

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

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

  60.     private final List<BookmarkedURL> bookmarkedURLS;
  61.     private final List<BookmarkedConference> bookmarkedConferences;

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

  193.         @Override
  194.         public PrivateData parsePrivateData(XmlPullParser parser) throws XmlPullParserException, IOException {
  195.             Bookmarks storage = new Bookmarks();

  196.             boolean done = false;
  197.             while (!done) {
  198.                 XmlPullParser.Event eventType = parser.next();
  199.                 if (eventType == XmlPullParser.Event.START_ELEMENT && "url".equals(parser.getName())) {
  200.                     final BookmarkedURL urlStorage = getURLStorage(parser);
  201.                     if (urlStorage != null) {
  202.                         storage.addBookmarkedURL(urlStorage);
  203.                     }
  204.                 }
  205.                 else if (eventType == XmlPullParser.Event.START_ELEMENT &&
  206.                         "conference".equals(parser.getName())) {
  207.                     final BookmarkedConference conference = getConferenceStorage(parser);
  208.                     storage.addBookmarkedConference(conference);
  209.                 }
  210.                 else if (eventType == XmlPullParser.Event.END_ELEMENT && "storage".equals(parser.getName())) {
  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.             XmlPullParser.Event eventType = parser.next();
  226.             if (eventType == XmlPullParser.Event.START_ELEMENT
  227.                         && "shared_bookmark".equals(parser.getName())) {
  228.                     urlStore.setShared(true);
  229.             }
  230.             else if (eventType == XmlPullParser.Event.END_ELEMENT && "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.         boolean autojoin = ParserUtils.getBooleanAttribute(parser, "autojoin", false);
  239.         EntityBareJid jid = ParserUtils.getBareJidAttribute(parser);

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

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


  262.         return conf;
  263.     }
  264. }