home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / GameLibrary.java < prev    next >
Text File  |  1998-05-08  |  5KB  |  169 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.server;
  21.  
  22. import java.util.*;
  23. import java.io.*;
  24.  
  25. class GameLibrary
  26. {
  27.   static Hashtable dir = new Hashtable();
  28.   File library;
  29.   String libraryName;
  30.   String [] dirlist;
  31.   String gameList = null;
  32.   GameLibrary parent;
  33.  
  34.   public GameLibrary(String directoryName,GameLibrary parent ) {
  35.     try {
  36.     if (directoryName == null && parent == null)
  37.         directoryName = "Library";
  38.     library = new File(directoryName);
  39.     if (!library.isDirectory())
  40.       System.out.println( library.getAbsolutePath()  + " is not a directory");
  41.     libraryName = directoryName;
  42.     dirlist = library.list();
  43.     this.parent = parent;
  44.     }
  45.     catch (Exception e) {
  46.       e.printStackTrace();
  47.     }  
  48.   }
  49.  
  50.   public String get(String gameName) {
  51.     if (gameName.startsWith(".") )
  52.       return getList();
  53.     else
  54.       return getGame(gameName);
  55.   }
  56.  
  57.   public String getList() {
  58.     if (gameList == null)
  59.     if (dirlist != null) {
  60.       if (parent == null)
  61.         gameList = "M" + dirlist[0] + "?";
  62.       else
  63.         gameList = "M..?M" + dirlist[0] + "?";
  64.       for (int i=1; i<dirlist.length;++i) {
  65.         gameList = gameList + "M" + dirlist[i] + "?";
  66.       }
  67.     }
  68.     else
  69.       System.out.println("Library directory is empty");
  70.     return gameList;
  71.   }
  72.  
  73.   String getLibraryName() {
  74.      return libraryName;
  75.     // if (parent == null)
  76.     //    return libraryName;
  77.     // else
  78.     //    return parent.getLibraryName() + "\\" + libraryName;
  79.   }
  80.   //return the contents of the file
  81.   static public GameLibrary getLibrary(String filename,GameLibrary gl){
  82.  
  83.     try {
  84.        if (gl ==  null) {
  85.          System.out.println("GameLibrary is null");
  86.          if (filename != null && GameLibrary.dir.containsKey(filename))
  87.            return (GameLibrary) GameLibrary.dir.get(filename);
  88.          else
  89.            return new GameLibrary (filename,null);
  90.       }
  91.       if (filename.equals(".."))    {
  92.          System.out.println("filename = ..");
  93.          if (gl.parent != null)
  94.             return gl.parent;
  95.          else
  96.             return gl;
  97.       }
  98.       if (filename.equals(".")) {
  99.          System.out.println("filename = ..");
  100.          return gl   ;
  101.       }
  102.       File gamefile = new File(gl.getLibraryName(),filename);
  103.       if (gamefile == null )
  104.          System.out.println("gamefile is null");
  105.       else {
  106.         if (gamefile.isDirectory()) {
  107.           GameLibrary g ;
  108.           String dirKey =  gl.getLibraryName() + "\\" + filename;
  109.           if (GameLibrary.dir.containsKey(dirKey))
  110.             g =(GameLibrary) GameLibrary.dir.get(dirKey);
  111.           else {
  112.             g = new GameLibrary(dirKey,gl);
  113.             GameLibrary.dir.put(g,dirKey);
  114.           }
  115.           return g;
  116.         }
  117.       }
  118.     }
  119.     catch (Exception e) {System.out.println("GameLibrary.getLibrary " + e);}
  120.     return gl;
  121.   }
  122.  
  123.   public String getGame(String filename){
  124.     System.out.println("getGame" + filename);
  125.     FileInputStream fs = null;
  126.     String moves = "";
  127.     try {
  128.       File gamefile = new File(getLibraryName(),filename);
  129.       if (gamefile != null) {
  130.         if (gamefile.isDirectory()) {
  131.           return null;
  132.         }
  133.         else {
  134.           fs = new FileInputStream(gamefile) ;
  135.           DataInputStream ds = new DataInputStream(fs);
  136.           InputStreamReader isr  = new InputStreamReader(ds);
  137.           BufferedReader br = new BufferedReader(isr);
  138.           try {
  139.  
  140.         ///    int offset = 0;
  141.          //   int arraySize = 1024;
  142.          //   byte moveBytes[] = new byte[1024];
  143.          //   int  length = ds.read(moveBytes,offset,arraySize);
  144.          //   while (length > 0)   {
  145.          //     moves =  moves + new String(moveBytes,255,0,length-1);
  146.            //   length = ds.read(moveBytes,offset,arraySize);
  147.  
  148.          //   }
  149.             String line = br.readLine() ;
  150.             moves = "";
  151.             while (line != null )  {
  152.               moves =  moves + line + " ";
  153.             line = br.readLine()  ;
  154.  
  155.             }
  156.           }
  157.           catch (IOException e) {}
  158.           fs.close();
  159.         }
  160.       }
  161.       else
  162.         System.out.println("nullFile " +  getLibraryName() + " " + filename);
  163.     }
  164.     catch (Exception e) {System.out.println("GameLibrary.getGame " + e);}
  165.     return moves;
  166.    }
  167.  
  168. }
  169.