Change 2: RawHTML-->ImageList
With the raw HTML stored in a string, we can code the next information changeextracting the list of (.gif) files from the raw HTML:
string:URL-->string:RawHTML-->ArrayList:ImageList-->Disk:Files
Following the method-naming convention established in the previous section, we call our method RawHTMLtoImageList. This method takes one parametera string representing the raw HTML (raw_html)and returns a list of files stored in an ArrayList, which we'll discuss shortly. Our method declaration is shown in Listing 27.
Listing 27Method Declaration: RawHTMLtoImageList
public ArrayList RawHTMLtoImageList(string raw_html) { }
We code this method's implementation using two motifs.
Motif 3: Finding and Extracting a SubString
Recall that the transformation we're coding is as follows:
string:URL-->string:RawHTML-->ArrayList:ImageList-->Disk:Files
Whenever you want to extract a substring that's in some arbitrary location within a much larger string, you can use the motif in Listing 28:
Listing 28Motif 3: RawHTML to One Substring
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
In our motif above, the much larger string is labeled raw_html and the substring is labeled file. The following detailed explanation of this motif is based on the steps in the example we covered earlier in the section "Understanding the Vampire Bot." If you skipped the earlier example, you might want to go back and read it first.
Step 1Finding a Close Pattern
We define a string patt and an integer ploc (see Listing 29):
Listing 29Explanation Motif 3: Defining "Close" Pattern Variables
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
Next, we set the patt to the value (".gif") that gets us close to the image (see Listing 30):
Listing 30Explanation Motif 3: Setting the "Close" Pattern
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
To find the pattern, we use the string object's built-in IndexOf() method. IndexOf() takes a string pattern and an integer representing the location from which to start searching, returning the position of the pattern as an integer. This is the general syntax of IndexOf():
int loc = ObjectName.IndexOf(string pattern, int start_search_location)
The code with this statement added looks as follows (see Listing 31):
Listing 31Explanation Motif 3: IndexOf() to Find the "Close" Pattern
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
Step 2: Finding the Starting Pattern
We define a string spat and an integer sloc (see Listing 32):
Listing 32Explanation Motif 3: Defining "Starting" Pattern Variable
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
Now that we have the location (ploc) of the pattern (patt) that gets us close to the image's filename, we need to search backward (starting at ploc) for the location of the starting patterna double quote. We set the starting pattern (spat) to this character, as shown in Listing 33.
Listing 33Explanation Motif 3: Setting "Starting" Pattern
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
NOTE
To put a double quote character between a pair of double quotes, you must preface the character with a backslash (\).
Every string object has a built-in method named LastIndexOf(), which takes both a string parameter representing a pattern to search for, and an integer denoting the position in the string to start searching for the pattern. LastIndexOf() then starts searching backward for the pattern at the specified location. This is the general syntax:
int loc=ObjectName.LastIndexOf(string pattern, int start_search_location)
The motif with this statement added looks as follows (see Listing 34):
Listing 34Explanation Motif 3: Using LastIndexOf() to Find the "Starting" Pattern
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
We add 1 to the value that LastIndexOf() returns, in order to position us at the actual start of the filename and not the starting pattern (the double quote).
Step 3: Finding the Ending Pattern
To find the ending pattern, we define a string epat and an integer eloc (see Listing 35).
Listing 35Explanation Motif 3: Defining "Ending" Pattern Variable
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
Now that we have the starting location (sloc) of the image's filename, we need to search forward (starting at sloc) for the location of the ending patternanother double quote. We set the ending pattern (epat) to this character, as shown in Listing 36.
Listing 36Explanation Motif 3: Setting "Ending" Patterns
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
To search forward, we once again use the IndexOf() method as described above, to find the ending pattern, epat (see Listing 37):
Listing 37Explanation Motif 3: Using IndexOf() to Find the "Ending" Pattern
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
Note that we subtract 1 from eloc to position it at the actual end of the filename, and not at the ending pattern (a double quote).
Step 4: Extracting the Filename Substring
With the image filename's starting (sloc) and ending (eloc) locations known, we use the Substring() method to extract the filename into a new string. Substring() takes two parameters, an integer starting location and another integer denoting the number of characters to grab, returning the substring bounded by those parameters. The general syntax is shown below:
string subs=ObjectName.Substring(int starting_loc, int num_characters)
First we define a string named file (see Listing 38):
Listing 38Explanation Motif 3: Defining Filename Variables
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
Next, use the Substring() method to get the filename (see Listing 39):
Listing 39Explanation Motif 3: Using Substring() to Extract the Image's Filename
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1);
Note that the number of characters to grab, given a starting and ending location, is always: eloc-sloc+1.
For our vampire bot, we need to modify this motif slightly so that instead of just grabbing the filename of one image, it collects all the image filenames.
Motif 3': Finding and Extracting All SubStrings
To modify motif 3 so that it finds all the substrings in a string, instead of just the first substring, you simply loop through motif 3 until there are no substrings left (see Listing 40):
Listing 40Modifying Motif 3 to Read All Substrings
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); while (ploc>=0) { sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1); ploc=raw_html.IndexOf (patt, eloc); }
IndexOf() returns a number less than 0 if it can't find a pattern (patt). Thus, in motif 3', the while loop's body is executed only if the pattern location (ploc) is greater than or equal to zero.
Note that the first call to IndexOf() started at the beginning of the string:
ploc=raw_html.IndexOf(patt,0)
The next call to IndexOf() should start looking for more patterns at the ending location (eloc) of the previous call, namely:
ploc=raw_html.IndexOf(patt,eloc)
By adding this line at the end of the loop, and moving the first call to IndexOf() prior to the loop, we generalize the motif to find and extract all the substrings we're interested in from a given string. The completed motif is shown in Listing 41.
Listing 41Motif 3': RawHTML to All Substrings
string patt, spat, epat; int ploc, sloc, eloc; string file; patt=".gif"; spat="\"" ; epat="\"" ; ploc=raw_html.IndexOf (patt, 0); while (ploc>=0) { sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1); ploc=raw_html.IndexOf (patt, eloc); }
As is, the motif extracts all substrings but doesn't do anything with themthe variable holding the substring's value gets (file) overwritten every trip through the loop. The next motif we'll examine uses an object known as an ArrayList to store the values.
Motif 4: Storing Strings in an ArrayList
Whenever you have a number of items you need to store and you don't know a priori how many items you'll have, you should use an ArrayList object to store those itemsparticularly if later on in your code you want to easily loop over or randomly access the items. The motif is straightforward (see Listing 42):
Listing 42Motif 4: Storing Substrings in an ArrayList
using System.Collections; ArrayList list; list = new ArrayList(); list.Add(file);
Detailed Explanation
You define an object of type ArrayList, which is part of the System.Collections namespace (see Listing 43):
Listing 43Explanation Motif 4: Defining an ArrayList
using System.Collections; ArrayList list; list = new ArrayList(); list.Add(file);
Instantiate the object (see Listing 44):
Listing 44Explanation Motif 4: Creating an ArrayList object
using System.Collections; ArrayList list; list = new ArrayList(); list.Add(file);
Then, whenever you want to add an item to the list, you use the ArrayList's built-in Add() method (see Listing 45):
Listing 45Explanation Motif 4: Adding to an ArrayList
using System.Collections; ArrayList list; list = new ArrayList(); list.Add(file);
Next we combine these two motifs to implement the method.
Completing the Method: Combining Motifs
To complete the method, combine the motifs and return the ArrayList (list). Motif 3' is in green and motif 4 in aqua (Listing 46):
Listing 46RawHTMLtoImageList, Completed Method: Combining Motifs 3' and 4
public ArrayList RawHTMLtoImageList(string raw_html) { string patt, spat, epat; int ploc, sloc, eloc; string file; ArrayList list; patt=".gif"; spat="\"" ; epat="\"" ; list = new ArrayList(); ploc=raw_html.IndexOf (patt, 0); while (ploc>=0) { sloc=raw_html.LastIndexOf(spat, ploc)+1; eloc=raw_html.IndexOf (epat, sloc)-1; file=raw_html.Substring (sloc, eloc-sloc+1); ploc=raw_html.IndexOf (patt, eloc); list.Add(file); } return list; }
The key to making the motifs work together is the placement of the Add() statement of motif 4' inside of the motif 3' while loop.