1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.htmlmonitor; |
---|
16 | |
---|
17 | import java.util.ArrayList; |
---|
18 | import java.util.List; |
---|
19 | |
---|
20 | /** |
---|
21 | * <p> |
---|
22 | * represents an element of an HTML GUI. This can be a server, which is usually the root of a |
---|
23 | * GUI structure, a web page, or a tag within a web page. |
---|
24 | * </p> |
---|
25 | * |
---|
26 | * @author Patrick Harms |
---|
27 | */ |
---|
28 | abstract class HtmlGUIElement { |
---|
29 | |
---|
30 | /** |
---|
31 | * the children of this element |
---|
32 | */ |
---|
33 | private List<HtmlGUIElement> children; |
---|
34 | |
---|
35 | /** |
---|
36 | * the id of this GUI element |
---|
37 | */ |
---|
38 | private String id; |
---|
39 | |
---|
40 | /** |
---|
41 | * the parent GUI element, null if none is present |
---|
42 | */ |
---|
43 | private HtmlGUIElement parent; |
---|
44 | |
---|
45 | /** |
---|
46 | * <p> |
---|
47 | * instantiates a new GUI element |
---|
48 | * </p> |
---|
49 | * |
---|
50 | * @param parentId the parent GUI element, null if none is present |
---|
51 | */ |
---|
52 | protected HtmlGUIElement(String id, HtmlGUIElement parent) { |
---|
53 | if (id == null) { |
---|
54 | throw new IllegalArgumentException("id must not be null"); |
---|
55 | } |
---|
56 | |
---|
57 | this.id = id; |
---|
58 | this.parent = parent; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * <p> |
---|
63 | * returns the children of this element if any, null else |
---|
64 | * </p> |
---|
65 | * |
---|
66 | * @return the children |
---|
67 | */ |
---|
68 | List<HtmlGUIElement> getChildren() { |
---|
69 | return children; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | * adds a child to this element. Merges children, for which the equal method returns true |
---|
75 | * |
---|
76 | * @param child the child to be added |
---|
77 | */ |
---|
78 | void addChild(HtmlGUIElement newChild) { |
---|
79 | if (newChild != null) { |
---|
80 | if (children == null) { |
---|
81 | children = new ArrayList<HtmlGUIElement>(); |
---|
82 | } |
---|
83 | |
---|
84 | boolean added = false; |
---|
85 | for (HtmlGUIElement child : children) { |
---|
86 | if (child.equals(newChild)) { |
---|
87 | if (newChild.getChildren() != null) { |
---|
88 | for (HtmlGUIElement subchild : newChild.getChildren()) { |
---|
89 | child.addChild(subchild); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | added = true; |
---|
94 | break; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | if (!added) { |
---|
99 | children.add(newChild); |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * returns the id of the GUI element. |
---|
106 | * |
---|
107 | * @return the id of the GUI element |
---|
108 | */ |
---|
109 | String getId() { |
---|
110 | return id; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * <p> |
---|
115 | * returns the id of the parent GUI element, if any |
---|
116 | * </p> |
---|
117 | * |
---|
118 | * @return the id of the parent GUI element, or null, if this element does not have a parent |
---|
119 | */ |
---|
120 | String getParentId() { |
---|
121 | if (parent == null) { |
---|
122 | return null; |
---|
123 | } |
---|
124 | else { |
---|
125 | return parent.getId(); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | } |
---|