📄 schoolconfig.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.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.DocumentBuilder;import org.w3c.dom.*;import com.cyberdemia.xml.XMLUtil;import com.cyberdemia.school.impl.*;import java.net.*;import java.io.InputStream;import javax.ejb.EJBException;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 directory * specified by in the getInstance() method.* * @author Alexander Yap*/public class SchoolConfig{ /** * Initializes the SchoolConfig with a URL to the school-config.xml. * This method must be called before calling getInstance(). * * @param configURL Configuration URL pointing to school-config.xml, or null if already configured. */ public static synchronized void init(URL configURL) { s_instance = new SchoolConfig(configURL); } /** * Gets singleton instance of SchoolConfig. * This method may only be called after calling init(URL). * * @return SchoolConfig instance. * @see #init(URL) */ public static synchronized 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 using a URL to school.xml. * This is private for internal use only. * @param configDir Configuration URL pointing to school.xml. */ private SchoolConfig(URL configURL) { m_context = "Parsing "+configURL; HttpURLConnection connection = null; int responseCode; try { assert configURL!=null : "Error: Missing configuration URL."; connection = (HttpURLConnection)configURL.openConnection(); responseCode = connection.getResponseCode(); } catch (Exception ex) { throw new EJBException("Error creating SchoolConfig", ex); } if (responseCode != HttpURLConnection.HTTP_OK) throw new EJBException("Error connecting to "+configURL); try { parseConfig(connection.getInputStream()); } catch (Exception ex) { throw new EJBException("Error parsing "+configURL,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(); assert schoolConfigElem!=null : "Missing root element"; parseSchoolConfigElement( schoolConfigElem ); } private void parseSchoolConfigElement(Element configElem) throws Exception { assert configElem.getNodeName().equals("school-config") : "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 { assert templateElem.getNodeName().equals("template") : "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 { assert hierElem.getNodeName().equals("hierarchy") : "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 { assert nodeElem.getNodeName().equals("node") : "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; /** Singleton instance. */ private static SchoolConfig s_instance = null;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -