📄 dictserviceimpl.java
字号:
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Created on 2005-12-30
* author 谢骋超
*
*/
package cn.edu.zju.dartsplitter.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Set;
import org.apache.log4j.Logger;
import cn.edu.zju.dartsplitter.DictService;
import cn.edu.zju.dartsplitter.DictTree;
import cn.edu.zju.dartsplitter.data.DictNode;
import cn.edu.zju.dartsplitter.exceptions.DictSerializeException;
/**
* @author xiecc
* @email xieccy@gmail.com xieccy@yahoo.com
* homepage: http://blog.itpub.net/xiecc
* projectpage: http://ccnt.zju.edu.cn/projects
*/
public class DictServiceImpl implements DictService {
/**
* Logger for this class
*/
private static final Logger logger = Logger
.getLogger(DictServiceImpl.class);
private DictTree dictTree;
private String dictDir;
/**
* @return Returns the dictDir.
*/
public String getDictDir() {
return dictDir;
}
/**
* @param dictDir The dictDir to set.
*/
public void setDictDir(String dictDir) {
this.dictDir = dictDir;
}
/**
* @return Returns the dictTree.
*/
public DictTree getDictTree() {
return dictTree;
}
/**
* @param dictTree
* The dictTree to set.
*/
public void setDictTree(DictTree dictTree) {
this.dictTree = dictTree;
}
private String getSerFileName(String strPrefix){
String dirName=getDictDir()+"/"+strPrefix.hashCode()/100;
File dir=new File(dirName);
if (!dir.exists()){
dir.mkdir();
}
String fileName=dirName+"/"+strPrefix+".ser";
return fileName;
}
public DictNode saveNode(String strPrefix) {
DictNode dictNode = getDictTree().buildSubNodes(strPrefix);
String fileName = getSerFileName(strPrefix);
try {
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(dictNode);
fos.close();
} catch (FileNotFoundException e) {
throw new DictSerializeException("file not find: " + fileName, e);
} catch (IOException e) {
throw new DictSerializeException(
"IOException on write serialize file: " + fileName, e);
}
return dictNode;
}
public DictNode readNode(String strPrefix) {
String fileName = getSerFileName(strPrefix);
try {
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
DictNode dictNode = (DictNode) ois.readObject();
return dictNode;
} catch (FileNotFoundException e) {
// logger.debug("file not find on deserliaze: " + fileName);
return null;
} catch (IOException e) {
throw new DictSerializeException(
"IOException on write serialize file: " + fileName, e);
} catch (ClassNotFoundException e) {
throw new DictSerializeException(
"class not found on write serialize: " + DictNode.class, e);
}
}
public void saveAllNodeToFile() {
Set<String> prefixSet=dictTree.getAllPrefixes();
for (String strPrefix:prefixSet){
if (isValidPrefix(strPrefix)){
saveNode(strPrefix);
}
}
}
private boolean isValidPrefix(String strPrefix) {
if (null==strPrefix){
return false;
}
char[] prefixChar=strPrefix.toCharArray();
if (prefixChar.length!=1){
logger.debug("wrong prefix, lengh!=1: "+strPrefix);
return false;
}
if (Character.isLetter(prefixChar[0]) || Character.isDigit(prefixChar[0])){
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -