Debian Codegraph @

src/dr/xml/StringAttributeRule.java

raw symbols SCIP
1 /*
2 * StringAttributeRule.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.ArrayList;
29 import java.util.List;
30 import java.util.Random;
31
32
33 public class StringAttributeRule extends AttributeRule {
34
35 /**
36 * Creates a required String attribute rule.
37 */
38 public StringAttributeRule(String name, String description) {
39 this(name, description, (String) null, false);
40 }
41
42 /**
43 * Creates a required String attribute rule.
44 */
45 public StringAttributeRule(String name, String description, String example) {
46 this(name, description, example, false);
47 }
48
49 /**
50 * Creates a String attribute rule.
51 */
52 public StringAttributeRule(String name, String description, boolean optional) {
53 this(name, description, null, optional, 0, Integer.MAX_VALUE);
54 }
55
56 /**
57 * Creates a String attribute rule.
58 */
59 public StringAttributeRule(String name, String description, String example, boolean optional) {
60 this(name, description, example, optional, 0, Integer.MAX_VALUE);
61 }
62
63 /**
64 * Creates a String attribute rule.
65 *
66 * @param valid a list of valid tokens for this attribute
67 */
68 public StringAttributeRule(String name, String description, String[] valid, boolean optional) {
69 this(name, description, null, optional, 0, Integer.MAX_VALUE);
70 validValues = new ArrayList<String>();
71 for (String aValid : valid) {
72 validValues.add(aValid);
73 }
74 this.example = null;
75 }
76
77 /**
78 * Creates a String attribute rule.
79 *
80 * @param valid a list of valid tokens for this attribute
81 */
82 public StringAttributeRule(String name, String description, Enum[] valid, boolean optional) {
83 this(name, description, null, optional, 0, Integer.MAX_VALUE);
84 validValues = new ArrayList<String>();
85 for (Enum aValid : valid) {
86 validValues.add(aValid.name());
87 }
88 this.example = null;
89 }
90
91 /**
92 * Creates a String attribute rule.
93 *
94 * @param valid a list of valid tokens for this attribute
95 */
96 public StringAttributeRule(String name, String description, String[][] valid, boolean optional) {
97 this(name, description, null, optional, 0, Integer.MAX_VALUE);
98 validValues = new ArrayList<String>();
99 for (String[] aValid : valid) {
100 for (String anAValid : aValid) {
101 validValues.add(anAValid);
102 }
103 }
104 this.example = null;
105 }
106
107
108 /**
109 * Creates a String attribute rule.
110 */
111 private StringAttributeRule(String name, String description, String example, boolean optional, int minLength, int maxLength) {
112 setName(name);
113 setAttributeClass(String.class);
114 setOptional(optional);
115 setDescription(description);
116 this.example = example;
117 this.minLength = minLength;
118 this.maxLength = maxLength;
119 }
120
121 /**
122 * @return true if the required attribute of the correct type is present.
123 */
124 public boolean isSatisfied(XMLObject xo) {
125 if (super.isSatisfied(xo)) {
126 if (!getOptional()) {
127 try {
128 final String str = (String) getAttribute(xo);
129 if (validValues != null) {
130 for (Object validValue : validValues) {
131 if (str.equals(validValue)) return true;
132 }
133 return false;
134 } else {
135 return (str.length() >= minLength || str.length() <= maxLength);
136 }
137 } catch (XMLParseException xpe) {
138 //
139 }
140 }
141 return true;
142 }
143 return false;
144 }
145
146 /**
147 * @return a string describing the rule.
148 */
149 public String ruleString() {
150 StringBuffer rule = new StringBuffer(super.ruleString());
151
152 if (validValues != null && validValues.size() > 0) {
153 rule.append(" from {");
154 rule.append(validValues.get(0));
155 for (int i = 1; i < validValues.size(); i++) {
156 rule.append(", ");
157 rule.append(validValues.get(i));
158 }
159 }
160 return rule.toString();
161 }
162
163
164 /**
165 * @return a string describing the rule.
166 */
167 public String htmlRuleString(XMLDocumentationHandler handler) {
168 String rule =
169 "<div class=\"" + (getOptional() ? "optional" : "required") + "rule\"> Attribute " +
170 " <span class=\"attrname\">" + getName() +
171 "</span>";
172
173 if (validValues != null) {
174 rule += " &isin; {<tt>" + validValues.get(0) + "</tt>";
175 for (int i = 1; i < validValues.size(); i++) {
176 rule += ", <tt>" + validValues.get(i) + "</tt>";
177 }
178 rule += "}";
179 } else {
180 rule += " is string";
181 }
182
183 rule += " <div class=\"description\">" + getDescription() + "</div>";
184
185 rule += "</div>";
186
187 return rule;
188 }
189
190 public String getExample() {
191 Random random = new Random();
192 if (validValues != null) {
193 return validValues.get(random.nextInt(validValues.size()));
194 } else return example;
195 }
196
197 public boolean hasExample() {
198 return (validValues != null || example != null);
199 }
200
201 private int minLength = 0, maxLength = Integer.MAX_VALUE;
202 private List<String> validValues = null;
203 private String example = null;
204 }