com.pureinfo.dolphin.script.param.ParameterMetadata.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.dolphin.script.param.ParameterMetadata.java

Source

/**
 * PureInfo Dolphin
 * @(#)FormatterDeclarationParameter.java   1.0 Aug 25, 2005
 * 
 * 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.param;

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.dom4j.Element;

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 Aug 25, 2005 5:06:51 PM <BR>
 * Last modified on Aug 25, 2005
 * </P>
 * The definement of the parameter
 * 
 * @author Freeman.Hu
 * @version 1.0, Aug 25, 2005
 * @since Dolphin 1.0
 */
public class ParameterMetadata implements IXMLSupporter {

    public static final String ELEMENT_PARAMETER = "param";

    public static final String ATTRIBUTE_NAME = "name";

    public static final String ATTRIBUTE_REQUIRED = "required";

    public static final String ATTRIBUTE_DESCRIPTION = "desc";

    /**
     * the name of the parameter.
     */
    private String m_sName;

    /**
     * Indicats whether the parameter is needed.
     */
    private boolean m_bRequired;

    /**
     * the description of the parameter.
     */
    private String m_sDescription;

    /**
     * the default value of the parameter.
     */
    private String m_sDefault;

    public ParameterMetadata() {

    }

    /**
     * Constructor
     * 
     * @param _sName
     *            the name of the parameter.
     * @param _bRequired
     *            Indicats whether the parameter is needed.
     * @param _sDescription
     *            the description of the parameter.
     * @param _sDefault
     *            the default value of the parameter.
     */
    public ParameterMetadata(String _sName, boolean _bRequired, String _sDescription, String _sDefault) {
        setName(_sName);
        setRequired(_bRequired);
        setDescription(_sDescription);
        setDefault(_sDefault);
    }

    public boolean isRequired() {
        return m_bRequired;
    }

    public void setRequired(boolean _bRequired) {
        m_bRequired = _bRequired;
    }

    public String getDefault() {
        return m_sDefault;
    }

    public void setDefault(String _sDefault) {
        if (_sDefault == null || _sDefault.length() == 0) {
            m_sDefault = null;
        } else {
            m_sDefault = _sDefault;
        }
    }

    public String getDescription() {
        return m_sDescription;
    }

    public void setDescription(String description) {
        m_sDescription = description;
    }

    public String getName() {
        return m_sName;
    }

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

    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 {
        if (_element == null) {
            throw new PureException(200, "request can't be null");
        }

        if (getName() == null) {
            throw new PureException(502, "element attribute[" + ATTRIBUTE_NAME + "] can't be null");
        }
        _element.addAttribute(ATTRIBUTE_NAME, getName());

        _element.addAttribute(ATTRIBUTE_REQUIRED, String.valueOf(isRequired()));

        if (getDescription() == null) {
            throw new PureException(502, "element attribute[" + ATTRIBUTE_DESCRIPTION + "] can't be null");
        }
        _element.addAttribute(ATTRIBUTE_DESCRIPTION, getDescription());

        if (getDefault() == null) {
            _element.addText("");
        } else {
            _element.addText(getDefault());
        }
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        CheckUtils.checkRequestNotNull(_element);
        CheckUtils.checkElementName(_element, ELEMENT_PARAMETER);
        setName(XMLUtil.getRequiredAttributeValue(_element, ATTRIBUTE_NAME));
        setRequired(XMLUtil.getRequiredAttributeBooleanValue(_element, ATTRIBUTE_REQUIRED));
        setDescription(XMLUtil.getRequiredAttributeValue(_element, ATTRIBUTE_DESCRIPTION));
        setDefault(_element.getText());
    }

    /**
     * Get the metadatas of the parameters from a dom4j <code>Element</code>
     * 
     * @param _element
     *            a dom4j <code>Element</code>
     * @return @throws
     *         Exception
     */
    public static Map getParameterMetadatas(Element _element, boolean _bRequired) throws PureException {
        Map params = new HashMap();
        if (_element == null) {
            if (_bRequired) {
                throw new PureException(PureException.INVALID_REQUEST, "_element can't be null.");
            }
            return params;
        }
        List paramEleList = _element.elements(ELEMENT_PARAMETER);
        for (Iterator iter = paramEleList.iterator(); iter.hasNext();) {
            Element paramEle = (Element) iter.next();
            ParameterMetadata param = new ParameterMetadata();
            param.fromXML(paramEle);
            params.put(param.getName(), param);
        }
        return params;
    }
}