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.bo.parser; |
17 | |
18 | import java.util.ArrayList; |
19 | import java.util.List; |
20 | |
21 | import org.qedeq.base.trace.Trace; |
22 | import org.qedeq.base.utility.StringUtility; |
23 | |
24 | /** |
25 | * Parsed term. |
26 | * |
27 | * @version $Revision: 1.1 $ |
28 | * @author Michael Meyling |
29 | */ |
30 | public class Term { |
31 | |
32 | /** This class. */ |
33 | private static final Class CLASS = Term.class; |
34 | |
35 | /** Operator, can be <code>null</code>. */ |
36 | private final Operator operator; |
37 | |
38 | /** Arguments, can be <code>null</code>. */ |
39 | private final List arguments; |
40 | |
41 | /** Atom, can be <code>null</code>. */ |
42 | private final TermAtom atom; |
43 | |
44 | /** |
45 | * Constructor. |
46 | * |
47 | * @param atom Term atom. |
48 | */ |
49 | public Term(final TermAtom atom) { |
50 | this.operator = null; |
51 | this.arguments = null; |
52 | this.atom = atom; |
53 | } |
54 | |
55 | |
56 | /** |
57 | * Constructor. |
58 | * |
59 | * @param operator Construct new term for this operator. |
60 | */ |
61 | public Term(final Operator operator) { |
62 | this.operator = operator; |
63 | this.arguments = new ArrayList(); |
64 | this.atom = null; |
65 | } |
66 | |
67 | /** |
68 | * Constructor. |
69 | * |
70 | * @param operator Construct new term for this operator. |
71 | * @param firstArgument First argument of operator. |
72 | */ |
73 | public Term(final Operator operator, final Term firstArgument) { |
74 | this.operator = operator; |
75 | this.arguments = new ArrayList(); |
76 | this.atom = null; |
77 | addArgument(firstArgument); |
78 | } |
79 | |
80 | /** |
81 | * Is this term an atom? |
82 | * |
83 | * @return Is this term an atom? |
84 | */ |
85 | public final boolean isAtom() { |
86 | return atom != null; |
87 | } |
88 | |
89 | /** |
90 | * Add next argument term to operator. Overall number of arguments must |
91 | * not exceed {@link Operator#getMax()} (if <code>>= 0</code>). Addition is only possible if |
92 | * this is no atom term (see {@link #Term(TermAtom)}). |
93 | * |
94 | * @param term Add this argument at last position. |
95 | * @throws IllegalArgumentException This is an atom term or argument |
96 | * maximum exceeded. |
97 | */ |
98 | public final void addArgument(final Term term) { |
99 | if (isAtom()) { |
100 | throw new IllegalArgumentException( |
101 | "this is an atom, no arguments could be added to " + atom.getValue()); |
102 | } |
103 | if (operator.getMax() >= 0 && operator.getMax() < arguments.size() + 1) { |
104 | throw new IllegalArgumentException("operator could have maximal " |
105 | + operator.getMax() + " arguments"); |
106 | } |
107 | arguments.add(term); |
108 | } |
109 | |
110 | /** |
111 | * Get operator of term. Can be <code>null</code> if this is an atom term. |
112 | * |
113 | * @return Term operator. |
114 | */ |
115 | public final Operator getOperator() { |
116 | return operator; |
117 | } |
118 | |
119 | /** |
120 | * Get number of arguments of this operator. |
121 | * |
122 | * @return Argument number. |
123 | */ |
124 | public final int size() { |
125 | if (arguments == null) { |
126 | return 0; |
127 | } |
128 | return arguments.size(); |
129 | } |
130 | |
131 | /** |
132 | * Get QEDEQ representation of this term. |
133 | * |
134 | * @return QEDEQ representation. |
135 | */ |
136 | public final String getQedeq() { |
137 | if (isAtom()) { |
138 | return atom.getValue(); |
139 | } |
140 | final StringBuffer buffer = new StringBuffer(); |
141 | buffer.append(operator.getQedeq()).append('('); |
142 | if (operator.getQedeqArgument() != null) { |
143 | buffer.append(StringUtility.quote(operator.getQedeqArgument())); |
144 | } |
145 | for (int i = 0; i < arguments.size(); i++) { |
146 | if (i > 0 || operator.getQedeqArgument() != null) { |
147 | buffer.append(", "); |
148 | } |
149 | buffer.append(((Term) |
150 | arguments.get(i)).getQedeq()); |
151 | } |
152 | buffer.append(')'); |
153 | return buffer.toString(); |
154 | } |
155 | |
156 | /** |
157 | * Get QEDEQ XML representation of this term. |
158 | * |
159 | * @return QEDEQ XML representation. |
160 | */ |
161 | public final String getQedeqXml() { |
162 | return getQedeqXml(0); |
163 | } |
164 | |
165 | /** |
166 | * Get QEDEQ XML representation of this term. |
167 | * |
168 | * @param level Tabulation level. |
169 | * @return QEDEQ XML representation. |
170 | */ |
171 | private final String getQedeqXml(final int level) { |
172 | if (isAtom()) { |
173 | return StringUtility.getSpaces(level * 2) + atom.getValue() + "\n"; |
174 | } |
175 | final StringBuffer buffer = new StringBuffer(); |
176 | buffer.append(StringUtility.getSpaces(level * 2)); |
177 | buffer.append("<").append(operator.getQedeq()); |
178 | if (operator.getQedeq().endsWith("VAR")) { // TODO mime 20060612: ok for all QEDEQ? |
179 | buffer.append(" id=" + quote(operator.getQedeqArgument())); |
180 | if (arguments == null || arguments.size() == 0) { |
181 | buffer.append(" />" + "\n"); |
182 | return buffer.toString(); |
183 | } |
184 | } else if (operator.getQedeq().endsWith("CON")) { |
185 | buffer.append(" ref=" + quote(operator.getQedeqArgument())); |
186 | if (arguments == null || arguments.size() == 0) { |
187 | buffer.append(" />" + "\n"); |
188 | return buffer.toString(); |
189 | } |
190 | } |
191 | |
192 | buffer.append(">\n"); |
193 | if (operator.getQedeqArgument() != null && !operator.getQedeq().endsWith("VAR") |
194 | && !operator.getQedeq().endsWith("CON")) { |
195 | // no arguments expected! |
196 | Trace.fatal(CLASS, this, "getQedeqXml(int)", "operator argument is not null but: " |
197 | + operator.getQedeqArgument(), new IllegalArgumentException()); |
198 | } |
199 | for (int i = 0; i < arguments.size(); i++) { |
200 | buffer.append(((Term) |
201 | arguments.get(i)).getQedeqXml(level + 1)); |
202 | } |
203 | buffer.append(StringUtility.getSpaces(level * 2)); |
204 | buffer.append("</").append(operator.getQedeq()).append(">\n"); |
205 | return buffer.toString(); |
206 | } |
207 | |
208 | /** |
209 | * Quote attribute value. |
210 | * |
211 | * @param text Attribute text. |
212 | * @return Quoted attribute. |
213 | */ |
214 | private String quote(final String text) { |
215 | return "\"" + StringUtility.replace(text, "\"", """) + "\""; |
216 | } |
217 | |
218 | } |