⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 schoolconfig.java

📁 老外的在线考试
💻 JAVA
字号:
/* * SchoolEJB - CyberDemia's library of EJBs for educational related services. * Copyright (C) 2003 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA  02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.school;import javax.servlet.ServletContext;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.DocumentBuilder;import org.w3c.dom.*;import com.cyberdemia.xml.XMLUtil;import java.io.InputStream;import java.util.*;/** * SchoolConfig is a reader and parser for the School module configuration file. * It expects to find the <i>school-config.xml</i> configuration file in the  * deployed WAR file. *  * @version $Revision: 1.4 $ at $Date: 2004/05/27 13:49:06 $ by $Author: alexycyap $ * @author Alexander Yap */public class SchoolConfig{    /**     * Initializes the SchoolConfig. This must be called before using SchoolConfig.     * @param app ServletContext     */    public static synchronized void init(ServletContext app)    {        if (s_instance==null)        {        	s_instance = new SchoolConfig(app);        }    }    	/**	* Gets singleton instance of SchoolConfig. This method may only be called    * after calling <code>init(ServletContext)</code>.	*	* @return SchoolConfig instance.	* @see #init(ServletContext)	*/	public static SchoolConfig getInstance()	{		return s_instance;	}		/**	* Gets the top level hierarchy root node as described in the	* School module configuration file.	* @return IHierarchy instance.	*/	public IHierarchy getHierarchyRoot()	{		return m_hierarchyRoot;	}	/**	* Creates SchoolConfig and configures it by loading its configuration file.	* This is private for internal use only.	*/	private SchoolConfig(ServletContext app)	{		m_context = "Parsing "+CONFIG_RESOURCE_NAME;		try		{			parseConfig(app.getResourceAsStream(CONFIG_RESOURCE_NAME));		}		catch (RuntimeException rtEx)		{			throw rtEx;		}		catch (Exception ex)		{			throw new RuntimeException("Error parsing "+CONFIG_RESOURCE_NAME,ex);		}	}		private void parseConfig(InputStream stream) throws Exception	{		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();		DocumentBuilder db = dbf.newDocumentBuilder();		Document doc = db.parse( stream );		Element schoolConfigElem = doc.getDocumentElement();		if (schoolConfigElem==null)		{  			throw new DOMException(DOMException.NOT_FOUND_ERR, "Missing root element");		}		parseSchoolConfigElement( schoolConfigElem );				}	private void parseSchoolConfigElement(Element configElem) throws Exception	{		if (!configElem.getNodeName().equals("school-config"))		{			throw new DOMException(DOMException.SYNTAX_ERR, "Invalid element "+configElem.getNodeName()+", expecting school-config.");		}			Element templateElem = XMLUtil.getSingleElement(configElem, "template", m_context);		parseTemplateElement(templateElem);			Element hierarchyElem = XMLUtil.getSingleElement(configElem, "hierarchy", m_context);		parseHierarchyElement(hierarchyElem);	}	private void parseTemplateElement(Element templateElem) throws Exception	{		if (!templateElem.getNodeName().equals("template"))		{			throw new DOMException(DOMException.SYNTAX_ERR, "Invalid element "+templateElem.getNodeName()+", expecting template.");		}			Element[] nodeElems = XMLUtil.getChildElements(templateElem, "node", m_context);		m_maxLevel = -1;		for (int n=0; n<nodeElems.length; n++)		{			Element nodeElem = nodeElems[n];			Integer level = new Integer(nodeElem.getAttribute("level"));			m_hierarchyTemplateMap.put( level, nodeElem.getAttribute("name") );			if (level.intValue()>m_maxLevel)			{				m_maxLevel = level.intValue();			}		}	}		private void parseHierarchyElement(Element hierElem) throws Exception	{		if (!hierElem.getNodeName().equals("hierarchy"))		{			throw new DOMException(DOMException.SYNTAX_ERR, "Invalid element "+hierElem.getNodeName()+", expecting hierarchy.");		}			Element rootNodeElem = XMLUtil.getSingleElement(hierElem, "node", m_context);		m_hierarchyRoot = parseHierarchyNodeElement(rootNodeElem, null, 0);	}	private IHierarchy parseHierarchyNodeElement(Element nodeElem, IHierarchy parentNode, int level) throws Exception	{		if (!nodeElem.getNodeName().equals("node"))		{			throw new DOMException(DOMException.SYNTAX_ERR, "Invalid element "+nodeElem.getNodeName()+", expecting node.");		}			IHierarchy node = new HierarchyNode(			XMLUtil.getIntAttribute(nodeElem,"id",m_context),			XMLUtil.getStringAttribute(nodeElem, "name", m_context),			(String)m_hierarchyTemplateMap.get(new Integer(level)),			level, parentNode, level==m_maxLevel );		if (parentNode!=null)		{			parentNode.addChild(node);		}				Element[] childElems = XMLUtil.getChildElements(nodeElem,"node",m_context);		for (int c=0; c<childElems.length; c++)		{			parseHierarchyNodeElement( childElems[c], node, level+1);			}					return node;	}	private String m_context = null;	private IHierarchy m_hierarchyRoot = null;	private Map m_hierarchyTemplateMap = new HashMap();	private int m_maxLevel = -1;    private static final String CONFIG_RESOURCE_NAME = "/conf/school-config.xml";    	/** Singleton instance. */	private static SchoolConfig s_instance = null;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -