RuleHandler.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.base.module.Rule;
019 import org.qedeq.kernel.se.dto.module.RuleVo;
020 import org.qedeq.kernel.xml.common.XmlSyntaxException;
021 import org.qedeq.kernel.xml.handler.common.AbstractSimpleHandler;
022 import org.qedeq.kernel.xml.handler.common.SimpleAttributes;
023 
024 
025 /**
026  * Parse a rule.
027  *
028  @author  Michael Meyling
029  */
030 public class RuleHandler extends AbstractSimpleHandler {
031 
032     /** Handler for rule description. */
033     private final LatexListHandler descriptionHandler;
034 
035     /** Handle proofs. */
036     private final ProofHandler proofHandler;
037 
038     /** Rule value object. */
039     private RuleVo rule;
040 
041     /**
042      * Deals with definitions.
043      *
044      @param   handler Parent handler.
045      */
046     public RuleHandler(final AbstractSimpleHandler handler) {
047         super(handler, "RULE");
048         descriptionHandler = new LatexListHandler(this, "DESCRIPTION");
049         proofHandler = new ProofHandler(this);
050     }
051 
052     public final void init() {
053         rule = null;
054     }
055 
056     /**
057      * Get Rule.
058      *
059      @return  Rule.
060      */
061     public final Rule getRule() {
062         return rule;
063     }
064 
065     public final void startElement(final String name, final SimpleAttributes attributes)
066             throws XmlSyntaxException {
067         if (getStartTag().equals(name)) {
068             rule = new RuleVo();
069             if (null != attributes.getString("name")) {
070                 rule.setName(attributes.getString("name"));
071             else {
072                 throw XmlSyntaxException.createMissingAttributeException(name, "name");
073             }
074         else if ("LINK".equals(name)) {
075             if (null != attributes.getString("id")) {
076                 rule.addLink(attributes.getString("id"));
077             else {
078                 throw XmlSyntaxException.createMissingAttributeException(name, "id");
079             }
080         else if (descriptionHandler.getStartTag().equals(name)) {
081             changeHandler(descriptionHandler, name, attributes);
082         else if (proofHandler.getStartTag().equals(name)) {
083             changeHandler(proofHandler, name, attributes);
084         else {
085             throw XmlSyntaxException.createUnexpectedTagException(name);
086         }
087     }
088 
089     public final void endElement(final String namethrows XmlSyntaxException {
090         if (getStartTag().equals(name)) {
091             // nothing to do
092         else if ("LINK".equals(name)) {
093             // nothing to do
094         else if (descriptionHandler.getStartTag().equals(name)) {
095             rule.setDescription(descriptionHandler.getLatexList());
096         else if (proofHandler.getStartTag().equals(name)) {
097             rule.addProof(proofHandler.getProof());
098         else {
099             throw XmlSyntaxException.createUnexpectedTagException(name);
100         }
101     }
102 
103 
104 }