in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 56 57 58 59 60 61 < 62 > 63 64 65 66 67 68 .. 166 >> Next

My approach to exploring these J2ME pitfalls is to port a J2SE application to the J2ME platform. First, we take the most direct approach and then optimize our approach. Unfortunately, to demonstrate a nontrivial application (which we do) requires significant amounts of code. To reduce the size of the code listings, I have deleted simple, similar, and redundant code. Additionally, to avoid long runs of uninterrupted code, I have interspersed the code descriptions and explanations for all major methods instead of putting it all after the code.
J2ME Performance and Pitfalls 163
The J2SE application we will port is called SwinginAmazonSearch. The main frame of the application is displayed in Figure 22.1.
The purpose of the application is to enable you to query's database using a Representational State Transfer (REST) approach. REST is an architectural style that describes how the World Wide Web (WWW) works. The term was coined by Roy Fielding in his Ph.D. thesis (available at http:// /dissertation/top.htm). The Web uses Uniform Resource Identifiers (URIs) to retrieve representation and to change state by transferring to other representations. Amazon implemented this by encoding all query parameters in the URI and returning an XML document as the resulting representation.
The layout of the main interface in Figure 22.1 is divided into two halves: the top half to select and enter search terms and the bottom half to display the results after clicking "Search."After the results are displayed, you can examine more detail on a single entry by clicking "Details" or receive an additional "page" of results by clicking "Next Results." limits all queries to a single "page" of 10 hits and requires all queries to include a page number. Table 22.1 lists and defines the key parameters in an query.
Figure 22.1 A Swing-based Amazon search application.
164 Item 22
Table 22.1 Query parameters
Operation KeywordSearch, Free text with BrowseNodeSearch, keywords, authors, AsinSearch, UpcSearch, etc. corresponding AuthorSearch, to the search type. ArtistSearch, ActorSearch, DirectorSearch, ManufacturerSearch, ListManiaSearch, SimilaritySearch The type of search to perform.
Mode mode baby, books, classical, dvd, electronics, garden, kitchen, magazines, music, pc-hardware, photo, software, toys, universal, vhs, videogames A taxonomy of areas to search. Would be better to have been called category.
Return Type type lite, heavy The DTD of the returned XML where the heavy version contains many more elements than the lite version.
Page # page An integer The page of 10 hits to return (if available).
Format f xml or URI to an XSLT stylesheet A return in XML or any format generated by the XSLT stylesheet.
Now let's examine the source code in Listing 22.1 that implements the application.
001: /* */ 002: package org.javapitfalls.item22; 003: // - removed Import statements 015: 016: class SwinginAmazonSearch extends JFrame implements ActionListener 017: { 018: public static final boolean debug; 019: // - removed static block to set debug variable
Listing 22.1
J2ME Performance and Pitfalls 165
036 public static final String CMD_SEARCH = "Search";
041 JButton searchButton = new JButton(CMD_SEARCH);
042 JButton detailsButton = new JButton(CMD_DETAILS);
043 JButton nextResultsButton = new JButton(CMD_NEXT_TEN);
044 JList results = new JList();
045 JComboBox opsCombo = new JComboBox(AmazonHttpGet.legalOps);
046 JComboBox modeCombo = new JComboBox(AmazonHttpGet.legalModes);
047 JTextField targetTF = new JTextField(40);
048 StatusPanel status = new StatusPanel();
049 AmazonHttpGet getter = new AmazonHttpGet();
050 int page = 1;
052 DocumentBuilderFactory dbf;
053 DocumentBuilder db;
054 Document doc;
055 NodeList productNodes;
Listing 22.1 (continued)
The class SwinginAmazonSearch extends a Swing JFrame and implements the Action Listener interface to receive events from the buttons on the main window. Lines 16-56 of the code contain the class declaration and class data members. The data members consist of some static constants, GUI components (JButton, JList, JComboBox), a reference to a class performing the HTTP networking functions called Amazon-HttpGet, and references to XML parsing and the W3C Document class to manipulate the returned XML. It is important to note the use of static constants for all fixed Strings (like in line 36 and those deleted) in case the protocol changes at a later date. This then makes it easy to modify the protocol by only changing the string in one location instead of hunting down the occurrence of each String where it is used. Such "future proofing" is a good habit for J2SE/J2EE development, but one that wastes precious heap space in J2ME. We will see workarounds for this later. The constructor (below) creates and displays the main frame.
Previous << 1 .. 56 57 58 59 60 61 < 62 > 63 64 65 66 67 68 .. 166 >> Next