📄 operatorservice.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.tools;
import edu.udo.cs.yale.Yale;
import edu.udo.cs.yale.tools.XMLException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
/** This class reads the description of the YALE operators. These descriptions are entries in a XML File like: <br>
* <code>
* <operators><br>
* <operator ><br>
* name="longOperatorName" <br>
* short="shortOperatorName" <br>
* path="java.classpath.operator" <br>
* /><br>
* </code><br>
*
* @version $Id: OperatorService.java,v 2.9 2004/08/27 11:57:45 ingomierswa Exp $
*/
public class OperatorService {
/** Map for class names <-> classes. */
private static Map classnameMap = new HashMap();
/** Map for classes <-> names. */
private static Map nameMap = new HashMap();
/** Map for names <-> descriptions. */
private static Map operatorDescriptions = new HashMap();
/** Map for group name <-> group (list). */
private static GroupTree groupTree = new GroupTree("");
public static void registerOperators(String name,
InputStream operatorsXML,
ClassLoader classLoader) {
if (classLoader == null) classLoader = OperatorService.class.getClassLoader();
LogService.logMessage("Loading operators from '"+name+"'.", LogService.INIT);
try {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(operatorsXML);
if (!document.getDocumentElement().getTagName().toLowerCase().equals("operators")) {
LogService.logMessage("Operator description file '"+name+"': outermost operator must be <operators>!",
LogService.ERROR);
return;
}
NodeList operatorTags = document.getDocumentElement().getElementsByTagName("operator");
for (int i = 0; i < operatorTags.getLength(); i++) {
registerOperator((Element)operatorTags.item(i), classLoader);
}
} catch (Throwable e) {
LogService.logException("Cannot read operator description file '"+name+"'", e);
}
}
private static void registerOperator(Element operatorTag, ClassLoader classLoader) {
Attr nameAttr = operatorTag.getAttributeNode("name");
Attr classAttr = operatorTag.getAttributeNode("class");
if (nameAttr == null) LogService.logMessage("Missing name for <operator> tag", LogService.ERROR);
if (classAttr == null) LogService.logMessage("Missing class for <operator> tag", LogService.ERROR);
try {
OperatorDescription description = new OperatorDescription(classLoader,
nameAttr.getValue(),
classAttr.getValue(),
operatorTag.getAttribute("description"),
operatorTag.getAttribute("group"),
operatorTag.getAttribute("icon"));
classnameMap.put(description.getName(), description.getOperatorClass());
nameMap.put(description.getOperatorClass(), description.getName());
operatorDescriptions.put(description.getName(), description);
String groupString = description.getGroup();
String[] groupNames = groupString.split("\\.");
GroupTree currentGroup = groupTree;
for (int j = 0; j < groupNames.length; j++) {
GroupTree subGroup = currentGroup.getSubGroup(groupNames[j]);
if (subGroup == null) {
subGroup = new GroupTree(groupNames[j]);
currentGroup.addSubGroup(subGroup);
}
currentGroup = subGroup;
}
currentGroup.addOperator(description.getName());
} catch (ClassNotFoundException e) {
LogService.logMessage("Class '" + classAttr.getValue() + "' not found!", LogService.ERROR);
} catch (Throwable t) {
LogService.logException("Cannot register '" + classAttr.getValue() + "'!", t);
}
}
public static OperatorDescription getOperatorDescription(String name) {
return (OperatorDescription)operatorDescriptions.get(name);
}
public static OperatorDescription getOperatorDescription(Class clazz) {
return (OperatorDescription)operatorDescriptions.get(getOperatorClassName(clazz));
}
public static Class getOperatorClass(String name) {
return (Class)classnameMap.get(name);
}
public static String getOperatorClassName(Class operatorClass) {
return (String)nameMap.get(operatorClass);
}
public static Set getOperatorNames() {
return classnameMap.keySet();
}
public static Set getOperatorClasses() {
return nameMap.keySet();
}
public static GroupTree getGroups() {
return groupTree;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -