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 access to single chapter.
   27    *
   28    * @author Kaloian Doganov <kaloian@europe.com>
   29    */
   30   
   31   public final class Chapter {
   32   	
   33   	private String fID = null;
   34   	private List fMap = null;
   35   	private Locale fLocale = null;
   36   	
   37   	/**
   38   	 * Create chapter from specific location. If data can not be read
   39   	 * from specified location, empty chapter is created.
   40   	 */
   41   	Chapter(String aID, URL aLocation, Locale aLocale) {
   42   		fID = aID;
   43   		fLocale = aLocale;
   44   		
   45   		try {
   46   			char[] buffer = new char[10240];
   47   			int count = 0;
   48   			StringBuffer content = new StringBuffer();
   49   			InputStream in = null;
   50   			InputStreamReader reader = null;
   51   			in = aLocation.openStream();
   52   			reader = new InputStreamReader(in, "Cp1251");
   53   			count = reader.read(buffer);
   54   			while (count != -1) {
   55   				content.append(buffer, 0, count);
   56   				count = reader.read(buffer);
   57   			}
   58   			StringTokenizer tokenizer = new StringTokenizer(content.toString(),
   59   				"\u0000");
   60   			int size = tokenizer.countTokens() / 2;
   61   			fMap = new ArrayList(size);
   62   			for (int i = 0; i < size; i++) {
   63   				String key = tokenizer.nextToken();
   64   				String value = tokenizer.nextToken();
   65   				fMap.add(new Entry(key, value));
   66   			}
   67   		}
   68   		catch (IOException e) {
   69   			// Can't read from this location. Create empty chapter.
   70   			fMap = new ArrayList();
   71   		}
   72   	}
   73   	
   74   	/** Return chapter ID. */
   75   	public String getID() {
   76   		return fID;
   77   	}
   78   	
   79   	/**
   80   	 * Return matching entry.
   81   	 *
   82   	 * @param aWord Converted (lowercased, without hyphens) key to search.
   83   	 * @see Database#displayToKey
   84   	 */
   85   	public Entry findExact(String aWord) throws ItemNotFoundException {
   86   		for (int i = 0; i < fMap.size(); i++) {
   87   			if ((((Entry) fMap.get(i)).getKey()).equals(aWord))
   88   				return (Entry) fMap.get(i);
   89   		}
   90   		throw new ItemNotFoundException(aWord);
   91   	}
   92   	
   93   	/**
   94   	 * Return matching entry.
   95   	 *
   96   	 * @param aWord Converted (lowercased, without hyphens) key to search.
   97   	 * @see Database#displayToKey
   98   	 */
   99   	public Entry findAutoComplete(String aWord) throws ItemNotFoundException {
  100   		for (int i = 0; i < fMap.size(); i++) {
  101   			if ((((Entry) fMap.get(i)).getKey()).startsWith(aWord))
  102   				return (Entry) fMap.get(i);
  103   		}
  104   		throw new ItemNotFoundException(aWord);
  105   	}
  106   	
  107   	/**
  108   	 * Return list of similar entries.
  109   	 *
  110   	 * @param aWord Converted (lowercased, without hyphens) key to search.
  111   	 * @see Database#displayToKey
  112   	 */
  113   	public List findSimilar(String aWord) {
  114   		List list = new ArrayList();
  115   		for (int i = aWord.length(); i > 0; i--) {
  116   			findStartingWith(list, aWord.substring(0, i));
  117   			if (!list.isEmpty()) break;
  118   		}
  119   		return list;
  120   	}
  121   	
  122   	/**
  123   	 * Return list of entries, starting with specific string.
  124   	 *
  125   	 * @param aList Existing list to get the result. 
  126   	 * @param aWord Converted (lowercased, without hyphens) key to search.
  127   	 * @see Database#displayToKey
  128   	 */
  129   	private void findStartingWith(List aList, String aWord) {
  130   		for (int i = 0; i < fMap.size(); i++) {
  131   			if ((((Entry) fMap.get(i)).getKey()).startsWith(aWord))
  132   				aList.add(fMap.get(i));
  133   		}
  134   	}
  135   	
  136   	/**
  137   	 * Return index of first matching entry, starting with specific string.
  138   	 *
  139   	 * @param aWord Converted (lowercased, without hyphens) key to search.
  140   	 * @see Database#displayToKey
  141   	 */
  142   	private int findStartingWith(String aWord) throws ItemNotFoundException {
  143   		for (int i = 0; i < fMap.size(); i++) {
  144   			if ((((Entry) fMap.get(i)).getKey()).startsWith(aWord))
  145   				return i;
  146   		}
  147   		throw new ItemNotFoundException(aWord);
  148   	}
  149   	
  150   	/** Test whether aString1 starts with String2, ignoring the case. *//*
  151   	private boolean startsWithIgnoreCase(String aString1, String aString2) {
  152   		int l1 = aString1.length();
  153   		int l2 = aString2.length();
  154   		if (l2 > l1) return false;
  155   		String s1 = aString1.toLowerCase(fLocale);
  156   		String s2 = aString2.toLowerCase(fLocale);
  157   		return s1.startsWith(s2);
  158   	}*/
  159   
  160   	/** Test whether aString1 is equal with String2, ignoring the case. *//*
  161   	private boolean equalsIgnoreCase(String aString1, String aString2) {
  162   		int l1 = aString1.length();
  163   		int l2 = aString2.length();
  164   		if (l2 != l1) return false;
  165   		String s1 = aString1.toLowerCase(fLocale);
  166   		String s2 = aString2.toLowerCase(fLocale);
  167   		return s1.equals(s2);
  168   	}*/
  169   	
  170   	/**
  171   	 * Return integer position of this word, or near value.
  172   	 *
  173   	 * @param aWord Converted (lowercased, without hyphens) key to search.
  174   	 * @see Database#displayToKey
  175   	 */
  176   	private int getPosition(String aWord) {
  177   		boolean success = false;
  178   		int pos = 0;
  179   		for (int i = aWord.length(); i > 0; i--) {
  180   			try {
  181   				pos = findStartingWith(aWord.substring(0, i));
  182   				success = true;
  183   				break;
  184   			}
  185   			catch (ItemNotFoundException e) {
  186   				success = false;
  187   			}
  188   		}
  189   		if (!success) return fMap.size() - 1; // return last index
  190   		Object closest = null;
  191   		return pos;
  192   	}
  193   	
  194   	/**
  195   	 * Return previous chapter item.
  196   	 *
  197   	 * @param aWord Converted (lowercased, without hyphens) key to search.
  198   	 * @return Empty entry if there is no any items in this chapter.
  199   	 * @see Database#displayToKey
  200   	 */
  201   	public Entry getPrevious(String aWord) {
  202   		if (fMap.isEmpty()) return Entry.createEmpty();
  203   		int pos = getPosition(aWord);
  204   		if (pos == 0)
  205   			return (Entry) fMap.get(0);
  206   		return (Entry) fMap.get(pos - 1);
  207   	}
  208   	
  209   	/**
  210   	 * Return next chapter item.
  211   	 *
  212   	 * @param aWord Converted (lowercased, without hyphens) key to search.
  213   	 * @return Empty entry if there is no any items in this chapter or
  214   	 *         if current item is the last one.
  215   	 * @see Database#displayToKey
  216   	 */
  217   	public Entry getNext(String aWord) {
  218   		if (fMap.isEmpty()) return Entry.createEmpty();
  219   		int pos = getPosition(aWord);
  220   		int last = fMap.size() - 1;
  221   		if (pos == last)
  222   			return Entry.createEmpty();
  223   		return (Entry) fMap.get(pos + 1);
  224   	}
  225   	
  226   	/**
  227   	 * Return next 20 chapter items.
  228   	 *
  229   	 * @param aWord Converted (lowercased, without hyphens) key to search.
  230   	 * @return List with the same entry if there is no more entries in
  231   	 *         this chapter; empty list if the chapter is empty.
  232   	 * @see Database#displayToKey
  233   	 */
  234   	public List getNext20(String aWord) {
  235   		if (fMap.isEmpty())
  236   			return new ArrayList();
  237   		int pos = getPosition(aWord);
  238   		int last = pos + 20;
  239   		if (last > fMap.size()) last = fMap.size();
  240   		return fMap.subList(pos, last);
  241   	}
  242   	
  243   	/**
  244   	 * Indicates whether some other <code>Chapter</code> is "equal to" this one.
  245   	 * Chapters are compared by ID.
  246   	 *
  247   	 * @param    aObject Another chapter to compare.
  248   	 * @return   <code>true</code> if the two chapters have same IDs;
  249   	 *           <code>false</code> otherwise.
  250   	 */
  251   	public boolean equals(Object aObject) {
  252   		// Chapter instance?
  253   		if (aObject instanceof Chapter) {
  254   			Chapter other = (Chapter) aObject;
  255   			return fID.equals(other.getID());
  256   		}
  257   		return super.equals(aObject);
  258   	}
  259   	
  260   	/** Return string representation of this chapter. */
  261   	public String toString() {
  262   		StringBuffer result = new StringBuffer();
  263   		result.append("[Chapter id=");
  264   		result.append(fID);
  265   		result.append("]");
  266   		return result.toString();
  267   	}
  268   	
  269   	/** Map entry to keep single dictionary item. */
  270   	public static final class Entry {
  271   		
  272   		private String fKey;
  273   		private String fKeyDisplay;
  274   		private String fValue;
  275   		
  276   		public Entry(String aKey, String aValue) {
  277   			fKey = Database.displayToKey(aKey);
  278   			fKeyDisplay = aKey;
  279   			fValue = aValue;
  280   		}
  281   		
  282   		/** Create empty entry object. */
  283   		public static Entry createEmpty() {
  284   			return new Entry("", "");
  285   		}
  286   		
  287   		public String getKey() {
  288   			return fKey;
  289   		}
  290   		
  291   		public String getKeyDisplay() {
  292   			return fKeyDisplay;
  293   		}
  294   		
  295   		public String getValue() {
  296   			return fValue;
  297   		}
  298   		
  299   		/** Test wheter this entry is empty. */
  300   		public boolean isEmpty() {
  301   			return fKey.equals("");
  302   		}
  303   	
  304   		/** Return string representation of this entry. */
  305   		public String toString() {
  306   			StringBuffer result = new StringBuffer();
  307   			result.append("[Chapter.Entry key=");
  308   			if (fKey != null)
  309   				result.append(fKey);
  310   			result.append("]");
  311   			return result.toString();
  312   		}
  313   	}
  314   	
  315   	/** Self test method. */
  316   	public static void main(String[] args) {
  317   		try {
  318   			DatabaseRegistry reg = new DatabaseRegistry();
  319   			Set dicts = reg.getDictionaries();
  320   			Iterator i = dicts.iterator();
  321   			DatabaseRegistry.DictInfo info = null;
  322   			while (i.hasNext()) {
  323   				DatabaseRegistry.DictInfo inf = reg.getDictInfo(i.next().toString());
  324   				System.out.println(inf.toString());
  325   				info = inf;
  326   			}
  327   			Chapter chapter = new Chapter("41",
  328   				new URL(info.getRoot() + "41.DAT"),
  329   				Database.kLocaleBG);
  330   			System.out.println(chapter.findSimilar("allanisse"));
  331   			System.out.println(chapter.findAutoComplete("alla").toString());
  332   			System.out.println(chapter.getNext20("allah").toString());
  333   		}
  334   		catch (Exception e) {
  335   			e.printStackTrace();
  336   		}
  337   	}
  338   }

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