Jskad/source/org/thdl/tib/scanner/RemoteTibetanScanner.java

161 lines
3.9 KiB
Java
Raw Normal View History

2002-10-03 19:28:09 +00:00
/*
The contents of this file are subject to the AMP Open Community License
Version 1.0 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License on the AMP web site
(http://www.tibet.iteso.mx/Guatemala/).
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific terms governing rights and limitations under the
License.
The Initial Developer of this software is Andres Montano Pellegrini. Portions
created by Andres Montano Pellegrini are Copyright 2001 Andres Montano
Pellegrini. All Rights Reserved.
Contributor(s): ______________________________________.
*/
package org.thdl.tib.scanner;
import org.thdl.util.*;
2002-10-03 19:28:09 +00:00
import java.net.*;
import java.io.*;
/** Used by applets and applications to access remote on-line dictionaries.
Sends Tibetan text, and receives the words with their definitions. The
{@link RemoteScannerFilter} receives the requests on the server.
@author Andrés Montano Pellegrini
@see RemoteScannerFilter
*/
2002-10-03 19:28:09 +00:00
public class RemoteTibetanScanner implements TibetanScanner
{
private String url;
private SimplifiedLinkedList wordList;
2002-10-03 19:28:09 +00:00
private DictionarySource defSourcesWanted;
public RemoteTibetanScanner(String url) throws Exception
{
defSourcesWanted = DictionarySource.getAllDictionaries();
this.url = url;
wordList = new SimplifiedLinkedList();
2002-10-03 19:28:09 +00:00
}
/** dont use */
public void scanLine(String linea)
{
scanBody(linea);
}
public void scanBody(String linea)
{
try
{
URLConnection uC = new URL(url + "?dicts=" + Integer.toString(defSourcesWanted.getDicts())).openConnection();
// URLConnection uC = new URL(url).openConnection();
uC.setDoInput(true);
uC.setDoOutput(true);
uC.setUseCaches(false);
uC.setRequestProperty("Content-type", "application/octet-stream");
OutputStream os = uC.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.println(linea);
pw.close();
BufferedReader in = new BufferedReader(new InputStreamReader(uC.getInputStream()));
String word, eachDef, def;
outside:
while((word = in.readLine())!=null && (def = in.readLine())!=null)
{
do
{
eachDef = in.readLine();
if (eachDef==null)
{
wordList.addLast(new Word(word, def));
break outside;
}
if (eachDef.equals("")) break;
def+='\n' + eachDef;
}
while(true);
wordList.addLast(new Word(word, def));
}
in.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
public SimplifiedLinkedList getTokenLinkedList()
2002-10-03 19:28:09 +00:00
{
return wordList;
}
public void clearTokens()
{
wordList = new SimplifiedLinkedList();
2002-10-03 19:28:09 +00:00
}
public Token[] getTokenArray()
{
int i=0;
Token token[] = new Token[wordList.size()];
SimplifiedListIterator li = wordList.listIterator();
2002-10-03 19:28:09 +00:00
while(li.hasNext())
token[i++] = (Token)li.next();
return token;
}
/** does not do anything */
public void finishUp()
{
}
public DictionarySource getDictionarySource()
{
return defSourcesWanted;
}
public String[] getDictionaryDescriptions()
{
int n;
try
{
URLConnection uC = new URL(url+"?dicts=names").openConnection();
uC.setDoInput(true);
uC.setDoOutput(false);
uC.setUseCaches(false);
uC.setRequestProperty("Content-type", "text/plain");
BufferedReader br = new BufferedReader(new InputStreamReader(uC.getInputStream()));
SimplifiedLinkedList ll1 = new SimplifiedLinkedList(), ll2 = new SimplifiedLinkedList();
2002-10-03 19:28:09 +00:00
String s;
while ((s=br.readLine())!=null)
{
n = s.indexOf(",");
if (n < 0)
{
ll1.addLast(null);
ll2.addLast(s);
}
else
{
ll1.addLast(s.substring(0,n).trim());
ll2.addLast(s.substring(n+1).trim());
}
}
br.close();
Definitions.defTags = ll2.toStringArray();
return ll1.toStringArray();
}
catch (Exception e)
{
return null;
}
}
}