Custom TOC Context Menu
com\esri\arcgis\sample\layerattributes\LayerAttributesRenderer.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.layerattributes;

import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebToc;
import com.esri.adf.web.faces.component.TocControl;
import com.esri.adf.web.faces.renderkit.xml.ajax.AJAXRenderer;
import com.esri.adf.web.util.WebUtil;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

/**
 * Class to render xml content to display attributes for features stored in 
 * context attribute 'myattr'.
 */
public class LayerAttributesRenderer implements AJAXRenderer {
  private static Logger logger = Logger.getLogger(LayerAttributesRenderer.class.getName());

  /**
   * No original state
   */
  public Object getOriginalState(UIComponent component) {
    return null;
  }

  /**
   * This renderer is to work with TocControls
   */
  public Class getControlClass() {
    return TocControl.class;
  }

  /**
   * Render xml content
   */
  public void renderAjaxResponse(FacesContext context, UIComponent component, Object state, boolean isEventSource, Element parent) {
    try {
      if (! (((TocControl) component).getTocModel() instanceof WebToc))
        return;
      
      WebContext ctx = WebUtil.getWebContext(component);
      if (ctx.getAttribute("myattr") == null)
        return;
      
      String xmlString = "<layer-attributes>" + (String)ctx.getAttribute("myattr") + "</layer-attributes>";
      
      if(xmlString==null)
        return;
      
      DocumentBuilderFactory factory =  DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      DocumentFragment frag = parseXml(parent.getOwnerDocument(), xmlString);
      parent.appendChild(frag);

      ctx.setAttribute("myattr", null);
    }
    catch (Exception e) {
      if (logger.isLoggable(Level.WARNING))
        logger.log(Level.WARNING, "Error in Tracker", e);
    }
  }
  
  /**
   * Parses a string containing XML and returns a DocumentFragment containing
   * the nodes of the parsed XML.
   */
  public static DocumentFragment parseXml(Document doc, String fragment) {
    // Wrap the fragment in an arbitrary element
    fragment = "<fragment>"+fragment+"</fragment>";
    try {
      // Create a DOM builder and parse the fragment
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(fragment)));
      
      // Import the nodes of the new document into doc so that they
      // will be compatible with doc
      Node node = doc.importNode(d.getDocumentElement(), true);
      
      // Create the document fragment node to hold the new nodes
      DocumentFragment docfrag = doc.createDocumentFragment();
      
      // Move the nodes into the fragment
      while (node.hasChildNodes()) {
        docfrag.appendChild(node.removeChild(node.getFirstChild()));
      }
      
      // Return the fragment
      return docfrag;
    }
    catch (Exception e) {
      if (logger.isLoggable(Level.WARNING))
        logger.log(Level.WARNING, "Error in Tracker", e);
    }
    return null;
  }
}