001
014
015 package com.liferay.portal.kernel.search;
016
017 import com.liferay.portal.kernel.util.StringPool;
018 import com.liferay.portal.kernel.util.Validator;
019
020 import java.io.BufferedReader;
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.io.InputStreamReader;
024 import java.io.UnsupportedEncodingException;
025
026 import java.util.Iterator;
027
028
031 public class DictionaryReader {
032
033 public DictionaryReader(InputStream inputStream)
034 throws UnsupportedEncodingException {
035
036 this(inputStream, StringPool.UTF8);
037 }
038
039 public DictionaryReader(InputStream inputStream, String encoding)
040 throws UnsupportedEncodingException {
041
042 _encoding = encoding;
043
044 _bufferedReader = new BufferedReader(
045 new InputStreamReader(inputStream, encoding));
046 }
047
048 public Iterator<DictionaryEntry> getDictionaryEntriesIterator() {
049 return new DictionaryIterator();
050 }
051
052 private static final int _UNICODE_BYTE_ORDER_MARK = 65279;
053
054 private BufferedReader _bufferedReader;
055 private final String _encoding;
056
057 private class DictionaryIterator implements Iterator<DictionaryEntry> {
058
059 @Override
060 public DictionaryEntry next() {
061 if (!_calledHasNext) {
062 hasNext();
063 }
064
065 _calledHasNext = false;
066
067 if (StringPool.UTF8.equals(_encoding) &&
068 (_line.charAt(0) == _UNICODE_BYTE_ORDER_MARK)) {
069
070 _line = _line.substring(1);
071 }
072
073 return new DictionaryEntry(_line);
074 }
075
076 @Override
077 public boolean hasNext() {
078 if (!_calledHasNext) {
079 try {
080 _line = _bufferedReader.readLine();
081
082 _calledHasNext = true;
083 }
084 catch (IOException ioe) {
085 throw new IllegalStateException(ioe);
086 }
087 }
088
089 if (Validator.isNotNull(_line)) {
090 return true;
091 }
092
093 return false;
094 }
095
096 @Override
097 public void remove() {
098 throw new UnsupportedOperationException();
099 }
100
101 private boolean _calledHasNext;
102 private String _line;
103
104 }
105
106 }