1   /**
2    *
3    */
4   package eu.fbk.dkm.pikes.resources.reader;
5   
6   import java.util.ArrayList;
7   import java.util.HashMap;
8   
9   
10  public class DataElementNode extends DataNode {
11  	public String name;
12  	public HashMap<String, String> attributes = new HashMap(0);
13  	public ArrayList<DataNode> children = new ArrayList(0);
14  
15  	DataElementNode(String name) {
16  		this.name = name;
17  	}
18  
19  	public String getText() {
20  		if (!name.equals("__ROOT__")) {
21  			throw new IllegalArgumentException("may only call on root node");
22  		}
23  		//if(children.size() != 1)
24  		//throw new IllegalArgumentException("must contain single text node: node = " + this);
25  		StringBuilder out = new StringBuilder();
26  
27  		for (DataNode n : children) {
28  			if (!(n instanceof DataTextNode)) {
29  				throw new IllegalArgumentException("must contain only text nodes");
30  			}
31  			out.append(((DataTextNode) n).text);
32  		}
33  		return out.toString();
34  	}
35  
36  	public String getTopAttribute(String key) {
37  		if (!name.equals("__ROOT__")) {
38  			throw new IllegalArgumentException("may only call on root node");
39  		}
40  		if (children.size() != 1) {
41  			throw new IllegalArgumentException("must contain single element node: node = " + this);
42  		}
43  		DataNode n = children.get(0);
44  
45  		if (!(n instanceof DataElementNode)) {
46  			throw new IllegalArgumentException("must contain single element node");
47  		}
48  		return ((DataElementNode) n).attributes.get(key);
49  	}
50  
51  	public String getTopName(String key) {
52  		if (!name.equals("__ROOT__")) {
53  			throw new IllegalArgumentException("may only call on root node");
54  		}
55  		if (children.size() != 1) {
56  			throw new IllegalArgumentException("must contain single element node: node = " + this);
57  		}
58  		DataNode n = children.get(0);
59  
60  		if (!(n instanceof DataElementNode)) {
61  			throw new IllegalArgumentException("must contain single element node");
62  		}
63  		return ((DataElementNode) n).name;
64  	}
65  
66  	@Override
67  	public String toString() {
68  		return "DataElementNode{" +
69  				"name='" + name + '\'' +
70  				", attributes=" + attributes +
71  				", children=" + children +
72  				'}';
73  	}
74  }