www.pudn.com > jsptree-1.0-b6-src.zip > JSPTreeTag.java


/* 
 * ==================================================================== 
 * The JSP Tree Software License, Version 1.1 
 * 
 * (this license is derived and fully compatible with the Apache Software 
 * License - see http://www.apache.org/LICENSE.txt) 
 * 
 * Copyright (c) 2002-2005 jsptree.sourceforge.net . All rights reserved. 
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 * 
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in 
 *    the documentation and/or other materials provided with the 
 *    distribution. 
 * 
 * 3. The end-user documentation included with the redistribution, 
 *    if any, must include the following acknowledgment: 
 *       "This product includes software developed by 
 *        jsptree.sourceforge.net (http://jsptree.sourceforge.net/)." 
 *    Alternately, this acknowledgment may appear in the software itself, 
 *    if and wherever such third-party acknowledgments normally appear. 
 * 
 * 4. The name "JSP Tree" must not be used to endorse or promote 
 *    products derived from this software without prior written permission. 
 *    For written permission, please contact modano@users.sourceforge.net . 
 * 
 * 5. Products derived from this software may not be called "JSP Tree", 
 *    nor may "JSP Tree" appear in their name, without prior written 
 *    permission of jsptree.sourceforge.net. 
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 * SUCH DAMAGE. 
 * ==================================================================== 
 */ 
 
package net.sf.jsptree; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.jsp.JspTagException; 
import javax.servlet.jsp.tagext.TagSupport; 
import java.io.IOException; 
 
import org.apache.log4j.Logger; 
 
import net.sf.jsptree.component.JSPTreeComponent; 
import net.sf.jsptree.skin.AbstractSkin; 
import net.sf.jsptree.tree.TreeFactory; 
 
/** 
 * @author Vladislav Kamensky 
 */ 
public class JSPTreeTag extends TagSupport { 
 
    private static Logger LOG = Logger.getLogger(JSPTreeTag.class); 
 
    private String name; 
 
    private TreeFactory treeFactory; 
 
    private String pathToXMLTree; 
 
    private String startAtDepth; 
 
    private String skinName; 
 
    private String imagePath; 
 
    private String templatePath; 
 
    private String stateManagerAction; 
 
    private String shareTreeStructure; 
 
    private String comparator; 
 
    public JSPTreeTag() { 
    } 
 
    public void setName(String name) { 
        this.name = name; 
    } 
 
    public void setTreeFactory(TreeFactory treeFactory) { 
        this.treeFactory = treeFactory; 
    } 
 
    public void setXtree(String pathToXMLTree) throws JspTagException { 
        this.pathToXMLTree = pathToXMLTree; 
    } 
 
    public void setSkin(String skinName) throws JspTagException { 
        this.skinName = skinName; 
    } 
 
    public void setStartAtDepth(String depth) throws JspTagException { 
        this.startAtDepth = depth; 
    } 
 
    public void setImagesPath(String imagePath) { 
        this.imagePath = imagePath; 
    } 
 
    public void setStateManagerAction(String stateManagerAction) { 
        this.stateManagerAction = stateManagerAction; 
    } 
 
    public void setTemplatePath(String path) { 
        this.templatePath = path; 
    } 
 
    public void setShareTreeStructure(String s) { 
        this.shareTreeStructure = s; 
    } 
 
    public void setComparator(String s) { 
        this.comparator = s; 
    } 
 
    public int doEndTag() throws JspTagException { 
        JSPTreeComponent treeComponent = new JSPTreeComponent(); 
        treeComponent.setContext(pageContext.getServletContext()); 
        treeComponent.setRequest((HttpServletRequest) pageContext.getRequest()); 
        treeComponent.setResponse((HttpServletResponse) pageContext.getResponse()); 
 
        if (treeComponent == null) { 
            LOG.fatal("JSPTree Component could not be constructed"); 
            throw new JspTagException("JSPTree Component could not be constructed"); 
        } 
        if (treeComponent.getRequestURI() == null) { 
            LOG.fatal("URL could not be constructed"); 
            throw new JspTagException("URL could not be constructed"); 
        } 
 
        if (name == null) { 
            LOG.fatal("Missing mandatory argument name"); 
            throw new JspTagException("Missing mandatory argument name"); 
        } else { 
            treeComponent.setName(name); 
        } 
 
        if (templatePath == null) { 
            LOG.fatal("Missing mandatory argument template path"); 
            throw new JspTagException("Missing mandatory template path"); 
        } else { 
            treeComponent.setTemplatePath(templatePath); 
        } 
 
        if (treeFactory == null && pathToXMLTree == null) { 
            LOG.fatal("Missing mandatory argument tree factory or argument xtree. One must be specified"); 
            throw new JspTagException("Missing argument treeFactory or argument xtree. One must be specified"); 
        } 
 
        if (treeFactory != null) { 
            treeComponent.setTreeFactory(treeFactory); 
        } else { 
            try { 
                treeComponent.setXMLTree(pathToXMLTree); 
            } catch (Exception exception) { 
                LOG.error("Exception while building tree from XTree: ", exception); 
                throw new JspTagException("Exception while building tree from XTree: " + exception.getMessage()); 
            } 
        } 
 
        if (skinName == null) { 
            LOG.fatal("Setting skin of tree to " + "default"); 
            treeComponent.setSkin(AbstractSkin.DEFAULT_SKIN); 
        } else { 
            treeComponent.setSkin(skinName); 
        } 
 
        try { 
            treeComponent.setStartAtDepth(Integer.parseInt(startAtDepth)); 
        } catch (NumberFormatException numberformatexception) { 
            LOG.error("NumberFormatException while trying to set attribute startAtDepth"); 
            throw new JspTagException("NumberFormatException while trying to set attribute startAtDepth"); 
        } 
 
        treeComponent.setImagesPath(imagePath); 
        treeComponent.setStateManagerAction(stateManagerAction); 
        treeComponent.setShareTreeStructure(shareTreeStructure); 
        if (comparator != null) { 
            try { 
                Class comparatorClass = Class.forName(comparator); 
                java.util.Comparator comp = (java.util.Comparator) comparatorClass.newInstance(); 
                treeComponent.setComparator(comp); 
            } catch (ClassNotFoundException e) { 
                throw new JspTagException("Exception while setting custom comparator" + e.getMessage()); 
            } catch (InstantiationException e) { 
                throw new JspTagException("Exception while setting custom comparator" + e.getMessage()); 
            } catch (IllegalAccessException e) { 
                throw new JspTagException("Exception while setting custom comparator" + e.getMessage()); 
            } 
        } 
 
        try { 
            treeComponent.printContent(new WebWriter(pageContext.getOut())); 
 
        } catch (IOException e) { 
            LOG.error("IO Error: ", e); 
            throw new JspTagException("IO Error: " + e.getMessage()); 
        } 
        if (LOG.isInfoEnabled()) { 
            LOG.info("ended"); 
        } 
 
        return EVAL_PAGE; 
    } 
 
    private boolean isTrueValue(String s, String attributeNmae) throws JspTagException { 
        boolean flag = false; 
        if (s != null && s.trim().equalsIgnoreCase("true")) 
            flag = true; 
        else if (s != null && s.trim().equalsIgnoreCase("false")) { 
            flag = false; 
        } else { 
            LOG.error("Incorrect value for " + attributeNmae + " attribute. It must be either true or false"); 
            throw new JspTagException("Incorrect value for " + attributeNmae + " attribute. It must be either true or false"); 
        } 
        return flag; 
    } 
}