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   import java.net.MalformedURLException;
   25   
   26   /**
   27    * Provides access to dictionary database.
   28    *
   29    * @author Kaloian Doganov <kaloian@europe.com>
   30    */
   31   
   32   public final class Database {
   33   	
   34   	/** Default locale (Bulgarian). */
   35   	public static final Locale kLocaleBG = new Locale("bg", "BG");
   36   	
   37   	/** Default data file (chapter) extension. */
   38   	public static final String kExt = ".dat";
   39   	
   40   	private DatabaseRegistry.DictInfo fInfo = null;
   41   	private History fHistory = null;
   42   	private Chapter fChapter = null;
   43   	
   44   	/**
   45   	 * Initialize new database.
   46   	 */
   47   	Database(DatabaseRegistry.DictInfo aInfo) {
   48   		fInfo = aInfo;
   49   		fHistory = new History();
   50   		fChapter = null;
   51   	}
   52   	
   53   	/** Return Database info. */
   54   	public DatabaseRegistry.DictInfo getInfo() {
   55   		return fInfo;
   56   	}
   57   	
   58   	/** Extract chapter ID from word. */
   59   	public String getChapterID(String aWord) {
   60   		if (aWord.length() < 1)
   61   			throw new IllegalArgumentException("Empty string passed as aWord.");
   62   		char c = aWord.charAt(0);
   63   		Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
   64   		String key = aWord;
   65   		
   66   		int i = 0;
   67   		
   68   		try {
   69   			byte[] chars = null;
   70   			key = aWord.toUpperCase(kLocaleBG);
   71   			chars = key.getBytes("Cp1251");
   72   			i = (int) chars[0];
   73   			if (block == Character.UnicodeBlock.CYRILLIC) {
   74   						 // Bytes in Java are signed, so the value
   75   			i = i + 256; // 0xC0 is represented as -64 (dec), not as 192 (dec).
   76   						 // We fix that value here.
   77   			}
   78   			
   79   		}
   80   		catch (UnsupportedEncodingException e) {
   81   			throw new RuntimeException("Cp1251 is unsupported encoding.");
   82   		}
   83   		return Integer.toHexString(i);
   84   	}
   85   	
   86   	/**
   87   	 * Return apropriate chapter object. If the requested chapter
   88   	 * equals to the current opened chapter, it is returned and no
   89   	 * new chapter is instantiated..
   90   	 */
   91   	public Chapter getChapter(String aWord) {
   92   		String id = getChapterID(aWord);
   93   		if ((fChapter != null) && fChapter.getID().equals(id))
   94   			return fChapter;
   95   
   96   		String urlString = fInfo.getRoot() + id + kExt;
   97   		URL url = null;
   98   		try {
   99   			url = new URL(urlString);
  100   		}
  101   		catch (MalformedURLException e) {
  102   			throw new RuntimeException("Malformed URL: " + urlString);
  103   		}
  104   		Chapter chapter = new Chapter(id, url, kLocaleBG);
  105   		fChapter = chapter;
  106   		return chapter;
  107   	}
  108   	
  109   	/**
  110   	 * Return matching entry.
  111   	 */
  112   	public Chapter.Entry findExact(String aWord) throws ItemNotFoundException {
  113   		String key = displayToKey(aWord);
  114   		Chapter chapter = getChapter(key);
  115   		Chapter.Entry result = chapter.findExact(key);
  116   		fHistory.add(result);
  117   		return result;
  118   	}
  119   	
  120   	/**
  121   	 * Return matching entry.
  122   	 */
  123   	public Chapter.Entry findAutoComplete(String aWord) throws ItemNotFoundException {
  124   		String key = displayToKey(aWord);
  125   		Chapter chapter = getChapter(key);
  126   		return chapter.findAutoComplete(key);
  127   	}
  128   	
  129   	/**
  130   	 * Return list of similar entries.
  131   	 */
  132   	public List findSimilar(String aWord) {
  133   		String key = displayToKey(aWord);
  134   		Chapter chapter = getChapter(key);
  135   		return chapter.findSimilar(key);
  136   	}
  137   	
  138   	/**
  139   	 * Return previous chapter item.
  140   	 */
  141   	public Chapter.Entry getPrevious(String aWord) {
  142   		String key = displayToKey(aWord);
  143   		Chapter chapter = getChapter(key);
  144   		return chapter.getPrevious(key);
  145   	}
  146   	
  147   	/**
  148   	 * Return next chapter item.
  149   	 */
  150   	public Chapter.Entry getNext(String aWord) {
  151   		String key = displayToKey(aWord);
  152   		Chapter chapter = getChapter(key);
  153   		return chapter.getNext(key);
  154   	}
  155   	
  156   	/**
  157   	 * Return next 20 chapter items.
  158   	 */
  159   	public List getNext20(String aWord) {
  160   		String key = displayToKey(aWord);
  161   		Chapter chapter = getChapter(key);
  162   		return chapter.getNext20(key);
  163   	}
  164   	
  165   	/**
  166   	 * Return history buffer display keys.
  167   	 */
  168   	public List getHistory() {
  169   		return fHistory.get();
  170   	}
  171   	
  172   	/**
  173   	 * Return previous item in the history buffer.
  174   	 *
  175   	 * @return Empty entry, if item is not found.
  176   	 */
  177   	public Chapter.Entry getHistoryPrevious() {
  178   		String key = fHistory.getPrevious();
  179   		try {
  180   			return findExact(key);
  181   		}
  182   		catch (ItemNotFoundException e) {
  183   			return Chapter.Entry.createEmpty();
  184   		}
  185   	}
  186   	
  187   	/**
  188   	 * Return next item in the history buffer.
  189   	 *
  190   	 * @return Empty entry, if item is not found.
  191   	 */
  192   	public Chapter.Entry getHistoryNext() {
  193   		String key = fHistory.getNext();
  194   		try {
  195   			return findExact(key);
  196   		}
  197   		catch (ItemNotFoundException e) {
  198   			return Chapter.Entry.createEmpty();
  199   		}
  200   	}
  201   
  202   	/**
  203   	 * Convert display key to searchable key (remove hyphens, if any
  204   	 * and lower-case all chars).
  205   	 */
  206   	public static String displayToKey(String aDisplay) {
  207   		
  208   		String lowercased = aDisplay.toLowerCase(kLocaleBG); // lowercase
  209   		
  210   		if (lowercased.indexOf('-') == -1) // doesn't contain hyphens
  211   			return lowercased;
  212   		
  213   		// Remove hyphens.
  214   		int len = lowercased.length();
  215   		StringBuffer result = new StringBuffer(len);
  216   		for (int i = 0; i < len; i++) {
  217   			char c = lowercased.charAt(i);
  218   			if (c != '-') result.append(c);
  219   		}
  220   		return result.toString();
  221   	}
  222   	
  223   	/** String-based history buffer. */
  224   	private final class History {
  225   
  226   		/** List of display keys in reverse order, e.g. - most recent is last. */
  227   		private List fBuffer;
  228   		
  229   		/** Pointer to current item in buffer. */
  230   		private int fPointer;
  231   
  232   		/** Uninitialized pointer magic value. */
  233   		private static final int kInvalid = -1;
  234   		
  235   		/** Create empty history. */
  236   		public History() {
  237   			fBuffer = new ArrayList();
  238   			fPointer = kInvalid;
  239   		}
  240   		
  241   		/**
  242   		 * Add chapter entry. If entry is already found in the history, it's not
  243   		 * added again. If the entry is empty, it is not added and
  244   		 * no exception is thrown.
  245   		 */
  246   		public synchronized void add(Chapter.Entry aEntry) {
  247   			if (aEntry.isEmpty()) return;
  248   			String key = aEntry.getKeyDisplay();
  249   			int i = fBuffer.indexOf(key);
  250   			if (i != kInvalid) {
  251   				// fBuffer contains the key
  252   				fPointer = i;
  253   			}
  254   			else {
  255   				fBuffer.add(key);
  256   				fPointer = fBuffer.size() - 1;
  257   			}
  258   		}
  259   		
  260   		/**
  261   		 * Get history buffer.
  262   		 *
  263   		 * @return List of display keys (String objects), or empty list if the
  264   		 *         history buffer is empty. Most recent first.
  265   		 */
  266   		public synchronized List get() {
  267   			List result = new ArrayList(fBuffer);
  268   			Collections.reverse(result);
  269   			return result;
  270   		}
  271   		
  272   		/**
  273   		 * Return previous item in the history buffer.
  274   		 *
  275   		 * @return Empty string, if the buffer is empty.
  276   		 */
  277   		public synchronized String getPrevious() {
  278   			if (fPointer == kInvalid) return "";
  279   			fPointer--; // calculate previous index
  280   			
  281   			// ring behaviour
  282   			if (fPointer == kInvalid) fPointer = fBuffer.size() - 1;
  283   			
  284   			return (String) fBuffer.get(fPointer);
  285   		}
  286   		
  287   		/**
  288   		 * Return next item in the history buffer.
  289   		 *
  290   		 * @return Empty string, if the buffer is empty.
  291   		 */
  292   		public synchronized String getNext() {
  293   			if (fPointer == kInvalid) return "";
  294   			fPointer++; // calculate next index
  295   			
  296   			// ring behaviour
  297   			if (fPointer == fBuffer.size()) fPointer = 0;
  298   			
  299   			return (String) fBuffer.get(fPointer);
  300   		}
  301   	}
  302   	
  303   	/** Self test method. */
  304   	public static void main(String[] args) {
  305   		try {
  306   			DatabaseRegistry reg = new DatabaseRegistry();
  307   			Set dicts = reg.getDictionaries();
  308   			Iterator i = dicts.iterator();
  309   			DatabaseRegistry.DictInfo info = null;
  310   			while (i.hasNext()) {
  311   				DatabaseRegistry.DictInfo inf = reg.getDictInfo(i.next().toString());
  312   				System.out.println(inf);
  313   				info = inf;
  314   			}
  315   			Database database = new Database(info);
  316   			System.out.println(database.getChapterID("????"));
  317   			System.out.println(database.getChapterID("alpha"));
  318   			System.out.println(database.getChapter("stone").findSimilar("stone"));
  319   			System.out.println(database.getChapter("sys").findAutoComplete("sys"));
  320   		}
  321   		catch (Exception e) {
  322   			e.printStackTrace();
  323   		}
  324   	}
  325   }

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