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, IOUtils.MAP_ENCODING)) {
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);
111  return ret;
112  }
113  }
114  }
115  } catch (final IOException ignored) {
116  return super.accept(pathName);
117  }
118  }
119 
120 }
net.sf.gridarta.utils.MapFileFilter.cache
final Map< File, Boolean > cache
Definition: MapFileFilter.java:52
net.sf.gridarta.utils.MapFileFilter
Definition: MapFileFilter.java:38
net.sf.gridarta.utils.MapFileFilter.isPerformingRealChecks
static boolean isPerformingRealChecks()
Definition: MapFileFilter.java:80
net.sf.gridarta
net.sf
net
net.sf.gridarta.utils.MapFileFilter.accept
boolean accept(@NotNull final File pathName)
Definition: MapFileFilter.java:88
net.sf.gridarta.utils.MapFileFilter.setPerformingRealChecks
static void setPerformingRealChecks(final boolean performingRealChecks)
Definition: MapFileFilter.java:70
net.sf.gridarta.utils.IOUtils
Definition: IOUtils.java:40
net.sf.gridarta.utils.MapFileFilter.performingRealChecks
static boolean performingRealChecks
Definition: MapFileFilter.java:44
net.sf.gridarta.utils.MapFileFilter.MapFileFilter
MapFileFilter(final boolean acceptDirectories, @NotNull final String description, @NotNull final String... endings)
Definition: MapFileFilter.java:61
net.sf.gridarta.utils.IOUtils.MAP_ENCODING
static final String MAP_ENCODING
Definition: IOUtils.java:52
net.sf.gridarta.MainControl
Definition: MainControl.java:27