001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.kernel.xml.handler.parser;
017
018 import java.io.File;
019 import java.util.List;
020
021 import javax.xml.parsers.ParserConfigurationException;
022
023 import org.qedeq.base.trace.Trace;
024 import org.qedeq.kernel.bo.module.InternalKernelServices;
025 import org.qedeq.kernel.se.common.Plugin;
026 import org.qedeq.kernel.se.common.SourceFileExceptionList;
027 import org.qedeq.kernel.xml.handler.common.SaxDefaultHandler;
028 import org.qedeq.kernel.xml.parser.SaxParser;
029 import org.xml.sax.SAXException;
030 import org.xml.sax.SAXParseException;
031
032
033 /**
034 * Load operator list from an XML file.
035 *
036 * @author Michael Meyling
037 */
038 public final class LoadXmlOperatorListUtility implements Plugin {
039
040 /** This class. */
041 private static final Class CLASS = LoadXmlOperatorListUtility.class;
042
043 /**
044 * Constructor.
045 */
046 private LoadXmlOperatorListUtility() {
047 // nothing to do
048 }
049
050 /**
051 * Get operator list out of XML file.
052 *
053 * @param services Kernel services. TODO m31 20100830: is this really necessary?
054 * @param from Read this XML file.
055 * @return Operator list.
056 * @throws SourceFileExceptionList Loading failed.
057 */
058 public static List getOperatorList(final InternalKernelServices services, final File from)
059 throws SourceFileExceptionList {
060 final String method = "List getOperatorList(String)";
061 final LoadXmlOperatorListUtility util = new LoadXmlOperatorListUtility();
062 try {
063 Trace.begin(CLASS, method);
064 Trace.param(CLASS, method, "from", from);
065 SaxDefaultHandler handler = new SaxDefaultHandler(util);
066 ParserHandler simple = new ParserHandler(handler);
067 handler.setBasisDocumentHandler(simple);
068 SaxParser parser = new SaxParser(util, handler);
069 parser.parse(from, null);
070 return simple.getOperators();
071 } catch (RuntimeException e) {
072 Trace.fatal(CLASS, "Programming error.", method, e);
073 throw services.createSourceFileExceptionList(
074 ParserErrors.PARSER_PROGRAMMING_ERROR_CODE,
075 ParserErrors.PARSER_PROGRAMMING_ERROR_TEXT,
076 "" + from, e);
077 } catch (ParserConfigurationException e) {
078 Trace.fatal(CLASS, "Parser configuration error.", method, e);
079 throw services.createSourceFileExceptionList(
080 ParserErrors.PARSER_CONFIGURATION_ERROR_CODE,
081 ParserErrors.PARSER_CONFIGURATION_ERROR_TEXT,
082 "" + from, e);
083 } catch (final SAXParseException e) {
084 Trace.fatal(CLASS, "Configuration error, file corrupt: " + from, method, e);
085 throw services.createSourceFileExceptionList(
086 ParserErrors.XML_FILE_PARSING_FAILED_CODE,
087 ParserErrors.XML_FILE_PARSING_FAILED_TEXT,
088 "" + from, e);
089 } catch (SAXException e) {
090 Trace.fatal(CLASS, "Configuration error, file corrupt: " + from, method, e);
091 throw services.createSourceFileExceptionList(
092 ParserErrors.XML_FILE_PARSING_FAILED_CODE,
093 ParserErrors.XML_FILE_PARSING_FAILED_TEXT,
094 "" + from, e);
095 } catch (javax.xml.parsers.FactoryConfigurationError e) {
096 Trace.trace(CLASS, method, e);
097 throw services.createSourceFileExceptionList(
098 ParserErrors.PARSER_FACTORY_CONFIGURATION_CODE,
099 ParserErrors.PARSER_FACTORY_CONFIGURATION_TEXT,
100 "" + from, new RuntimeException(
101 ParserErrors.PARSER_FACTORY_CONFIGURATION_TEXT, e));
102 } finally {
103 Trace.end(CLASS, method);
104 }
105 }
106
107 public String getPluginId() {
108 return CLASS.getName();
109 }
110
111 public String getPluginActionName() {
112 return "Operator Loader";
113 }
114
115 public String getPluginDescription() {
116 return "loads XML descriptoin of mathematical operators";
117 }
118
119 }
|