Bookmark
com\esri\arcgis\sample\bookmarks\XMLBookmarks.java
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions.
* 
*/
package com.esri.arcgis.sample.bookmarks;

import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebContextInitialize;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.util.XMLUtil;
import com.esri.arcgis.sample.util.ExtentUtil;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class XMLBookmarks implements Bookmarks, WebContextInitialize, Serializable {
  private String filename;
  private HashMap bookmarks = new HashMap();

  public Bookmark get(String id) {
    Object b = bookmarks.get(id);
    if (b == null)
      return null;
    else
      return (Bookmark) b;
  }

  public Object remove(String id) {
    return bookmarks.remove(id);
  }

  public String add(String label, String description, WebExtent extent) {
    String id = Long.toString(GregorianCalendar.getInstance().getTimeInMillis());
    add(id, label, description, extent);
    return id;
  }

  protected void add(String id, String label, String description, WebExtent extent) {
    Bookmark bookmark = new Bookmark();
    bookmark.setId(id);
    bookmark.setLabel(label);
    bookmark.setDescription(description);
    bookmark.setExtent(extent);
    bookmarks.put(id, bookmark);
  }

  public void update(String id, String label, String description, WebExtent extent) {
    Bookmark bookmark = get(id);
    bookmark.setId(id);
    bookmark.setLabel(label);
    bookmark.setDescription(description);
    bookmark.setExtent(extent);
  }

  public Iterator getBookmarkIds() {
    return bookmarks.keySet().iterator();
  }

  /**
   * Set name of file which contains bookmarks
   */
  public void setFilename(String filename) {
    this.filename = filename;
  }

  /**
   * Load bookmarks from xml file
   */
  public boolean load() {
    try {
      Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream(filename));

      Element bookmarksElement = (Element) document.getElementsByTagName("bookmarks").item(0);
      NodeList bookmarkElements = bookmarksElement.getElementsByTagName("bookmark");
      for (int i=0;i<bookmarkElements.getLength();i++) {
        Element bookmarkElement = (Element) bookmarkElements.item(i);
        add(bookmarkElement.getAttribute("id"),
          bookmarkElement.getAttribute("label"),
          bookmarkElement.getAttribute("description"),
          ExtentUtil.stringToExtent(bookmarkElement.getAttribute("extent")));
      }

      return true;
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    return false;
  }

  /**
   * Serialize bookmarks into xml file
   */
  public boolean save() {
    try {
      Document doc = XMLUtil.newDocument();
      Element bookmarksElement = XMLUtil.createElement(doc, "bookmarks", null, null);

      Iterator iter = getBookmarkIds();
      while (iter.hasNext()) {
        Bookmark bookmark = get((String) iter.next());
        Element bookmarkElement = XMLUtil.createElement("bookmark", null, bookmarksElement);
        bookmarkElement.setAttribute("id", bookmark.getId());
        bookmarkElement.setAttribute("label", bookmark.getLabel());
        bookmarkElement.setAttribute("description", bookmark.getDescription());
        bookmarkElement.setAttribute("extent", ExtentUtil.extentToString(bookmark.getExtent()));
      }
      String xml = XMLUtil.transform(doc, null);

      FileOutputStream out = new FileOutputStream(filename);
      out.write(xml.getBytes());
      out.flush();
      out.close();
      return true;
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    return false;
  }

  /**
   * Return true as bookmarks are editable
   */
  public boolean isEditable() {
    return true;
  }

  /**
   * Call load() when web context is initialized
   */
  public void init(WebContext webContext) {
    this.load();
  }

  /**
   * Call save() when web context is destroyed
   */
  public void destroy() {
    if (this.isEditable())
      this.save();
  }
}