Help is available for each task.
Task 1
The first task with virtually any Bean is to handle the implications of the no-argument constructor. In this case, you can't open/access the URL from the constructor; you must wait until the URL is established with an access method, either programmatically or via a property sheet. With this in mind, design and implement:
- A no-argument constructor
- A method openUrl() that creates a URL instance, given a string URL that's passed to the URL constructor
- A string URL property to establish the value used by openUrl()
One approach is to have openUrl() manipulate an instance variable of type URL that is null except upon a valid access of a URL and establishment of a connection. Thus, other Bean functionality is conditional on a valid URL connection. The URL object should be private and not exposed via access methods. Instead, design the property url with get and set access methods that expose the URL as a string.
Task 2
Next, design and implement the method retrieveUrlContent() that, given an established URL connection, opens an input stream from the URL, processes the content, and returns the content as a string. The skeleton code provides several content-related class members:
- private int segment[] = new int[SEG_SIZE];
- private void readSegment(DataInputStream in);
- private String formatSegment();
- private void printSegment();
The instance variable segment is used globally, for convenience, by the xxxSegment() methods. If you allow these methods to work as designed, for example, don't change the segment size, they will read and format the URL content producing a traditional assembly-style dump:
000 54 ... 20--74 ... 31 |This is -text..01|
010 32 ... 39--0a ... 0a |23456789-....end.|
020 00 ... 00--00 ... 00 |........-........|
The dump has the segment offset on the left, hex values in the center, and ASCII interpretation on the right, if any.
In this case, you simply use the URL object's openStream() to open the input stream:
in = new DataInputStream(url.openStream());
Also, using this stream, process the content in a loop until end-of-file is raised--the end-of-file logic is provided. Invoke readSegment() and formatSegment() repeatedly. For each iteration, you must "collect" the formatted text returned by formatSegment(), possibly, for example, using simple string concatenation. You don't need printSegment(); it's a convenience method for displaying the content segments as they are read from the stream.
Task 3
Having designed retrieveUrlContent() to return the content as a string, consider adding functionality to support graphical display of the content.
Recall the use of control methods in earlier Magercises. One approach is to provide a void control method, for example, retrieveUrlContentGraphical(), that creates a Frame instance with graceful window-close functionality, a "Dismiss" or "OK" button, and so on, plus a TextArea object as the principal window component that populates itself with text by invoking retrieveUrlContent().
Task 4
Optionally, provide several (perhaps convenience) properties that facilitate the use of UrlHexDump from an IDE.
For example, you could provide a Boolean property that is off by default, and when set (to true), performs the URL connect and retrieve operations, and then sets itself back to off (false). Another example would be a showGraphically property so that you can toggle on the graphical display from an IDE.
Task 5
Lastly (and optionally), consider expanding UrlHexDump, say, SearchUrlsHexDump, so that it searches multiple sites and sends event notifications to registered targets.
In this case, the principal task, of course, is the search logic. Adding the event notification logic is quite simple, as with earlier Magercises.
Copyright © 1998-1999
MageLang Institute.
All Rights Reserved.