![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Expanding URLsWhen SiteMapper encounters a partial URL, it must convert it into an absolute URL for two reasons. First, SiteMapper uses complete URLs so that it can correctly identify different documents. Suppose one document refers to a file using a partial URL and another document refers to the same file using a complete URL. SiteMapper uses the complete URL to determine that the two refer to the same file. This allows the program to visit each file only once. The second reason SiteMapper uses complete URLs is that OpenURL does not understand partial URLs. It has no concept of a current document, so it does not have values it can use for missing portions of a partial URL. For that reason, SiteMapper needs a complete URL whenever it invokes OpenURL. The starting URL entered by the user must include a protocol and site. Initially SiteMapper has not visited any document so it cannot use the current documents protocol and site to fill in missing values. If the user does not provide a protocol and site, SiteMapper presents an error message. If the URL does not contain a file name, the Web page server at the site decides which file it will return to OpenURL. Some servers return Index.HTM; others return Home.HTM if those files exist in the directory specified by the URL. If the appropriate file is not present, and if the sites directory permissions allow it, the server may return an index of the directory. This will include a list of every document in that directoryit will give SiteMapper lots of URLs to explore. In these cases, SiteMapper does not know whether it received Index.HTM, Home.HTM, or a directory listing. Later, if it encounters a document that contains a reference to this file, SiteMapper will not realize that the files are the same. This will make the program visit the file again. For example, suppose the user enters http://Beast/Home as the starting URL. This does not include a file name, so the server running on Beast decides to return the file Index.HTM. Now suppose one of the files referenced in Index.HTM refers back to Index.HTM. SiteMapper will not realize it has already visited Index.HTM, and it will visit it again. Once SiteMapper begins, it knows the site and path of the current document. It stores these values in the global variables CurrentSite and CurrentPath and uses them to fill in missing parts of any relative URLs it encounters. Subroutine CompleteURL performs this task. It begins by checking the parsed URL for a site name. If no site is present, the subroutine uses the value CurrentSite. At that point, if the path is an absolute path (starting with a slash), the URL is complete. If the site is not present and the path is a relative path (not starting with a slash), CompleteURL has more work to do. The routine determines whether the new path begins with ../ indicating an upward movement through the sites directory structure. If so, the routine removes the ../ characters from the new path and removes the rightmost directory from CurrentPath. It repeats this process until the new path does not begin with ../. For example, suppose CurrentPath is /dir1/dir2/dir3/ and the new path is ../../subdir/. Then these paths would undergo the following series of transformations: /dir1/dir2/dir3/ ../../subdir/ /dir1/dir2/ ../subdir/ /dir1/ subdir/ At this point CompleteURL combines CurrentPath and the remainder of the new path to find the new absolute path. In the previous example, the new absolute path would be /dir1/subdir/.
The URL components currently in use.
Private CurrentSite As String
Private CurrentPath As String
Private CurrentFile As String
Private Sub CompleteURL(the_site As String, the_path As String)
Dim i As Integer
If no site is specified, use CurrentSite
and check for a relative path.
If the_site = NO_SITE Then
No site. Use CurrentSite.
the_site = CurrentSite
If the_path is an absolute path, its
already set. Look for a relative path.
If Left$(the_path, 1) <> / Then
Its a relative path.
Check for .. in the path.
Do While Left$(the_path, 3) = ../
Remove the last directory
from CurrentPath.
For i = Len(CurrentPath) - 1 To 1 Step -1
If Mid$(CurrentPath, i, 1) = / Then Exit For
Next i
If i <= 1 Then
CurrentPath = /
Else
CurrentPath = Left$(CurrentPath, i)
End If
Remove the ../ from the_path.
the_path = Right$(the_path, Len(the_path) - 3)
Loop
The ..s are gone. Add the_path
to CurrentPath.
the_path = CurrentPath & the_path
End If
End If
End Sub
Mapping Web SitesSiteMapper performs a depth-first traversal of the documents it can reach from the initial URL without leaving the initial site. The process is controlled by four collections: GotoSite, GotoPath, GotoFile, and BeenThere. The Goto collections store the sites, paths, and files of documents the program has not yet visited. BeenThere stores the complete URLs of documents that the program has already visited. Before visiting a document, SiteMapper checks BeenThere to make sure it has not already visited the document. The document traversal begins with subroutine ReadSite. This subroutine uses ParseURL to break the initial URL into its components. It adds the URLs site, path, and file name to the Goto collections. Then it repeatedly invokes the VisitURL subroutine until there are no more URLs to visit.
Private Sub ReadSite()
Dim the_protocol As String
Dim the_site As String
Dim the_path As String
Dim the_file As String
Start with no results.
ResultText.Text =
Results =
Parse the URL.
ParseURL Trim$(URLText.Text), the_protocol, _
the_site, the_path, the_file
Make sure we have a site name.
If the_site = NO_SITE Then
Beep
MsgBox The URL must include a site name., _
vbOKOnly + vbInformation, _
Site Required
URLText.SetFocus
Exit Sub
End If
Allocate empty collections.
Set GotoSite = New Collection
Set GotoPath = New Collection
Set GotoFile = New Collection
Set BeenThere = New Collection
Put the URL in the list of URLs to visit.
GotoSite.Add the_site
GotoPath.Add the_path
GotoFile.Add the_file
NumUrls = 1
Repeatedly loop until we have visited all
the URLs or were told to stop.
ContinueReading = True
Do
Visit a URL.
VisitURL
Process events (including the Stop
button press).
DoEvents
Loop While ContinueReading And NumUrls > 0
Display the results.
ResultText.Text = Results
Free the collections.
Set GotoSite = Nothing
Set GotoPath = Nothing
Set GotoFile = Nothing
Set BeenThere = Nothing
End Sub
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|