Debian Codegraph @

src/dr/xml/XMLObject.java

raw symbols SCIP
1 /*
2 * XMLObject.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 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 * This class wraps a DOM Element for the purposes of parsing.
40 *
41 * @author Alexei Drummond
42 * @version $Id: XMLObject.java,v 1.30 2005/07/11 14:06:25 rambaut Exp $
43 */
44 public class XMLObject {
45
46 public static final String missingValue = "NA";
47
48 /**
49 * @param e the element the construct this XML object from
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 * @return the number of children this XMLObject has.
64 */
65 public final int getChildCount() {
66 return children.size();
67 }
68
69 /**
70 * @param i the index of the child to return
71 * @return the ith child in native format if available, otherwise as
72 * an XMLObject.
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 * @param c the class of the child to return
92 * @return the first child with a native format of the given class, or null if no such child exists.
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 * @return all children with or empty list if no children.
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 * @param c the class of the children to return
121 * @return all children with a native format of the given class, or empty if no such child exists.
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 * @param name the name of the child to return
139 * @return the first child of type XMLObject with a given name, or null if no such child exists.
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 * @param name the name of the children
156 * @return all children with a given name.
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 * @param elementName the name of the XML wrapper element the child resides in
173 * @return the first child element out of a named XMLObject element
174 * @throws XMLParseException if no wrapper element exists, or it the child in
175 * wrapper element is not an XMLObject
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 * @param name the name of the child element being tested for.
208 * @return true if a child element of the given name exists.
209 */
210 public boolean hasChildNamed(String name) {
211 final Object child = getChild(name);
212 return (child != null) && (child instanceof XMLObject);
213 }
214
215 /**
216 * @return all attributes
217 */
218 public NamedNodeMap getAttributes() {
219 return element.getAttributes();
220 }
221
222 /**
223 * @param i the index of the child to return
224 * @return the ith child as a boolean.
225 * @throws XMLParseException if getChild(i) would
226 */
227 public boolean getBooleanChild(int i) throws XMLParseException {
228 return getBoolean(getChild(i));
229 }
230
231 /**
232 * @param i the index of the child to return
233 * @return the ith child as a double.
234 * @throws XMLParseException if getChild(i) would
235 */
236 public double getDoubleChild(int i) throws XMLParseException {
237 return getDouble(getChild(i));
238 }
239
240 /**
241 * @param i the index of the child to return
242 * @return the ith child as a double[].
243 * @throws XMLParseException if getChild(i) would
244 */
245 public double[] getDoubleArrayChild(int i) throws XMLParseException {
246 return getDoubleArray(getChild(i));
247 }
248
249 /**
250 * @param i the index of the child to return
251 * @return the ith child as an integer.
252 * @throws XMLParseException if getChild(i) would
253 */
254 public int getIntegerChild(int i) throws XMLParseException {
255 return getInteger(getChild(i));
256 }
257
258 /**
259 * @param i the index of the child to return
260 * @return the ith child as a string.
261 * @throws XMLParseException if getChild(i) would
262 */
263 public String getStringChild(int i) throws XMLParseException {
264 return getString(getChild(i));
265 }
266
267 /**
268 * @param i the index of the child to return
269 * @return the ith child as a String[].
270 * @throws XMLParseException if getChild(i) would
271 */
272 public String[] getStringArrayChild(int i) throws XMLParseException {
273 return getStringArray(getChild(i));
274 }
275
276 /**
277 * Attribute value, if present - default otherwise.
278 *
279 * @param name attribute name
280 * @param defaultValue the default value
281 * @return the given attribute if it exists, otherwise the default value
282 * @throws XMLParseException if attribute can't be converted to desired type
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 * @return the named attribute
305 */
306 public Object getAttribute(String name) throws XMLParseException {
307 return getAndTest(name);
308 }
309
310 /**
311 * @return the named attribute as a boolean.
312 */
313 public boolean getBooleanAttribute(String name) throws XMLParseException {
314 return getBoolean(getAndTest(name));
315 }
316
317
318 /**
319 * @return the named attribute as a double.
320 */
321 public double getDoubleAttribute(String name) throws XMLParseException {
322 return getDouble(getAndTest(name));
323 }
324
325 /**
326 * @return the named attribute as a double[].
327 */
328 public double[] getDoubleArrayAttribute(String name) throws XMLParseException {
329 return getDoubleArray(getAndTest(name));
330 }
331
332 /**
333 * @return the named attribute as a double[].
334 */
335 public int[] getIntegerArrayAttribute(String name) throws XMLParseException {
336 return getIntegerArray(getAndTest(name));
337 }
338
339 /**
340 * @return the named attribute as an integer.
341 */
342 public int getIntegerAttribute(String name) throws XMLParseException {
343 return getInteger(getAndTest(name));
344 }
345
346 /**
347 * @return the named attribute as a long integer.
348 */
349 public long getLongIntegerAttribute(String name) throws XMLParseException {
350 return getLongInteger(getAndTest(name));
351 }
352
353 /**
354 * @return the named attribute as a string.
355 */
356 public String getStringAttribute(String name) throws XMLParseException {
357 return getString(getAndTest(name));
358 }
359
360 /**
361 * @return the named attribute as a String[].
362 */
363 public String[] getStringArrayAttribute(String name) throws XMLParseException {
364 return getStringArray(getAndTest(name));
365 }
366
367 /**
368 * @param valueList if this ArrayList is not null it is populated by the double array
369 * that the given string represents.
370 * @return true if the given string represents a whitespaced-delimited array of doubles.
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 * @param valueList if this ArrayList is not null it is populated by the integer array
392 * that the given string represents.
393 * @return true if the given string represents a whitespaced-delimited array of integers.
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 * @return true if either an attribute exists.
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 // this shouldn't happen
445 assert false;
446 }
447 }
448 //if (nativeObject != null) return prefix + ":" + nativeObject.toString();
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 // Package functions
466 //*********************************************************************
467
468 /**
469 * Adds a child.
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 * @return the ith child of this XMLObject, without processing.
482 */
483 public Object getRawChild(int i) {
484 return children.get(i);
485 }
486
487 /**
488 * Sets the native object represented by this XMLObject.
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 // Static members
500 //*********************************************************************
501
502 //*********************************************************************
503 // Private methods
504 //*********************************************************************
505
506 /**
507 * @return the object as a boolean if possible
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 * @return the object as an double if possible
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 * @return the object as an double[] if possible
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 * @return the object as an double[] if possible
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 * @return the object as an integer if possible
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 * @return the object as an integer if possible
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 * @return the object as a string if possible
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 * @return the object as an double[] if possible
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 * @return the named attribute if it exists, throws XMLParseException otherwise.
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 // Private instance variables
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 // The objectStore representing the local scope of this element.
660 // private ObjectStore store;
661 }