FunctionDefinitionHandler.java
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.module;
017 
018 import org.qedeq.kernel.se.dto.module.FunctionDefinitionVo;
019 import org.qedeq.kernel.xml.common.XmlSyntaxException;
020 import org.qedeq.kernel.xml.handler.common.AbstractSimpleHandler;
021 import org.qedeq.kernel.xml.handler.common.SimpleAttributes;
022 
023 
024 /**
025  * Parse a function definition.
026  *
027  @author  Michael Meyling
028  */
029 public class FunctionDefinitionHandler extends AbstractSimpleHandler {
030 
031     /** Handler for formulas. */
032     private final FormulaHandler formulaHandler;
033 
034     /** Handler for rule description. */
035     private final LatexListHandler descriptionHandler;
036 
037     /** Definition value object. */
038     private FunctionDefinitionVo definition;
039 
040     /** LaTeX pattern for the definition. */
041     private String latexPattern;
042 
043 
044     /**
045      * Deals with definitions.
046      *
047      @param   handler Parent handler.
048      */
049     public FunctionDefinitionHandler(final AbstractSimpleHandler handler) {
050         super(handler, "DEFINITION_FUNCTION");
051         formulaHandler = new FormulaHandler(this);
052         descriptionHandler = new LatexListHandler(this, "DESCRIPTION");
053     }
054 
055     public final void init() {
056         definition = null;
057         latexPattern = null;
058     }
059 
060     /**
061      * Get definition.
062      *
063      @return  Definition.
064      */
065     public final FunctionDefinitionVo getDefinition() {
066         return definition;
067     }
068 
069     public final void startElement(final String name, final SimpleAttributes attributes)
070             throws XmlSyntaxException {
071         if (getStartTag().equals(name)) {
072             definition = new FunctionDefinitionVo();
073             definition.setArgumentNumber(attributes.getString("arguments"));
074             definition.setName(attributes.getString("name"));
075         else if ("LATEXPATTERN".equals(name)) {
076             // nothing to do yet
077         else if (formulaHandler.getStartTag().equals(name)) {
078             changeHandler(formulaHandler, name, attributes);
079         else if (descriptionHandler.getStartTag().equals(name)) {
080             changeHandler(descriptionHandler, name, attributes);
081         else {
082             throw XmlSyntaxException.createUnexpectedTagException(name);
083         }
084     }
085 
086     public final void endElement(final String namethrows XmlSyntaxException {
087         if (getStartTag().equals(name)) {
088             // nothing to do
089         else if ("LATEXPATTERN".equals(name)) {
090             definition.setLatexPattern(latexPattern);
091         else if (formulaHandler.getStartTag().equals(name)) {
092             definition.setFormula(formulaHandler.getFormula());
093         else if (descriptionHandler.getStartTag().equals(name)) {
094             definition.setDescription(descriptionHandler.getLatexList());
095         else {
096             throw XmlSyntaxException.createUnexpectedTagException(name);
097         }
098     }
099 
100     public final void characters(final String name, final String data) {
101         if ("LATEXPATTERN".equals(name)) {
102             latexPattern = data;
103         else {
104             throw new RuntimeException("Unexpected character data in tag: " + name);
105         }
106     }
107 
108 }