| 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 dr.inference.model.AbstractModel; |
| 29 |
import org.w3c.dom.Element; |
| 30 |
import org.w3c.dom.NamedNodeMap; |
| 31 |
|
| 32 |
import java.lang.reflect.Constructor; |
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
import java.util.StringTokenizer; |
| 36 |
import java.util.Vector; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class XMLObject { |
| 45 |
|
| 46 |
public static final String missingValue = "NA"; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public XMLObject(Element e, XMLObject parent) { |
| 52 |
this.element = e; |
| 53 |
this.parent = parent; |
| 54 |
} |
| 55 |
|
| 56 |
public XMLObject(XMLObject obj, int index) { |
| 57 |
|
| 58 |
this(obj.element, null); |
| 59 |
nativeObject = ((List)obj.getNativeObject()).get(index); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public final int getChildCount() { |
| 66 |
return children.size(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public Object getChild(int i) { |
| 75 |
Object obj = getRawChild(i); |
| 76 |
XMLObject xo = null; |
| 77 |
|
| 78 |
if( obj instanceof XMLObject ) { |
| 79 |
xo = (XMLObject) obj; |
| 80 |
} else if( obj instanceof Reference ) { |
| 81 |
xo = ((Reference) obj).getReferenceObject(); |
| 82 |
} |
| 83 |
|
| 84 |
if( xo != null && xo.hasNativeObject() ) { |
| 85 |
return xo.getNativeObject(); |
| 86 |
} |
| 87 |
return obj; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public Object getChild(Class c) { |
| 95 |
|
| 96 |
for (int i = 0; i < getChildCount(); i++) { |
| 97 |
Object child = getChild(i); |
| 98 |
if( c.isInstance(child) ) { |
| 99 |
return child; |
| 100 |
} |
| 101 |
} |
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public List<Object> getChildren() { |
| 109 |
|
| 110 |
List<Object> allChildren = new ArrayList<Object>(); |
| 111 |
for (int i = 0; i < getChildCount(); i++) { |
| 112 |
Object child = getChild(i); |
| 113 |
allChildren.add(child); |
| 114 |
|
| 115 |
} |
| 116 |
return allChildren; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public <T> List<T> getAllChildren(Class<T> c) { |
| 124 |
|
| 125 |
List<T> allChildren = new ArrayList<T>();; |
| 126 |
for (int i = 0; i < getChildCount(); i++) { |
| 127 |
Object child = getChild(i); |
| 128 |
if( c.isInstance(child) ) { |
| 129 |
allChildren.add(c.cast(child)); |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
return allChildren; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public XMLObject getChild(String name) { |
| 142 |
|
| 143 |
for (int i = 0; i < getChildCount(); i++) { |
| 144 |
Object child = getChild(i); |
| 145 |
if( child instanceof XMLObject ) { |
| 146 |
if( ((XMLObject) child).getName().equals(name) ) { |
| 147 |
return (XMLObject) child; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
return null; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public List<XMLObject> getAllChildren(String name) { |
| 159 |
|
| 160 |
List<XMLObject> allChildren = new ArrayList<XMLObject>(); |
| 161 |
for (int i = 0; i < getChildCount(); i++) { |
| 162 |
Object child = getChild(i); |
| 163 |
if( child instanceof XMLObject && ((XMLObject)child).getName().equals(name)) { |
| 164 |
allChildren.add((XMLObject)child); |
| 165 |
} |
| 166 |
} |
| 167 |
return allChildren; |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public Object getElementFirstChild(String elementName) throws XMLParseException { |
| 178 |
Object child = getChild(elementName); |
| 179 |
|
| 180 |
if (child == null) |
| 181 |
throw new XMLParseException("Child element called " + elementName + |
| 182 |
" does not exist inside element " + getName()); |
| 183 |
if (!(child instanceof XMLObject)) |
| 184 |
throw new XMLParseException("Child element called " + elementName + |
| 185 |
" inside element " + getName() + " is not an XMLObject."); |
| 186 |
|
| 187 |
return ((XMLObject) child).getChild(0); |
| 188 |
} |
| 189 |
|
| 190 |
public String getChildName(int i) { |
| 191 |
|
| 192 |
Object obj = getRawChild(i); |
| 193 |
XMLObject xo; |
| 194 |
|
| 195 |
if (obj instanceof XMLObject) { |
| 196 |
xo = (XMLObject) obj; |
| 197 |
} else if (obj instanceof Reference) { |
| 198 |
xo = ((Reference) obj).getReferenceObject(); |
| 199 |
} else { |
| 200 |
return ""; |
| 201 |
} |
| 202 |
|
| 203 |
return xo.getName(); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public boolean hasChildNamed(String name) { |
| 211 |
final Object child = getChild(name); |
| 212 |
return (child != null) && (child instanceof XMLObject); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public NamedNodeMap getAttributes() { |
| 219 |
return element.getAttributes(); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public boolean getBooleanChild(int i) throws XMLParseException { |
| 228 |
return getBoolean(getChild(i)); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public double getDoubleChild(int i) throws XMLParseException { |
| 237 |
return getDouble(getChild(i)); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public double[] getDoubleArrayChild(int i) throws XMLParseException { |
| 246 |
return getDoubleArray(getChild(i)); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public int getIntegerChild(int i) throws XMLParseException { |
| 255 |
return getInteger(getChild(i)); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public String getStringChild(int i) throws XMLParseException { |
| 264 |
return getString(getChild(i)); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public String[] getStringArrayChild(int i) throws XMLParseException { |
| 273 |
return getStringArray(getChild(i)); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public <T> T getAttribute(String name, T defaultValue) throws XMLParseException { |
| 285 |
if (element.hasAttribute(name)) { |
| 286 |
|
| 287 |
final String s = element.getAttribute(name); |
| 288 |
for (Constructor c : defaultValue.getClass().getConstructors()) { |
| 289 |
final Class[] classes = c.getParameterTypes(); |
| 290 |
if (classes.length == 1 && classes[0].equals(String.class)) { |
| 291 |
try { |
| 292 |
return (T) c.newInstance(s); |
| 293 |
} catch (Exception e) { |
| 294 |
throw new XMLParseException(" conversion of '" + s + "' to " + |
| 295 |
defaultValue.getClass().getName() + " failed"); |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
return defaultValue; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public Object getAttribute(String name) throws XMLParseException { |
| 307 |
return getAndTest(name); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public boolean getBooleanAttribute(String name) throws XMLParseException { |
| 314 |
return getBoolean(getAndTest(name)); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public double getDoubleAttribute(String name) throws XMLParseException { |
| 322 |
return getDouble(getAndTest(name)); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public double[] getDoubleArrayAttribute(String name) throws XMLParseException { |
| 329 |
return getDoubleArray(getAndTest(name)); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public int[] getIntegerArrayAttribute(String name) throws XMLParseException { |
| 336 |
return getIntegerArray(getAndTest(name)); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public int getIntegerAttribute(String name) throws XMLParseException { |
| 343 |
return getInteger(getAndTest(name)); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public long getLongIntegerAttribute(String name) throws XMLParseException { |
| 350 |
return getLongInteger(getAndTest(name)); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public String getStringAttribute(String name) throws XMLParseException { |
| 357 |
return getString(getAndTest(name)); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public String[] getStringArrayAttribute(String name) throws XMLParseException { |
| 364 |
return getStringArray(getAndTest(name)); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public static boolean isDoubleArray(String s, List<Double> valueList) { |
| 373 |
try { |
| 374 |
StringTokenizer st = new StringTokenizer(s); |
| 375 |
while (st.hasMoreTokens()) { |
| 376 |
String token = st.nextToken(); |
| 377 |
Double d; |
| 378 |
if (token.compareToIgnoreCase(missingValue) == 0) |
| 379 |
d = Double.NaN; |
| 380 |
else |
| 381 |
d = new Double(token); |
| 382 |
if (valueList != null) valueList.add(d); |
| 383 |
} |
| 384 |
return true; |
| 385 |
} catch (NumberFormatException e) { |
| 386 |
return false; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public static boolean isIntegerArray(String s, List<Integer> valueList) { |
| 396 |
try { |
| 397 |
StringTokenizer st = new StringTokenizer(s); |
| 398 |
while (st.hasMoreTokens()) { |
| 399 |
Integer d = new Integer(st.nextToken()); |
| 400 |
if (valueList != null) valueList.add(d); |
| 401 |
} |
| 402 |
return true; |
| 403 |
} catch (NumberFormatException e) { |
| 404 |
return false; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
public final static String ID = "id"; |
| 409 |
|
| 410 |
public boolean hasId() { |
| 411 |
return hasAttribute(ID); |
| 412 |
} |
| 413 |
|
| 414 |
public String getId() throws XMLParseException { |
| 415 |
return getStringAttribute(ID); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public boolean hasAttribute(String name) { |
| 422 |
return (element.hasAttribute(name)); |
| 423 |
} |
| 424 |
|
| 425 |
public String getName() { |
| 426 |
return element.getTagName(); |
| 427 |
} |
| 428 |
|
| 429 |
public Object getNativeObject() { |
| 430 |
return nativeObject; |
| 431 |
} |
| 432 |
|
| 433 |
public boolean hasNativeObject() { |
| 434 |
return nativeObject != null; |
| 435 |
} |
| 436 |
|
| 437 |
public String toString() { |
| 438 |
|
| 439 |
String prefix = getName(); |
| 440 |
if (hasId()) { |
| 441 |
try { |
| 442 |
prefix += ":" + getId(); |
| 443 |
} catch (XMLParseException e) { |
| 444 |
|
| 445 |
assert false; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
return prefix; |
| 450 |
} |
| 451 |
|
| 452 |
public String content() { |
| 453 |
|
| 454 |
if (nativeObject != null) { |
| 455 |
if (nativeObject instanceof dr.util.XHTMLable) { |
| 456 |
return ((dr.util.XHTMLable) nativeObject).toXHTML(); |
| 457 |
} else { |
| 458 |
return nativeObject.toString(); |
| 459 |
} |
| 460 |
} |
| 461 |
return ""; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
void addChild(Object child) { |
| 472 |
if (child instanceof XMLObject || |
| 473 |
child instanceof Reference || |
| 474 |
child instanceof String) { |
| 475 |
|
| 476 |
children.add(child); |
| 477 |
} else throw new IllegalArgumentException(); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
public Object getRawChild(int i) { |
| 484 |
return children.get(i); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
public void setNativeObject(Object obj) { |
| 491 |
nativeObject = obj; |
| 492 |
} |
| 493 |
|
| 494 |
boolean isReference(int child) { |
| 495 |
return (getRawChild(child) instanceof Reference); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
private boolean getBoolean(Object obj) throws XMLParseException { |
| 510 |
|
| 511 |
if (obj instanceof Boolean) return (Boolean) obj; |
| 512 |
if (obj instanceof String) { |
| 513 |
if (obj.equals("true")) return true; |
| 514 |
if (obj.equals("false")) return false; |
| 515 |
} |
| 516 |
throw new XMLParseException("Expected a boolean (true|false), but got " + obj); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
private double getDouble(Object obj) throws XMLParseException { |
| 523 |
try { |
| 524 |
if (obj instanceof Number) { |
| 525 |
return ((Number) obj).doubleValue(); |
| 526 |
} |
| 527 |
if (obj instanceof String) { |
| 528 |
return Double.parseDouble((String) obj); |
| 529 |
} |
| 530 |
} catch (NumberFormatException nfe) { |
| 531 |
throw new XMLParseException("Expected double precision number, but got " + obj); |
| 532 |
} |
| 533 |
throw new XMLParseException("Expected double precision number, but got " + obj); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
private double[] getDoubleArray(Object obj) throws XMLParseException { |
| 540 |
|
| 541 |
if (obj instanceof Number) return new double[]{((Number) obj).doubleValue()}; |
| 542 |
if (obj instanceof double[]) return (double[]) obj; |
| 543 |
if (obj instanceof String) { |
| 544 |
List<Double> valueList = new ArrayList<Double>(); |
| 545 |
if (isDoubleArray((String) obj, valueList)) { |
| 546 |
double[] values = new double[valueList.size()]; |
| 547 |
for (int i = 0; i < values.length; i++) { |
| 548 |
values[i] = valueList.get(i); |
| 549 |
} |
| 550 |
return values; |
| 551 |
} else { |
| 552 |
throw new XMLParseException("Expected array of double precision numbers, but got " + obj); |
| 553 |
} |
| 554 |
} |
| 555 |
throw new XMLParseException("Expected array of double precision numbers, but got " + obj); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
private int[] getIntegerArray(Object obj) throws XMLParseException { |
| 562 |
|
| 563 |
if (obj instanceof Number) return new int[]{((Number) obj).intValue()}; |
| 564 |
if (obj instanceof int[]) return (int[]) obj; |
| 565 |
if (obj instanceof String) { |
| 566 |
ArrayList<Integer> valueList = new ArrayList<Integer>(); |
| 567 |
if (isIntegerArray((String) obj, valueList)) { |
| 568 |
int[] values = new int[valueList.size()]; |
| 569 |
for (int i = 0; i < values.length; i++) { |
| 570 |
values[i] = valueList.get(i); |
| 571 |
} |
| 572 |
return values; |
| 573 |
} else { |
| 574 |
throw new XMLParseException("Expected array of integers, but got " + obj); |
| 575 |
} |
| 576 |
} |
| 577 |
throw new XMLParseException("Expected array of integers, but got " + obj); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
private int getInteger(Object obj) throws XMLParseException { |
| 584 |
if (obj instanceof Number) return ((Number) obj).intValue(); |
| 585 |
try { |
| 586 |
return Integer.parseInt((String) obj); |
| 587 |
} catch (NumberFormatException e) { |
| 588 |
throw new XMLParseException("Expected integer, got " + obj); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
private long getLongInteger(Object obj) throws XMLParseException { |
| 596 |
if (obj instanceof Number) return ((Number) obj).longValue(); |
| 597 |
try { |
| 598 |
return Long.parseLong((String) obj); |
| 599 |
} catch (NumberFormatException e) { |
| 600 |
throw new XMLParseException("Expected long integer, got " + obj); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
private String getString(Object obj) throws XMLParseException { |
| 608 |
|
| 609 |
if (obj instanceof String) return (String) obj; |
| 610 |
throw new XMLParseException("Expected string, but got " + obj); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
private String[] getStringArray(Object obj) throws XMLParseException { |
| 617 |
|
| 618 |
if (obj instanceof String[]) return (String[]) obj; |
| 619 |
if (obj instanceof String) { |
| 620 |
ArrayList<String> stringList = new ArrayList<String>(); |
| 621 |
StringTokenizer st = new StringTokenizer((String) obj); |
| 622 |
while (st.hasMoreTokens()) { |
| 623 |
stringList.add(st.nextToken()); |
| 624 |
} |
| 625 |
String[] strings = new String[stringList.size()]; |
| 626 |
for (int i = 0; i < strings.length; i++) { |
| 627 |
strings[i] = stringList.get(i); |
| 628 |
} |
| 629 |
return strings; |
| 630 |
} |
| 631 |
throw new XMLParseException("Expected array of strings, but got " + obj); |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
private Object getAndTest(String name) throws XMLParseException { |
| 638 |
|
| 639 |
if (element.hasAttribute(name)) { |
| 640 |
return element.getAttribute(name); |
| 641 |
} |
| 642 |
throw new XMLParseException("'" + name + "' attribute was not found in " + element.getTagName() + " element."); |
| 643 |
} |
| 644 |
|
| 645 |
public XMLObject getParent() { |
| 646 |
return parent; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
private final Vector<Object> children = new Vector<Object>(); |
| 654 |
private final Element element; |
| 655 |
private final XMLObject parent; |
| 656 |
|
| 657 |
private Object nativeObject; |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
} |