Cover V06, I12
Article
Listing 1
Listing 2

dec97.tar


Listing 1: vgen.java - Man program for the vGen byte code

Note: Backslashes have been inserted in the following to reflect editorial breaks on lines that are too long to fit our format. Such lines should actually appear as a single line of code in the source.

// vgen-  The web version generator
// Copyright (c) 1997 by James L. Pinson
// look to http://www.polaris.net/~jpinson/home/vgen.html for info on the
// latest version.

import java.io.*;
import java.util.*;
public class vgen


{
static String section = "";
static String tempstring = "";
static String homepage = "";
static String uppage = "";
static String currentpage = "";
static String lastpage = "";
static String nextpage = "";

static String homeimage = "btn-home.gif";
static String lastimage = "btn-last.gif";
static String upimage = "btn-up.gif";
static String nextimage = "btn-next.gif";

static String sourcedirectory = "";
static String outputdirectory = "";
static Vector modes = new Vector(10,10);
static boolean config_finished = false;

public static void main (String[] args)
{


int i=0;
String str="";
StringBuffer temp;
String level[] = new String [20];
int ch;
int index=0;
int currentdepth=0;
int done=0;
String left = "";
String right = "";
String tempstring = "";
String currentfile = "";
String infile = "";
String outfile = "";

printwelcome();

if (args.length < 1){
System.out.println("Usage: vgen [config_file] <mode=modename>
<sourcedirectory=path> <outputdirectory=path> ......");
System.exit(1);
}

addmode("common");  // the common mode is always active
// now add modes from command line
for (i=1;i < args.length; i++){
if (args[i].indexOf("=") != -1){ // all our valid args have an '='
left=stringlib.GetLeftValue(args[i],'=');
right=stringlib.GetRightValue(args[i],'=');
left=left.trim(); //trim spaces
right=right.trim();//trim spaces
if  (left.equals("mode") && right.length()!=0 ){
addmode(right);
System.out.println("Using mode "+ right);
System.out.println(""); // let user know mode was recognized
}// end if(left
}// end if args[]
}// end for()

// now check for source/destination directories from command line
for (i=1;i < args.length; i++){
left=stringlib.GetLeftValue(args[i],'=');
right=stringlib.GetRightValue(args[i],'=');
if  (left.equals("sourcedirectory")){
sourcedirectory=right;
}
if  (left.equals("outputdirectory")){
outputdirectory=right;
}

}

//let's see if we can read the config file, args[0]
if (! isfile(args[0])){
errorexit("The configuration file (" + args[0] + ") is not found");
}
try
{
// debug i=3;
DataInputStream is = new DataInputStream (new FileInputStream (args[0]));

while (done == 0 ) {  //While not done
if( (str = is.readLine()) == null) break; // if end of file quit
if (str.length()==0) errorexit("No blank lines allowed in the config file");
tempstring= str.trim();
// is this a section heading (in []) ?
if ((tempstring.charAt(0)== '[') &&
(tempstring.charAt(tempstring.length()-1)==']')){
section=tempstring.substring(1,tempstring.length()-1);

// read another line to skip over heading
if( (str = is.readLine()) != null) done=0; // if end of file quit
}



if (section.compareTo("config")==0){
left=stringlib.GetLeftValue(str,'=');
right=stringlib.GetRightValue(str,'=');

if (left.equals("sourcedirectory")&& sourcedirectory.equals("") ){
sourcedirectory = right; // don't update sourcedirectory if it was set on
// command line
}

if (left.equals("outputdirectory")&& outputdirectory.equals("")) {
outputdirectory = right; // don't update sourcedirectory if it was set
// on command line
}




if (left.equals("mode")){
addmode(right);
System.out.println("adding mode "+ right);
}

}


if (section.compareTo("webtree")==0){

// if we are in webtree, then see if everything is OK from config section
if (config_finished==false){
verifyconfig();
// now flag this section so we don't re-run it
config_finished=true;
} // end  config_finished section

// this loop is only done for the first file, the homepage
//we know it is the first file, because hompage=""
if (homepage.compareTo("")==0){
if (countleadingspaces(str)>0){  // first file must be at top level, not
// indented
System.out.println("The first file in [webtree], ie: the homepage, must
not be indented");
}
// check for illegal stuff

homepage=str.trim();
} // end hompage.compareTo


if (str.indexOf(  '\t' ) >= 0) errorexit("The line (" + str + ") contains a
tab, please use spaces");

nextpage=str;

if (currentpage.compareTo("")!=0){
if (currentdepth >0) uppage=level[currentdepth-1]; // set uppage

CopyTextFile(infile,outfile );
}

currentfile=str;
infile= stringlib.forcelastchar(sourcedirectory, File.separatorChar);
infile=infile.concat(str.trim());

i=countleadingspaces (str);

if ( (i-currentdepth) > 1){  //increasing levels too rapidly
tempstring="Webtree entry -" + str.trim() + "- is indented by more than
the maximum one level.";
errorexit ( tempstring );
}
currentdepth=countleadingspaces(str);

level[currentdepth]=str;  //How deep is this page?

lastpage=currentpage; //save current page as last page
currentpage=str;

if(currentpage.compareTo( "")!=0){
outfile= stringlib.forcelastchar(outputdirectory, File.separatorChar);
outfile=outfile.concat(currentfile.trim());


}

} //end of webtree section



} // eof

// we finished, but still need to process the last page
nextpage= "";  // there is no next page
if (currentdepth >0) uppage=level[currentdepth-1];


CopyTextFile(infile,outfile );

}
catch (IOException e)
{
errorexit("error: " + e );
}
} //end main



public static void addmode(String string){
modes.addElement ( string);
}

public static String getmode( int i){
String string = (String) modes.elementAt(i);
return (string);
}

public static int countleadingspaces (String str)

{
int index=0,count=0;
if (str.length()==0) return (0);
while (str.charAt(index++) == ' '){
count++;
}
return (count);
}

public static void CopyTextFile(String infile, String outfile){
boolean output=true;  // we start outputting everything
boolean skip=false;   // a flag to skip a line
String modeflag ="";

System.out.println("Processing file: " + currentpage);
if(! isfile(infile)){     // make sure directory exists
errorexit ("The file (" + infile + ") does not exist");
}

try {

DataInputStream istream = new DataInputStream(new
FileInputStream(infile));
PrintStream ostream = new PrintStream(new
FileOutputStream(outfile));
String line;
while ((line = istream.readLine()) != null) {
if (line.indexOf("$$$graphic-link") == 0) {
line=BuildGraphicNavLine();
}
if (line.indexOf("$$$text-link") == 0) {
line=BuildTextNavLine();

}
if (line.indexOf("$$$mode=")==0){
modeflag=stringlib.GetRightValue(line,'=');
if (modes.contains(modeflag)== true) output=true; else \
output=false;
skip=true;
}
if ((output==true)&& (skip==false)) ostream.println(line);
skip=false;
}

istream.close();
ostream.close();
} catch (IOException e) {
System.err.println( "error" + e);
return;
}
}

public static String BuildGraphicNavLine()
{

// define base strings
String graphhomestring="<a href=\"$home.html\"><IMG SRC=\"$home.gif\" \
BORDER=0 ALT=\"Home\"></a>";
String graphlaststring="<a href=\"$last.html\"><IMG SRC=\"$last.gif \" \
BORDER=0 ALT=\"Previous\"></a>";
String graphupstring="<a href=\"$up.html\"><IMG SRC=\"$up.gif \" BORDER=0 \
ALT=\"UP\"></a>";
String graphnextstring="<a href=\"$next.html\"><IMG SRC=\"$next.gif \" \
BORDER=0 ALT=\"Next\"></a>";

graphhomestring=stringlib.ReplaceFirst(graphhomestring,"$home.html", homepage);
graphhomestring=stringlib.ReplaceFirst(graphhomestring,"$home.gif", homeimage);

graphlaststring=stringlib.ReplaceFirst(graphlaststring,"$last.html", lastpage);
graphlaststring=stringlib.ReplaceFirst(graphlaststring,"$last.gif", lastimage);

graphupstring=stringlib.ReplaceFirst(graphupstring,"$up.html", uppage);
graphupstring=stringlib.ReplaceFirst(graphupstring,"$up.gif", upimage);

graphnextstring=stringlib.ReplaceFirst(graphnextstring,"$next.html", nextpage);
graphnextstring=stringlib.ReplaceFirst(graphnextstring,"$next.gif", nextimage);
String string = "";
if (homepage.compareTo( currentpage) != 0) string=string.concat(graphhomestring);
if (lastpage.compareTo( "") != 0) string=string.concat(graphlaststring);
if (uppage.compareTo( "") != 0) string=string.concat(graphupstring);
if (nextpage.compareTo( "") != 0) string=string.concat (graphnextstring);
return (string);

}

public static String BuildTextNavLine()
{

// define base strings
String texthomestring="<a href=\"$home.html\">[Home]</a>";
String textlaststring="<a href=\"$last.html\">[Previous]</a>";
String textupstring="<a href=\"$up.html\">[Up]</a>";
String textnextstring="<a href=\"$next.html\">[Next]</a>";

// update base string with new value
//do home
texthomestring=stringlib.ReplaceFirst(texthomestring,"$home.html", homepage);
textlaststring=stringlib.ReplaceFirst(textlaststring,"$last.html", lastpage);
textupstring=stringlib.ReplaceFirst(textupstring,"$up.html", uppage);
textnextstring=stringlib.ReplaceFirst(textnextstring,"$next.html", nextpage);

String string = "";

if (homepage.compareTo( currentpage) != 0) string=string.concat(texthomestring);
if (lastpage.compareTo( "") != 0) string=string.concat(textlaststring);
if (uppage.compareTo( "") != 0) string=string.concat(textupstring);
if (nextpage.compareTo( "") != 0) string=string.concat (textnextstring);
return (string);

}




public static void printwelcome(){
System.out.println("vgen 1.0" );
}


public  static boolean isdirectory(String directoryname ){
File directory = new File(directoryname ) ;
if(directory.isDirectory()){     // make sure directory exists
return (true);
}
else
return (false);
}


public  static boolean isfile(String filename ){
File file = new File(filename ) ;
if(file.isFile()){     // make sure directory exists
return (true);
}
else
return (false);
}


private static void errorexit(String message){
System.out.println("vgen Error: " + message);
System.exit(1);
}

public static void verifyconfig(){
if(sourcedirectory.equals(outputdirectory)){     // make sure source and
// destination are different
errorexit ("Source and destination must be different");
}



if(! isdirectory(sourcedirectory)){     // make sure directory exists
errorexit("The source directory (" + sourcedirectory + ") does not exist");
}

if(! isdirectory(outputdirectory)){     // make sure directory exists
errorexit("The destination directory (" + outputdirectory + ") does not
exist");
}

// by now we know what the source/output directories are, so we print them
System.out.println("");
System.out.println("The source directory is " + sourcedirectory);
System.out.println("The output directory is " + outputdirectory);
System.out.println("");
}
} //end class vgen