Save This Page
Home » openjdk-7 » net.sourceforge » dictport » [javadoc | source]
    1   /*
    2    *    Dictionary port to Java
    3    *    Copyright (C) 2000  Kaloian Doganov
    4    *
    5    *    This program is free software; you can redistribute it and/or modify
    6    *    it under the terms of the GNU General Public License as published by
    7    *    the Free Software Foundation; either version 2 of the License, or
    8    *    (at your option) any later version.
    9    *
   10    *    This program is distributed in the hope that it will be useful,
   11    *    but WITHOUT ANY WARRANTY; without even the implied warranty of
   12    *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13    *    GNU General Public License for more details.
   14   
   15    *    You should have received a copy of the GNU General Public License
   16    *    along with this program; if not, write to the Free Software
   17    *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18    */
   19   package net.sourceforge.dictport;
   20   
   21   import java.util;
   22   import java.io;
   23   import java.net.URL;
   24   
   25   /**
   26    * Provides convenient way to access dictionary database roots.
   27    *
   28    * @author Kaloian Doganov <kaloian@europe.com>
   29    */
   30    
   31   public final class DatabaseRegistry {
   32   	
   33   	private Map fDicts = null;
   34   	private Database fOpened = null;
   35   	private static final String kDictDir = "data";
   36   	private static final String kDatFile = "00.dat";
   37   	
   38   	/** Instantiate and search for available dictionaries. */
   39   	public DatabaseRegistry() {
   40   		fDicts = new HashMap();
   41   		
   42   		// Dirty hack to find and process dict/00.dat file.
   43   		Class c = this.getClass();
   44   		URL dirty = c.getResource(kDictDir + "/" +kDatFile);
   45   		if (dirty != null) {
   46   			try {
   47   				DictInfo first = extractDictInfo(dirty);
   48   				fDicts.put(first.getName(), first);
   49   			}
   50   			catch (IOException e) {
   51   				e.printStackTrace();
   52   				throw new RuntimeException("IOError. Can not handle the root file.");
   53   			}
   54   		}
   55   		else throw new RuntimeException(kDictDir + " resource is not found");
   56   	}
   57   	
   58   	/** Return list of available dictionaries. */
   59   	public Set getDictionaries() {
   60   		return fDicts.keySet();
   61   	}
   62   
   63   	/** Return info about spefific dictionary. */
   64   	public DictInfo getDictInfo(String aDictID) {
   65   		return (DictInfo) fDicts.get(aDictID);
   66   	}
   67   	
   68   	/** Open dictionary. */
   69   	public Database open(String aDictID) {
   70   		if (!fDicts.keySet().contains(aDictID))
   71   			throw new IllegalArgumentException("Invalid dicionary name");
   72   		
   73   		fOpened = new Database((DictInfo) fDicts.get(aDictID));
   74   		return fOpened;
   75   	}
   76   	
   77   	/**
   78   	 * Return opened dictionary.
   79   	 *
   80   	 * @return Current database or null if there is no open database.
   81   	 */
   82   	public Database getOpened() {
   83   		return fOpened;
   84   	}
   85   
   86   	/** Extract all fields from 00.dat file. */
   87   	private DictInfo extractDictInfo(URL aData) throws IOException {
   88   		// read bytes
   89   		InputStream in = aData.openStream();
   90   		String name = extractName(in);
   91   		boolean isDual = extractBoolean(in);
   92   		boolean isCaseSensitive = extractBoolean(in);
   93   		in.close();
   94   		DictInfo result = new DictInfo(name, removeFileFromURL(aData.toString()),
   95   			isDual, isCaseSensitive);
   96   		return result;
   97   	}
   98   	
   99   	/** Remove file part from an URL. */
  100   	private String removeFileFromURL(String aURL) {
  101   		int i = aURL.lastIndexOf('/');
  102   		if (i == -1) throw new RuntimeException("Invalid URL");
  103   		return aURL.substring(0, i) + '/';
  104   	}
  105   	
  106   	/** Extract name field from 00.dat file. */
  107   	private String extractName(InputStream aIn) throws IOException {
  108   		// initialize array
  109   		byte[] buffer = new byte[64];
  110   		Arrays.fill(buffer, (byte) 0);
  111   			
  112   		// read bytes
  113   		aIn.read(buffer, 0, buffer.length);
  114   		
  115   		// handle C-style string
  116   		int size = buffer.length - 1;
  117   		for (int i = 0; i < buffer.length; i++) { // find first ocurring of 0 (zero)
  118   			if (buffer[i] == 0) {
  119   				size = i;
  120   				break;
  121   			}
  122   		}
  123   		
  124   		String result = new String(buffer, 0, size);
  125   		return result;
  126   	}
  127   	
  128   	/** Extract boolean field from 00.dat file. */
  129   	private boolean extractBoolean(InputStream aIn) throws IOException {
  130   		// initialize array
  131   		byte[] buffer = new byte[4];
  132   		Arrays.fill(buffer, (byte) 0);
  133   			
  134   		// read bytes
  135   		aIn.read(buffer, 0, buffer.length);
  136   		int result = buffer[0];
  137   		result <<= 4;
  138   		result = result | buffer[1];
  139   		result <<= 4;
  140   		result = result | buffer[2];
  141   		result <<= 4;
  142   		result = result | buffer[3];
  143   		
  144   		return result > 0;
  145   	}
  146   	
  147   	/** Encapsulate dictionary info in one immutable object. */
  148   	public final static class DictInfo {
  149   		private String fName;
  150   		private String fRootDir;
  151   		private boolean fIsDual;
  152   		private boolean fIsCaseSensitive;
  153   		
  154   		DictInfo(String aName, String aRootDir, boolean aIsDual,
  155   			boolean aIsCaseSensitive) {
  156   			
  157   			fName = aName;
  158   			fRootDir = aRootDir;
  159   			fIsDual = aIsDual;
  160   			fIsCaseSensitive = aIsCaseSensitive;
  161   		}
  162   		
  163   		/** Return dictionary's name. */
  164   		public String getName() {
  165   			return fName;
  166   		}
  167   		
  168   		/** Return dictionary's root directory (in URL form). */
  169   		public String getRoot() {
  170   			return fRootDir;
  171   		}
  172   		
  173   		public boolean isDual() {
  174   			return fIsDual;
  175   		}
  176   		
  177   		public boolean isCaseSensitive() {
  178   			return fIsCaseSensitive;
  179   		}
  180   		
  181   		public String toString() {
  182   			StringBuffer result = new StringBuffer();
  183   			result.append("[DictInfo: name=");
  184   			result.append(fName);
  185   			result.append(", root=");
  186   			result.append(fRootDir);
  187   			result.append(", isDual=");
  188   			result.append((new Boolean(fIsDual)).toString());
  189   			result.append(", isCaseSensitive=");
  190   			result.append((new Boolean(fIsCaseSensitive)).toString());
  191   			result.append("]");
  192   			return result.toString();
  193   		}
  194   	}
  195   	
  196   	/** Self test method. */
  197   	public static void main(String[] args) {
  198   		DatabaseRegistry reg = new DatabaseRegistry();
  199   		Set dicts = reg.getDictionaries();
  200   		Iterator i = dicts.iterator();
  201   		while (i.hasNext()) {
  202   			DictInfo info = reg.getDictInfo(i.next().toString());
  203   			System.out.println(info.toString());
  204   		}
  205   	}
  206   }

Save This Page
Home » openjdk-7 » net.sourceforge » dictport » [javadoc | source]