home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / Java / LEDSign / LED / Script.java < prev   
Encoding:
Java Source  |  2017-09-21  |  19.5 KB  |  668 lines

  1. ///////////////////////////////////////////////////////////////////
  2. //  Script.java   -- LED Sign V2.5
  3. //
  4. //  Contains the following classes:
  5. //      Script     -- The class that manages the script
  6. //                    including parsing, storage, and
  7. //                    retrieval.
  8. //
  9. //  Revisions:
  10. //     V2.5: Fixed all known bugs in previous versions!  Added
  11. //           the new feature of ledsize, which allows the user
  12. //           to specify in pixels how big the LED's (1-4).
  13. //           Thanks to Robert B. Denny (rdenny@dc3.com) for
  14. //           code and input!
  15. //           Modified Dec 20-26, 1995
  16. //
  17. //     V2.0beta: Modified V1.0 to comply with Pre-Beta java.
  18. //               A problem with delay causes a jerky display.
  19. //               Modified Oct 20 - 29, 1995
  20. //
  21. //     V1.0: Written July 17 - August 6, 1995
  22. //
  23. //  by Darrick Brown
  24. //     dbrown@cs.hope.edu
  25. //     http://www.cs.hope.edu/~dbrown/
  26. //
  27. //  ⌐ Copyright 1995
  28. ///////////////////////////////////////////////////////////////////
  29.  
  30. /*package LED;*/
  31.  
  32. import java.awt.*;
  33. import java.io.*;
  34. import java.util.*;
  35. import java.net.*;
  36. import FuncInfo;
  37. import linkList;
  38.  
  39. ///////////////////////////////////////////////////////////////////
  40. // Function            Code
  41. // --------            ----
  42. // Appear               0
  43. // Sleep                1
  44. // ScrollLeft           2
  45. // ScrollRight          3
  46. // ScrollUp             4
  47. // ScrollDown           5
  48. // Pixel                6
  49. // Blink                7
  50. // OverRight            8
  51. // ScrollCenter         9
  52. // OverCenter           10
  53. // OverLeft             11
  54. // OverUp               12
  55. // OverDown             13
  56. // Do                   97
  57. // Repeat               98
  58. // Reload               99
  59. ///////////////////////////////////////////////////////////////////
  60.  
  61. ///////////////////////////////////////////////////////////////////
  62. ///////////////////////////////////////////////////////////////////
  63. // The class that parses the script and keeps it in memory 
  64. public class Script
  65. {
  66.    linkList list;               // the linked list for the script
  67.    linkList ptr,start;          // the current line and start of the list
  68.    String scrpt;
  69.    URL documentURL;
  70.  
  71.    ///////////////////////////////////////////////////////////////////
  72.    // The constructor
  73.    public Script(URL url, String s)
  74.    {
  75.       scrpt = s;
  76.       documentURL = url;
  77.       initScript();
  78.    }
  79.  
  80.    ///////////////////////////////////////////////////////////////////
  81.    // get the parameters from the functions in the script
  82.    String getParam(String s, String sub)
  83.    {
  84.       int i,j;
  85.       String tmp;
  86.  
  87.       i = s.indexOf(sub);
  88.       j = s.indexOf("text");
  89.  
  90.       if(j == -1 || i <= j)  // if the first occurance of "sub" is before 
  91.       {                    // the "text=" (ie not in the message)
  92.          if(i == -1)
  93.             return null;
  94.          else
  95.          {
  96.             tmp = s.substring(i);  // forget everything before the sub
  97.             i = tmp.indexOf("=");
  98.             if(i == -1)
  99.             {
  100.                System.out.println("Error in '"+sub+"' parameter in "+s);
  101.                return null;
  102.             }
  103.             else
  104.             {
  105.                i++;  // one spot after the "="
  106.                if(sub.compareTo("text") == 0)
  107.                   tmp = tmp.substring(i);
  108.                else
  109.                {
  110.                   tmp = tmp.substring(i);
  111.                   if(tmp.indexOf(" ") != -1)
  112.                      tmp = tmp.substring(0,tmp.indexOf(" "));
  113.                }
  114.                tmp.trim();
  115.                return tmp;
  116.             }
  117.          }
  118.       }
  119.       else
  120.          return null;
  121.  
  122.    }  // End getParam()
  123.    
  124.    ///////////////////////////////////////////////////////////////////
  125.    // get the function info
  126.    FuncInfo getFunc(String s)
  127.    {
  128.       int i;
  129.       String tmp;
  130.       FuncInfo fi = new FuncInfo();
  131.       
  132.       // Assign the defaults
  133.       fi.func = -1;
  134.       fi.delay = 40;
  135.       fi.startspace = 10;
  136.       fi.endspace = 20;
  137.       fi.times = -1;
  138.       fi.remaining = 0;
  139.       fi.centered = false;
  140.       fi.color = new String("");
  141.       fi.text = new String("No text specified");
  142.       fi.url = null;
  143.       fi.ret = null;
  144.       
  145.       //get rid of any starting (and ending) white space, just to be sure.
  146.       s = s.trim(); 
  147.  
  148.       ////////////////////////////////////////////////////
  149.       // Any parameters that might exist.  This will
  150.       // read in any command line parameters for each
  151.       // function.  For example: Sleep text=blah blah
  152.       // is accepted, but the text will never be used
  153.  
  154.       tmp = getParam(s,"delay");
  155.       if(tmp != null)
  156.          fi.delay = (new Integer(tmp)).intValue();
  157.  
  158.       tmp = getParam(s,"clear");
  159.       if(tmp != null && tmp.compareTo("true") == 0)
  160.       {
  161.          fi.centered = true;
  162.          fi.text = new String("");
  163.       }
  164.       else
  165.       {
  166.          tmp = getParam(s,"center");
  167.          if(tmp != null && tmp.compareTo("true") == 0)
  168.             fi.centered = true;
  169.          else
  170.          {
  171.             fi.centered = false;
  172.             tmp = getParam(s,"startspace");
  173.             if(tmp != null)
  174.                fi.startspace = (new Integer(tmp)).intValue();
  175.  
  176.             tmp = getParam(s,"endspace");
  177.             if(tmp != null)
  178.                fi.endspace = (new Integer(tmp)).intValue();
  179.          }
  180.  
  181.          tmp = getParam(s,"text");
  182.          if(tmp != null)
  183.             fi.text = tmp;
  184.       }
  185.  
  186.       tmp = getParam(s,"times");
  187.       if(tmp != null)
  188.       {
  189.          fi.times = (new Integer(tmp)).intValue();
  190.          fi.remaining = fi.times;
  191.       }
  192.  
  193.       tmp = getParam(s,"pixels");
  194.       if(tmp != null)
  195.       {
  196.          fi.times = (new Integer(tmp)).intValue();
  197.          fi.remaining = fi.times;
  198.       }
  199.  
  200.       tmp = getParam(s,"URL");
  201.       if(tmp != null)
  202.       {
  203.          try
  204.          {
  205.             fi.url = new URL(tmp);
  206.          }
  207.          catch(MalformedURLException e)
  208.          {
  209.             System.out.println("Bad URL: "+tmp);
  210.             fi.url = null;
  211.          }
  212.       }
  213.       else
  214.       {
  215.          fi.url = null;
  216.       }
  217.  
  218.       ////////////////////////////////////////////////////
  219.       // set the function number (and some minor
  220.       // tweeks/precautions)
  221.       i = s.indexOf(" ");
  222.       if(i != -1)
  223.          tmp = s.substring(0,i);
  224.       else
  225.          tmp = s;
  226.          
  227.       if(tmp.compareTo("Appear") == 0)
  228.       {
  229.          fi.func = 0;
  230.       }
  231.       else if(tmp.compareTo("Sleep") == 0)
  232.       {
  233.          fi.func = 1;
  234.       }
  235.       else if(tmp.compareTo("ScrollLeft") == 0)
  236.       {
  237.          fi.func = 2;
  238.       }
  239.       else if(tmp.compareTo("ScrollRight") == 0)
  240.       {
  241.          fi.func = 3;
  242.       }
  243.       else if(tmp.compareTo("ScrollUp") == 0)
  244.       {
  245.          fi.func = 4;
  246.       }
  247.       else if(tmp.compareTo("ScrollDown") == 0)
  248.       {
  249.          fi.func = 5;
  250.       }
  251.       else if(tmp.compareTo("Pixel") == 0)
  252.       {
  253.          fi.func = 6;
  254.          
  255.          // Just for precautions dealing with a delay problem.
  256.          // This shouldn't be noticable.
  257.          if(fi.delay < 1)
  258.             fi.delay = 1;
  259.  
  260.          // Can't allow "times" to be 0 or less, it will cause
  261.          // the sign to freeze (not procede).
  262.          if(fi.times < 1)
  263.             fi.times = 15;
  264.       }
  265.       else if(tmp.compareTo("Blink") == 0)
  266.       {
  267.          fi.func = 7;
  268.          
  269.          if(fi.times < 1)
  270.             fi.times = 2;
  271.       }
  272.       else if(tmp.compareTo("OverRight") == 0)
  273.       {
  274.          fi.func = 8;
  275.       }
  276.       else if(tmp.compareTo("ScrollCenter") == 0)
  277.       {
  278.          fi.func = 9;
  279.       }
  280.       else if(tmp.compareTo("OverCenter") == 0)
  281.       {
  282.          fi.func = 10;
  283.       }
  284.       else if(tmp.compareTo("OverLeft") == 0)
  285.       {
  286.          fi.func = 11;
  287.       }
  288.       else if(tmp.compareTo("OverUp") == 0)
  289.       {
  290.          fi.func = 12;
  291.       }
  292.       else if(tmp.compareTo("OverDown") == 0)
  293.       {
  294.          fi.func = 13;
  295.       }
  296.       else if(tmp.compareTo("Do") == 0)
  297.       {
  298.          fi.func = 97;  // This marks a place for the "repeats" to go back to.
  299.       }
  300.       else if(tmp.compareTo("Repeat") == 0)
  301.       {
  302.          fi.func = 98;
  303.       }
  304.       else if(tmp.compareTo("Reload") == 0)
  305.       {
  306.          fi.func = 99;
  307.       }
  308.  
  309.       fi.store = fi.text;
  310.  
  311.       return fi;
  312.    }  // End getFunc()
  313.  
  314.    //////////////////////////////////////////////////////////////////
  315.    // get the next function
  316.    FuncInfo nextFunc()
  317.    {
  318.       FuncInfo fi;
  319.  
  320.       fi = ptr.fi;
  321.       ptr = ptr.next;
  322.  
  323.       switch(fi.func)
  324.       {
  325.          case 97:  // Do
  326.             fi = nextFunc();   // skip the "Do function; its just a marker
  327.            break;
  328.  
  329.          case 98:  // a Repeat
  330.  
  331.             // If it doesn't repeat infinitely...
  332.             if(fi.times >= 0)
  333.             {
  334.                // One less time
  335.                fi.remaining--;
  336.                if(fi.remaining <= 0)
  337.                {
  338.                   fi.remaining = fi.times;  // reset the loop
  339.                   fi = nextFunc();
  340.                }
  341.                else
  342.                {
  343.                   ptr = fi.ret;  // Jump back to the last "Do"
  344.                   fi = nextFunc();
  345.                }
  346.             }
  347.             else
  348.             {
  349.                ptr = fi.ret;  // Jump back to the last "Do"
  350.                fi = nextFunc();
  351.             }
  352.            break;
  353.  
  354.          case 99:  // Reload
  355.             initScript();      // Reload the script from the URL
  356.             fi = nextFunc();   // and get the first function.
  357.            break;
  358.       }
  359.  
  360.       return fi;
  361.    }  // End nextFunc()
  362.  
  363.    //////////////////////////////////////////////////////////////////
  364.    // just a simple function to see if it is a color code
  365.    boolean isColor(char t)
  366.    {
  367.       if(t == 'r' || t == 'g' || t == 'b' || t == 'y' || t == 'o' || t == 'p' || t == 'w' || t == 'c')
  368.          return true;
  369.       else
  370.          return false;
  371.    }
  372.       
  373.  
  374.    //////////////////////////////////////////////////////////////////
  375.    // Get the varible defined
  376.    String getVar(String s, int i)
  377.    {
  378.       String t;
  379.       
  380.       if(s.charAt(i) == '{')
  381.       {
  382.          t = s.substring(i+1);
  383.          t = t.substring(0,t.indexOf('}'));
  384.       }
  385.       else
  386.          t = String.valueOf(s.charAt(i));
  387.  
  388.       return t;
  389.    }
  390.  
  391.    //////////////////////////////////////////////////////////////////
  392.    // create the final text line from parsing the store line
  393.    //   Add any codes (ie \t, \r, \g, \b, etc.) here to parse
  394.    //   out of the text line.
  395.    FuncInfo parseLine(FuncInfo fi)
  396.    {
  397.       String tmp;
  398.       String time;
  399.       String month[] = {"Jan","Feb","Mar","Apr","May","Jun",
  400.                         "Jul","Aug","Sept","Oct","Nov","Dec"};
  401.       String Month[] = {"January","February","March","April","May","June",
  402.                         "July","August","September","October","November","December"};
  403.       String day[] = {"Sun","Mon","Tues","Wed","Thur","Fri","Sat"};
  404.       String Day[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  405.       String ddmmyy;
  406.       int min;
  407.       int pm;
  408.       Date date = new Date();
  409.       int a,b;
  410.       int i;
  411.       char c;
  412.       String t;         // The tag  (eg. text=Hello \ythere,  t=y)
  413.  
  414.       tmp = fi.store;
  415.       fi.color = "";
  416.  
  417.       if(fi.func == 0 || (fi.func >= 2 && fi.func <= 97))
  418.       {
  419.          c = 'r';  // the default color
  420.          b = 0;
  421.          while(b < tmp.length())
  422.          {
  423.             if(tmp.charAt(b) == '\\')  // if there is a '\' does the following
  424.             {                                          // letter indicate a color.
  425.                b++;
  426.                // Get the tag!
  427.                if(tmp.charAt(b) == '{')
  428.                {
  429.                   t = tmp.substring(b+1);
  430.  
  431.                   // cut out the \{XX}
  432.                   tmp = tmp.substring(0,b-1).concat(t.substring(t.indexOf('}')+1));
  433.                   t = t.substring(0,t.indexOf('}'));
  434.                   b -= 1;
  435.                }
  436.                else
  437.                {
  438.                   t = tmp.substring(b,b+1);
  439.                   tmp = (tmp.substring(0,b-1)).concat(tmp.substring(b+1));  // take the "\r" out
  440.                   b -= 1;
  441.                }
  442.  
  443.                // set the 
  444.                if(t.length() == 1 && isColor(t.charAt(0)))
  445.                {
  446.                   c = t.charAt(0);
  447.                }
  448.                else if(t.compareTo("tt") == 0)
  449.                {
  450.                   // it is the "time" variable!!
  451.                   if(date.getHours() >= 12)
  452.                      pm = 1;
  453.                   else 
  454.                      pm = 0;
  455.  
  456.                   if(pm == 1)
  457.                   {
  458.                      a = date.getHours();
  459.                      if(a == 12)
  460.                         time  = String.valueOf(12);
  461.                      else
  462.                         time = String.valueOf(date.getHours()-12);
  463.                   }
  464.                   else
  465.                   {
  466.                      a = date.getHours();
  467.                      if(a == 0)
  468.                         time = String.valueOf(12);
  469.                      else
  470.                         time = String.valueOf(a);
  471.                   }
  472.  
  473.                   time = time.concat(":");
  474.                   
  475.                   min = date.getMinutes();
  476.                   if(min >= 10)
  477.                      time = time.concat(String.valueOf(min));
  478.                   else
  479.                   {
  480.                      time = time.concat("0");
  481.                      time = time.concat(String.valueOf(min));
  482.                   }
  483.  
  484.                   if(pm == 1)
  485.                      time = time.concat(" pm");
  486.                      else
  487.                      time = time.concat(" am");
  488.  
  489.                   tmp = ((tmp.substring(0,b)).concat(time)).concat(tmp.substring(b));
  490.  
  491.                   b += time.length();
  492.  
  493.                   for(i = 0; i < time.length(); i++)
  494.                      fi.color = (fi.color).concat((new Character(c)).toString());
  495.  
  496.                } // End time
  497.                else if(t.compareTo("dd") == 0 || t.compareTo("DD") == 0)   // Set the current date
  498.                {
  499.                   if(t.compareTo("dd") == 0)
  500.                      ddmmyy = day[date.getDay()];
  501.                   else
  502.                      ddmmyy = Day[date.getDay()];
  503.                   
  504.                   // Set up the color
  505.                   for(i = 0; i < ddmmyy.length(); i++)
  506.                      fi.color = (fi.color).concat((new Character(c)).toString());
  507.  
  508.                   tmp = ((tmp.substring(0,b)).concat(ddmmyy)).concat(tmp.substring(b));
  509.                   b += ddmmyy.length();
  510.                }
  511.                else if(t.compareTo("dn") == 0)
  512.                {
  513.                   ddmmyy = String.valueOf(date.getDate());
  514.  
  515.                   // Set up the color
  516.                   for(i = 0; i < ddmmyy.length(); i++)
  517.                      fi.color = (fi.color).concat((new Character(c)).toString());
  518.  
  519.                   tmp = ((tmp.substring(0,b)).concat(ddmmyy)).concat(tmp.substring(b));
  520.                   b += ddmmyy.length();
  521.                }
  522.                else if(t.compareTo("mm") == 0 || t.compareTo("MM") == 0)  
  523.                {
  524.                   if(t.compareTo("mm") == 0)
  525.                      ddmmyy = month[date.getMonth()];
  526.                   else
  527.                      ddmmyy = Month[date.getMonth()];
  528.  
  529.                   // Set up the color
  530.                   for(i = 0; i < ddmmyy.length(); i++)
  531.                      fi.color = (fi.color).concat((new Character(c)).toString());
  532.  
  533.                   tmp = ((tmp.substring(0,b)).concat(ddmmyy)).concat(tmp.substring(b));
  534.                   b += ddmmyy.length();
  535.                }
  536.                else if(t.compareTo("mn") == 0)
  537.                {
  538.                   ddmmyy = String.valueOf(date.getMonth()+1);
  539.  
  540.                   // Set up the color
  541.                   for(i = 0; i < ddmmyy.length(); i++)
  542.                      fi.color = (fi.color).concat((new Character(c)).toString());
  543.  
  544.                   tmp = ((tmp.substring(0,b)).concat(ddmmyy)).concat(tmp.substring(b));
  545.                   b += ddmmyy.length();
  546.                }
  547.                else if(t.compareTo("yy") == 0 || t.compareTo("YY") == 0)
  548.                {
  549.                   if(t.compareTo("YY") == 0)
  550.                      ddmmyy = String.valueOf(date.getYear()+1900);
  551.                   else
  552.                      ddmmyy = String.valueOf(date.getYear()%100);
  553.  
  554.                   // Set up the color 
  555.                   for(i = 0; i < ddmmyy.length(); i++)
  556.                      fi.color = (fi.color).concat((new Character(c)).toString());
  557.  
  558.                   tmp = ((tmp.substring(0,b)).concat(ddmmyy)).concat(tmp.substring(b));
  559.                   b += ddmmyy.length();
  560.  
  561.                }  // End short date
  562.                else if(t.compareTo("\\") == 0)  // Are they trying to delimit the backslash?
  563.                {
  564.                   tmp = (tmp.substring(0,b)).concat(tmp.substring(b+1));  // delimit the '\'
  565.                   b--;
  566.                }
  567.                else
  568.                {
  569.                   // A little error output
  570.                   System.out.println("Backslash (\\) error in text line: "+ fi.store);
  571.                }
  572.                
  573.             }  // END - if(tmp.charAt(b) == '\\') 
  574.             else
  575.             {
  576.                b++;
  577.                fi.color = fi.color.concat((new Character(c)).toString());
  578.             }
  579.             
  580.          }  // END - for(...) 
  581.  
  582.       } // END - if(fi.func == ...)
  583.  
  584.       fi.text = tmp;
  585.       
  586.       return fi;
  587.       
  588.    }
  589.  
  590.    //////////////////////////////////////////////////////////////////
  591.    // Read in the script into a linked list of FuncInfo's 
  592.    void initScript()
  593.    {
  594.       InputStream file;
  595.       DataInputStream dis;
  596.       URL url;
  597.       String line;
  598.       int listlen;
  599.       int dos;
  600.       int a;
  601.  
  602.       try
  603.       {
  604.          url = new URL(documentURL,scrpt);
  605.             
  606.          file = url.openStream();
  607.          dis = new DataInputStream(file);
  608.  
  609.          list = new linkList();                                    // The linked list
  610.          start = list;                                             // The head of the list
  611.          ptr = list;                                               // The current element
  612.          listlen = 0;
  613.          dos = 0;                                                  // Used to know how many Do's there are
  614.          while((line = dis.readLine()) != null)
  615.          {
  616.             line = line.trim();                                    // cut off white space at the beginning and end
  617.             if(!(line.startsWith("!!")) && (line.length() != 0))   // Not a comment or blank line
  618.             {
  619.                listlen++;
  620.                ptr.fi = getFunc(line);                             // Get the function number
  621.                if(ptr.fi.func == 97)
  622.                   dos++;                                           // Chalk up another "Do"
  623.                ptr.next = new linkList();
  624.                ptr = ptr.next;  // advance to the next command
  625.             }
  626.          }
  627.  
  628.          // Ok now lets set the return pointers for the loops
  629.          ptr = start;
  630.          linkList stack[] = new linkList[dos];  // Allocate the array
  631.          dos = 0;
  632.          for(a=0;a<listlen;a++)
  633.          {
  634.             if(ptr.fi.func == 97) // A "Do"
  635.             {
  636.                stack[dos] = new linkList();
  637.                stack[dos] = ptr;
  638.                dos++;
  639.             }
  640.             else if(ptr.fi.func == 98)  // A Repeat
  641.             {
  642.                if(dos > 0)
  643.                {
  644.                   dos--;
  645.                   ptr.fi.ret = stack[dos];
  646.                }
  647.                else
  648.                {
  649.                   // OMYGOSH!! Script error output!!!!
  650.                   System.out.println("Repeat error in line : Repeat times="+ptr.fi.times);
  651.                   System.out.println("     Mismatched Do/Repeats?");
  652.                }
  653.             }
  654.             ptr = ptr.next;
  655.          }
  656.  
  657.          ptr = start;
  658.  
  659.          file.close();
  660.          dis.close();
  661.       }
  662.       catch (IOException e)
  663.       {
  664.          e.printStackTrace();
  665.       }
  666.    }  // End initScript()
  667. }  // End Class Script
  668.