InitialFunctionDefinitionHandler.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  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.InitialFunctionDefinitionVo;
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 import org.qedeq.kernel.xml.handler.list.ElementHandler;
023 
024 
025 /**
026  * Parse a function definition.
027  *
028  @author  Michael Meyling
029  */
030 public class InitialFunctionDefinitionHandler extends AbstractSimpleHandler {
031 
032     /** Handler for terms. */
033     private final ElementHandler elementHandler;
034 
035     /** Handler for rule description. */
036     private final LatexListHandler descriptionHandler;
037 
038     /** Definition value object. */
039     private InitialFunctionDefinitionVo definition;
040 
041     /** LaTeX pattern for the definition. */
042     private String latexPattern;
043 
044 
045     /**
046      * Deals with definitions.
047      *
048      @param   handler Parent handler.
049      */
050     public InitialFunctionDefinitionHandler(final AbstractSimpleHandler handler) {
051         super(handler, "DEFINITION_FUNCTION");
052         elementHandler = new ElementHandler(this);
053         descriptionHandler = new LatexListHandler(this, "DESCRIPTION");
054     }
055 
056     public final void init() {
057         definition = null;
058         latexPattern = null;
059     }
060 
061     /**
062      * Get definition.
063      *
064      @return  Definition.
065      */
066     public final InitialFunctionDefinitionVo getDefinition() {
067         return definition;
068     }
069 
070     public final void startElement(final String name, final SimpleAttributes attributes)
071             throws XmlSyntaxException {
072         if (getStartTag().equals(name)) {
073             definition = new InitialFunctionDefinitionVo();
074             definition.setArgumentNumber(attributes.getString("arguments"));
075             definition.setName(attributes.getString("name"));
076         else if ("LATEXPATTERN".equals(name)) {
077             // nothing to do yet
078         else if ("FUNCON".equals(name)) {
079             changeHandler(elementHandler, name, attributes);
080         else if (descriptionHandler.getStartTag().equals(name)) {
081             changeHandler(descriptionHandler, name, attributes);
082         else {
083             throw XmlSyntaxException.createUnexpectedTagException(name);
084         }
085     }
086 
087     public final void endElement(final String namethrows XmlSyntaxException {
088         if (getStartTag().equals(name)) {
089             // nothing to do
090         else if ("LATEXPATTERN".equals(name)) {
091             definition.setLatexPattern(latexPattern);
092         else if ("FUNCON".equals(name)) {
093             definition.setFunCon(elementHandler.getElement());
094         else if (descriptionHandler.getStartTag().equals(name)) {
095             definition.setDescription(descriptionHandler.getLatexList());
096         else {
097             throw XmlSyntaxException.createUnexpectedTagException(name);
098         }
099     }
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 }