home *** CD-ROM | disk | FTP | other *** search
/ Inside Dreamweaver 4 / IDW4.ISO / pc / Projects / ch20 / java / quote / src / CustomParser.java next >
Encoding:
Java Source  |  1998-08-28  |  2.8 KB  |  93 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.awt.*;
  28. import java.util.*;
  29.  
  30. public class CustomParser extends Object
  31. {
  32.    public String[] parseStrings(String str, String sep)
  33.    {
  34.       if (str != null)
  35.       {
  36.          StringTokenizer st = new StringTokenizer(str, sep);
  37.          String result[] = new String[st.countTokens()];
  38.  
  39.          for (int i = 0; i < result.length; i++)
  40.          {
  41.             result[i] = st.nextToken();
  42.          }
  43.  
  44.          return result;
  45.       }
  46.  
  47.       else
  48.       {
  49.          return null;
  50.       }
  51.    }
  52.  
  53.    public String[] wordWrap(String str, FontMetrics fm, int width)
  54.    {
  55.       Vector list = new Vector(0, 1);
  56.       String word[] = parseStrings(str, " ");
  57.       String line = null;
  58.  
  59.       line = word[0];
  60.  
  61.       for (int i = 1; i < word.length; i++)
  62.       {
  63.          if ((fm.stringWidth(line) + fm.stringWidth(word[i] + " ")) >= (width))
  64.          {
  65.             list.insertElementAt(line, list.size());
  66.             line = word[i];
  67.          }
  68.  
  69.          else
  70.          {
  71.             line = line + " " + word[i];
  72.          }
  73.  
  74.          if (i == (word.length - 1))
  75.          {
  76.             list.insertElementAt(line, list.size());
  77.          }
  78.       }
  79.  
  80.       int num = list.size();
  81.       String result[] = new String[num];
  82.  
  83.       for (int i = 0; i < num; i++)
  84.       {
  85.          result[i] = (String) (list.elementAt(i));
  86.       }
  87.  
  88.       return result;
  89.    }
  90. }
  91.