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.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 /** Handle rule changes. */
039 private final ChangedRuleHandler changedRuleHandler;
040
041 /** Rule value object. */
042 private RuleVo rule;
043
044 /**
045 * Deals with definitions.
046 *
047 * @param handler Parent handler.
048 */
049 public RuleHandler(final AbstractSimpleHandler handler) {
050 super(handler, "RULE");
051 descriptionHandler = new LatexListHandler(this, "DESCRIPTION");
052 changedRuleHandler = new ChangedRuleHandler(this);
053 proofHandler = new ProofHandler(this);
054 }
055
056 public final void init() {
057 rule = null;
058 }
059
060 /**
061 * Get Rule.
062 *
063 * @return Rule.
064 */
065 public final Rule getRule() {
066 return rule;
067 }
068
069 public final void startElement(final String name, final SimpleAttributes attributes)
070 throws XmlSyntaxException {
071 if (getStartTag().equals(name)) {
072 rule = new RuleVo();
073 if (null != attributes.getString("name")) {
074 rule.setName(attributes.getString("name"));
075 } else {
076 throw XmlSyntaxException.createMissingAttributeException(name, "name");
077 }
078 if (null != attributes.getString("version")) {
079 rule.setVersion(attributes.getString("version"));
080 } else {
081 throw XmlSyntaxException.createMissingAttributeException(name, "version");
082 }
083 } else if ("LINK".equals(name)) {
084 if (null != attributes.getString("id")) {
085 rule.addLink(attributes.getString("id"));
086 } else {
087 throw XmlSyntaxException.createMissingAttributeException(name, "id");
088 }
089 } else if (descriptionHandler.getStartTag().equals(name)) {
090 changeHandler(descriptionHandler, name, attributes);
091 } else if (proofHandler.getStartTag().equals(name)) {
092 changeHandler(proofHandler, name, attributes);
093 } else if (changedRuleHandler.getStartTag().equals(name)) {
094 changeHandler(changedRuleHandler, name, attributes);
095 } else {
096 throw XmlSyntaxException.createUnexpectedTagException(name);
097 }
098 }
099
100 public final void endElement(final String name) throws XmlSyntaxException {
101 if (getStartTag().equals(name)) {
102 // nothing to do
103 } else if ("LINK".equals(name)) {
104 // nothing to do
105 } else if (descriptionHandler.getStartTag().equals(name)) {
106 rule.setDescription(descriptionHandler.getLatexList());
107 } else if (proofHandler.getStartTag().equals(name)) {
108 rule.addProof(proofHandler.getProof());
109 } else if (changedRuleHandler.getStartTag().equals(name)) {
110 rule.addChangedRule(changedRuleHandler.getChangedRule());
111 } else {
112 throw XmlSyntaxException.createUnexpectedTagException(name);
113 }
114 }
115
116
117 }
|