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