Debian Codegraph @

src/dr/xml/XORRule.java

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