Skip to content

Commit 9eaa2b9

Browse files
committed
fix NPE
1 parent 1d750a9 commit 9eaa2b9

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

src/main/java/org/wltea/analyzer/dic/Dictionary.java

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import java.util.concurrent.ScheduledExecutorService;
4444
import java.util.concurrent.TimeUnit;
4545

46+
import org.apache.http.Header;
47+
import org.apache.http.HttpEntity;
4648
import org.apache.http.client.ClientProtocolException;
4749
import org.apache.http.client.config.RequestConfig;
4850
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -69,14 +71,8 @@ public class Dictionary {
6971

7072
private DictSegment _MainDict;
7173

72-
private DictSegment _SurnameDict;
73-
7474
private DictSegment _QuantifierDict;
7575

76-
private DictSegment _SuffixDict;
77-
78-
private DictSegment _PrepDict;
79-
8076
private DictSegment _StopWords;
8177

8278
/**
@@ -88,12 +84,12 @@ public class Dictionary {
8884

8985
private static ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
9086

91-
public static final String PATH_DIC_MAIN = "main.dic";
92-
public static final String PATH_DIC_SURNAME = "surname.dic";
93-
public static final String PATH_DIC_QUANTIFIER = "quantifier.dic";
94-
public static final String PATH_DIC_SUFFIX = "suffix.dic";
95-
public static final String PATH_DIC_PREP = "preposition.dic";
96-
public static final String PATH_DIC_STOP = "stopword.dic";
87+
private static final String PATH_DIC_MAIN = "main.dic";
88+
private static final String PATH_DIC_SURNAME = "surname.dic";
89+
private static final String PATH_DIC_QUANTIFIER = "quantifier.dic";
90+
private static final String PATH_DIC_SUFFIX = "suffix.dic";
91+
private static final String PATH_DIC_PREP = "preposition.dic";
92+
private static final String PATH_DIC_STOP = "stopword.dic";
9793

9894
private final static String FILE_NAME = "IKAnalyzer.cfg.xml";
9995
private final static String EXT_DICT = "ext_dict";
@@ -128,15 +124,13 @@ private Dictionary(Configuration cfg) {
128124
if (input != null) {
129125
try {
130126
props.loadFromXML(input);
131-
} catch (InvalidPropertiesFormatException e) {
132-
logger.error("ik-analyzer", e);
133127
} catch (IOException e) {
134128
logger.error("ik-analyzer", e);
135129
}
136130
}
137131
}
138132

139-
public String getProperty(String key){
133+
private String getProperty(String key){
140134
if(props!=null){
141135
return props.getProperty(key);
142136
}
@@ -148,7 +142,7 @@ public String getProperty(String key){
148142
*
149143
* @return Dictionary
150144
*/
151-
public static synchronized Dictionary initial(Configuration cfg) {
145+
public static synchronized void initial(Configuration cfg) {
152146
if (singleton == null) {
153147
synchronized (Dictionary.class) {
154148
if (singleton == null) {
@@ -172,14 +166,12 @@ public static synchronized Dictionary initial(Configuration cfg) {
172166
}
173167
}
174168

175-
return singleton;
176169
}
177170
}
178171
}
179-
return singleton;
180172
}
181173

182-
private List<String> walkFileTree(List<String> files, Path path) {
174+
private void walkFileTree(List<String> files, Path path) {
183175
if (Files.isRegularFile(path)) {
184176
files.add(path.toString());
185177
} else if (Files.isDirectory(path)) try {
@@ -200,7 +192,6 @@ public FileVisitResult visitFileFailed(Path file, IOException e) {
200192
} else {
201193
logger.warn("[Ext Loading] file not found: " + path);
202194
}
203-
return files;
204195
}
205196

206197
private void loadDictFile(DictSegment dict, Path file, boolean critical, String name) {
@@ -225,7 +216,7 @@ private void loadDictFile(DictSegment dict, Path file, boolean critical, String
225216
}
226217
}
227218

228-
public List<String> getExtDictionarys() {
219+
private List<String> getExtDictionarys() {
229220
List<String> extDictFiles = new ArrayList<String>(2);
230221
String extDictCfg = getProperty(EXT_DICT);
231222
if (extDictCfg != null) {
@@ -242,7 +233,7 @@ public List<String> getExtDictionarys() {
242233
return extDictFiles;
243234
}
244235

245-
public List<String> getRemoteExtDictionarys() {
236+
private List<String> getRemoteExtDictionarys() {
246237
List<String> remoteExtDictFiles = new ArrayList<String>(2);
247238
String remoteExtDictCfg = getProperty(REMOTE_EXT_DICT);
248239
if (remoteExtDictCfg != null) {
@@ -258,7 +249,7 @@ public List<String> getRemoteExtDictionarys() {
258249
return remoteExtDictFiles;
259250
}
260251

261-
public List<String> getExtStopWordDictionarys() {
252+
private List<String> getExtStopWordDictionarys() {
262253
List<String> extStopWordDictFiles = new ArrayList<String>(2);
263254
String extStopWordDictCfg = getProperty(EXT_STOP);
264255
if (extStopWordDictCfg != null) {
@@ -275,7 +266,7 @@ public List<String> getExtStopWordDictionarys() {
275266
return extStopWordDictFiles;
276267
}
277268

278-
public List<String> getRemoteExtStopWordDictionarys() {
269+
private List<String> getRemoteExtStopWordDictionarys() {
279270
List<String> remoteExtStopWordDictFiles = new ArrayList<String>(2);
280271
String remoteExtStopWordDictCfg = getProperty(REMOTE_EXT_STOP);
281272
if (remoteExtStopWordDictCfg != null) {
@@ -291,7 +282,7 @@ public List<String> getRemoteExtStopWordDictionarys() {
291282
return remoteExtStopWordDictFiles;
292283
}
293284

294-
public String getDictRoot() {
285+
private String getDictRoot() {
295286
return conf_dir.toAbsolutePath().toString();
296287
}
297288

@@ -468,25 +459,30 @@ private static List<String> getRemoteWordsUnprivileged(String location) {
468459

469460
String charset = "UTF-8";
470461
// 获取编码,默认为utf-8
471-
if (response.getEntity().getContentType().getValue().contains("charset=")) {
472-
String contentType = response.getEntity().getContentType().getValue();
473-
charset = contentType.substring(contentType.lastIndexOf("=") + 1);
474-
}
475-
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
476-
String line;
477-
while ((line = in.readLine()) != null) {
478-
buffer.add(line);
479-
}
480-
in.close();
481-
response.close();
482-
return buffer;
462+
HttpEntity entity = response.getEntity();
463+
if(entity!=null){
464+
Header contentType = entity.getContentType();
465+
if(contentType!=null&&contentType.getValue()!=null){
466+
String typeValue = contentType.getValue();
467+
if(typeValue!=null&&typeValue.contains("charset=")){
468+
charset = typeValue.substring(typeValue.lastIndexOf("=") + 1);
469+
}
470+
}
471+
472+
if (entity.getContentLength() > 0) {
473+
in = new BufferedReader(new InputStreamReader(entity.getContent(), charset));
474+
String line;
475+
while ((line = in.readLine()) != null) {
476+
buffer.add(line);
477+
}
478+
in.close();
479+
response.close();
480+
return buffer;
481+
}
482+
}
483483
}
484484
response.close();
485-
} catch (ClientProtocolException e) {
486-
logger.error("getRemoteWords {} error", e, location);
487-
} catch (IllegalStateException e) {
488-
logger.error("getRemoteWords {} error", e, location);
489-
} catch (IOException e) {
485+
} catch (IllegalStateException | IOException e) {
490486
logger.error("getRemoteWords {} error", e, location);
491487
}
492488
return buffer;
@@ -548,24 +544,24 @@ private void loadQuantifierDict() {
548544
}
549545

550546
private void loadSurnameDict() {
551-
_SurnameDict = new DictSegment((char) 0);
547+
DictSegment _SurnameDict = new DictSegment((char) 0);
552548
Path file = PathUtils.get(getDictRoot(), Dictionary.PATH_DIC_SURNAME);
553549
loadDictFile(_SurnameDict, file, true, "Surname");
554550
}
555551

556552
private void loadSuffixDict() {
557-
_SuffixDict = new DictSegment((char) 0);
553+
DictSegment _SuffixDict = new DictSegment((char) 0);
558554
Path file = PathUtils.get(getDictRoot(), Dictionary.PATH_DIC_SUFFIX);
559555
loadDictFile(_SuffixDict, file, true, "Suffix");
560556
}
561557

562558
private void loadPrepDict() {
563-
_PrepDict = new DictSegment((char) 0);
559+
DictSegment _PrepDict = new DictSegment((char) 0);
564560
Path file = PathUtils.get(getDictRoot(), Dictionary.PATH_DIC_PREP);
565561
loadDictFile(_PrepDict, file, true, "Preposition");
566562
}
567563

568-
public void reLoadMainDict() {
564+
void reLoadMainDict() {
569565
logger.info("重新加载词典...");
570566
// 新开一个实例加载词典,减少加载过程对当前词典使用的影响
571567
Dictionary tmpDict = new Dictionary(configuration);

0 commit comments

Comments
 (0)