home *** CD-ROM | disk | FTP | other *** search
/ Inside Dreamweaver 4 / IDW4.ISO / pc / Projects / ch20 / java / quote / src / ParamParser.java < prev    next >
Encoding:
Java Source  |  1998-08-28  |  5.3 KB  |  238 lines

  1. /*
  2. * Copyright (c) 1994-1998 Sun Microsystems, Inc. All Rights Reserved.
  3. *
  4. * Permission to use, copy, modify, and distribute this software and its
  5. * documentation for NON-COMMERCIAL or COMMERCIAL purposes and without fee is
  6. * hereby granted. Please refer to the file
  7. * http://java.sun.com/nav/business/trademark_guidelines.html for further
  8. * important copyright and trademark information and to
  9. * http://java.sun.com/nav/business/index.html for further important licensing
  10. * information for the Java (tm) Technology.
  11. * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  12. * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
  14. * NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  15. * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  16. * ITS DERIVATIVES.
  17. * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  18. * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE PERFORMANCE,
  19. * SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT NAVIGATION OR
  20. * COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE SUPPORT MACHINES, OR
  21. * WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE SOFTWARE COULD LEAD DIRECTLY TO
  22. * DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  23. * RISK ACTIVITIES"). SUN SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED
  24. * WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  25. */
  26.  
  27. import java.applet.*;
  28. import java.awt.*;
  29. import java.util.*;
  30.  
  31. public class ParamParser extends Object
  32. {
  33.    public CustomParser parser;
  34.    private Applet applet;
  35.  
  36.    public ParamParser(Applet applet)
  37.    {
  38.       this.applet = applet;
  39.       parser = new CustomParser();
  40.    }
  41.  
  42.    // Parse an integer from a string.
  43.  
  44.    public int parseInt(String name, int def)
  45.    {
  46.       String value = applet.getParameter(name);
  47.  
  48.       // The parameter is defined.
  49.  
  50.       if (value != null)
  51.       {
  52.          return Integer.parseInt(value);
  53.       }
  54.  
  55.       // Return the default value.
  56.  
  57.       else
  58.       {
  59.          return def;
  60.       }
  61.    }
  62.  
  63.    public int[] parseInts(String value, String sep)
  64.    {
  65.       if (value != null)
  66.       {
  67.          String[] str = parser.parseStrings(value, sep);
  68.          int[] data = new int[str.length];
  69.  
  70.          for (int i = 0; i < str.length; i++)
  71.          {
  72.             data[i] = Integer.parseInt(str[i]); 
  73.          }
  74.  
  75.          return data;
  76.       }
  77.  
  78.       else
  79.       {
  80.          return null;
  81.       }
  82.    }
  83.  
  84.    public long parseLong(String name, long def)
  85.    {
  86.       String value = applet.getParameter(name);
  87.  
  88.       if (value != null)
  89.       {
  90.          return Long.parseLong(value);
  91.       }
  92.  
  93.       else
  94.       {
  95.          return def;
  96.       }
  97.    }
  98.  
  99.    public Color parseColor(String name, Color def)
  100.    {
  101.       String value = applet.getParameter(name);
  102.  
  103.       if (value != null)
  104.       {
  105.          return new Color(Integer.parseInt(value, 16));
  106.       }
  107.  
  108.       else
  109.       {
  110.          return def;
  111.       }
  112.    }
  113.  
  114.    public Color parseColor(String name, String def)
  115.    {
  116.       String value = applet.getParameter(name);
  117.  
  118.       if (value != null)
  119.       {
  120.          return new Color(Integer.parseInt(value, 16));
  121.       }
  122.  
  123.       else
  124.       {
  125.          return new Color(Integer.parseInt(def, 16));
  126.       }
  127.    }
  128.  
  129.    public Font parseFont(String name, String fname, int style, int size)
  130.    {
  131.       String value = applet.getParameter(name);
  132.  
  133.       if (value != null)
  134.       {
  135.          String[] str = parser.parseStrings(value, "|");
  136.          int fsize = Integer.parseInt(str[2]);
  137.          int fstyle = Font.PLAIN;
  138.  
  139.          if (str[1].equalsIgnoreCase("PLAIN"))
  140.          {
  141.             fstyle = Font.PLAIN;
  142.          }
  143.  
  144.          if (str[1].equalsIgnoreCase("BOLD"))
  145.          {
  146.             fstyle = Font.BOLD;
  147.          }
  148.  
  149.          if (str[1].equalsIgnoreCase("ITALIC"))
  150.          {
  151.             fstyle = Font.ITALIC;
  152.          }
  153.  
  154.          return new Font(str[0], fstyle, fsize);
  155.       }
  156.  
  157.       else
  158.       {
  159.          return new Font(fname, style, size);
  160.       }
  161.    }
  162.  
  163.    public boolean parseBoolean(String name, boolean def)
  164.    {
  165.       String value = applet.getParameter(name);
  166.  
  167.       if (value != null)
  168.       {
  169.          return (Boolean.valueOf(value)).booleanValue();
  170.       }
  171.  
  172.       else
  173.       {
  174.          return def;
  175.       }
  176.    }
  177.  
  178.    public String parseString(String name, String def)
  179.    {
  180.       String value = applet.getParameter(name);
  181.  
  182.       if (value != null)
  183.       {
  184.          return value;
  185.       }
  186.  
  187.       else
  188.       {
  189.          return def;
  190.       }
  191.    }
  192.  
  193.    public String[] parseStrings(String name, String sep)
  194.    {
  195.       String value = applet.getParameter(name);
  196.  
  197.       if (value != null)
  198.       {
  199.          return parser.parseStrings(value, sep);
  200.       }
  201.  
  202.       else
  203.       {
  204.          System.err.println("name=\"" + name + "\" not defined.");
  205.          return null;
  206.       }
  207.    }
  208.  
  209.    public String[] parseStrings(String name, String sep, int num)
  210.    {
  211.       String value = applet.getParameter(name);
  212.  
  213.       if (value != null)
  214.       {
  215.          String result[] = parser.parseStrings(value, sep);
  216.  
  217.          if (result.length == num)
  218.          {
  219.             return result;
  220.          }
  221.  
  222.          else
  223.          {
  224.             System.err.println("name=\"" + name + "\" format incorrect.");
  225.             return null;
  226.          }
  227.       }
  228.  
  229.       else
  230.       {
  231.          System.err.println("name=\"" + name + "\" not defined.");
  232.          return null;
  233.       }
  234.    }
  235. }
  236.