home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / weasl101.zip / Weasel.java < prev    next >
Text File  |  1997-04-23  |  8KB  |  265 lines

  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. /*    Weasel Version 1.01    23rd April 1997
  6.  
  7.         Author:     Stephen Summerfield
  8.           Email:     s.summerfield@dial.pipex.com
  9.         URL:         http://ds.dial.pipex.com/s.summerfield/
  10.         
  11. Changes history (dates in European format ;>):
  12.  
  13.     23/4/1997        Made remote host optional to allow only update of local file.
  14. */
  15.  
  16. class Weasel extends FilterInputStream
  17. {
  18. /* Characters used to embed commands in the template file eg ##COMMAND# */
  19. private final byte Include_Start_Marker_Char  = '#';
  20. private final byte Include_End_Marker_Char  = '#';
  21.  
  22. /* Name of temporary file used to store the results of processing the template file */
  23. static private final String TempFileName = "temp.tmp";
  24.  
  25. private StringBuffer IncludeBuffer = new StringBuffer();    /* Temporary storage of included text */
  26. private int IncludeBufferIndex = -1;                                             /* Index of current char in above StringBuffer */
  27.  
  28. /*-----------------------------------------------------------------------
  29. Constructor
  30. -----------------------------------------------------------------------*/
  31. Weasel( InputStream in )
  32.     {
  33.    super( in );
  34.    }
  35.  
  36. /* The embedded commands */
  37. /*-----------------------------------------------------------------------*/
  38. private StringBuffer ProcessHostname( StringBuffer IncludeBuffer )
  39.     {
  40.     try
  41.          {
  42.         InetAddress Addr = InetAddress.getLocalHost();
  43.          IncludeBuffer.append( Addr.getHostName() );
  44.           }
  45.       catch( IOException e )
  46.           {
  47.         System.err.println( "getLocalHost failed" );
  48.           /* A bit of a hack, but this SHOULD never happen 
  49.            (except in OS/2, sometimes) */
  50.          IncludeBuffer.append( "UNKNOWN" );
  51.          }
  52.      return( IncludeBuffer );
  53.     }
  54.  
  55. /*-----------------------------------------------------------------------*/
  56. private StringBuffer ProcessIPAddress( StringBuffer IncludeBuffer )
  57.     {
  58.     try
  59.          {
  60.         InetAddress Addr = InetAddress.getLocalHost();
  61.          IncludeBuffer.append( Addr.getHostAddress() );
  62.           }
  63.       catch( IOException e )
  64.           {
  65.           System.err.println( "getLocalHost failed" );
  66.           /* A bit of a hack, but this SHOULD never happen 
  67.            (except in OS/2, sometimes) */
  68.          IncludeBuffer.append( "?.?.?.?" );
  69.          }
  70.      return( IncludeBuffer );
  71.     }
  72.  
  73. /*-----------------------------------------------------------------------*/
  74. private StringBuffer ProcessTime( StringBuffer IncludeBuffer )
  75.     {
  76.       IncludeBuffer.append( new Date().toGMTString() );
  77. //           IncludeBuffer.append( new Date().toLocaleString() );
  78.      return( IncludeBuffer );
  79.     }
  80.  
  81. /*-----------------------------------------------------------------------
  82. Takes embedded command and processes it
  83. -----------------------------------------------------------------------*/
  84. private StringBuffer ProcessCommand( StringBuffer IncludeCommand )
  85.     {
  86.    IncludeBuffer.setLength(0);
  87.     StringTokenizer st = new StringTokenizer( IncludeCommand.toString(), 
  88.                                                                                      new Character( (char)Include_End_Marker_Char) + " \r\n\t" );
  89.  
  90.     if( st.hasMoreTokens() )
  91.          {
  92.       String cmd = st.nextToken();
  93.       if( cmd.equalsIgnoreCase( "HOSTNAME" ) )
  94.           {
  95.          IncludeBuffer = ProcessHostname( IncludeBuffer );
  96.          }
  97.       if( cmd.equalsIgnoreCase( "IPADDRESS" ) )
  98.           {
  99.          IncludeBuffer = ProcessIPAddress( IncludeBuffer );
  100.          }
  101.       if( cmd.equalsIgnoreCase( "TIME" ) )
  102.           {
  103.          IncludeBuffer = ProcessTime( IncludeBuffer );
  104.          }
  105.  
  106.       /* Add new commands here */
  107.  
  108.       }
  109.  
  110.      return( IncludeBuffer );
  111.       }
  112.  
  113. /*-----------------------------------------------------------------------*/
  114. private int ProcessIncludeCommand() throws IOException
  115.     {
  116.     int chr;
  117.      StringBuffer IncludeCommand = new StringBuffer();
  118.  
  119.    IncludeCommand.setLength(0);
  120.  
  121.     /* Read command string */
  122.      do
  123.           {
  124.       if( (chr = in.read()) < 0 )
  125.          {    /* End of file, no terminating include marker found */
  126.          IncludeBuffer.setLength(0);
  127.             IncludeBufferIndex = -1;
  128.          return(chr);
  129.          }
  130.       IncludeCommand.append( (char)chr );
  131.       }
  132.    while( chr != Include_End_Marker_Char );
  133.  
  134.     if( IncludeCommand.length() <= 0 )
  135.          {    /* Empty command string, return marker character (like an escape sequence) */
  136.       return( Include_Start_Marker_Char );
  137.       }
  138.  
  139.      /* Now actually process include command string */
  140.      ProcessCommand( IncludeCommand ); 
  141.  
  142.    if( IncludeBuffer.length() <= 0 )
  143.       {    /* Empty include command, return a marker character */
  144.       IncludeBuffer.setLength(0);
  145.         IncludeBufferIndex = -1;
  146.           return( Include_Start_Marker_Char );
  147.       }
  148.     IncludeBufferIndex = 1;
  149.  
  150.     return( IncludeBuffer.charAt(0) );
  151.    }
  152.  
  153. /*-----------------------------------------------------------------------*/
  154. /* Gets next character of data, if the include marker character is found the include command text 
  155. is replaced by the appropriate replacement text
  156. */
  157. public int read() throws IOException
  158.    {                                                                        
  159.    int chr=-1;
  160.  
  161.     if( IncludeBufferIndex >= 0 )
  162.          {    /* Read char from the include buffer */
  163.       chr = IncludeBuffer. charAt(IncludeBufferIndex);
  164.           if( ++IncludeBufferIndex >= IncludeBuffer.length() )
  165.             {
  166.            IncludeBufferIndex = -1;
  167.          }
  168.          }
  169.    else
  170.       {    /* Read char from the input stream */
  171.        if( (chr = in.read()) == Include_Start_Marker_Char )
  172.          {    /* Read next char */
  173.            int chr2;
  174.  
  175.             if( (chr2 = in.read()) == Include_Start_Marker_Char )
  176.                 {        /* Two consecutive marker chars means an include follows */
  177.               /* Process include command */
  178.             chr = ProcessIncludeCommand();    /* Return first char of include */
  179.                }
  180.          else
  181.             {  /* Single marker char found, not an include, push back last read char */
  182.                 ((PushbackInputStream) in).unread(chr2);
  183.             } 
  184.          }
  185.       }
  186.  
  187.    return( chr );
  188.    }
  189.  
  190. /*-----------------------------------------------------------------------*/
  191. public static void process( InputStream istream, OutputStream ostream  ) throws IOException
  192.      {
  193.         // Read stream
  194.         Weasel in = new Weasel( new PushbackInputStream( istream ) );
  195.  
  196.           int chr;
  197.         while( true )
  198.                {
  199.            if( (chr = in.read())  < 0 )
  200.               {
  201.               break;
  202.               }
  203.             ostream.write( chr );
  204.               }
  205.     }
  206.     
  207. /*-----------------------------------------------------------------------*/
  208. //public static void main(String[] args) throws MalformedURLException, IOException
  209. public static void main(String[] args) throws IOException
  210.    {
  211.    String HFName = null;
  212.    String HDir = null;
  213.  
  214.     if( args.length < 1 )
  215.          {
  216.       System.out.println();
  217.       System.out.println( "Too few parameters" );
  218.       System.out.println();
  219.       System.out.println( "Usage: " );
  220.       System.out.println( "   src_filespec [ftphostname username password] [remotedirspec] [remotefilename] " );
  221.       System.out.println();
  222.       System.exit(-1);
  223.          }
  224.  
  225.     if(  args.length == 1 )
  226.         {        /* Give warning that patched file will remain on local drive */
  227.       System.out.println( "No remote host specified, no upload attempted." );
  228.          }
  229.          
  230.     if( args.length > 4 )
  231.          {                         /* Optional parameters */
  232.       HDir = args[4];
  233.         if( args.length > 5 )
  234.              {
  235.               HFName = args[5];
  236.           }
  237.         else
  238.              {        /* Strip the filename out of the template's file path and use that for the remote file name */
  239.             HFName =  (new File( args[0] )).getName();
  240.           }
  241.        }
  242.  
  243.     FileInputStream InStream= new FileInputStream( args[0] );
  244.  
  245.     File TempFile = new File( TempFileName );
  246.      TempFile.delete();
  247.     FileOutputStream OutStream= new FileOutputStream( TempFileName );
  248.  
  249.     process( InStream, OutStream );
  250.  
  251.     if(  args.length > 1 )
  252.         {     /* Only attempt upload if host, etc has been specified */
  253.          SendFileByftpSun trans = new SendFileByftpSun( TempFileName, args[1], args[2], args[3], HDir, HFName );
  254.          }
  255.  
  256. //    TempFile = new File( TempFileName );
  257. //     TempFile.delete();        /* Leave temporary file after processing. */
  258.    }
  259.  
  260. /*-----------------------------------------------------------------------*/
  261. /* End of class */
  262. }
  263. /*-----------------------------------------------------------------------*/
  264.  
  265.