| 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.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 |
|
| 44 |
|
| 45 |
private AttributeRule(String name, Class c) { |
| 46 |
this.name = name; |
| 47 |
this.c = c; |
| 48 |
this.optional = false; |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |