com.pureinfo.dolphinview.style.model.ViewStyle.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.dolphinview.style.model.ViewStyle.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.style.model;

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

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

import com.pureinfo.dolphin.script.lang.ScriptBlock;
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 Aug 25, 2005
 * </P>
 * The style of view component
 * 
 * @author Freeman.Hu
 * @version 1.0, Jul 15, 2005
 * @since Dolphin 1.0
 */
public class ViewStyle implements IXMLSupporter {
    public static final int TYPE_SCENERY = 1;

    public static final int TYPE_ACT = 2;

    public static final int TYPE_ELEMENT = 3;

    private static final Logger logger = Logger.getLogger(ViewStyle.class);

    public static final String ELEMENT_STYLE = "view-style";

    public static final String ATTRIBUTE_NAME = "name";

    public static final String ELEMENT_SCENERY = "scenery";

    public static final String ELEMENT_ACT = "act";

    public static final String ELEMENT_ELEMENT = "element";

    public static final String ELEMENT_PROPERTY = "property";

    /**
     * the name of style
     */
    private String m_sName;

    /**
     * the propertie used in the scenery context, a map of <code>String</code>
     * and <code>String</code> for name-value pair.
     */
    private Map m_sceneryProperties = new HashMap();

    /**
     * the properties used in the act context ,a map of <code>String</code>
     * and <code>String</code> for name-value pair.
     */
    private Map m_actProperties = new HashMap();

    /**
     * the properties used in the element context ,a map of <code>String</code>
     * and <code>String</code> for name-value pair.
     */
    private Map m_elementProperties = new HashMap();

    private ScriptBlockCache m_sceneryPropertyCache;

    private ScriptBlockCache m_actPropertyCache;

    private ScriptBlockCache m_elementPropertyCache;

    public ViewStyle() {
        this("Unknown");
        setSceneryProperties(new HashMap());
    }

    public ViewStyle(String _sName) {
        this(_sName, new HashMap());
    }

    public ViewStyle(String _sName, Map _attrs) {
        setName(_sName);
        setSceneryProperties(_attrs);
    }

    public Map getSceneryProperties() {
        return m_sceneryProperties;
    }

    private void setSceneryProperties(Map attributes) {
        m_sceneryProperties = attributes;
    }

    public void putAttribute(String _sName, String _sValue) {
        m_sceneryProperties.put(_sName, _sValue);
    }

    public String getName() {
        return m_sName;
    }

    private void setName(String name) {
        m_sName = name;
    }

    public String getAttributeValue(String _sName) {
        return (String) m_sceneryProperties.get(_sName);
    }

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

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

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

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        throw new UnsupportedOperationException("plz diy");
    }

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

        setName(XMLUtil.getRequiredAttributeValue(_element, ATTRIBUTE_NAME));

        m_sceneryProperties = getProperties(TYPE_SCENERY, _element);
        m_sceneryPropertyCache = new ScriptBlockCache(m_sceneryProperties);

        m_actProperties = getProperties(TYPE_ACT, _element);
        m_actPropertyCache = new ScriptBlockCache(m_actProperties);

        m_elementProperties = getProperties(TYPE_ELEMENT, _element);
        m_elementPropertyCache = new ScriptBlockCache(m_elementProperties);
    }

    private Map getProperties(int _nType, Element _element) throws PureException {
        switch (_nType) {
        case TYPE_SCENERY:
            return getProperties(XMLUtil.getElement(_element, ELEMENT_SCENERY, true));
        case TYPE_ACT:
            return getProperties(XMLUtil.getElement(_element, ELEMENT_ACT, true));
        case TYPE_ELEMENT:
            return getProperties(XMLUtil.getElement(_element, ELEMENT_ELEMENT, true));
        default:
            throw new PureException(DolphinViewExceptionTypes.ILLEGAL_STYLE_TYPE, String.valueOf(_nType));
        }
    }

    private Map getProperties(Element _element) throws PureException {
        List list = _element.elements(ELEMENT_PROPERTY);
        Map props = new HashMap(list.size());
        if (list.size() == 0) {
            logger.warn("style[" + getName() + "] element[" + _element.getName() + "] has no element["
                    + ELEMENT_PROPERTY + "]");
        }
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Element ele = (Element) iter.next();
            String sName = XMLUtil.getRequiredAttributeValue(ele, ATTRIBUTE_NAME);
            String sValue = ele.getText();
            props.put(sName, sValue);
        }
        return props;
    }

    /**
     * 
     * @param _nType
     * @param _sName
     * @return
     * @throws PureException
     */
    public boolean hasProperty(int _nType, String _sName) throws PureException {
        ScriptBlockCache cache = getPropertyCache(_nType);
        return cache.has(_sName);
    }

    public ScriptBlock getPropertyScriptBlock(int _sType, String _sName) throws PureException {
        ScriptBlockCache cache = getPropertyCache(_sType);
        return cache.get(_sName);
    }

    private ScriptBlockCache getPropertyCache(int _sType) throws PureException {
        switch (_sType) {
        case TYPE_SCENERY:
            return m_sceneryPropertyCache;
        case TYPE_ACT:
            return m_actPropertyCache;
        case TYPE_ELEMENT:
            return m_elementPropertyCache;
        default:
            throw new PureException(DolphinViewExceptionTypes.ILLEGAL_STYLE_TYPE, String.valueOf(_sType));
        }
    }

    public String getProperty(int _sType, String _sName) throws PureException {
        switch (_sType) {
        case TYPE_SCENERY:
            return (String) m_sceneryProperties.get(_sName);
        case TYPE_ACT:
            return (String) m_actProperties.get(_sName);
        case TYPE_ELEMENT:
            return (String) m_elementProperties.get(_sName);
        default:
            throw new PureException(DolphinViewExceptionTypes.ILLEGAL_STYLE_TYPE, String.valueOf(_sType));
        }
    }
}