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 | class HtmlPageElement { |
---|
29 | |
---|
30 | /** |
---|
31 | * path to the parent of this page element or null, if this is the root element |
---|
32 | */ |
---|
33 | private String parentPath; |
---|
34 | |
---|
35 | /** |
---|
36 | * the name of the tag represented by this page element. May also be the name of the server |
---|
37 | * or the page. |
---|
38 | */ |
---|
39 | private String tagName; |
---|
40 | |
---|
41 | /** |
---|
42 | * the id of the page element. May also be the name of the server including port number or the |
---|
43 | * URL of the page |
---|
44 | */ |
---|
45 | private String id; |
---|
46 | |
---|
47 | /** |
---|
48 | * the title of the page if a page is represented through this element |
---|
49 | */ |
---|
50 | private String title; |
---|
51 | |
---|
52 | /** |
---|
53 | * the index of this element regarding all elements with the same tag name in the list of |
---|
54 | * children of the parent element |
---|
55 | */ |
---|
56 | private Integer index; |
---|
57 | |
---|
58 | /** |
---|
59 | * the children of this element |
---|
60 | */ |
---|
61 | private List<HtmlPageElement> children; |
---|
62 | |
---|
63 | /** |
---|
64 | * <p> |
---|
65 | * instantiates a new element representing a tag in an HTML page |
---|
66 | * </p> |
---|
67 | * |
---|
68 | * @param parentPath the path through the DOM to the parent node of the represented tag |
---|
69 | * @param tagName the name of the represented tag |
---|
70 | * @param id the id of the tag in the DOM |
---|
71 | * @param index the index of the represented tag regarding all tags with the same tag name |
---|
72 | * in the list of children of the parent tag |
---|
73 | */ |
---|
74 | HtmlPageElement(String parentPath, String tagName, String id, Integer index) { |
---|
75 | this.parentPath = parentPath; |
---|
76 | this.tagName = tagName; |
---|
77 | this.id = id; |
---|
78 | this.index = index; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * <p> |
---|
83 | * instantiates a new element representing an HTML page |
---|
84 | * </p> |
---|
85 | * |
---|
86 | * @param parentPath the path through the DOM to the parent node of the represented tag which is |
---|
87 | * usually a server |
---|
88 | * @param tagName the name of the represented tag which is the path of the web page |
---|
89 | * @param title the title of the web page |
---|
90 | * @param id the id of the web page which is its path |
---|
91 | * @param index usually 0 |
---|
92 | */ |
---|
93 | HtmlPageElement(String parentPath, String tagName, String id, String title, Integer index) { |
---|
94 | this(parentPath, tagName, id, index); |
---|
95 | this.title = title; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * <p> |
---|
100 | * returns the name of the tag represented by this page element. May also be the name of the |
---|
101 | * server or the page. |
---|
102 | * </p> |
---|
103 | * |
---|
104 | * @return the tagName |
---|
105 | */ |
---|
106 | String getTagName() { |
---|
107 | return tagName; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * <p> |
---|
112 | * returns the id of the page element. May also be the name of the server including port |
---|
113 | * number or the URL of the page |
---|
114 | * </p> |
---|
115 | * |
---|
116 | * @return the id |
---|
117 | */ |
---|
118 | String getId() { |
---|
119 | return id; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * <p> |
---|
124 | * returns the title of the page if a page is represented through this element |
---|
125 | * </p> |
---|
126 | * |
---|
127 | * @return the title |
---|
128 | */ |
---|
129 | String getTitle() { |
---|
130 | return title; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * <p> |
---|
135 | * returns the index of this element regarding all elements with the same tag name in the list |
---|
136 | * of children of the parent element |
---|
137 | * </p> |
---|
138 | * |
---|
139 | * @return the index |
---|
140 | */ |
---|
141 | Integer getIndex() { |
---|
142 | return index; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * <p> |
---|
147 | * returns the children of this element if any, null else |
---|
148 | * </p> |
---|
149 | * |
---|
150 | * @return the children |
---|
151 | */ |
---|
152 | List<HtmlPageElement> getChildren() { |
---|
153 | return children; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /** |
---|
158 | * adds a child to this element |
---|
159 | * |
---|
160 | * @param child the child to be added |
---|
161 | */ |
---|
162 | void addChild(HtmlPageElement child) { |
---|
163 | if (child != null) { |
---|
164 | if (children == null) { |
---|
165 | children = new ArrayList<HtmlPageElement>(); |
---|
166 | } |
---|
167 | |
---|
168 | children.add(child); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * <p> |
---|
174 | * returns the path to the parent of this page element or null, if this is the root element |
---|
175 | * </p> |
---|
176 | * |
---|
177 | * @return as described |
---|
178 | */ |
---|
179 | String getParentPath() { |
---|
180 | return parentPath; |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * <p> |
---|
185 | * calculates and returns the path to the represented page element through the DOM. Includes |
---|
186 | * the parent path if any. The represented element is included in the path with its tag name |
---|
187 | * and its id if it has one, or its index. |
---|
188 | * </p> |
---|
189 | * |
---|
190 | * @return as described |
---|
191 | */ |
---|
192 | String getPath() { |
---|
193 | StringBuffer result = new StringBuffer(); |
---|
194 | if (parentPath != null) { |
---|
195 | result.append(parentPath); |
---|
196 | } |
---|
197 | |
---|
198 | result.append("/"); |
---|
199 | result.append(tagName); |
---|
200 | |
---|
201 | if ((id != null) && (!"".equals(id))) { |
---|
202 | result.append("(id="); |
---|
203 | result.append(id); |
---|
204 | result.append(")"); |
---|
205 | } |
---|
206 | else { |
---|
207 | result.append("["); |
---|
208 | result.append(index); |
---|
209 | result.append("]"); |
---|
210 | } |
---|
211 | |
---|
212 | return result.toString(); |
---|
213 | } |
---|
214 | |
---|
215 | } |
---|