Gridarta Editor
DirectoryCacheFiles.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 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.model.io;
21 
22 import java.io.File;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.annotations.Nullable;
25 
31 public class DirectoryCacheFiles implements CacheFiles {
32 
36  @NotNull
37  private final File directory;
38 
42  @Nullable
43  private final String suffix;
44 
50  public DirectoryCacheFiles(@NotNull final File directory, @Nullable final String suffix) {
51  this.directory = directory;
52  this.suffix = suffix;
53  }
54 
55  @NotNull
56  @Override
57  public File getCacheFile(@NotNull final File file, @Nullable final String prefix) {
58  final StringBuilder sb = new StringBuilder();
59  for (File tmp = file; tmp != null; tmp = tmp.getParentFile()) {
60  final CharSequence name = tmp.getName();
61  int pos = 0;
62  for (int i = 0; i < name.length(); i++) {
63  final char ch = name.charAt(i);
64  if (('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ch == ' ' || ch == '.' || ch == '-' || ch == '_') {
65  sb.insert(pos++, ch);
66  } else {
67  sb.insert(pos++, '%');
68  final String str = Integer.toHexString(ch);
69  sb.insert(pos, str);
70  pos += str.length();
71  }
72  }
73  sb.insert(0, '!');
74  }
75  if (suffix != null) {
76  sb.append(suffix);
77  }
78  final File dir = prefix == null ? directory : new File(directory, prefix);
79  return new File(dir, sb.toString());
80  }
81 
82 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.model.io.DirectoryCacheFiles
A CacheFiles implementation that stores all files in a flat directory.
Definition: DirectoryCacheFiles.java:31
net.sf.gridarta.model.io.DirectoryCacheFiles.getCacheFile
File getCacheFile(@NotNull final File file, @Nullable final String prefix)
Returns the File for caching a given file.
Definition: DirectoryCacheFiles.java:57
net.sf.gridarta.model.io.DirectoryCacheFiles.suffix
final String suffix
An option suffix string.
Definition: DirectoryCacheFiles.java:43
net.sf.gridarta.model.io.DirectoryCacheFiles.DirectoryCacheFiles
DirectoryCacheFiles(@NotNull final File directory, @Nullable final String suffix)
Creates a new instance.
Definition: DirectoryCacheFiles.java:50
net.sf.gridarta.model.io.DirectoryCacheFiles.directory
final File directory
The cache directory.
Definition: DirectoryCacheFiles.java:37
net.sf.gridarta.model.io.CacheFiles
Creates derived files for caching files.
Definition: CacheFiles.java:30