001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.sample.simplexml.client;
017:
018: import com.google.gwt.core.client.EntryPoint;
019: import com.google.gwt.http.client.Request;
020: import com.google.gwt.http.client.RequestBuilder;
021: import com.google.gwt.http.client.RequestCallback;
022: import com.google.gwt.http.client.RequestException;
023: import com.google.gwt.http.client.Response;
024: import com.google.gwt.user.client.Window;
025: import com.google.gwt.user.client.ui.FlexTable;
026: import com.google.gwt.user.client.ui.FlowPanel;
027: import com.google.gwt.user.client.ui.HTML;
028: import com.google.gwt.user.client.ui.HTMLTable;
029: import com.google.gwt.user.client.ui.Label;
030: import com.google.gwt.user.client.ui.RootPanel;
031: import com.google.gwt.user.client.ui.TabPanel;
032: import com.google.gwt.xml.client.Document;
033: import com.google.gwt.xml.client.Element;
034: import com.google.gwt.xml.client.Node;
035: import com.google.gwt.xml.client.NodeList;
036: import com.google.gwt.xml.client.XMLParser;
037:
038: /**
039: * A very simple XML Example where we take a customer profile and display it on
040: * a page.
041: */
042: public class SimpleXML implements EntryPoint {
043: private static final String XML_LABEL_STYLE = "xmlLabel";
044: private static final String USER_TABLE_LABEL_STYLE = "userTableLabel";
045: private static final String USER_TABLE_STYLE = "userTable";
046: private static final String NOTES_STYLE = "notes";
047:
048: public void onModuleLoad() {
049: RequestBuilder requestBuilder = new RequestBuilder(
050: RequestBuilder.GET, "customerRecord.xml");
051:
052: try {
053: requestBuilder.sendRequest(null, new RequestCallback() {
054: public void onError(Request request, Throwable exception) {
055: requestFailed(exception);
056: }
057:
058: public void onResponseReceived(Request request,
059: Response response) {
060: renderXML(response.getText());
061: }
062: });
063: } catch (RequestException ex) {
064: requestFailed(ex);
065: }
066: }
067:
068: private FlexTable createOrderTable(FlowPanel xmlParsed, String label) {
069: HTML orderTableLabel = new HTML("<h2>" + label + "</h2>");
070: xmlParsed.add(orderTableLabel);
071: FlexTable orderTable = new FlexTable();
072: orderTable.setStyleName(USER_TABLE_STYLE);
073: orderTable.setBorderWidth(3);
074: orderTable.getRowFormatter().setStyleName(0,
075: USER_TABLE_LABEL_STYLE);
076: orderTable.setText(0, 0, "Order ID");
077: orderTable.setText(0, 1, "Item");
078: orderTable.setText(0, 2, "Ordered On");
079: orderTable.setText(0, 3, "Street");
080: orderTable.setText(0, 4, "City");
081: orderTable.setText(0, 5, "State");
082: orderTable.setText(0, 6, "Zip");
083: xmlParsed.add(orderTable);
084: return orderTable;
085: }
086:
087: /**
088: * Creates the xml representation of xmlText. xmlText is assumed to have been
089: * validated for structure on the server.
090: *
091: * @param xmlText xml text
092: * @param xmlParsed panel to display customer record
093: */
094: private void customerPane(String xmlText, FlowPanel xmlParsed) {
095: Document customerDom = XMLParser.parse(xmlText);
096: Element customerElement = customerDom.getDocumentElement();
097: // Must do this if you ever use a raw node list that you expect to be
098: // all elements.
099: XMLParser.removeWhitespace(customerElement);
100:
101: // Customer Name
102: String nameValue = getElementTextValue(customerElement, "name");
103: String title = "<h1>" + nameValue + "</h1>";
104: HTML titleHTML = new HTML(title);
105: xmlParsed.add(titleHTML);
106:
107: // Customer Notes
108: String notesValue = getElementTextValue(customerElement,
109: "notes");
110: Label notesText = new Label();
111: notesText.setStyleName(NOTES_STYLE);
112: notesText.setText(notesValue);
113: xmlParsed.add(notesText);
114:
115: // Pending orders UI setup
116: FlexTable pendingTable = createOrderTable(xmlParsed,
117: "Pending Orders");
118: FlexTable completedTable = createOrderTable(xmlParsed,
119: "Completed");
120: completedTable.setText(0, 7, "Shipped by");
121:
122: // Fill Orders Table
123: NodeList orders = customerElement.getElementsByTagName("order");
124: int pendingRowPos = 0;
125: int completedRowPos = 0;
126: for (int i = 0; i < orders.getLength(); i++) {
127: Element order = (Element) orders.item(i);
128: HTMLTable table;
129: int rowPos;
130: if (order.getAttribute("status").equals("pending")) {
131: table = pendingTable;
132: rowPos = ++pendingRowPos;
133: } else {
134: table = completedTable;
135: rowPos = ++completedRowPos;
136: }
137: int columnPos = 0;
138: fillInOrderTableRow(customerElement, order, table, rowPos,
139: columnPos);
140: }
141: }
142:
143: private void fillInOrderTableRow(Element customerElement,
144: Element order, HTMLTable table, int rowPos, int columnPos) {
145: // Order ID
146: String orderId = order.getAttribute("id");
147: table.setText(rowPos, columnPos++, orderId);
148:
149: // Item
150: Element item = (Element) order.getElementsByTagName("item")
151: .item(0);
152: String itemUPC = item.getAttribute("upc");
153: String itemName = item.getFirstChild().getNodeValue();
154: Label itemLabel = new Label(itemUPC);
155: itemLabel.setTitle(itemName);
156: table.setWidget(rowPos, columnPos++, itemLabel);
157:
158: // Ordered On
159: String orderedOnValue = getElementTextValue(customerElement,
160: "orderedOn");
161: table.setText(rowPos, columnPos++, orderedOnValue);
162:
163: // Address
164: Element address = (Element) order.getElementsByTagName(
165: "address").item(0);
166: XMLParser.removeWhitespace(address);
167: NodeList lst = address.getChildNodes();
168: for (int j = 0; j < lst.getLength(); j++) {
169: Element next = (Element) lst.item(j);
170: String addressPartText = next.getFirstChild()
171: .getNodeValue();
172: table.setText(rowPos, columnPos++, addressPartText);
173: }
174:
175: // Shipped By (optional attribute)
176: NodeList shippedByList = order
177: .getElementsByTagName("shippingInfo");
178: if (shippedByList.getLength() == 1) {
179: Element shippedBy = (Element) shippedByList.item(0);
180: // Depending upon the shipper, different attributes might be
181: // available, so XML carries the display info
182: FlexTable shippedByTable = new FlexTable();
183: shippedByTable.getRowFormatter().setStyleName(0,
184: USER_TABLE_LABEL_STYLE);
185: shippedByTable.setBorderWidth(1);
186: NodeList shippedByParts = shippedBy.getChildNodes();
187: for (int j = 0; j < shippedByParts.getLength(); j++) {
188: Node next = shippedByParts.item(j);
189: Element elem = (Element) next;
190: shippedByTable
191: .setText(0, j, elem.getAttribute("title"));
192: shippedByTable.setText(1, j, elem.getFirstChild()
193: .getNodeValue());
194: }
195: table.setWidget(rowPos, columnPos++, shippedByTable);
196: }
197: }
198:
199: /**
200: * Utility method to return the values of elements of the form <myTag>tag
201: * value</myTag>
202: */
203: private String getElementTextValue(Element parent, String elementTag) {
204: // If the xml is not coming from a known good source, this method would
205: // have to include safety checks.
206: return parent.getElementsByTagName(elementTag).item(0)
207: .getFirstChild().getNodeValue();
208: }
209:
210: private void renderXML(String xmlText) {
211: final TabPanel tab = new TabPanel();
212: final FlowPanel xmlSource = new FlowPanel();
213: final FlowPanel xmlParsed = new FlowPanel();
214: tab.add(xmlParsed, "Customer Pane");
215: tab.add(xmlSource, "XML Source");
216: tab.selectTab(0);
217: RootPanel.get().add(tab);
218: xmlPane(xmlText, xmlSource);
219: customerPane(xmlText, xmlParsed);
220: }
221:
222: private void requestFailed(Throwable exception) {
223: Window
224: .alert("Failed to send the request. The error message was: "
225: + exception.getMessage());
226: }
227:
228: /**
229: * Show the raw XML.
230: *
231: * @param xmlText
232: * @param xmlSource
233: */
234: private void xmlPane(String xmlText, final FlowPanel xmlSource) {
235: xmlText = xmlText.replaceAll("<", "<");
236: xmlText = xmlText.replaceAll(">", ">");
237: Label xml = new HTML("<pre>" + xmlText + "</pre>", false);
238: xml.setStyleName(XML_LABEL_STYLE);
239: xmlSource.add(xml);
240: }
241: }
|