Debian Codegraph @

src/dr/xml/ElementRule.java

raw symbols SCIP
1 /*
2 * ElementRule.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.xml;
27
28 import java.util.Collections;
29 import java.util.Set;
30 import java.util.TreeSet;
31
32 /**
33 * A syntax rule to ensure that exactly one of the given element
34 * appears as a child.
35 *
36 * @version $Id: ElementRule.java,v 1.22 2005/05/24 20:26:01 rambaut Exp $
37 *
38 * @author Alexei Drummond
39 * @author Andrew Rambaut
40 */
41 public class ElementRule implements XMLSyntaxRule {
42
43 /**
44 * Creates a required element rule.
45 * @param type Class
46 */
47 public ElementRule(Class type) {
48 this(type, null, null, 1, 1);
49 }
50
51 /**
52 * Creates a required element rule.
53 * @param type Class
54 * @param optional boolean
55 */
56 public ElementRule(Class type, boolean optional) {
57 this(type, null, null, (optional ? 0 : 1), 1);
58 }
59
60 /**
61 * Creates a required element rule.
62 * @param type Class
63 * @param description String
64 */
65 public ElementRule(Class type, String description) {
66 this(type, description, null, 1, 1);
67 }
68
69 /**
70 * Creates a required element rule.
71 * @param type Class
72 * @param min int
73 * @param max int
74 */
75 public ElementRule(Class type, int min, int max) {
76 this(type, null, null, min, max);
77 }
78
79 /**
80 * Creates a required element rule.
81 * @param type Class
82 * @param description String
83 * @param example String
84 */
85 public ElementRule(Class type, String description, String example) {
86 this(type, description, example, 1, 1);
87 }
88
89 /**
90 * Creates a required element rule.
91 * @param type Class
92 * @param description String
93 * @param min int
94 * @param max int
95 */
96 public ElementRule(Class type, String description, int min, int max) {
97 this(type, description, null, min, max);
98 }
99
100 /**
101 * Creates a required element rule.
102 */
103 public ElementRule(Class type, String description, String example, int min, int max) {
104 if (type == null) throw new IllegalArgumentException("Class cannot be null!");
105 this.c = type;
106 this.min = min;
107 this.max = max;
108 this.description = description;
109 this.example = example;
110 }
111
112
113
114 /**
115 * Creates a required element rule.
116 */
117 public ElementRule(String name, Class type) {
118 this.name = name;
119 this.rules = new XMLSyntaxRule[] { new ElementRule(type)};
120 }
121
122 /**
123 * Creates a required element rule.
124 */
125 public ElementRule(String name, Class type, String description) {
126 this.name = name;
127 this.description = description;
128 this.rules = new XMLSyntaxRule[] { new ElementRule(type)};
129 }
130
131 /**
132 * Creates a required element rule.
133 */
134 public ElementRule(String name, Class type, String description, boolean optional) {
135 this.name = name;
136 this.description = description;
137 this.rules = new XMLSyntaxRule[] { new ElementRule(type)};
138 this.min = 1;
139 this.max = 1;
140 if (optional) this.min = 0;
141 }
142
143 /**
144 * Creates a required element rule.
145 */
146 public ElementRule(String name, Class type, String description, int min, int max) {
147 this.name = name;
148 this.description = description;
149 this.rules = new XMLSyntaxRule[] { new ElementRule(type)};
150 this.min = min;
151 this.max = max;
152 }
153
154 /**
155 * Creates a required element rule.
156 */
157 public ElementRule(String name, XMLSyntaxRule[] rules) {
158 this.name = name;
159 this.rules = rules;
160 }
161
162 /**
163 * Creates an element rule.
164 */
165 public ElementRule(String name, XMLSyntaxRule[] rules, boolean optional) {
166 this.name = name;
167 this.rules = rules;
168 this.min = 1;
169 this.max = 1;
170 if (optional) this.min = 0;
171 }
172
173 /**
174 * Creates an element rule.
175 */
176 public ElementRule(String name, XMLSyntaxRule[] rules, int min, int max) {
177 this.name = name;
178 this.rules = rules;
179 this.min = min;
180 this.max = max;
181 }
182
183 /**
184 * Creates a required element rule.
185 * @param name String
186 * @param rules XMLSyntaxRule[]
187 * @param description String
188 */
189 public ElementRule(String name, XMLSyntaxRule[] rules, String description) {
190 this.name = name;
191 this.rules = rules;
192 this.description = description;
193 }
194
195 public ElementRule(String name, XMLSyntaxRule[] rules, String description, boolean optional) {
196 this.name = name;
197 this.rules = rules;
198 this.description = description;
199 this.min = 1;
200 this.max = 1;
201 if (optional) this.min = 0;
202 }
203
204 /**
205 * Creates an element rule.
206 * @param name String
207 * @param rules XMLSyntaxRule[]
208 * @param description String
209 * @param min int
210 * @param max int
211 */
212 public ElementRule(String name, XMLSyntaxRule[] rules, String description, int min, int max) {
213 this.name = name;
214 this.rules = rules;
215 this.description = description;
216 this.min = min;
217 this.max = max;
218 }
219
220 public Class getElementClass() { return c; }
221
222 public String getDescription() {
223 return description;
224 }
225
226 public boolean hasDescription() { return description != null; }
227
228 public String getExample() {
229 return example;
230 }
231
232 public boolean hasExample() { return example != null; }
233
234
235 /**
236 * @return true if the required attribute of the correct type is present.
237 */
238 public boolean isSatisfied(XMLObject xo) {
239
240 // first check if no matches and its optional
241 int nameCount = 0;
242 for (int i = 0; i < xo.getChildCount(); i++) {
243 Object xoc = xo.getChild(i);
244 if (xoc instanceof XMLObject && ((XMLObject)xoc).getName().equals(name)) {
245 nameCount += 1;
246 }
247 }
248
249 if (min == 0 && nameCount == 0) return true;
250
251 // if !optional or nameCount > 0 then check if exactly one match exists
252 int matchCount = 0;
253 for (int i = 0; i < xo.getChildCount(); i++) {
254 Object xoc = xo.getChild(i);
255 if (isCompatible(xoc)) {
256 matchCount += 1;
257 }
258 }
259
260 return (matchCount >= min && matchCount <= max);
261 }
262
263 public boolean containsAttribute(String name) {
264 return false;
265 }
266
267 /**
268 * @return a string describing the rule.
269 */
270 public String ruleString() {
271 String howMany;
272 if( min == 1 && max == 1 ) {
273 howMany = "Exactly one";
274 } else if (min == max) {
275 howMany = "Exactly " + min;
276 } else if( (min <= 1) && max == Integer.MAX_VALUE ) {
277 howMany = "Any number of";
278 } else {
279 howMany = "between " + min + " and " + max;
280 }
281
282 if (c != null) {
283 return howMany + " ELEMENT of type " + getTypeName() + " REQUIRED";
284 } else {
285 StringBuffer buffer = new StringBuffer(howMany + " ELEMENT of name " + name + " REQUIRED containing");
286 for (XMLSyntaxRule rule : rules) {
287 buffer.append("\n ").append(rule.ruleString());
288 }
289 return buffer.toString();
290 }
291 }
292
293 /**
294 * @return a string describing the rule.
295 */
296 public String htmlRuleString(XMLDocumentationHandler handler) {
297 if (c != null) {
298 String html = "<div class=\"" + (min == 0 ? "optional" : "required") + "rule\">" + handler.getHTMLForClass(c);
299 if (max > 1) {
300 html += " elements (";
301 if (min == 0) {
302 html += "zero";
303 } else if (min == 1) {
304 html += "one";
305 } else if (min == max) {
306 html += "exactly " + min;
307 }
308 if (max != min) {
309 if (max < Integer.MAX_VALUE) {
310 html += " to " + max;
311 } else {
312 html += " or more";
313 }
314 }
315 } else {
316 html += " element (";
317 if (min == 0) {
318 html += "zero or one";
319 } else {
320 html += "exactly one";
321 }
322 }
323 html += ")";
324
325 if (hasDescription()) {
326 html += "<div class=\"description\">" + getDescription() + "</div>\n";
327 }
328 return html + "</div>\n";
329
330 } else {
331 StringBuffer buffer = new StringBuffer("<div class=\"" + (min == 0 ? "optional" : "required") + "compoundrule\">Element named <span class=\"elemname\">" + name + "</span> containing:");
332 for (XMLSyntaxRule rule : rules) {
333 buffer.append(rule.htmlRuleString(handler));
334 }
335 if (hasDescription()) {
336 buffer.append("<div class=\"description\">").append(getDescription()).append("</div>\n");
337 }
338 buffer.append("</div>\n");
339 return buffer.toString();
340 }
341 }
342
343 public String wikiRuleString(XMLDocumentationHandler handler, String prefix) {
344 if (c != null) {
345 String wiki = prefix + handler.getHTMLForClass(c);
346 if (max > 1) {
347 wiki += " elements (";
348 if (min == 0) {
349 wiki += "zero";
350 } else if (min == 1) {
351 wiki += "one";
352 } else if (min == max) {
353 wiki += "exactly " + min;
354 }
355 if (max != min) {
356 if (max < Integer.MAX_VALUE) {
357 wiki += " to " + max;
358 } else {
359 wiki += " or more";
360 }
361 }
362 } else {
363 wiki += " element (";
364 if (min == 0) {
365 wiki += "zero or one";
366 } else {
367 wiki += "exactly one";
368 }
369 }
370 wiki += ")\n";
371
372 if (hasDescription()) {
373 wiki += prefix + ":''" + getDescription() + "''\n";
374 } else {
375 wiki += prefix + ":\n";
376 }
377 return wiki;
378
379 } else {
380 StringBuffer buffer = new StringBuffer(prefix + "Element named <code>&lt;" + name + "&gt;</code> containing:\n");
381 for (XMLSyntaxRule rule : rules) {
382 buffer.append(rule.wikiRuleString(handler, prefix + "*"));
383 }
384 if (hasDescription()) {
385 buffer.append(prefix).append("*:''").append(getDescription()).append("''\n");
386 } else {
387 buffer.append(prefix).append("*:\n");
388 }
389 return buffer.toString();
390 }
391 }
392
393 public String markdownRuleString(XMLDocumentationHandler handler, String prefix) {
394 if (c != null) {
395 String markdown = prefix + "* " + handler.getHTMLForClass(c);
396 if (max > 1) {
397 markdown += " elements (";
398 if (min == 0) {
399 markdown += "zero";
400 } else if (min == 1) {
401 markdown += "one";
402 } else if (min == max) {
403 markdown += "exactly " + min;
404 }
405 if (max != min) {
406 if (max < Integer.MAX_VALUE) {
407 markdown += " to " + max;
408 } else {
409 markdown += " or more";
410 }
411 }
412 } else {
413 markdown += " element (";
414 if (min == 0) {
415 markdown += "zero or one";
416 } else {
417 markdown += "exactly one";
418 }
419 }
420 markdown += ")\n";
421
422 if (hasDescription()) {
423 markdown += ": " + getDescription() + "\n\n";
424 } else {
425 markdown += "\n";
426 }
427 return markdown;
428
429 } else {
430 StringBuffer buffer = new StringBuffer(prefix + "* Element named <code>&lt;" + name + "&gt;</code>\n");
431 if (hasDescription()) {
432 buffer.append(": ").append(getDescription()).append("\n\n");
433 } else {
434 buffer.append("\n");
435 }
436 if (rules.length > 0) {
437 buffer.append(prefix + " Containing:\n\n");
438 }
439 for (XMLSyntaxRule rule : rules) {
440 buffer.append(rule.markdownRuleString(handler, prefix + " "));
441 }
442 return buffer.toString();
443 }
444 }
445
446 /**
447 * @return a string describing the rule.
448 */
449 public String ruleString(XMLObject xo) {
450 return ruleString();
451 }
452
453
454 public boolean isAttributeRule() { return false; }
455
456 public String getName() { return name; }
457
458 public XMLSyntaxRule[] getRules() { return rules; }
459
460 /**
461 * @param o Object
462 * @return true if the given object is compatible with the required class.
463 */
464 private boolean isCompatible(Object o) {
465
466 if (rules != null) {
467
468 if (o instanceof XMLObject) {
469 XMLObject xo = (XMLObject)o;
470
471 if (xo.getName().equals(name)) {
472 for (XMLSyntaxRule rule : rules) {
473 if (!rule.isSatisfied(xo)) {
474 return false;
475 }
476 }
477
478 return true;
479 }
480 }
481 } else {
482
483 if (c == null) {
484 return true;
485 }
486
487 if (c.isInstance(o)) {
488 return true;
489 }
490
491 if (o instanceof String) {
492
493 if (c == Double.class) {
494 try {
495 Double.parseDouble((String)o);
496 return true;
497 } catch (NumberFormatException nfe) { return false; }
498 }
499 if (c == Integer.class) {
500 try {
501 Integer.parseInt((String)o);
502 return true;
503 } catch (NumberFormatException nfe) { return false; }
504 }
505 if (c == Float.class) {
506 try {
507 Float.parseFloat((String)o);
508 return true;
509 } catch (NumberFormatException nfe) { return false; }
510 }
511 if (c == Boolean.class) {
512 return (o.equals("true") || o.equals("false"));
513 }
514 if (c == Number.class) {
515 try {
516 Double.parseDouble((String)o);
517 return true;
518 } catch (NumberFormatException nfe) { return false; }
519 }
520 }
521 }
522
523 return false;
524 }
525
526 /**
527 * @return a pretty name for a class.
528 */
529 private String getTypeName() {
530
531 if (c == null) return "Object";
532 String name = c.getName();
533 return name.substring(name.lastIndexOf('.')+1);
534 }
535
536 /**
537 * @return a set containing the required types of this rule.
538 */
539 public Set<Class> getRequiredTypes() {
540 if (c != null) {
541 return Collections.singleton(c);
542 } else {
543 Set<Class> set = new TreeSet<Class>(ClassComparator.INSTANCE);
544
545 for (XMLSyntaxRule rule : rules) {
546 set.addAll(rule.getRequiredTypes());
547 }
548 return set;
549 }
550 }
551
552
553 public boolean isLegalElementName(String elementName) {
554 return c == null && name != null && name.equals(elementName);
555 }
556
557 public boolean isLegalElementClass(Class c) {
558 return this.c != null && this.c.isAssignableFrom(c);
559 }
560
561 public boolean isLegalSubelementName(String elementName) {
562 for( XMLSyntaxRule r : rules ) {
563 if( r.isLegalElementName(elementName) ) {
564 return true;
565 }
566 }
567 return false;
568 }
569
570 public int getMin() { return min; }
571 public int getMax() { return max; }
572
573 public String toString() { return ruleString(); }
574
575 private Class c = null;
576 private String name = null;
577 private XMLSyntaxRule[] rules = null;
578
579 private int min = 1;
580 private int max = 1;
581
582 private String description = null;
583 private String example = null;
584 }