PPWIZARD is a free preprocessor for HTML, REXX, Visual Basic or any text files.
[Bottom][Contents][Search][Prev]: Create Multiple Files From Template[Next]: SHARING HEADERS - HTML + REXX CGI
\ -> Examples / Tips -> Resource Validation - Local

Resource Validation - Local

This example shows how you could ensure that your local resources actually exist (you should also have a look at the validation of remote resources support that PPWIZARD supplies). This helps to eliminate mistakes (its assumed that you won't forget to FTP local resources to the web server!). The example is HTML-based but would work for any language or text file where external resources are referenced.

While the macros I will show will work, typically everyone has a different way of working which would probably require these macros to be modified, however they are great as a guide.

Generic Macro

This macro makes no assumptions about your development environment and causes PPWIZARD to fail with an appropriate error message if the resource does not exist.

The macro takes 2 manditory parameters, one identifies the full name of the file on the development machine and the other is used for resource identification purposes (when an error occurs) and is normally how your HTML references it.

The macro is as follows:

;--- Generic check for resource validity routine ---
#define    CheckResource                                                     \
           #if stream('{$LocalName}', 'c', 'query exists') = ''              \
               #error ^Resource "{$Name}" appears to be invalid. The file "{$LocalName}" could not be located!^ \
           #endif

Of course you may not wish to abort PPWIZARD for what you consider to be a warning situation, in this case you could use #warning instead.

Development Environment Specific Macro

In HTML, when I refer to an image such as "graphics/fred.gif", the file "fred.gif" should exist in the "graphics" directory when the HTML is "run". The image file could exist anywhere on my development machine, so I will taylor a graphics validation setup as follows:

;--- Check if Graphic exists ------------------------------------------------
#option    PUSH LineContinuation="NULL"      ;;Next line ends with continuation char!
#define    MyLocalGraphicsDirectory    D:\PROJECTS\HOMEPAGE\GRAPHICS\
#option    POP                               ;;Restore original continuation char
#define    MyRemoteGraphicsDirectory   graphics/
#define    ValGraphic                                                        \
           <$CheckResource LocalName=^<$MyLocalGraphicsDirectory>{$SNAME}^   \
                                Name=^<$MyRemoteGraphicsDirectory>{$SNAME}^  \
           >

The above macros are probably defined in a validation or other common header file. Note that you might wish to use shorter names such as "vlg" for "validate local graphic" etc. To actually test that an image file exists I could use:

<$ValGraphic SNAME="fred.gif">
<IMG SRC="graphics/fred.gif">

Even the above macro and reference make some assumptions about how I personally wish to use them, this is another reason why everyone would probably "roll their own".

An Alternative Way - #1

The problem with the above set of macros is that you have to do 2 operations, the first is to validate the resource and the second is to use it.

A better alternative is for the same routine that does the validation to generate the "remote" resource so that the generic macro (let's also change to only warning if resource is missing) becomes:

;--- Generic check for resource validity routine ---
#define    CheckResource                                                     \
           #if stream('{$LocalName}', 'c', 'query exists') = ''              \
               #Warning ^$MR00^ ^Resource "{$Name}" appears to be invalid. The file "{$LocalName}" could not be located!^ \
           #endif                                                            \
           {$Name}             ;;"output" the "runtime" file name

Now the image-based macros might look like:

;--- Check if Graphic exists ------------------------------------------------
#option    PUSH LineContinuation="NULL"      ;;Next line ends with continuation char!
#define    MyLocalGraphicsDirectory    D:\PROJECTS\HOMEPAGE\GRAPHICS\
#option    POP                               ;;Restore original continuation char
#define    MyRemoteGraphicsDirectory   graphics/
#define    UseGraphic                                                        \
           <$CheckResource LocalName=^<$MyLocalGraphicsDirectory>{$SNAME}^   \
                                Name=^<$MyRemoteGraphicsDirectory>{$SNAME}^  \
           >

And to use the macro becomes automatic and simple:

<IMG SRC="<$UseGraphic SNAME='fred.gif'>">

Note that I kept the "UseGraphic" macro simple for demonstration purposes, for my purposes I would create a higher level "img" macro which not only validated the file exists but used GetImageHeightWidth() (by default) to automatically calculate the image size. Other "img" tag parameters could be passed as macro parameters (with suitable defaults).

An HTML validation version would be slightly more complex than the above, since you would probably wish to also handle the "#location" suffix. The simplest (and maybe best) way to handle it might be just to have a new optional parameter. Another complication is that you would probably look for a local file with the extension ".IT", again exactly how it looks is really a personal preference.

An Alternative Way - #2

Under OS/2 there is a free URL checking program which can be given a list of URLs to check. Similar programs will exist for other operating systems.

You could create a ".URL" file for each HTML page and have your macros not only check that everything is fine locally, but add each URL in turn to the new file (deleted at start of build). Later, when online and after updating your web site, you could run the lists through the URL checking program.

Case Sensitivity

None of the code shown actually checks the case of the filename so its possible that although the code shows that the file exists locally, if copied to a unix server a "browser" will not find the file. There are a number of changes you can make to check the case or, even simpler, to transfer the files to the unix server in lower case and ensure that you refer to the files in lower case.

SiteCopy

So as to be very sure that you never forget to correctly update your web site you can use an automated tool such as "SiteCopy" to FTP new or modified files and directories. It will also delete obsolete files and directories.

This tool is a bit rough around the edges but it is free and if you provide the developers with constructive feedback they will probably listen. I am using this tool for my site updating.

There are OS/2, Windows, and other ports of the unix program; the main sitecopy page is at http://www.lyra.org/sitecopy/.

I recommend that Windows users download my "SITECPY" download as this has been repackaged so it is a breeze to install and setup and does not require command line access for typical tasks. It is available from http://dennisbareis.com/freew32.htm.


email me  any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Search][Prev]: Create Multiple Files From Template[Next]: SHARING HEADERS - HTML + REXX CGI


PPWIZARD Manual
My whole website and this manual itself was developed using PPWIZARD (free preprocessor written by Dennis Bareis)
Saturday May 28 2022 at 2:55pm