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

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