home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / PersistString.java < prev    next >
Text File  |  1998-05-08  |  3KB  |  81 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.apps.chess.client;
  21. import java.io.*;
  22. import java.util.Vector;
  23.  
  24. /**re-invent the wheel: Concatenate an array of Strings in such a way that they can hold any data
  25. * and be parsed back into the array of strings they were created from
  26. * Simplifies passing multiparameter messages between client and server
  27. */
  28. public class PersistString {
  29.   static public String concat(String[] data ) {
  30.     String retVal = "";
  31.     for (int i=0;i < data.length && data[i] != null ;i++)
  32.        retVal = retVal + i + "\"" + PersistString.padQuotes(data[i])+ "\"" ;
  33.     return  retVal;
  34.   }
  35.   
  36.   static public String [] parse(String infoData) {
  37.     Vector output = new Vector(10);
  38.     StringReader stream = new StringReader(infoData);
  39.     //StringBufferInputStream stream  = new StringBufferInputStream(infoData);
  40.     //StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(stream, 1000));
  41.     StreamTokenizer st = new StreamTokenizer(stream);
  42.  
  43.     try {
  44.       st.nextToken();
  45.       while(true) {
  46.         String data = null;
  47.         while (st.nextToken() == '"') {
  48.           if (data == null)
  49.             data = st.sval;
  50.           else
  51.             data = data + "\"" + st.sval;
  52.         }
  53.         output.addElement(data);
  54.         if (st.ttype == StreamTokenizer.TT_EOF)
  55.           break;
  56.       }
  57.     }
  58.     catch (Exception e) {
  59.       System.out.println("PersistStream error " + e);
  60.     }
  61.     String [] outputArray = new String [output.size()];
  62.     for (int i=0;i<output.size();i++)
  63.       outputArray[i] = (String) output.elementAt(i);
  64.     return outputArray;
  65.   }
  66.  
  67.   static String padQuotes(String data) {
  68.     int index = 0 ;
  69.     int oldIndex = 0;
  70.     while (index >= 0) {
  71.       index = data.indexOf('\"',oldIndex);
  72.       if (index > 0) {
  73.         data = data.substring(0,index) + "\"" + data.substring(index );
  74.         //System.out.println ("Found a quote in the data " + data);
  75.         oldIndex = index + 2;
  76.       }
  77.     }
  78.     return data;
  79.   }
  80. }
  81.