source: trunk/autoquest-htmlmonitor/src/main/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlGUIElementManager.java @ 1075

Last change on this file since 1075 was 1075, checked in by pharms, 11 years ago
  • prevented findbugs warnings
File size: 5.8 KB
Line 
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
15package de.ugoe.cs.autoquest.htmlmonitor;
16
17import java.security.MessageDigest;
18import java.util.HashMap;
19import java.util.LinkedList;
20import java.util.List;
21import java.util.Map;
22
23import org.apache.commons.codec.binary.Base64;
24
25/**
26 * <p>
27 * TODO comment
28 * </p>
29 *
30 * @author Patrick Harms
31 */
32class HtmlGUIElementManager {
33
34    /** */
35    private Map<String, HtmlGUIElement> idMap = new HashMap<String, HtmlGUIElement>();
36   
37    /** */
38    private Map<String, List<HtmlPageElement>> domPathMap =
39          new HashMap<String, List<HtmlPageElement>>();
40   
41    /**
42     * <p>
43     * TODO: comment
44     * </p>
45     *
46     */
47    HtmlServer createHtmlServer(String name, int port) {
48        if (name == null) {
49            throw new IllegalArgumentException("name must not be null");
50        }
51       
52        String id = calculateId(name, Integer.toString(port));
53       
54        HtmlGUIElement server = idMap.get(id);
55       
56        if (server == null) {
57            server = new HtmlServer(id, name, port);
58            idMap.put(id, server);
59        }
60        else if (!(server instanceof HtmlServer)) {
61            throw new RuntimeException
62                ("id conflict: calculated the same id for two different GUI elements");
63        }
64       
65        return (HtmlServer) server;
66    }
67   
68    /**
69     * <p>
70     * TODO: comment
71     * </p>
72     *
73     */
74    HtmlDocument createHtmlDocument(HtmlServer server, String path, String query, String title) {
75        if (server == null) {
76            throw new IllegalArgumentException("server must not be null");
77        }
78        if (path == null) {
79            throw new IllegalArgumentException("path must not be null");
80        }
81
82        String id = calculateId(server.getId(), path, query, title);
83       
84        HtmlGUIElement document = idMap.get(id);
85
86        if (document == null) {
87            document = new HtmlDocument(id, server, path, query, title);
88            idMap.put(id, document);
89        }
90        else if (!(document instanceof HtmlDocument)) {
91            throw new RuntimeException
92                ("id conflict: calculated the same id for two different GUI elements");
93        }
94       
95        return (HtmlDocument) document;
96    }
97
98    /**
99     * <p>
100     * TODO: comment
101     * </p>
102     *
103     * @param document
104     * @param parent
105     * @param tagName
106     * @param htmlid
107     * @param index
108     * @return
109     */
110    HtmlPageElement createHtmlPageElement(HtmlDocument    document,
111                                          HtmlPageElement parent,
112                                          String          tagName,
113                                          String          htmlId,
114                                          Integer         index)
115    {
116        if (document == null) {
117            throw new IllegalArgumentException("document must not be null");
118        }
119        if (tagName == null) {
120            throw new IllegalArgumentException("document must not be null");
121        }
122
123        String id = calculateId
124            (document.getId(), parent != null ? parent.getDOMPath() : null, tagName, htmlId,
125             index != null ? index.toString() : "-1");
126
127        HtmlGUIElement pageElement = idMap.get(id);
128
129        if (pageElement == null) {
130            pageElement = new HtmlPageElement(id, document, parent, tagName, htmlId, index);
131            idMap.put(id, pageElement);
132        }
133        else if (!(pageElement instanceof HtmlPageElement)) {
134            throw new RuntimeException
135                ("id conflict: calculated the same id for two different GUI elements");
136        }
137           
138        List<HtmlPageElement> candidates =
139            domPathMap.get(((HtmlPageElement) pageElement).getDOMPath());
140       
141        if (candidates == null) {
142            candidates = new LinkedList<HtmlPageElement>();
143            domPathMap.put(((HtmlPageElement) pageElement).getDOMPath(), candidates);
144        }
145       
146        if (!candidates.contains(pageElement)) {
147            candidates.add((HtmlPageElement) pageElement);
148        }
149
150        return (HtmlPageElement) pageElement;
151    }
152
153    /**
154     * <p>
155     * TODO: comment
156     * </p>
157     *
158     * @param document
159     * @param domPath
160     * @return
161     */
162    HtmlPageElement getPageElement(HtmlDocument document, String domPath) {
163        List<HtmlPageElement> candidates = domPathMap.get(domPath);
164       
165        if (candidates != null) {
166            for (HtmlPageElement candidate : candidates) {
167                if (document.equals(candidate.getDocument())) {
168                    return candidate;
169                }
170            }
171        }
172       
173        return null;
174    }
175
176    /**
177     * <p>
178     * TODO: comment
179     * </p>
180     *
181     * @param name
182     * @param string
183     * @return
184     */
185    private String calculateId(String... fragments) {
186        try {
187            MessageDigest md = MessageDigest.getInstance("SHA-512");
188           
189            for (String fragment : fragments) {
190                if (fragment != null) {
191                    md.update(fragment.getBytes("UTF-8"));
192                }
193            }
194           
195            return Base64.encodeBase64String(md.digest());
196        }
197        catch (Exception e) {
198            throw new IllegalStateException("Java VM does not support this code");
199        }
200    }
201
202}
Note: See TracBrowser for help on using the repository browser.