![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Understanding URLsAs far as SiteMapper is concerned, a URL has four pieces: protocol://site/path/file The pieces are described in the following list:
<A NAME=marker>Text displayed at this point</A> SiteMapper ignores markers because it does not matter what part of a document the program visits. When the program opens a document, it reads the entire file including all of the marked locations. Many documents contain partial URLs. Missing pieces of the URL are filled in using the components of the current documents URL. For instance, suppose the document located at http://www.mysite.com/Public/Index.HTM contains the following reference: <A HREF=Details.HTM>Jump to details page.</A> The URL Details.HTM does not contain a protocol, site, or path. They are filled in using the values given by the current document. The complete URL is http://www.mysite.com/Public/Details.HTM. Table 19.1 shows some partial URLs and their corresponding complete URLs. The table assumes the URLs are being read from the file http://site/dir1/dir2/File1.HTM. SiteMapper must be able to parse each of these different kinds of URLs correctly. ParseURLSiteMapper uses the ParseURL subroutine to separate URLs into their pieces. ParseURL begins by removing markers from the URL. It does this by stripping out any text that comes after a number sign. If the original URL was http://www.mysite.com/dir1/dir2/File.HTM#marker, the address becomes http://www.mysite.com/dir1/dir2/File.HTM.
Next, ParseURL locates the addresss protocol. The routine assumes the protocol is followed by the string ://. After recording the protocol, ParseURL removes it from the URL string. The example URL becomes www.mysite.com/dir1/dir2/File.HTM. If the protocol is not present, the URL is a partial URL that should not include a site either. Otherwise, the site name includes the text up to the next slash. The routine saves the site name and removes it from the URL, giving /dir1/dir2/File.HTM. ParseURL then locates the file name in the URL by finding the rightmost slash. Whatever text comes after this slash must be the file name. If this text includes a period as in File.HTM, ParseURL records the file name and removes it from the URL. Otherwise, the subroutine assumes this text is a subdirectory name and there is no file name in the URL. For example, the URL /dir1/dir2 contains no period after the rightmost slash so ParseURL assumes dir2 is a directory name. Whatever remains of the text is the path. If ParseURL found a site name, then the path must be absolutely not relative. In this case, ParseURL makes sure the path begins with a slash. The subroutine also makes sure the path includes a trailing slash whether this is an absolute path or not.
Sub ParseURL(ByVal the_url As String, the_protocol As String, _
the_site As String, the_path As String, the_file As String)
Dim pos As Integer
Dim got_site As Boolean
the_url = Trim$(the_url)
Remove any reference to a marker.
pos = InStr(the_url, #)
If pos > 0 Then
the_url = Left$(the_url, pos - 1)
End If
Find the protocol.
pos = InStr(the_url, ://)
If pos = 0 Then
There is no protocol or site.
the_protocol = NO_PROTOCOL
the_site = NO_SITE
got_site = False
Else
Record the protocol.
the_protocol = Left$(the_url, pos - 1)
Remove the protocol from the URL.
the_url = Right$(the_url, Len(the_url) - pos - 2)
Find the site.
If the_url = Then
There is no site.
the_site = NO_SITE
got_site = False
Else
pos = InStr(the_url, /)
If pos = 0 Then pos = Len(the_url) + 1
the_site = Left$(the_url, pos - 1)
got_site = True
Remove the site from the URL.
the_url = Right$(the_url, Len(the_url) - pos + 1)
If theres nothing left, use the
root path.
If the_url = Then the_url = /
End If
End If
Find the file.
If the_url = Then
There is no file.
the_file = NO_FILE
Else
Find the last /.
pos = 1
Do While InStr(pos, the_url, /) > 0
pos = InStr(pos, the_url, /) + 1
Loop
If pos > Len(the_url) Then
The url ends with a /. No file.
the_file = NO_FILE
Else
See if the file has a . in it.
If InStr(pos, the_url, .) > 0 Then
It does. This is the file.
the_file = Right$(the_url, _
Len(the_url) - pos + 1)
Remove the file from the URL.
the_url = Left$(the_url, pos - 1)
Else
No .. There is no file.
the_file = NO_FILE
End If
End If
End If
Whatever is left is the path.
If the_url = Then
There is no path.
the_path = NO_PATH
Else
the_path = the_url
If we got a site, make sure theres
a leading slash.
If got_site And Left$(the_path, 1) <> / Then
the_path = / & the_path
End If
Make sure theres a trailing slash.
If Right$(the_path, 1) <> / Then
the_path = the_path & /
End If
End If
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.
|