For this Magercise you use the BeanBox, or another IDE, to design and test a nongraphical URL Bean. The Bean makes a URL connection to a remote HTTP address and then performs a hex dump of the data at the specified URL. The skeleton includes the hex dump-related functionality so that you can focus strictly on setting up properties, making the connection, and related issues.
Magercise 10 Prerequisites
Skeleton Code
Tasks
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()
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.
Having designed retrieveUrlContent() to return the content as a string, consider adding functionality to support graphical display of the content.
Optionally, provide several (perhaps convenience) properties that facilitate the use of UrlHexDump from an IDE.
Lastly (and optionally), consider expanding UrlHexDump, say, SearchUrlsHexDump, so that it searches multiple sites and sends event notifications to registered targets.
Where help exists, the task numbers above are linked to the step-by-step
help page.
Solution Source
Demonstration
After loading the UrlHexDump Bean, it should appear in the palette, from where it can be selected and instantiated on the worksheet:
In this case, the worksheet contains a nongraphical UrlHexDump Bean and a command button labeled "Hex Dump HTTP". The command button's actionPerformed() method is connected to UrlHexDump's retrieveUrlContentGraphical() method.
The property sheet displays the published properties:
You can add a TextArea instance in a top-level window for displaying the results of a URL hex-dump operation:
Your design and implementation will certainly differ, but the basic objective is to process a URL, produce a hex dump of its contents, and make the dump available for retrieval as a string, and, perhaps optionally, in a graphical window. Note that even though it may be desirable to display the result graphically, the Bean itself is not graphical and will function properly outside a graphical context.
- Magercise 11
- Short Course
- About This Short Course
Copyright © 1998-1999
MageLang Institute.
All Rights Reserved.