com.pureinfo.ark.auth2.model.RightsConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.ark.auth2.model.RightsConfig.java

Source

/**
 * PureInfo Ark
 * @(#)RightsConfig.java   1.0 2006-9-30
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.ark.auth2.model;

import java.util.List;

import org.dom4j.Element;

import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.xml.IXMLSupporter;
import com.pureinfo.force.xml.XMLUtil;

/**
 * <P>
 * Created on 2006-9-30 13:08:39 <BR>
 * Last modified on 2006-9-30
 * </P>
 * RightsConfig: configuration of rights on resource.
 * 
 * @author Why
 * @version 1.0, 2006-9-30
 * @since Ark 1.2
 */
public class RightsConfig implements IXMLSupporter {

    private RoleDef[] m_roles;

    private ActionDef[] m_actions;

    private ResourceDef[] m_resources;

    /**
     * Constructor: default
     */
    public RightsConfig() {
        super();
    }

    /**
     * Returns the baseActions.
     * 
     * @return the baseActions.
     */
    public ActionDef[] getActions() {
        return m_actions;
    }

    /**
     * Sets the baseActions.
     * 
     * @param _actions
     *            the baseActions to set.
     */
    public void setActions(ActionDef[] _actions) {
        m_actions = _actions;
    }

    /**
     * Returns the resources.
     * 
     * @return the resources.
     */
    public ResourceDef[] getResources() {
        return m_resources;
    }

    /**
     * Sets the resources.
     * 
     * @param _resources
     *            the resources to set.
     */
    public void setResources(ResourceDef[] _resources) {
        m_resources = _resources;
    }

    /**
     * Returns the roles.
     * 
     * @return the roles.
     */
    public RoleDef[] getRoles() {
        return m_roles;
    }

    /**
     * Sets the roles.
     * 
     * @param _roles
     *            the roles to set.
     */
    public void setRoles(RoleDef[] _roles) {
        m_roles = _roles;
    }

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

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        Element ele = _element.addElement("roles");
        if (m_roles != null) {
            for (int i = 0; i < m_roles.length; i++) {
                ele.add(XMLUtil.toXMLElement(m_roles[i], "role"));
            }
        }

        ele = _element.addElement("actions");
        if (m_actions != null) {
            for (int i = 0; i < m_actions.length; i++) {
                ele.add(XMLUtil.toXMLElement(m_actions[i], "action"));
            }
        }

        if (m_resources != null) {
            for (int i = 0; i < m_resources.length; i++) {
                _element.add(XMLUtil.toXMLElement(m_resources[i], "resource"));
            }
        }
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        Element ele = _element.element("roles");
        if (ele == null) {
            m_roles = null;
        } else {
            List list = ele.elements("role");
            if (list == null || list.isEmpty()) {
                m_roles = null;
            } else {
                try {
                    m_roles = new RoleDef[list.size()];
                    RoleDef roleDef;
                    for (int i = 0; i < list.size(); i++) {
                        roleDef = new RoleDef();
                        roleDef.fromXML((Element) list.get(i));
                        m_roles[i] = roleDef;
                    }
                } finally {
                    list.clear();
                }
            }
        } //endif

        //to read action-base
        ele = _element.element("actions");
        if (ele == null) {
            m_actions = null;
        } else {
            List list = ele.elements("action");
            if (list == null || list.isEmpty()) {
                m_actions = null;
            } else {
                try {
                    m_actions = new ActionDef[list.size()];
                    ActionDef actionDef;
                    for (int i = 0; i < list.size(); i++) {
                        actionDef = new ActionDef();
                        actionDef.fromXML((Element) list.get(i));
                        m_actions[i] = actionDef;
                    }
                } finally {
                    list.clear();
                }
            }
        } //endif

        //to read resources
        List list = _element.elements("resource");
        if (list == null || list.isEmpty()) {
            m_resources = null;
        } else {
            try {
                m_resources = new ResourceDef[list.size()];
                ResourceDef resourceDef;
                for (int i = 0; i < list.size(); i++) {
                    resourceDef = new ResourceDef();
                    resourceDef.fromXML((Element) list.get(i));
                    m_resources[i] = resourceDef;
                }
            } finally {
                list.clear();
            }
        }
    }

}