Debian Codegraph @

src/dr/xml/OrRule.java

raw symbols SCIP
1 /*
2 * OrRule.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.Set;
29 import java.util.TreeSet;
30
31 public class OrRule implements XMLSyntaxRule {
32
33 public OrRule(XMLSyntaxRule a, XMLSyntaxRule b) {
34 rules = new XMLSyntaxRule[] {a,b};
35 }
36
37 public OrRule(XMLSyntaxRule[] rules) {
38 this.rules = rules;
39 }
40
41 public XMLSyntaxRule[] getRules() {
42 return rules;
43 }
44
45 /**
46 * Returns true if the rule is satisfied for the given XML object.
47 */
48 public boolean isSatisfied(XMLObject xo) {
49 for (XMLSyntaxRule rule : rules) {
50 if (rule.isSatisfied(xo)) return true;
51 }
52 return false;
53 }
54
55 public boolean containsAttribute(String name) {
56 for( XMLSyntaxRule rule : rules ) {
57 if( rule.containsAttribute((name)) ) {
58 return true;
59 }
60 }
61 return false;
62 }
63
64 /**
65 * Describes the rule.
66 */
67 public String ruleString() {
68 String ruleString = "(" + rules[0].ruleString();
69 for (int i = 1; i < rules.length; i++) {
70 ruleString += "| " + rules[i].ruleString();
71 }
72 return ruleString + ")";
73 }
74
75 /**
76 * Describes the rule.
77 */
78 public String htmlRuleString(XMLDocumentationHandler handler) {
79 String html = "<div class=\"requiredcompoundrule\">At least one of:";
80 for (XMLSyntaxRule rule : rules) {
81 html += rule.htmlRuleString(handler);
82 }
83 html += "</div>";
84 return html;
85 }
86
87 /**
88 * Describes the rule.
89 */
90 public String wikiRuleString(XMLDocumentationHandler handler, String prefix) {
91 String html = prefix + "At least one of:";
92 for (XMLSyntaxRule rule : rules) {
93 html += rule.wikiRuleString(handler, prefix + "*");
94 }
95 html += "\n";
96 return html;
97 }
98
99 /**
100 * Describes the rule.
101 */
102 public String markdownRuleString(XMLDocumentationHandler handler, String prefix) {
103 String html = prefix + "At least one of:\n";
104 for (XMLSyntaxRule rule : rules) {
105 html += rule.markdownRuleString(handler, prefix + " ");
106 }
107 html += "\n\n";
108 return html;
109 }
110
111 /**
112 * Describes the rule.
113 */
114 public String ruleString(XMLObject xo) {
115
116 for (XMLSyntaxRule rule : rules) {
117 if (!rule.isSatisfied(xo)) return rule.ruleString(xo);
118 }
119 return ruleString();
120 }
121
122 /**
123 * @return a set containing the required types of this rule.
124 */
125 public Set<Class> getRequiredTypes() {
126 Set<Class> set = new TreeSet<Class>(ClassComparator.INSTANCE);
127 for (XMLSyntaxRule rule : rules) {
128 set.addAll(rule.getRequiredTypes());
129 }
130 return set;
131 }
132
133 public boolean isLegalElementName(String elementName) {
134 for (XMLSyntaxRule rule : rules) {
135 if( rule.isLegalElementName(elementName) ) {
136 return true;
137 }
138 }
139 return false;
140 }
141
142 public boolean isLegalElementClass(Class c) {
143 for (XMLSyntaxRule rule : rules) {
144 if( rule.isLegalElementClass(c) ) {
145 return true;
146 }
147 }
148 return false;
149 }
150
151 public boolean isLegalSubelementName(String elementName) {
152 for (XMLSyntaxRule rule : rules) {
153 if( rule.isLegalSubelementName(elementName) ) {
154 return true;
155 }
156 }
157 return false;
158 }
159
160 XMLSyntaxRule[] rules;
161 }