Index: trunk/autoquest-htmlmonitor/src/main/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlMonitorServlet.java
===================================================================
--- trunk/autoquest-htmlmonitor/src/main/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlMonitorServlet.java	(revision 1022)
+++ trunk/autoquest-htmlmonitor/src/main/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlMonitorServlet.java	(revision 1069)
@@ -78,4 +78,9 @@
      * the message listener to forward received messages to.
      */
+    private HtmlGUIElementManager guiElementManager = new HtmlGUIElementManager();
+
+    /**
+     * the message listener to forward received messages to.
+     */
     private transient HtmlMonitorMessageListener messageListener;
 
@@ -199,5 +204,5 @@
             }
             else {
-                HtmlPageElement guiStructure = extractHtmlPageElements(message, clientInfos);
+                HtmlGUIElement guiStructure = extractHtmlPageElements(message, clientInfos);
                 HtmlEvent[] events = extractHtmlEvents(message, clientInfos);
                 
@@ -275,6 +280,6 @@
             events = new ArrayList<HtmlEvent>();
             
-            HtmlPageElement server = getServerElement(clientInfos);
-            HtmlPageElement page = getPageElementRepresentingWebPage(clientInfos, server);
+            HtmlServer server = getServerElement(clientInfos);
+            HtmlDocument document = getPageElementRepresentingWebPage(clientInfos, server);
 
             for (int i = 0; i < eventArray.size(); i++) {
@@ -285,5 +290,5 @@
                 else {
                     Long time = assertValue(((JSONObject) eventObj), "time", Long.class);
-                    String path = assertValue(((JSONObject) eventObj), "path", String.class);
+                    String domPath = assertValue(((JSONObject) eventObj), "path", String.class);
                     String eventType =
                         assertValue(((JSONObject) eventObj), "eventType", String.class);
@@ -302,6 +307,6 @@
                         Console.printerrln(eventType + " event has no valid timestamp");
                     }
-                    else if (path == null) {
-                        Console.printerrln(eventType + " event has no valid path");
+                    else if (domPath == null) {
+                        Console.printerrln(eventType + " event has no valid DOM path");
                     }
                     else if ((coordinates != null) && (coordinates.length != 2)) {
@@ -311,7 +316,17 @@
                                 (eventType, coordinates, key, scrollPosition, selectedValue))
                     {
-                        path = page.getPath() + path;
-                        events.add(new HtmlEvent(clientInfos, time, path, eventType,
-                                                 coordinates, key, scrollPosition, selectedValue));
+                        HtmlPageElement target =
+                            guiElementManager.getPageElement(document, domPath);
+                        
+                        if (target != null) {
+                            events.add(new HtmlEvent(clientInfos, time, target, eventType,
+                                                     coordinates, key, scrollPosition,
+                                                     selectedValue));
+                        }
+                        else {
+                            events.add(new HtmlEvent(clientInfos, time, document, domPath,
+                                                     eventType, coordinates, key, scrollPosition,
+                                                     selectedValue));
+                        }
                     }
                     else {
@@ -342,12 +357,12 @@
      *         representation of the server of the HTML page that was observed 
      */
-    private HtmlPageElement extractHtmlPageElements(JSONObject      object,
-                                                    HtmlClientInfos clientInfos)
+    private HtmlServer extractHtmlPageElements(JSONObject      object,
+                                               HtmlClientInfos clientInfos)
     {
-        HtmlPageElement server = getServerElement(clientInfos);
-        HtmlPageElement page = getPageElementRepresentingWebPage(clientInfos, server);
+        HtmlServer server = getServerElement(clientInfos);
+        HtmlDocument document = getPageElementRepresentingWebPage(clientInfos, server);
 
         JSONObject jsonPageElement = assertValue(object, "guiModel", JSONObject.class);
-        page.addChild(convert(jsonPageElement, page.getPath()));
+        document.addChild(convert(jsonPageElement, document, null));
         
         return server;
@@ -364,11 +379,13 @@
      * @return as described
      */
-    private HtmlPageElement getServerElement(HtmlClientInfos clientInfos) {
-        String id = clientInfos.getUrl().getHost();
+    private HtmlServer getServerElement(HtmlClientInfos clientInfos) {
+        String host = clientInfos.getUrl().getHost();
+        int port = 80;
+        
         if (clientInfos.getUrl().getPort() > -1) {
-            id += ":" + clientInfos.getUrl().getPort();
-        }
-        
-        return new HtmlPageElement(null, "server", id, 0);
+            port = clientInfos.getUrl().getPort();
+        }
+        
+        return guiElementManager.createHtmlServer(host, port);
     }
 
@@ -384,19 +401,20 @@
      * @return as described
      */
-    private HtmlPageElement getPageElementRepresentingWebPage(HtmlClientInfos clientInfos,
-                                                              HtmlPageElement server)
+    private HtmlDocument getPageElementRepresentingWebPage(HtmlClientInfos clientInfos,
+                                                           HtmlServer      server)
     {
-        String id = clientInfos.getUrl().getPath();
+        String path = clientInfos.getUrl().getPath();
+        String query = null;
         
         if (clientInfos.getUrl().getQuery() != null) {
-            id += "?" + clientInfos.getUrl().getQuery();
-        }
-        
-        HtmlPageElement page =
-            new HtmlPageElement(server.getPath(), "document", id, clientInfos.getTitle(), 0);
-        
-        server.addChild(page);
-        
-        return page;
+            query = "?" + clientInfos.getUrl().getQuery();
+        }
+        
+        HtmlDocument document = guiElementManager.createHtmlDocument
+            (server, path, query, clientInfos.getTitle());
+        
+        server.addChild(document);
+        
+        return document;
     }
 
@@ -408,18 +426,24 @@
      *
      * @param jsonPageElement the JSON object to be converted
-     * @param parentPath      the path through the DOM of the parent HTML page element of the
-     *                        converted one
+     * @param document        the document to which the page element belongs
+     * @param parent          the parent page element of the converted element, of null, if none
+     *                        is present. In this case the document is considered the parent
+     *                        element.
      *                        
      * @return as described.
      */
-    private HtmlPageElement convert(JSONObject jsonPageElement, String parentPath) {
+    private HtmlPageElement convert(JSONObject      jsonPageElement,
+                                    HtmlDocument    document,
+                                    HtmlPageElement parent)
+    {
         HtmlPageElement result = null;
 
         if (jsonPageElement != null) {
             String tagName = assertValue(jsonPageElement, "tagName", String.class);
-            String id = assertValue(jsonPageElement, "id", String.class);
+            String htmlid = assertValue(jsonPageElement, "htmlId", String.class);
             Integer index = assertValue(jsonPageElement, "index", Integer.class);
 
-            result = new HtmlPageElement(parentPath, tagName, id, index);
+            result = guiElementManager.createHtmlPageElement
+                (document, parent, tagName, htmlid, index);
 
             JSONArray childElements = assertValue(jsonPageElement, "children", JSONArray.class);
@@ -435,5 +459,5 @@
                     }
                     else {
-                        result.addChild(convert((JSONObject) jsonChild, result.getPath()));
+                        result.addChild(convert((JSONObject) jsonChild, document, result));
                     }
                 }
