home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / dek17tau / classes / spt / applets / appletparamparser.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  7.0 KB  |  278 lines

  1.  
  2. /*
  3.  * AppletParamParser.java
  4.  *
  5.  * Copyright (C) 1996 Shaun Terry. All Rights Reserved.
  6.  */
  7.  
  8. package spt.applets;
  9.  
  10. import java.lang.String;
  11. import java.lang.Boolean;
  12. import java.util.StringTokenizer;
  13. import java.awt.Font;
  14. import java.awt.Color;
  15. import java.net.URL;
  16. import java.net.MalformedURLException;
  17. import java.applet.*;
  18.  
  19. import spt.gui.BorderedComponent;
  20. import spt.util.StringUtil;
  21.  
  22.  
  23. /**
  24.  * A utility class that knows how to parse a wide variety of applet
  25.  * parameters (int, Colors, URLs, etc.)
  26.  *
  27.  * @author Shaun Terry
  28.  */
  29.  
  30. public class AppletParamParser {
  31.  
  32.     Applet applet;
  33.  
  34.     public AppletParamParser(Applet a) { applet = a; }
  35.  
  36.     /** Was the parameter supplied?
  37.      */
  38.     public boolean parameterExists(String name) {
  39.         return applet.getParameter(name) != null;
  40.     }
  41.  
  42.     /** Get the value of a parameter in String format. Returns
  43.      *    null if no parameter exists. (Really just calls
  44.      *    the applet version of the function).
  45.      * @see java.applet.Applet#getParameter
  46.      */
  47.     public String getParameter(String name) {
  48.         return applet.getParameter(name);
  49.     }
  50.  
  51.     /** Get a String parameter or return a default value if it
  52.      *    doesn't exist.
  53.      */
  54.     public String getParameter(String name, String defaultVal) {
  55.         String s = getParameter(name);
  56.         return s != null ? s : defaultVal;
  57.     }
  58.  
  59.     /** Get a Boolean parameter. A true value is the string "TRUE",
  60.      *    "YES" or "1", all case insensitive. False is anything else.
  61.      *     null is returned if it doesn't exist.
  62.      * @see java.lang.Boolean
  63.      */
  64.     public Boolean getBoolean(String name) {
  65.         String s = getParameter(name);
  66.         if (s == null) return null;
  67.  
  68.         if (s.equalsIgnoreCase("TRUE") ||
  69.                 s.equalsIgnoreCase("YES") || s.equalsIgnoreCase("1"))
  70.             return new Boolean(true);
  71.         else return new Boolean(false);
  72.     }
  73.  
  74.     /** Return a Boolean or default value if it doesn't exist.
  75.      */
  76.     public Boolean getBoolean(String name, boolean defaultVal) {
  77.         Boolean b = getBoolean(name);
  78.         return b != null ? b : new Boolean(defaultVal);
  79.     }
  80.  
  81.     /** Get an Integer parameter. null is returned if it doesn't exist.
  82.      * @see java.lang.Integer
  83.      */
  84.     public Integer getInteger(String name) {
  85.         String s = getParameter(name);
  86.         if (s == null) return null;
  87.  
  88.         try {
  89.             int i = Integer.parseInt(s);
  90.             return new Integer(i);
  91.         }
  92.         catch (NumberFormatException e) {
  93.             return null;
  94.         }
  95.     }
  96.  
  97.     /** Return an Integer or default value if it doesn't exist.
  98.      */
  99.     public Integer getInteger(String name, int defaultVal) {
  100.         Integer i = getInteger(name);
  101.         return i != null ? i : new Integer(defaultVal);
  102.     }
  103.  
  104.     /** Get a Float parameter. null is returned if it doesn't exist.
  105.      * @see java.lang.Float
  106.      */
  107.     public Float getFloat(String name) {
  108.         String s = getParameter(name);
  109.         if (s == null) return null;
  110.  
  111.         try {
  112.             return Float.valueOf(s);
  113.         }
  114.         catch (NumberFormatException e) {
  115.             return null;
  116.         }
  117.     }
  118.  
  119.     /** Return a Float or default value if it doesn't exist.
  120.      */
  121.     public Float getFloat(String name, float defaultVal) {
  122.         Float i = getFloat(name);
  123.         return i != null ? i : new Float(defaultVal);
  124.     }
  125.  
  126.     /** Get a Double parameter. null is returned if it doesn't exist.
  127.      * @see java.lang.Double
  128.      */
  129.     public Double getDouble(String name) {
  130.         String s = getParameter(name);
  131.         if (s == null) return null;
  132.  
  133.         try {
  134.             return Double.valueOf(s);
  135.         }
  136.         catch (NumberFormatException e) {
  137.             return null;
  138.         }
  139.     }
  140.  
  141.     /** Return a Double or default value if it doesn't exist.
  142.      */
  143.     public Double getDouble(String name, float defaultVal) {
  144.         Double i = getDouble(name);
  145.         return i != null ? i : new Double(defaultVal);
  146.     }
  147.  
  148.     /** Get a Color parameter. null is returned if it doesn't exist.
  149.      *    Colors can be in several formats.
  150.      * @see spt.util.StringUtil
  151.      * @see java.awt.Color
  152.      */
  153.     public Color getColor(String name) {
  154.         String s = getParameter(name);
  155.         if (s == null) return null;
  156.  
  157.         s = s.trim();
  158.  
  159.         return StringUtil.stringToColor(s);
  160.     }
  161.  
  162.     /** Return a Color or default value if it doesn't exist.
  163.      */
  164.     public Color getColor(String name, Color defaultVal) {
  165.         Color c = getColor(name);
  166.         return c != null ? c : defaultVal;
  167.     }
  168.  
  169.     /** Get a URL parameter. null is returned if it doesn't exist.
  170.      *    URLs can be relative or absolute.
  171.      * @see java.net.URL
  172.      */
  173.     public URL getURL(String name) throws MalformedURLException {
  174.         String s = getParameter(name);
  175.         return (s == null) ? null : new URL(applet.getCodeBase(), s);
  176.     }
  177.  
  178.     /** Return a URL or default value if it doesn't exist.
  179.      */
  180.     public URL getURL(String name, URL defaultVal) throws MalformedURLException {
  181.         URL u = getURL(name);
  182.         return u != null ? u : defaultVal;
  183.     }
  184.  
  185.     /** Get a Font parameter. null is returned if it doesn't exist.
  186.      *    Fonts are specified as: "FontName,Style,Point"
  187.      *    (e.g. "Helvetica,BOLD,14"). Style can be one of: PLAIN, BOLD,
  188.      *    ITALIC or BOLDITALIC.
  189.      * @see java.awt.Font
  190.      */
  191.     public Font getFont(String name) {
  192.         String s = getParameter(name);
  193.         if (s == null) return null;
  194.         
  195.         StringTokenizer tok = new StringTokenizer(s, " ,");
  196.  
  197.         int i=0;
  198.         String family=null;
  199.         int style=(-1), pointSize=0;
  200.  
  201.         while (tok.hasMoreTokens()) {
  202.             s = tok.nextToken();
  203.             if (i==0) family = s;
  204.             else if (i==1) {
  205.                 if (s.equalsIgnoreCase("PLAIN")) style = Font.PLAIN;
  206.                 else if (s.equalsIgnoreCase("BOLD")) style = Font.BOLD;
  207.                 else if (s.equalsIgnoreCase("ITALIC")) style = Font.ITALIC;
  208.                 else if (s.equalsIgnoreCase("BOLDITALIC"))
  209.                         style = Font.BOLD|Font.ITALIC;
  210.             }
  211.             else if (i==2) {
  212.                 try
  213.                     pointSize = Integer.parseInt(s, 10);
  214.                 catch (NumberFormatException e) {}
  215.             }
  216.             ++i;
  217.         }
  218.  
  219.         if (family != null && style >= 0 && pointSize > 0)
  220.             return new Font(family, style, pointSize);
  221.         return null;
  222.     }
  223.  
  224.     /** Return a Font or default value if it doesn't exist.
  225.      */
  226.     public Font getFont(String name, Font defaultVal) {
  227.         Font f = getFont(name);
  228.         return f != null ? f : defaultVal;
  229.     }
  230.  
  231.     /** Gets border style useful for the BorderComponent class.
  232.      * @see spt.gui.BorderedComponent
  233.      */
  234.     public int getBorderStyle() {
  235.         String s = getParameter("BORDERSTYLE");
  236.         if (s == null) return (-1);
  237.  
  238.         if (s.equalsIgnoreCase("NONE")) return BorderedComponent.NONE;
  239.         else if (s.equalsIgnoreCase("IN")) return BorderedComponent.IN;
  240.         else if (s.equalsIgnoreCase("OUT")) return BorderedComponent.OUT;
  241.         else if (s.equalsIgnoreCase("BORDER_IN")) return BorderedComponent.BORDER_IN;
  242.         else if (s.equalsIgnoreCase("BORDER_OUT")) return BorderedComponent.BORDER_OUT;
  243.         return (-1);
  244.     }
  245.  
  246.     // These are some common params you'd want to set for applets
  247.  
  248.     /** Gets the color specified by the BGCOLOR parameter and sets
  249.      *    the applet background to it.
  250.      */
  251.     public void applyBGColorParam() {
  252.         Color c;
  253.         if ((c = getColor("BGCOLOR")) != null) applet.setBackground(c);
  254.     }
  255.  
  256.     /** Gets the color specified by the FGCOLOR parameter and sets
  257.      *    the applet foreground to it.
  258.      */
  259.     public void applyFGColorParam() {
  260.         Color c;
  261.         if ((c = getColor("FGCOLOR")) != null) applet.setForeground(c);
  262.     }
  263.  
  264.     /** Processes FGCOLOR and BGCOLOR at once.
  265.      */
  266.     public void applyFGBGColorParam() {
  267.         applyFGColorParam();
  268.         applyBGColorParam();
  269.     }
  270.  
  271.     /** Gets the font specified by the FONT parameter and sets
  272.      *    the applet font to it.
  273.      */
  274.     public void applyFontParam() {
  275.         Font f = getFont("FONT");
  276.         if (f != null) applet.setFont(f);
  277.     }
  278. }