| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 131 |
|
| 132 |
public String ruleString(XMLObject xo) { |
| 133 |
return ruleString(); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 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 |
} |