KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kelp > webapp > presentation > TableServlet


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22 package kelp.webapp.presentation;
23
24 // XMLC imports
25
import org.enhydra.xml.xmlc.XMLCUtil;
26
27 // Xerces imports
28
import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.html.HTMLTableElement;
31 import org.w3c.dom.html.HTMLTableRowElement;
32
33 // Servlet imports
34
import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.ServletOutputStream JavaDoc;
36 import javax.servlet.http.HttpServlet JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39
40 // Standard imports
41
import java.io.IOException JavaDoc;
42
43 /**
44  * <p>
45  * This presentation object dynamically creates an HTML page
46  * showing the greeting from the application configuration file.
47  * </p>
48  */

49 public class TableServlet extends HttpServlet JavaDoc {
50     public void doGet(HttpServletRequest JavaDoc request,
51                       HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
52                       IOException JavaDoc {
53
54         TableHTML page = null;
55         ServletOutputStream JavaDoc out;
56         byte[] buffer;
57
58         page = createPage();
59         buffer = page.toDocument().getBytes();
60         response.setContentType( "text/html" );
61         response.setContentLength( buffer.length );
62         out = response.getOutputStream();
63         out.write(buffer);
64         out.flush();
65         response.flushBuffer();
66
67     }
68
69     public TableHTML createPage() {
70         String JavaDoc[] names = {
71             "Travis Shook", "Kelly Rose", "Lloyd Tabb", "David Toth",
72             "Adam Menkes", "Randy Solton"
73         };
74         TableHTML page = null;
75         HTMLTableRowElement newRow = null;
76         HTMLTableRowElement dataRow = null;
77         HTMLTableElement table = null;
78
79         page = new TableHTML();
80         table = page.getElementSampleTable();
81         dataRow = page.getElementDataRow();
82         clearTable(table, dataRow);
83         for (int i = 0; i < names.length; i++) {
84             newRow = createNewRow(dataRow, names[i], i);
85             table.appendChild(newRow);
86         }
87         return page;
88     }
89
90     private HTMLTableRowElement createNewRow(HTMLTableRowElement dataRow,
91                                              String JavaDoc newData, int i) {
92         HTMLTableRowElement rowNew = null;
93         Element JavaDoc dataCell = null;
94
95         rowNew = (HTMLTableRowElement) dataRow.cloneNode(true);
96         dataCell = XMLCUtil.getRequiredElementById("cellOne", rowNew);
97         XMLCUtil.getFirstText(dataCell).setData("" + (i + 1));
98         dataCell = XMLCUtil.getRequiredElementById("cellTwo", rowNew);
99         XMLCUtil.getFirstText(dataCell).setData(newData);
100         return rowNew;
101     }
102
103     /**
104      * Delete all table elements that do not have IDs and
105      * also delete data row.
106      */

107     protected void clearTable(Node JavaDoc node, HTMLTableRowElement dataRow) {
108         if (node instanceof HTMLTableRowElement) {
109             String JavaDoc id = ((HTMLTableRowElement) node).getId();
110
111             if ((id == null) || (id.length() == 0) || (node == dataRow)) {
112                 node.getParentNode().removeChild(node);
113             }
114         } else {
115
116             // Not a row, search children.
117
Node JavaDoc child = node.getFirstChild();
118
119             while (child != null) {
120                 Node JavaDoc next = child.getNextSibling();
121
122                 clearTable(child, dataRow);
123                 child = next;
124             }
125         }
126     }
127
128 }
129
Popular Tags