| 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.module; |
| 17 | |
| 18 | import org.qedeq.kernel.se.base.module.PredicateDefinition; |
| 19 | import org.qedeq.kernel.se.dto.module.PredicateDefinitionVo; |
| 20 | import org.qedeq.kernel.xml.common.XmlSyntaxException; |
| 21 | import org.qedeq.kernel.xml.handler.common.AbstractSimpleHandler; |
| 22 | import org.qedeq.kernel.xml.handler.common.SimpleAttributes; |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Parse a predicate definition. |
| 27 | * |
| 28 | * @author Michael Meyling |
| 29 | */ |
| 30 | public class PredicateDefinitionHandler extends AbstractSimpleHandler { |
| 31 | |
| 32 | /** Handler for formula. */ |
| 33 | private final FormulaHandler formulaHandler; |
| 34 | |
| 35 | /** Handler for rule description. */ |
| 36 | private final LatexListHandler descriptionHandler; |
| 37 | |
| 38 | /** Definition value object. */ |
| 39 | private PredicateDefinitionVo definition; |
| 40 | |
| 41 | /** LaTeX pattern for the definition. */ |
| 42 | private String latexPattern; |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Deals with definitions. |
| 47 | * |
| 48 | * @param handler Parent handler. |
| 49 | */ |
| 50 | public PredicateDefinitionHandler(final AbstractSimpleHandler handler) { |
| 51 | super(handler, "DEFINITION_PREDICATE"); |
| 52 | formulaHandler = new FormulaHandler(this); |
| 53 | descriptionHandler = new LatexListHandler(this, "DESCRIPTION"); |
| 54 | } |
| 55 | |
| 56 | public final void init() { |
| 57 | definition = null; |
| 58 | latexPattern = null; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get definition. |
| 63 | * |
| 64 | * @return Definition. |
| 65 | */ |
| 66 | public final PredicateDefinition getDefinition() { |
| 67 | return definition; |
| 68 | } |
| 69 | |
| 70 | public final void startElement(final String name, final SimpleAttributes attributes) |
| 71 | throws XmlSyntaxException { |
| 72 | if (getStartTag().equals(name)) { |
| 73 | definition = new PredicateDefinitionVo(); |
| 74 | definition.setArgumentNumber(attributes.getString("arguments")); |
| 75 | definition.setName(attributes.getString("name")); |
| 76 | } else if ("LATEXPATTERN".equals(name)) { |
| 77 | // nothing to do yet |
| 78 | } else if (formulaHandler.getStartTag().equals(name)) { |
| 79 | changeHandler(formulaHandler, name, attributes); |
| 80 | } else if (descriptionHandler.getStartTag().equals(name)) { |
| 81 | changeHandler(descriptionHandler, name, attributes); |
| 82 | } else { |
| 83 | throw XmlSyntaxException.createUnexpectedTagException(name); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public final void endElement(final String name) throws XmlSyntaxException { |
| 88 | if (getStartTag().equals(name)) { |
| 89 | // nothing to do |
| 90 | } else if ("LATEXPATTERN".equals(name)) { |
| 91 | definition.setLatexPattern(latexPattern); |
| 92 | } else if (formulaHandler.getStartTag().equals(name)) { |
| 93 | definition.setFormula(formulaHandler.getFormula()); |
| 94 | } else if (descriptionHandler.getStartTag().equals(name)) { |
| 95 | definition.setDescription(descriptionHandler.getLatexList()); |
| 96 | } else { |
| 97 | throw XmlSyntaxException.createUnexpectedTagException(name); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public final void characters(final String name, final String data) { |
| 102 | if ("LATEXPATTERN".equals(name)) { |
| 103 | latexPattern = data; |
| 104 | } else { |
| 105 | throw new RuntimeException("Unexpected character data in tag: " + name); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
| 110 | } |