Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../img/srcFileCovDistChart5.png 87% of files have more coverage
57   218   31   5.18
28   109   0.54   11
11     2.82  
1    
 
  Term       Line # 30 57 31 41.7% 0.41666666
 
  (2)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, 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  913 toggle public Term(final TermAtom atom) {
50  913 this.operator = null;
51  913 this.arguments = null;
52  913 this.atom = atom;
53    }
54   
55   
56    /**
57    * Constructor.
58    *
59    * @param operator Construct new term for this operator.
60    */
 
61  443 toggle public Term(final Operator operator) {
62  443 this.operator = operator;
63  443 this.arguments = new ArrayList();
64  443 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  586 toggle public Term(final Operator operator, final Term firstArgument) {
74  586 this.operator = operator;
75  586 this.arguments = new ArrayList();
76  586 this.atom = null;
77  586 addArgument(firstArgument);
78    }
79   
80    /**
81    * Is this term an atom?
82    *
83    * @return Is this term an atom?
84    */
 
85  21726 toggle public final boolean isAtom() {
86  21726 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  1706 toggle public final void addArgument(final Term term) {
99  1706 if (isAtom()) {
100  0 throw new IllegalArgumentException(
101    "this is an atom, no arguments could be added to " + atom.getValue());
102    }
103  1706 if (operator.getMax() >= 0 && operator.getMax() < arguments.size() + 1) {
104  0 throw new IllegalArgumentException("operator could have maximal "
105    + operator.getMax() + " arguments");
106    }
107  1706 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  0 toggle public final Operator getOperator() {
116  0 return operator;
117    }
118   
119    /**
120    * Get number of arguments of this operator.
121    *
122    * @return Argument number.
123    */
 
124  0 toggle public final int size() {
125  0 if (arguments == null) {
126  0 return 0;
127    }
128  0 return arguments.size();
129    }
130   
131    /**
132    * Get QEDEQ representation of this term.
133    *
134    * @return QEDEQ representation.
135    */
 
136  20020 toggle public final String getQedeq() {
137  20020 if (isAtom()) {
138  10577 return atom.getValue();
139    }
140  9443 final StringBuffer buffer = new StringBuffer();
141  9443 buffer.append(operator.getQedeq()).append('(');
142  9443 if (operator.getQedeqArgument() != null) {
143  0 buffer.append(StringUtility.quote(operator.getQedeqArgument()));
144    }
145  24249 for (int i = 0; i < arguments.size(); i++) {
146  14806 if (i > 0 || operator.getQedeqArgument() != null) {
147  6149 buffer.append(", ");
148    }
149  14806 buffer.append(((Term)
150    arguments.get(i)).getQedeq());
151    }
152  9443 buffer.append(')');
153  9443 return buffer.toString();
154    }
155   
156    /**
157    * Get QEDEQ XML representation of this term.
158    *
159    * @return QEDEQ XML representation.
160    */
 
161  0 toggle public final String getQedeqXml() {
162  0 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  0 toggle private final String getQedeqXml(final int level) {
172  0 if (isAtom()) {
173  0 return StringUtility.getSpaces(level * 2) + atom.getValue() + "\n";
174    }
175  0 final StringBuffer buffer = new StringBuffer();
176  0 buffer.append(StringUtility.getSpaces(level * 2));
177  0 buffer.append("<").append(operator.getQedeq());
178  0 if (operator.getQedeq().endsWith("VAR")) { // TODO mime 20060612: ok for all QEDEQ?
179  0 buffer.append(" id=" + quote(operator.getQedeqArgument()));
180  0 if (arguments == null || arguments.size() == 0) {
181  0 buffer.append(" />" + "\n");
182  0 return buffer.toString();
183    }
184  0 } else if (operator.getQedeq().endsWith("CON")) {
185  0 buffer.append(" ref=" + quote(operator.getQedeqArgument()));
186  0 if (arguments == null || arguments.size() == 0) {
187  0 buffer.append(" />" + "\n");
188  0 return buffer.toString();
189    }
190    }
191   
192  0 buffer.append(">\n");
193  0 if (operator.getQedeqArgument() != null && !operator.getQedeq().endsWith("VAR")
194    && !operator.getQedeq().endsWith("CON")) {
195    // no arguments expected!
196  0 Trace.fatal(CLASS, this, "getQedeqXml(int)", "operator argument is not null but: "
197    + operator.getQedeqArgument(), new IllegalArgumentException());
198    }
199  0 for (int i = 0; i < arguments.size(); i++) {
200  0 buffer.append(((Term)
201    arguments.get(i)).getQedeqXml(level + 1));
202    }
203  0 buffer.append(StringUtility.getSpaces(level * 2));
204  0 buffer.append("</").append(operator.getQedeq()).append(">\n");
205  0 return buffer.toString();
206    }
207   
208    /**
209    * Quote attribute value.
210    *
211    * @param text Attribute text.
212    * @return Quoted attribute.
213    */
 
214  0 toggle private String quote(final String text) {
215  0 return "\"" + StringUtility.replace(text, "\"", "&quot;") + "\"";
216    }
217   
218    }