com.pureinfo.dolphinview.component.model.ViewComponent.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.dolphinview.component.model.ViewComponent.java

Source

/**
 * PureInfo Dolphin
 * @(#)FormatterDeclarationParameter.java   1.0 Jul 15, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */
package com.pureinfo.dolphinview.component.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.dom4j.Element;
import org.dom4j.XPath;

import com.pureinfo.dolphin.script.lang.ScriptBlock;
import com.pureinfo.dolphin.script.lang.ScriptReader;
import com.pureinfo.dolphin.script.param.ParameterMetadata;
import com.pureinfo.dolphinview.DolphinViewExceptionTypes;
import com.pureinfo.dolphinview.template.model.ScriptBlockCache;
import com.pureinfo.force.exception.CheckUtils;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.xml.IXMLSupporter;
import com.pureinfo.force.xml.XMLUtil;

/**
 * <P>
 * Created on Jul 15, 2005 5:06:51 PM <BR>
 * Last modified on Jul 15, 2005
 * </P>
 * the view component
 * 
 * @author Freeman.Hu
 * @version 1.0, Aug 25, 2005
 * @since Dolphin 1.0
 */
public class ViewComponent implements IXMLSupporter {

    public static final String ATTRIBUTE_DESCRIPTION = "desc";

    public static final String ATTRIBUTE_ID = "id";

    public static final String ATTRIBUTE_NAME = "name";

    public static final String ATTRIBUTE_VERSION = "version";

    public static final String ELEMENT_PARAMETERS = "params";

    public static final String ELEMENT_STYLE = "style";

    public static final String PATH_BODY = "/component/body";

    public static final String PATH_RESOURCE = "/component/head/resource-ref";

    public static final String PATH_VALIDATE = "/component/validate";

    private ScriptBlockCache m_paramCache;

    /**
     * a map of <code>ParameterDefinement</code> and her name.
     */
    private Map m_parameters;

    /**
     * The references of the resources that will be filled in head. a set of
     * <code>String</code>
     */
    private Set m_ResourceIds;

    /**
     * The html code will be filled in body.
     */
    private String m_sBody;

    /**
     * The description of the view component
     */
    private String m_sDescription;

    /**
     * The name of the view component
     */
    private String m_sName;

    private ScriptBlockCache m_styleCache;

    private Map m_styleParams;

    /**
     * The script code will be filled in vilidate script.
     */
    private String m_sValidate;

    /**
     * The version of the view component
     */
    private String m_sVersion;

    public ViewComponent() {

    }

    public String getName() {
        return m_sName;
    }

    public String getBody() {
        return m_sBody;
    }

    /**
     * Set formatter: &lt;resource-script-block(ScriptBlock)&gt; <br>
     */
    public Set getResourceIds() {
        return m_ResourceIds;
    }

    public String getValidate() {
        return m_sValidate;
    }

    /**
     * Mapping format:
     * &lt;paramter-name(String),paramter-matadata(ParameterMetadata)&gt; <br>
     * 
     * @return
     */
    public Map getParameters() {
        return m_parameters;
    }

    public boolean equals(Object _obj) {
        return EqualsBuilder.reflectionEquals(this, _obj);
    }

    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    public String getDescription() {
        return m_sDescription;
    }

    public String getVersion() {
        return m_sVersion;
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        if (_element == null) {
            throw new PureException(200, "request can't be null");
        }

        throw new UnsupportedOperationException("If you wan't to use the operation,please implement it.");
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        CheckUtils.checkRequestNotNull(_element);

        m_sVersion = _element.attributeValue(ATTRIBUTE_VERSION, "unknown");
        m_sName = XMLUtil.getRequiredAttributeValue(_element, ATTRIBUTE_NAME);
        m_sDescription = XMLUtil.getRequiredAttributeValue(_element, ATTRIBUTE_DESCRIPTION);

        m_parameters = ParameterMetadata.getParameterMetadatas(_element.element(ELEMENT_PARAMETERS), false);
        m_paramCache = new ScriptBlockCache(getParamDefaults(m_parameters));

        m_styleParams = ParameterMetadata.getParameterMetadatas(_element.element(ELEMENT_STYLE), false);
        m_styleCache = new ScriptBlockCache(getParamDefaults(m_styleParams));

        m_sBody = getPathText(_element, PATH_BODY);
        m_sValidate = getPathText(_element, PATH_VALIDATE);

        m_ResourceIds = getResources(_element);
    }

    private String getPathText(Element _elemant, String _sPath) {
        XPath elementPath = _elemant.createXPath(_sPath);
        Element element = (Element) elementPath.selectSingleNode(_elemant);
        if (element == null) {
            return "";
        }
        return element.getText();
    }

    /**
     * Set formatter: &lt;resource-script-block(ScriptBlock)&gt; <br>
     */
    private Set getResources(Element _element) throws PureException {
        XPath elementPath = _element.createXPath(PATH_RESOURCE);
        List elementList = elementPath.selectNodes(_element);
        Set ids = new HashSet();
        for (Iterator iter = elementList.iterator(); iter.hasNext();) {
            Element element = (Element) iter.next();
            String sId = XMLUtil.getRequiredAttributeValue(element, ATTRIBUTE_ID);
            ScriptBlock sb = ScriptReader.readBlock(sId);
            ids.add(sb);
        }
        return ids;
    }

    public ScriptBlock getParameterScriptBlock(String _sName) throws PureException {
        return m_paramCache.get(_sName);
    }

    public ScriptBlock getStyleScriptBlock(String _sName) throws PureException {
        return m_styleCache.get(_sName);
    }

    /**
     * Get the default values of the parameters
     * 
     * @return
     */
    private Map getParamDefaults(Map _params) {
        Map defaults = new HashMap(_params.size());
        for (Iterator iter = _params.keySet().iterator(); iter.hasNext();) {
            String sName = (String) iter.next();
            ParameterMetadata paramMeta = (ParameterMetadata) _params.get(sName);
            String sValue = paramMeta.getDefault();
            defaults.put(sName, sValue);
        }
        return defaults;
    }

    /**
     * mapping format:
     * &lt;parameter-id(String),parameter-metadata(ParameterMetadata)&gt; <br>
     *  
     */
    public Map getStyleParams() {
        return m_styleParams;
    }

    public ParameterMetadata getStyleParameterMetadata(String _sName, boolean _bRequired) throws PureException {
        ParameterMetadata paramMeta = (ParameterMetadata) m_styleParams.get(_sName);
        if (paramMeta == null && _bRequired) {
            throw new PureException(DolphinViewExceptionTypes.STYLEVAR_NOT_DECLARED,
                    _sName + " in " + this.getName());
        }
        return paramMeta;
    }
}