4747import java .io .InputStream ;
4848import java .io .InputStreamReader ;
4949import java .io .Reader ;
50- import java .io .UnsupportedEncodingException ;
5150import java .util .ArrayList ;
5251import java .util .List ;
5352import java .util .Set ;
8988@ MimeRegistration (mimeType = FileUtils .PHP_MIME_TYPE , service = CompletionProvider .class )
9089public final class FilterAndActionCompletion extends WordPressCompletionProvider {
9190
91+ enum Type {
92+
93+ Filter ,
94+ Action
95+ }
96+
9297 private static final Logger LOGGER = Logger .getLogger (FilterAndActionCompletion .class .getName ());
9398 private static final String CUSTOM_FILTER_CODE_COMPLETION_XML = "nbproject/code-completion-filter.xml" ; // NOI18N
9499 private static final String CUSTOM_ACTION_CODE_COMPLETION_XML = "nbproject/code-completion-action.xml" ; // NOI18N
@@ -97,8 +102,8 @@ public final class FilterAndActionCompletion extends WordPressCompletionProvider
97102 private int argCount ;
98103 private boolean isFilter = false ;
99104 private boolean isAction = false ;
100- private List <WordPressCompletionItem > filterItems ;
101- private List <WordPressCompletionItem > actionItems ;
105+ private final List <WordPressCompletionItem > filterItems = new ArrayList <>() ;
106+ private final List <WordPressCompletionItem > actionItems = new ArrayList <>() ;
102107 private String currentInput ;
103108
104109 public FilterAndActionCompletion () {
@@ -211,7 +216,7 @@ private boolean isAction(String name) {
211216 }
212217
213218 private List <WordPressCompletionItem > getCodeCompletionList (PhpModule phpModule ) {
214- List <WordPressCompletionItem > list = new ArrayList <WordPressCompletionItem >();
219+ List <WordPressCompletionItem > list = new ArrayList <>();
215220 if (argCount == 1 ) {
216221 if (isFilter ) {
217222 list = filterItems ;
@@ -225,7 +230,7 @@ private List<WordPressCompletionItem> getCodeCompletionList(PhpModule phpModule)
225230 }
226231
227232 private List <WordPressCompletionItem > getFunctionsList (PhpModule phpModule ) {
228- List <WordPressCompletionItem > list = new ArrayList <WordPressCompletionItem >();
233+ List <WordPressCompletionItem > list = new ArrayList <>();
229234 FileObject sourceDirectory = phpModule .getSourceDirectory ();
230235 String rootPath = "" ; // NOI18N
231236 if (sourceDirectory != null ) {
@@ -252,47 +257,69 @@ private List<WordPressCompletionItem> getFunctionsList(PhpModule phpModule) {
252257
253258 public void refresh () {
254259 // read file for filter
255- filterItems = new ArrayList < WordPressCompletionItem > ();
256- actionItems = new ArrayList < WordPressCompletionItem > ();
257- FileObject filterXml = null ;
258- FileObject actionXml = null ;
260+ filterItems . clear ();
261+ actionItems . clear ();
262+ FileObject customFilterXml = null ;
263+ FileObject customActionXml = null ;
259264 PhpModule phpModule = PhpModule .Factory .inferPhpModule ();
260- // TODO improve for each locales
261- // String locale = "";
265+
262266 // use custom file
263267 if (phpModule != null ) {
264268 FileObject projectDirectory = phpModule .getProjectDirectory ();
265- filterXml = projectDirectory .getFileObject (CUSTOM_FILTER_CODE_COMPLETION_XML );
266- actionXml = projectDirectory .getFileObject (CUSTOM_ACTION_CODE_COMPLETION_XML );
269+ customFilterXml = projectDirectory .getFileObject (CUSTOM_FILTER_CODE_COMPLETION_XML );
270+ customActionXml = projectDirectory .getFileObject (CUSTOM_ACTION_CODE_COMPLETION_XML );
267271 }
268- // TODO improve loop
269- InputStream filterInputStream = null ;
270- InputStream actionInputStream = null ;
271- if ( filterXml == null ) {
272- filterInputStream = FilterAndActionCompletion . class . getResourceAsStream ( DEFAULT_FILTER_CODE_COMPLETION_XML );
273- } else {
274- try {
275- filterInputStream = filterXml . getInputStream ();
276- } catch ( FileNotFoundException ex ) {
277- LOGGER . log ( Level . WARNING , null , ex );
272+ refresh ( customFilterXml , Type . Filter );
273+ refresh ( customActionXml , Type . Action ) ;
274+ }
275+
276+ private void refresh ( FileObject customXml , Type type ) {
277+ try ( InputStream inputStream = getInputStream ( customXml , type )) {
278+ if ( inputStream != null ) {
279+ try ( Reader reader = new BufferedReader ( new InputStreamReader ( inputStream , Charset . UTF8 ))) {
280+ parse ( reader , type );
281+ }
278282 }
283+ } catch (IOException ex ) {
284+ LOGGER .log (Level .WARNING , null , ex );
279285 }
280- if (actionXml == null ) {
281- actionInputStream = FilterAndActionCompletion .class .getResourceAsStream (DEFAULT_ACTION_CODE_COMPLETION_XML );
286+ }
287+
288+ private void parse (Reader reader , Type type ) {
289+ switch (type ) {
290+ case Filter :
291+ WordPressCodeCompletionParser .parse (reader , filterItems );
292+ break ;
293+ case Action :
294+ WordPressCodeCompletionParser .parse (reader , actionItems );
295+ break ;
296+ default :
297+ throw new AssertionError ();
298+ }
299+ }
300+
301+ private InputStream getInputStream (FileObject customXml , Type type ) {
302+ InputStream inputStream = null ;
303+ if (customXml == null ) {
304+ inputStream = getDefaultInputStream (type );
282305 } else {
283306 try {
284- actionInputStream = actionXml .getInputStream ();
307+ inputStream = customXml .getInputStream ();
285308 } catch (FileNotFoundException ex ) {
286309 LOGGER .log (Level .WARNING , null , ex );
287310 }
288311 }
289- try {
290- Reader filterReader = new BufferedReader (new InputStreamReader (filterInputStream , Charset .UTF8 ));
291- WordPressCodeCompletionParser .parse (filterReader , filterItems );
292- Reader actionReader = new BufferedReader (new InputStreamReader (actionInputStream , Charset .UTF8 ));
293- WordPressCodeCompletionParser .parse (actionReader , actionItems );
294- } catch (UnsupportedEncodingException ex ) {
295- LOGGER .log (Level .WARNING , null , ex );
312+ return inputStream ;
313+ }
314+
315+ private InputStream getDefaultInputStream (Type type ) {
316+ switch (type ) {
317+ case Filter :
318+ return FilterAndActionCompletion .class .getResourceAsStream (DEFAULT_FILTER_CODE_COMPLETION_XML );
319+ case Action :
320+ return FilterAndActionCompletion .class .getResourceAsStream (DEFAULT_ACTION_CODE_COMPLETION_XML );
321+ default :
322+ return null ;
296323 }
297324 }
298325
@@ -321,9 +348,7 @@ public static void parse(Reader reader, List<WordPressCompletionItem> items) {
321348 try {
322349 WordPressCodeCompletionParser parser = new WordPressCodeCompletionParser (items );
323350 parser .xmlReader .parse (new InputSource (reader ));
324- } catch (SAXException ex ) {
325- Exceptions .printStackTrace (ex );
326- } catch (IOException ex ) {
351+ } catch (SAXException | IOException ex ) {
327352 Exceptions .printStackTrace (ex );
328353 } finally {
329354 try {
0 commit comments