|
@@ -16,12 +16,14 @@
|
|
|
|
|
|
package org.codelibs.fess.dict.kuromoji;
|
|
package org.codelibs.fess.dict.kuromoji;
|
|
|
|
|
|
|
|
+import java.io.BufferedInputStream;
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.BufferedWriter;
|
|
import java.io.Closeable;
|
|
import java.io.Closeable;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStreamWriter;
|
|
import java.io.OutputStreamWriter;
|
|
import java.io.Writer;
|
|
import java.io.Writer;
|
|
@@ -60,7 +62,7 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
|
|
@Override
|
|
@Override
|
|
public OptionalEntity<KuromojiItem> get(final long id) {
|
|
public OptionalEntity<KuromojiItem> get(final long id) {
|
|
if (kuromojiItemList == null) {
|
|
if (kuromojiItemList == null) {
|
|
- reload(null);
|
|
|
|
|
|
+ reload(null, null);
|
|
}
|
|
}
|
|
|
|
|
|
for (final KuromojiItem kuromojiItem : kuromojiItemList) {
|
|
for (final KuromojiItem kuromojiItem : kuromojiItemList) {
|
|
@@ -74,7 +76,7 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
|
|
@Override
|
|
@Override
|
|
public synchronized PagingList<KuromojiItem> selectList(final int offset, final int size) {
|
|
public synchronized PagingList<KuromojiItem> selectList(final int offset, final int size) {
|
|
if (kuromojiItemList == null) {
|
|
if (kuromojiItemList == null) {
|
|
- reload(null);
|
|
|
|
|
|
+ reload(null, null);
|
|
}
|
|
}
|
|
|
|
|
|
if (offset >= kuromojiItemList.size() || offset < 0) {
|
|
if (offset >= kuromojiItemList.size() || offset < 0) {
|
|
@@ -92,14 +94,14 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
|
|
@Override
|
|
@Override
|
|
public synchronized void insert(final KuromojiItem item) {
|
|
public synchronized void insert(final KuromojiItem item) {
|
|
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
|
|
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
|
|
- reload(updater);
|
|
|
|
|
|
+ reload(updater, null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public synchronized void update(final KuromojiItem item) {
|
|
public synchronized void update(final KuromojiItem item) {
|
|
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
|
|
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
|
|
- reload(updater);
|
|
|
|
|
|
+ reload(updater, null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -108,15 +110,14 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
|
|
final KuromojiItem kuromojiItem = item;
|
|
final KuromojiItem kuromojiItem = item;
|
|
kuromojiItem.setNewToken(StringUtil.EMPTY);
|
|
kuromojiItem.setNewToken(StringUtil.EMPTY);
|
|
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
|
|
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
|
|
- reload(updater);
|
|
|
|
|
|
+ reload(updater, null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- protected void reload(final KuromojiUpdater updater) {
|
|
|
|
|
|
+ protected void reload(final KuromojiUpdater updater, InputStream in) {
|
|
final List<KuromojiItem> itemList = new ArrayList<KuromojiItem>();
|
|
final List<KuromojiItem> itemList = new ArrayList<KuromojiItem>();
|
|
- BufferedReader reader = null;
|
|
|
|
- try {
|
|
|
|
- reader = new BufferedReader(new InputStreamReader(dictionaryManager.getContentInputStream(this), Constants.UTF_8));
|
|
|
|
|
|
+ try (BufferedReader reader =
|
|
|
|
+ new BufferedReader(new InputStreamReader(in != null ? in : dictionaryManager.getContentInputStream(this), Constants.UTF_8))) {
|
|
long id = 0;
|
|
long id = 0;
|
|
String line = null;
|
|
String line = null;
|
|
while ((line = reader.readLine()) != null) {
|
|
while ((line = reader.readLine()) != null) {
|
|
@@ -171,8 +172,6 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
|
|
kuromojiItemList = itemList;
|
|
kuromojiItemList = itemList;
|
|
} catch (final IOException e) {
|
|
} catch (final IOException e) {
|
|
throw new DictionaryException("Failed to parse " + path, e);
|
|
throw new DictionaryException("Failed to parse " + path, e);
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(reader);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -180,15 +179,15 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
|
|
return new File(path).getName();
|
|
return new File(path).getName();
|
|
}
|
|
}
|
|
|
|
|
|
- // TODO
|
|
|
|
- // public InputStream getInputStream() throws IOException {
|
|
|
|
- // return new BufferedInputStream(new FileInputStream(file));
|
|
|
|
- // }
|
|
|
|
- //
|
|
|
|
- // public void update(final InputStream in) throws IOException {
|
|
|
|
- // CopyUtil.copy(in, file);
|
|
|
|
- // reload(null);
|
|
|
|
- // }
|
|
|
|
|
|
+ public InputStream getInputStream() throws IOException {
|
|
|
|
+ return new BufferedInputStream(dictionaryManager.getContentInputStream(this));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void update(final InputStream in) throws IOException {
|
|
|
|
+ try (KuromojiUpdater updater = new KuromojiUpdater(null)) {
|
|
|
|
+ reload(updater, in);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public String toString() {
|
|
public String toString() {
|
|
@@ -220,7 +219,7 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
|
|
|
|
|
|
public KuromojiItem write(final KuromojiItem oldItem) {
|
|
public KuromojiItem write(final KuromojiItem oldItem) {
|
|
try {
|
|
try {
|
|
- if (item.getId() == oldItem.getId() && item.isUpdated()) {
|
|
|
|
|
|
+ if (item != null && item.getId() == oldItem.getId() && item.isUpdated()) {
|
|
if (item.equals(oldItem)) {
|
|
if (item.equals(oldItem)) {
|
|
try {
|
|
try {
|
|
if (!item.isDeleted()) {
|
|
if (!item.isDeleted()) {
|
|
@@ -259,7 +258,7 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
|
|
|
|
|
|
public KuromojiItem commit() {
|
|
public KuromojiItem commit() {
|
|
isCommit = true;
|
|
isCommit = true;
|
|
- if (item.isUpdated()) {
|
|
|
|
|
|
+ if (item != null && item.isUpdated()) {
|
|
try {
|
|
try {
|
|
writer.write(item.toLineString());
|
|
writer.write(item.toLineString());
|
|
writer.write(Constants.LINE_SEPARATOR);
|
|
writer.write(Constants.LINE_SEPARATOR);
|