com.pureinfo.dolphin.script.lang.Variable.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.dolphin.script.lang.Variable.java

Source

/**
 * PureInfo Dolphin
 * @(#)Variable.java   1.0 2005-8-29
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.dolphin.script.lang;

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

import com.pureinfo.dolphin.script.ScriptConstants;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.xml.IXMLSupporter;
import com.pureinfo.force.xml.XMLUtil;

/**
 * <P>
 * Created on 2005-8-29 23:20:01 <BR>
 * Last modified on 2005-8-29
 * </P>
 * Variable: variable in Dolphin view script.
 * 
 * @author Why
 * @version 1.0, 2005-8-29
 * @since Dolphin 1.0
 */
public class Variable implements IXMLSupporter {
    //variable namespace
    private String m_sNamespace;

    //variable name
    private String m_sName;

    /**
     * Constructor
     * 
     * @param _sNamesapce
     *            variable namespace
     * @param _sName
     *            variable name
     */
    public Variable(String _sNamesapce, String _sName) {
        super();

        this.setNamespace(_sNamesapce);
        this.setName(_sName);
    }

    /**
     * Constructor
     * 
     * @param _sFullName
     *            variable name with namespace.
     */
    public Variable(String _sFullName) {
        int nPos = _sFullName.indexOf(ScriptConstants.NAMESPACE_CHAR);
        if (nPos >= 0) {
            this.setNamespace(_sFullName.substring(0, nPos));
            this.setName(_sFullName.substring(nPos + 1));
        } else {
            this.setNamespace(null);
            this.setName(_sFullName);
        }
    }

    /**
     * Returns the variable namespace.
     * 
     * @return the variable namespace.
     */
    public String getNamespace() {
        return m_sNamespace;
    }

    /**
     * Sets the variable namespace.
     * 
     * @param _sNamespace
     *            the variable namespace.
     */
    public void setNamespace(String _sNamespace) {
        m_sNamespace = (_sNamespace == null ? "" : _sNamespace);
    }

    /**
     * Returns the variable name.
     * 
     * @return the variable name.
     */
    public String getName() {
        return m_sName;
    }

    /**
     * Sets the variable name.
     * 
     * @param _sName
     *            the variable name.
     */
    public void setName(String _sName) {
        m_sName = _sName;
    }

    /**
     * Returns the variable full name with namespace.
     * 
     * @return the variable full name with namespace.
     */
    public String getFullName() {
        if (m_sNamespace.length() > 0) {
            return m_sNamespace + ScriptConstants.NAMESPACE_CHAR + m_sName;
        }

        //else
        return m_sName;
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return ScriptConstants.PREFIX_VAR + getFullName();
    }

    //=========================================================================
    //implementation for IXMLSupporter

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        _element.addAttribute("namespace", m_sNamespace);
        _element.addAttribute("name", m_sName);
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        this.setNamespace(XMLUtil.getAttributeValueTrim(_element, "namespace"));
        this.setName(XMLUtil.getAttributeValueTrim(_element, "name"));
    }

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

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