Debian Codegraph @

src/dr/xml/AttributeRule.java

raw symbols SCIP
1 /*
2 * AttributeRule.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
31 public class AttributeRule implements XMLSyntaxRule {
32
33 AttributeRule() {}
34
35 public void setName(String name) { this.name = name; }
36 public void setDescription(String description) { this.description = description; }
37 public void setAttributeClass(Class c) { this.c = c; }
38 public void setOptional(boolean optional) { this.optional = optional; }
39
40 public boolean getOptional() { return optional; }
41
42 /**
43 * Creates a required attribute rule.
44 */
45 private AttributeRule(String name, Class c) {
46 this.name = name;
47 this.c = c;
48 this.optional = false;
49 //this.description = null;
50 }
51
52 /**
53 * Creates an attribute rule.
54 */
55 private AttributeRule(String name, Class c, boolean optional) {
56 this.name = name;
57 this.c = c;
58 this.optional = optional;
59 }
60
61 /**
62 * Creates an attribute rule.
63 */
64 private AttributeRule(String name, Class c, boolean optional, String description) {
65 this.name = name;
66 this.c = c;
67 this.optional = optional;
68 this.description = description;
69 }
70
71 public static AttributeRule newIntegerRule(String name) { return new AttributeRule(name, Integer.class); }
72 public static AttributeRule newLongIntegerRule(String name) { return new AttributeRule(name, Long.class); }
73 public static AttributeRule newDoubleRule(String name) { return new AttributeRule(name, Double.class); }
74 public static AttributeRule newDoubleArrayRule(String name) { return new AttributeRule(name, Double[].class); }
75 public static AttributeRule newBooleanRule(String name) { return new AttributeRule(name, Boolean.class); }
76 public static AttributeRule newStringRule(String name) { return new AttributeRule(name, String.class); }
77 public static AttributeRule newStringArrayRule(String name) { return new AttributeRule(name, String[].class); }
78
79 public static AttributeRule newIntegerRule(String name, boolean optional) { return new AttributeRule(name, Integer.class, optional); }
80 public static AttributeRule newIntegerArrayRule(String name, boolean optional) { return new AttributeRule(name, Integer[].class, optional); }
81 public static AttributeRule newLongIntegerRule(String name, boolean optional) { return new AttributeRule(name, Long.class, optional); }
82 public static AttributeRule newDoubleRule(String name, boolean optional) { return new AttributeRule(name, Double.class, optional); }
83 public static AttributeRule newDoubleArrayRule(String name, boolean optional) { return new AttributeRule(name, Double[].class, optional); }
84 public static AttributeRule newBooleanRule(String name, boolean optional) { return new AttributeRule(name, Boolean.class, optional); }
85 public static AttributeRule newStringRule(String name, boolean optional) { return new AttributeRule(name, String.class, optional); }
86 public static AttributeRule newStringArrayRule(String name, boolean optional) { return new AttributeRule(name, String[].class, optional); }
87
88 public static AttributeRule newIntegerRule(String name, boolean optional, String description) { return new AttributeRule(name, Integer.class, optional, description); }
89 public static AttributeRule newLongIntegerRule(String name, boolean optional, String description) { return new AttributeRule(name, Long.class, optional, description); }
90 public static AttributeRule newDoubleRule(String name, boolean optional, String description) { return new AttributeRule(name, Double.class, optional, description); }
91 public static AttributeRule newDoubleArrayRule(String name, boolean optional, String description) { return new AttributeRule(name, Double[].class, optional, description); }
92 public static AttributeRule newBooleanRule(String name, boolean optional, String description) { return new AttributeRule(name, Boolean.class, optional, description); }
93 public static AttributeRule newStringRule(String name, boolean optional, String description) { return new AttributeRule(name, String.class, optional, description); }
94 public static AttributeRule newStringArrayRule(String name, boolean optional, String description) { return new AttributeRule(name, String[].class, optional, description); }
95
96 public String getName() { return name; }
97 public Class getAttributeClass() { return c; }
98 public String getDescription() { return description; }
99
100 public Object getAttribute(XMLObject xo) throws XMLParseException {
101 return xo.getAttribute(name);
102 }
103
104 public boolean hasDescription() { return description != null; }
105
106 public boolean hasExample() { return false; }
107
108 public String getExample() { return null; }
109
110 /**
111 * @return true if the required attribute of the correct type is present.
112 */
113 public boolean isSatisfied(XMLObject xo) {
114
115 if (xo.hasAttribute(name)) {
116 try {
117 Object obj = xo.getAttribute(name);
118 return isCompatible(obj);
119 } catch (XMLParseException xpe) { return false; }
120 } else if (optional) {
121 return true;
122 }
123 return false;
124 }
125
126 public boolean containsAttribute(String name) {
127 return name.equals(getName());
128 }
129
130 /**
131 * @return a string describing the rule.
132 */
133 public String ruleString() {
134 String rule = "ATTRIBUTE " + getTypeName() + " " + name;
135 if (optional) {
136 rule += " OPTIONAL";
137 } else {
138 rule += " REQUIRED";
139 }
140 return rule;
141 }
142
143 /**
144 * @return a string describing the rule.
145 */
146 public String htmlRuleString(XMLDocumentationHandler handler) {
147 String rule =
148 "<div class=\"" + (optional ? "optional" : "required") + "rule\"> " +
149 "Attribute <span class=\"attrname\">" + name + "</span> is " +
150 handler.getHTMLForClass(c) + " " +
151 (hasDescription() ? "<div class=\"description\">" + description + "</div>" : "") +
152 "</div>" ;
153
154 /*if (optional) {
155 rule += " <span class=\"optional\">optional</span></div>";
156 } else {
157 rule += " <span class=\"required\">required</span></div>";
158 }*/
159
160 /* <span class=\"symbol\">&#206;</span> */
161
162 return rule;
163 }
164
165 public String wikiRuleString(XMLDocumentationHandler handler, String prefix) {
166 String rule = prefix + "Attribute " + (optional ? "(optional) " : "") + "<code>" + name + "</code> is " +
167 handler.getHTMLForClass(c) + "\n" + prefix + ":" +
168 (hasDescription() ? "\"" + description + "\"" : "") + "\n";
169
170 return rule;
171 }
172
173 public String markdownRuleString(XMLDocumentationHandler handler, String prefix) {
174 String rule = prefix + "* <code>" + name + "</code> " + (optional ? "(optional)" : "") + " is of type " +
175 handler.getHTMLForClass(c) + "\n" +
176 ": " +
177 (hasDescription() ? "\"" + description + "\"" : "") + "\n\n";
178
179 return rule;
180 }
181
182 /**
183 * @return a string describing the rule.
184 */
185 public String ruleString(XMLObject xo) {
186
187 if (xo.hasAttribute(name)) {
188 try {
189 Object obj = xo.getAttribute(name);
190 boolean compatible = isCompatible(obj);
191 if (compatible) {
192 return ruleString();
193 } else {
194 return "ATTRIBUTE " + name + " expected to be of type " + getTypeName();
195 }
196
197 } catch (XMLParseException xpe) {
198 return xpe.toString();
199 }
200 }
201 return ruleString();
202 }
203
204 /**
205 * @return a set containing the required types of this rule.
206 */
207 public Set<Class> getRequiredTypes() { return Collections.singleton(c); }
208
209 public boolean isLegalElementName(String elementName) {
210 return false;
211 }
212
213 public boolean isLegalElementClass(Class c) {
214 return false;
215 }
216
217 public boolean isLegalSubelementName(String elementName) {
218 return false;
219 }
220
221 public boolean isAttributeRule() { return true; }
222
223 /**
224 * @return true if the given object is compatible with the required class.
225 */
226 private boolean isCompatible(Object o) {
227
228 if (c == null) return true;
229
230 if (c.isInstance(o)) { return true; }
231
232 if (o instanceof String) {
233
234 if (c == Double[].class) {
235
236 return XMLObject.isDoubleArray((String)o, null);
237 }
238 if (c == Integer[].class) {
239
240 return XMLObject.isIntegerArray((String)o, null);
241 }
242 if (c == String[].class) {
243
244 return true;
245 }
246 if (c == Double.class) {
247 try {
248 Double.parseDouble((String)o);
249 return true;
250 } catch (NumberFormatException nfe) { return false; }
251 }
252 if (c == Integer.class) {
253 try {
254 Integer.parseInt((String)o);
255 return true;
256 } catch (NumberFormatException nfe) { return false; }
257 }
258 if (c == Long.class) {
259 try {
260 Long.parseLong((String)o);
261 return true;
262 } catch (NumberFormatException nfe) { return false; }
263 }
264 if (c == Float.class) {
265 try {
266 Float.parseFloat((String)o);
267 return true;
268 } catch (NumberFormatException nfe) { return false; }
269 }
270 if (c == Boolean.class) {
271 return (o.equals("true") || o.equals("false"));
272 }
273 if (c == Number.class) {
274 try {
275 Double.parseDouble((String)o);
276 return true;
277 } catch (NumberFormatException nfe) { return false; }
278 }
279 }
280
281 return false;
282 }
283
284 /**
285 * @return a pretty name for a class.
286 */
287 private String getTypeName() {
288
289 if (c == null) return "Object";
290 final String name = c.getName();
291 final String cBaseName = name.substring(name.lastIndexOf('.') + 1);
292 if( c.isArray() ) {
293 return "Array of " + cBaseName;
294 }
295 return cBaseName;
296 }
297
298 private String name;
299 private Class c;
300 private boolean optional;
301 private String description;
302 }