| 1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
| 2 | * |
| 3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
| 4 | * |
| 5 | * "Hilbert II" is free software; you can redistribute |
| 6 | * it and/or modify it under the terms of the GNU General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | package org.qedeq.kernel.xml.handler.parser; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import javax.xml.parsers.ParserConfigurationException; |
| 22 | |
| 23 | import org.qedeq.base.trace.Trace; |
| 24 | import org.qedeq.kernel.bo.module.InternalKernelServices; |
| 25 | import org.qedeq.kernel.se.common.ModuleService; |
| 26 | import org.qedeq.kernel.se.common.SourceFileExceptionList; |
| 27 | import org.qedeq.kernel.xml.handler.common.SaxDefaultHandler; |
| 28 | import org.qedeq.kernel.xml.parser.SaxParser; |
| 29 | import org.xml.sax.SAXException; |
| 30 | import org.xml.sax.SAXParseException; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Load operator list from an XML file. |
| 35 | * |
| 36 | * @author Michael Meyling |
| 37 | */ |
| 38 | public final class LoadXmlOperatorListUtility implements ModuleService { |
| 39 | |
| 40 | /** This class. */ |
| 41 | private static final Class CLASS = LoadXmlOperatorListUtility.class; |
| 42 | |
| 43 | /** |
| 44 | * Constructor. |
| 45 | */ |
| 46 | private LoadXmlOperatorListUtility() { |
| 47 | // nothing to do |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get operator list out of XML file. |
| 52 | * |
| 53 | * @param services Kernel services. TODO m31 20100830: is this really necessary? |
| 54 | * @param from Read this XML file. |
| 55 | * @return Operator list. |
| 56 | * @throws SourceFileExceptionList Loading failed. |
| 57 | */ |
| 58 | public static List getOperatorList(final InternalKernelServices services, final File from) |
| 59 | throws SourceFileExceptionList { |
| 60 | final String method = "List getOperatorList(String)"; |
| 61 | final LoadXmlOperatorListUtility util = new LoadXmlOperatorListUtility(); |
| 62 | try { |
| 63 | Trace.begin(CLASS, method); |
| 64 | Trace.param(CLASS, method, "from", from); |
| 65 | SaxDefaultHandler handler = new SaxDefaultHandler(util); |
| 66 | ParserHandler simple = new ParserHandler(handler); |
| 67 | handler.setBasisDocumentHandler(simple); |
| 68 | SaxParser parser = new SaxParser(util, handler); |
| 69 | parser.parse(from, null); |
| 70 | return simple.getOperators(); |
| 71 | } catch (RuntimeException e) { |
| 72 | Trace.fatal(CLASS, "Programming error.", method, e); |
| 73 | throw services.createSourceFileExceptionList( |
| 74 | ParserErrors.PARSER_PROGRAMMING_ERROR_CODE, |
| 75 | ParserErrors.PARSER_PROGRAMMING_ERROR_TEXT, |
| 76 | "" + from, e); |
| 77 | } catch (ParserConfigurationException e) { |
| 78 | Trace.fatal(CLASS, "Parser configuration error.", method, e); |
| 79 | throw services.createSourceFileExceptionList( |
| 80 | ParserErrors.PARSER_CONFIGURATION_ERROR_CODE, |
| 81 | ParserErrors.PARSER_CONFIGURATION_ERROR_TEXT, |
| 82 | "" + from, e); |
| 83 | } catch (final SAXParseException e) { |
| 84 | Trace.fatal(CLASS, "Configuration error, file corrupt: " + from, method, e); |
| 85 | throw services.createSourceFileExceptionList( |
| 86 | ParserErrors.XML_FILE_PARSING_FAILED_CODE, |
| 87 | ParserErrors.XML_FILE_PARSING_FAILED_TEXT, |
| 88 | "" + from, e); |
| 89 | } catch (SAXException e) { |
| 90 | Trace.fatal(CLASS, "Configuration error, file corrupt: " + from, method, e); |
| 91 | throw services.createSourceFileExceptionList( |
| 92 | ParserErrors.XML_FILE_PARSING_FAILED_CODE, |
| 93 | ParserErrors.XML_FILE_PARSING_FAILED_TEXT, |
| 94 | "" + from, e); |
| 95 | } catch (javax.xml.parsers.FactoryConfigurationError e) { |
| 96 | Trace.trace(CLASS, method, e); |
| 97 | throw services.createSourceFileExceptionList( |
| 98 | ParserErrors.PARSER_FACTORY_CONFIGURATION_CODE, |
| 99 | 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 getServiceId() { |
| 108 | return CLASS.getName(); |
| 109 | } |
| 110 | |
| 111 | public String getServiceAction() { |
| 112 | return "Operator Loader"; |
| 113 | } |
| 114 | |
| 115 | public String getServiceDescription() { |
| 116 | return "loads XML descriptoin of mathematical operators"; |
| 117 | } |
| 118 | |
| 119 | } |