KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > simplexml > client > SimpleXML


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.sample.simplexml.client;
17
18 import com.google.gwt.core.client.EntryPoint;
19 import com.google.gwt.user.client.HTTPRequest;
20 import com.google.gwt.user.client.ResponseTextHandler;
21 import com.google.gwt.user.client.ui.FlexTable;
22 import com.google.gwt.user.client.ui.FlowPanel;
23 import com.google.gwt.user.client.ui.HTML;
24 import com.google.gwt.user.client.ui.HTMLTable;
25 import com.google.gwt.user.client.ui.Label;
26 import com.google.gwt.user.client.ui.RootPanel;
27 import com.google.gwt.user.client.ui.TabPanel;
28 import com.google.gwt.xml.client.Document;
29 import com.google.gwt.xml.client.Element;
30 import com.google.gwt.xml.client.Node;
31 import com.google.gwt.xml.client.NodeList;
32 import com.google.gwt.xml.client.XMLParser;
33
34 /**
35  * A very simple XML Example where we take a customer profile and display it on
36  * a page.
37  */

38 public class SimpleXML implements EntryPoint {
39   private static final String JavaDoc XML_LABEL_STYLE = "xmlLabel";
40   private static final String JavaDoc USER_TABLE_LABEL_STYLE = "userTableLabel";
41   private static final String JavaDoc USER_TABLE_STYLE = "userTable";
42   private static final String JavaDoc NOTES_STYLE = "notes";
43
44   public void onModuleLoad() {
45     HTTPRequest.asyncGet("customerRecord.xml", new ResponseTextHandler() {
46       public void onCompletion(String JavaDoc responseText) {
47         // In the real world, this text would come as a RPC response. This
48
// technique is great for testing and samples though!
49
renderXML(responseText);
50       }
51
52       private FlexTable createOrderTable(FlowPanel xmlParsed, String JavaDoc label) {
53         HTML orderTableLabel = new HTML("<h2>" + label + "</h2>");
54         xmlParsed.add(orderTableLabel);
55         FlexTable orderTable = new FlexTable();
56         orderTable.setStyleName(USER_TABLE_STYLE);
57         orderTable.setBorderWidth(3);
58         orderTable.getRowFormatter().setStyleName(0, USER_TABLE_LABEL_STYLE);
59         orderTable.setText(0, 0, "Order ID");
60         orderTable.setText(0, 1, "Item");
61         orderTable.setText(0, 2, "Ordered On");
62         orderTable.setText(0, 3, "Street");
63         orderTable.setText(0, 4, "City");
64         orderTable.setText(0, 5, "State");
65         orderTable.setText(0, 6, "Zip");
66         xmlParsed.add(orderTable);
67         return orderTable;
68       }
69
70       /**
71        * Creates the xml representation of xmlText. xmlText is assumed to have
72        * been validated for structure on the server.
73        *
74        * @param xmlText xml text
75        * @param xmlParsed panel to display customer record
76        */

77       private void customerPane(String JavaDoc xmlText, FlowPanel xmlParsed) {
78         Document customerDom = XMLParser.parse(xmlText);
79         Element customerElement = customerDom.getDocumentElement();
80         // Must do this if you ever use a raw node list that you expect to be
81
// all elements.
82
XMLParser.removeWhitespace(customerElement);
83
84         // Customer Name
85
String JavaDoc nameValue = getElementTextValue(customerElement, "name");
86         String JavaDoc title = "<h1>" + nameValue + "</h1>";
87         HTML titleHTML = new HTML(title);
88         xmlParsed.add(titleHTML);
89
90         // Customer Notes
91
String JavaDoc notesValue = getElementTextValue(customerElement, "notes");
92         Label notesText = new Label();
93         notesText.setStyleName(NOTES_STYLE);
94         notesText.setText(notesValue);
95         xmlParsed.add(notesText);
96
97         // Pending orders UI setup
98
FlexTable pendingTable = createOrderTable(xmlParsed, "Pending Orders");
99         FlexTable completedTable = createOrderTable(xmlParsed, "Completed");
100         completedTable.setText(0, 7, "Shipped by");
101
102         // Fill Orders Table
103
NodeList orders = customerElement.getElementsByTagName("order");
104         int pendingRowPos = 0;
105         int completedRowPos = 0;
106         for (int i = 0; i < orders.getLength(); i++) {
107           Element order = (Element) orders.item(i);
108           HTMLTable table;
109           int rowPos;
110           if (order.getAttribute("status").equals("pending")) {
111             table = pendingTable;
112             rowPos = ++pendingRowPos;
113           } else {
114             table = completedTable;
115             rowPos = ++completedRowPos;
116           }
117           int columnPos = 0;
118           fillInOrderTableRow(customerElement, order, table, rowPos, columnPos);
119         }
120       }
121
122       private void fillInOrderTableRow(Element customerElement, Element order,
123           HTMLTable table, int rowPos, int columnPos) {
124         // Order ID
125
String JavaDoc orderId = order.getAttribute("id");
126         table.setText(rowPos, columnPos++, orderId);
127
128         // Item
129
Element item = (Element) order.getElementsByTagName("item").item(0);
130         String JavaDoc itemUPC = item.getAttribute("upc");
131         String JavaDoc itemName = item.getFirstChild().getNodeValue();
132         Label itemLabel = new Label(itemUPC);
133         itemLabel.setTitle(itemName);
134         table.setWidget(rowPos, columnPos++, itemLabel);
135
136         // Ordered On
137
String JavaDoc orderedOnValue = getElementTextValue(customerElement,
138           "orderedOn");
139         table.setText(rowPos, columnPos++, orderedOnValue);
140
141         // Address
142
Element address = (Element) order.getElementsByTagName("address").item(
143           0);
144         XMLParser.removeWhitespace(address);
145         NodeList lst = address.getChildNodes();
146         for (int j = 0; j < lst.getLength(); j++) {
147           Element next = (Element) lst.item(j);
148           String JavaDoc addressPartText = next.getFirstChild().getNodeValue();
149           table.setText(rowPos, columnPos++, addressPartText);
150         }
151
152         // Shipped By (optional attribute)
153
NodeList shippedByList = order.getElementsByTagName("shippingInfo");
154         if (shippedByList.getLength() == 1) {
155           Element shippedBy = (Element) shippedByList.item(0);
156           // Depending upon the shipper, different attributes might be
157
// available, so XML carries the display info
158
FlexTable shippedByTable = new FlexTable();
159           shippedByTable.getRowFormatter().setStyleName(0,
160             USER_TABLE_LABEL_STYLE);
161           shippedByTable.setBorderWidth(1);
162           NodeList shippedByParts = shippedBy.getChildNodes();
163           for (int j = 0; j < shippedByParts.getLength(); j++) {
164             Node next = shippedByParts.item(j);
165             Element elem = (Element) next;
166             shippedByTable.setText(0, j, elem.getAttribute("title"));
167             shippedByTable.setText(1, j, elem.getFirstChild().getNodeValue());
168           }
169           table.setWidget(rowPos, columnPos++, shippedByTable);
170         }
171       }
172
173       /**
174        * Utility method to return the values of elements of the form <myTag>tag
175        * value</myTag>
176        */

177       private String JavaDoc getElementTextValue(Element parent, String JavaDoc elementTag) {
178         // If the xml is not coming from a known good source, this method would
179
// have to include safety checks.
180
return parent.getElementsByTagName(elementTag).item(0).getFirstChild()
181           .getNodeValue();
182       }
183
184       private void renderXML(String JavaDoc xmlText) {
185      
186         final TabPanel tab = new TabPanel();
187         final FlowPanel xmlSource = new FlowPanel();
188         final FlowPanel xmlParsed = new FlowPanel();
189         tab.add(xmlParsed, "Customer Pane");
190         tab.add(xmlSource, "XML Source");
191         tab.selectTab(0);
192         RootPanel.get().add(tab);
193         xmlPane(xmlText, xmlSource);
194         customerPane(xmlText, xmlParsed);
195       }
196
197       /**
198        * Show the raw XML.
199        *
200        * @param xmlText
201        * @param xmlSource
202        */

203       private void xmlPane(String JavaDoc xmlText, final FlowPanel xmlSource) {
204         xmlText = xmlText.replaceAll("<", "&#60;");
205         xmlText = xmlText.replaceAll(">", "&#62;");
206         Label xml = new HTML("<pre>" + xmlText + "</pre>", false);
207         xml.setStyleName(XML_LABEL_STYLE);
208         xmlSource.add(xml);
209       }
210     });
211   }
212 }
213
Popular Tags