Gridarta Editor
MapFileFilter.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils;
21 
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.util.Map;
28 import java.util.WeakHashMap;
29 import java.util.prefs.Preferences;
30 import net.sf.gridarta.MainControl;
31 import net.sf.japi.util.filter.file.EndingFileFilter;
32 import org.jetbrains.annotations.NotNull;
33 
38 public class MapFileFilter extends EndingFileFilter {
39 
44  private static boolean performingRealChecks = Preferences.userNodeForPackage(MainControl.class).getBoolean("filterRealMaps", true);
45 
51  @NotNull
52  private final Map<File, Boolean> cache = new WeakHashMap<>();
53 
61  public MapFileFilter(final boolean acceptDirectories, @NotNull final String description, @NotNull final String... endings) {
62  super(acceptDirectories, true, description, endings);
63  }
64 
70  public static void setPerformingRealChecks(final boolean performingRealChecks) {
72  Preferences.userNodeForPackage(MainControl.class).putBoolean("filterRealMaps", performingRealChecks);
73  }
74 
80  public static boolean isPerformingRealChecks() {
81  return performingRealChecks;
82  }
83 
87  @Override
88  public boolean accept(@NotNull final File pathName) {
89  if (pathName.isDirectory()) {
90  return true;
91  }
92 
93  //NullPointerException is expected when no mapping exists
94  //noinspection ProhibitedExceptionCaught
95  try {
96  return cache.get(pathName);
97  } catch (final NullPointerException ignored) {
98  }
99 
100  if (!performingRealChecks) {
101  return super.accept(pathName);
102  }
103 
104  try {
105  try (FileInputStream fis = new FileInputStream(pathName)) {
106  try (InputStreamReader isr = new InputStreamReader(fis)) {
107  try (BufferedReader in = new BufferedReader(isr)) {
108  final String line = in.readLine();
109  final boolean ret = line != null && line.equals("arch map");
110  cache.put(pathName, ret ? Boolean.TRUE : Boolean.FALSE);
111  return ret;
112  }
113  }
114  }
115  } catch (final IOException ignored) {
116  return super.accept(pathName);
117  }
118  }
119 
120 }
MapFileFilter(final boolean acceptDirectories, @NotNull final String description, @NotNull final String... endings)
Creates a new instance.
static boolean isPerformingRealChecks()
Get whether to actually perform real checks or just file endings.
static void setPerformingRealChecks(final boolean performingRealChecks)
Set whether to actually perform real checks or just file endings.
Base package of all Gridarta classes.
static boolean performingRealChecks
Whether to actually perform real checks or just file endings.
Swing FileFilter implementation that filters Daimonin map files.
Interface used as preferences location.
boolean accept(@NotNull final File pathName)
ParameterNameDiffersFromOverriddenParameter
final Map< File, Boolean > cache
Already filtered files.