Java tutorial
/** * PureInfo Quake * @(#)ModulesConfig.java 1.0 2007-2-8 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.config; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.dom4j.Element; import com.pureinfo.force.container.IClearable; import com.pureinfo.force.exception.PureException; import com.pureinfo.force.xml.IXMLSupporter; import com.pureinfo.force.xml.XMLUtil; import com.pureinfo.srm.config.register.SNDecoder; /** * <P> * Created on 2007-2-8 04:35:06<BR> * Last modified on 2007-2-8 * </P> * ModulesConfig: config for module. * * @author Administrator * @version 1.0, 2007-2-8 * @since Quake 1.2 */ public class ModulesConfig implements IXMLSupporter, IClearable { private String m_sUnivCode; // modules map: <name(String), Module> private Map m_hMoudules = new HashMap(); /** * Constructor * * @param _sUnivCode * the university code */ public ModulesConfig(String _sUnivCode) { m_sUnivCode = _sUnivCode; } /** * @see com.pureinfo.force.container.IClearable#clear() */ public void clear() { m_hMoudules.clear(); } /** * Finds the module by name. * * @param _sName * the module name * @return the module */ public Module getModule(String _sName) { return (Module) m_hMoudules.get(_sName); } /** * Returns <code>true</code> if the specified module is enabled. * * @param _sName * the module name * @return <code>true</code> if the specified module is enabled; * <code>false</code> otherwise. */ public boolean isModuleEnabled(String _sName) { Module module = this.getModule(_sName); return module != null && module.isEnabled(); } /** * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element) */ public void fromXML(Element _element) throws PureException { // 1. to clear the old this.clear(); // 2. to read the modules if (_element == null) return; // skip List list = _element.elements("module"); if (list != null) { try { for (int i = 0; i < list.size(); i++) { Module module = new Module(m_sUnivCode); module.fromXML((Element) list.get(i)); m_hMoudules.put(module.getName(), module); } } finally { list.clear(); } } } /** * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element) */ public void toXMLElement(Element _element) throws PureException { Iterator itr = m_hMoudules.values().iterator(); while (itr.hasNext()) { Module module = (Module) itr.next(); Element item = _element.addElement("module"); module.toXMLElement(item); } } // ======================================================================== // inside class public static class Module implements IXMLSupporter { private String m_sUnivCode; private String m_sName; private String m_sDesc; private boolean m_bEnabled; private String m_sLicence; /** * Constructor: default */ public Module(String _sUnivCode) { m_sUnivCode = _sUnivCode; } /** * Returns the name. * * @return the name. */ public String getName() { return m_sName; } /** * Sets the name. * * @param _sName * the name to set. */ public void setName(String _sName) { m_sName = _sName; } /** * Returns the desc. * * @return the desc. */ public String getDesc() { return m_sDesc; } /** * Sets the desc. * * @param _sDesc * the desc to set. */ public void setDesc(String _sDesc) { m_sDesc = _sDesc; } /** * Returns the enabled. * * @return the enabled. */ public boolean isEnabled() { return m_bEnabled; } /** * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element) */ public void fromXML(Element _element) throws PureException { m_sName = XMLUtil.getAttributeValueTrim(_element, "name"); m_sDesc = XMLUtil.getAttributeValueTrim(_element, "desc"); m_bEnabled = XMLUtil.getAttributeValueAsBool(_element, "enabled", false); m_sLicence = XMLUtil.getAttributeValueTrim(_element, "licence"); if (m_bEnabled) { if (m_sLicence == null || m_sLicence.length() == 0) { m_bEnabled = false; } else { try { String[] args = SNDecoder.decode(m_sLicence, 2); if (!m_sName.equals(args[0]) || !m_sUnivCode.equals(args[1])) { m_bEnabled = false; } } catch (Exception ex) { m_bEnabled = false; } } } } /** * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element) */ public void toXMLElement(Element _element) throws PureException { _element.addAttribute("name", m_sName); _element.addAttribute("desc", m_sDesc); _element.addAttribute("enabled", String.valueOf(m_bEnabled)); _element.addAttribute("licence", m_sLicence); } } }