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

Java tutorial

Introduction

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

Source

/**
 * PureInfo Dolphin
 * @(#)Expression.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 java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import com.pureinfo.dolphin.script.ScriptConstants;
import com.pureinfo.force.container.ContainerUtil;
import com.pureinfo.force.container.IClearable;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.xml.IXMLSupporter;

/**
 * <P>
 * Created on 2005-8-29 22:55:14 <BR>
 * Last modified on 2005-8-29
 * </P>
 * Expression: Dolphin view script statement.
 * 
 * @author Why
 * @version 1.0, 2005-8-29
 * @since Dolphin 1.0
 */
public class Expression implements IClearable, IXMLSupporter {
    public static final int TYPE_STRING = ScriptConstants.STRING;

    public static final int TYPE_VARIABLE = ScriptConstants.VARIABLE;

    public static final int TYPE_FUNCTION = ScriptConstants.FUNCTION;

    //statement body: string / var / function
    private Object m_body;

    //statement type which is defined in ScriptConstants
    private int m_nType;

    //formatters list
    private List m_formatters;

    /**
     * Constructor
     */
    public Expression() {
        super();

        m_formatters = new ArrayList(0);
    }

    /**
     * Constructor
     * 
     * @param _string
     *            a string as expression body
     */
    public Expression(String _string) {
        this();
        this.setBody(_string);
    }

    /**
     * Constructor
     * 
     * @param _var
     *            a variable as expression body
     */
    public Expression(Variable _var) {
        this();
        this.setBody(_var);
    }

    /**
     * Constructor
     * 
     * @param _function
     *            a function as expression body
     */
    public Expression(Function _function) {
        this();
        this.setBody(_function);
    }

    /**
     * @see com.pureinfo.force.container.IClearable#clear()
     */
    public void clear() {
        if (m_body != null && !(m_body instanceof String)) {
            ContainerUtil.clear(m_body);
        }
        if (m_formatters != null) {
            ContainerUtil.clear(m_formatters);
        }
    }

    /**
     * Returns the statement body.
     * 
     * @return the statement body.
     */
    public Object getBody() {
        return m_body;
    }

    /**
     * Returns the statement type.
     * 
     * @return the statement type.
     */
    public int getType() {
        return m_nType;
    }

    /**
     * Sets the statement body of string type.
     * 
     * @param _string
     *            the statement body of string type.
     */
    public void setBody(String _string) {
        m_body = _string;
        m_nType = ScriptConstants.STRING;
    }

    /**
     * Sets the statement body of variable type.
     * 
     * @param _sBody
     *            the statement body of variable type.
     */
    public void setBody(Variable _variable) {
        m_body = _variable;
        m_nType = ScriptConstants.VARIABLE;
    }

    /**
     * Sets the statement body of function type.
     * 
     * @param _function
     *            the statement body of function type.
     */
    public void setBody(Function _function) {
        m_body = _function;
        m_nType = ScriptConstants.FUNCTION;
    }

    /**
     * Returns the formatters list.
     * 
     * @return the formatters list.
     */
    public List getFormatters() {
        return m_formatters;
    }

    /**
     * Adds a formatter.
     * 
     * @param _sFormatter
     *            a formatter name
     */
    public void addFormatter(String _sFormatter) {
        m_formatters.add(_sFormatter);
    }

    /**
     * Adds a formatter.
     * 
     * @param _varFormatter
     *            a formatter variable
     */
    public void addFormatter(Variable _varFormatter) {
        m_formatters.add(_varFormatter);
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        StringBuffer buff = new StringBuffer();
        Object formatter;
        try {
            if (m_nType == ScriptConstants.STRING) {
                buff.append(ScriptConstants.QUOTE_CHAR);
                buff.append((String) m_body);
                buff.append(ScriptConstants.QUOTE_CHAR);
            } else {
                buff.append(m_body.toString());
            }

            for (int i = 0; i < m_formatters.size(); i++) {
                formatter = m_formatters.get(i);
                buff.append(ScriptConstants.OP_FORMAT);
                if (formatter instanceof String) {
                    buff.append(ScriptConstants.QUOTE_CHAR);
                    buff.append((String) formatter);
                    buff.append(ScriptConstants.QUOTE_CHAR);
                } else {
                    buff.append(formatter.toString());
                }
            }
            return buff.toString();
        } finally {
            buff.setLength(0);
        }
    }

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

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        //to export body
        Element ele;
        switch (m_nType) {
        case TYPE_STRING: {
            ele = _element.addElement("str");
            ele.addCDATA((String) m_body);
            break;
        }
        case TYPE_VARIABLE: {
            ele = _element.addElement("var");
            ((IXMLSupporter) m_body).toXMLElement(ele);
            break;
        }
        case TYPE_FUNCTION: {
            ele = _element.addElement("func");
            ((IXMLSupporter) m_body).toXMLElement(ele);
            break;
        }
        }

        //to export formatters
        Object formatter;
        for (int i = 0; i < m_formatters.size(); i++) {
            formatter = m_formatters.get(i);
            ele = _element.addElement("op");
            ele.addAttribute("name", ">>");

            if (formatter instanceof String) {
                ele = _element.addElement("str");
                ele.addCDATA((String) formatter);
            } else {
                ele = _element.addElement("var");
                ((Variable) formatter).toXMLElement(ele);
            }
        }
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        // TODO Auto-generated method stub

    }

}