001 /** 002 * Java WordNetBridge Library (JWNL) 003 * See the documentation for copyright information. 004 */ 005 package jcolibri.extensions.textual.wordnet; 006 007 import net.didion.jwnl.JWNLRuntimeException; 008 import net.didion.jwnl.data.POS; 009 import net.didion.jwnl.dictionary.file.DictionaryFile; 010 import net.didion.jwnl.dictionary.file.DictionaryFileType; 011 import net.didion.jwnl.dictionary.file.ObjectDictionaryFile; 012 import net.didion.jwnl.princeton.file.*; 013 014 import java.io.File; 015 import java.io.FileOutputStream; 016 import java.io.IOException; 017 import java.io.ObjectInputStream; 018 import java.io.ObjectOutputStream; 019 020 /** <code>ObjectDictionaryFile</code> that accesses files names with the Princeton dictionary file naming convention. */ 021 public class JColibriPrincetonObjectDictionaryFile extends AbstractPrincetonDictionaryFile implements ObjectDictionaryFile { 022 private File _file = null; 023 private ObjectInputStream _in = null; 024 private ObjectOutputStream _out = null; 025 026 public JColibriPrincetonObjectDictionaryFile() {} 027 028 public DictionaryFile newInstance(String path, POS pos, DictionaryFileType fileType) { 029 return new JColibriPrincetonObjectDictionaryFile(path, pos, fileType); 030 } 031 032 public JColibriPrincetonObjectDictionaryFile(String path, POS pos, DictionaryFileType fileType) { 033 super(path, pos, fileType); 034 } 035 036 public boolean isOpen() { 037 return (_file != null); 038 } 039 040 public void close() { 041 try { 042 if (canRead()) 043 getInputStream().close(); 044 if (canWrite()) 045 getOutputStream().close(); 046 } catch (Exception ex) { 047 } finally { 048 _in = null; 049 _out = null; 050 _file = null; 051 } 052 } 053 054 /** Open the input and output streams. */ 055 public void openStreams() throws IOException { 056 if (!canWrite()) 057 openOutputStream(); 058 if (!canRead()) 059 openInputStream(); 060 } 061 062 private void openOutputStream() throws IOException { 063 _out = new ObjectOutputStream(new FileOutputStream(_file)); 064 } 065 066 private void openInputStream() throws IOException { 067 _in = new ObjectInputStream(jcolibri.util.FileIO.findFile(_file.getPath()).openStream()); 068 } 069 070 public ObjectInputStream getInputStream() throws IOException { 071 if (!canRead()) openInputStream(); 072 return _in; 073 } 074 075 public ObjectOutputStream getOutputStream() throws IOException { 076 if (!canWrite()) openOutputStream(); 077 return _out; 078 } 079 080 public boolean canRead() { 081 return _in != null; 082 } 083 084 public boolean canWrite() { 085 return _out != null; 086 } 087 088 public Object readObject() throws IOException, ClassNotFoundException { 089 if (isOpen() && canRead()) { 090 return getInputStream().readObject(); 091 } else { 092 throw new JWNLRuntimeException("PRINCETON_EXCEPTION_001"); 093 } 094 } 095 096 public void writeObject(Object obj) throws IOException { 097 if (isOpen() && canWrite()) { 098 getOutputStream().writeObject(obj); 099 } else { 100 throw new JWNLRuntimeException("PRINCETON_EXCEPTION_002"); 101 } 102 } 103 104 /** 105 * Here we try to be intelligent about opening streams. 106 * If the file does not already exist, we assume that we are going 107 * to be creating it and writing to it, otherwise we assume that 108 * we are going to be reading from it. If you want the other stream 109 * open, you must do it explicitly by calling <code>openStreams</code>. 110 */ 111 protected void openFile(File path) throws IOException { 112 _file = path; 113 openInputStream(); 114 } 115 116 }