Gridarta Editor
TokenMarkerFactory.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.textedit.textarea.tokenmarker;
21 
22 import java.io.File;
23 import java.util.HashMap;
24 import java.util.Map;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
27 
32 public class TokenMarkerFactory {
33 
37  private static final String CROSSFIRE_DIALOG = "crossfire-dialog";
38 
42  private static final String DAIMONIN_AI = "daimonin-ai";
43 
47  private static final Map<String, Class<? extends TokenMarker>> TOKEN_MARKERS = new HashMap<>();
48 
49  static {
50  TOKEN_MARKERS.put("c", CTokenMarker.class);
51  TOKEN_MARKERS.put("cc", CTokenMarker.class);
52  TOKEN_MARKERS.put("cpp", CTokenMarker.class);
53  TOKEN_MARKERS.put("h", CTokenMarker.class);
54  TOKEN_MARKERS.put("hh", CTokenMarker.class);
55  TOKEN_MARKERS.put("htm", HTMLTokenMarker.class);
56  TOKEN_MARKERS.put("html", HTMLTokenMarker.class);
57  TOKEN_MARKERS.put("js", JavaScriptTokenMarker.class);
58  TOKEN_MARKERS.put("lua", LuaTokenMarker.class);
59  TOKEN_MARKERS.put("py", PythonTokenMarker.class);
60  TOKEN_MARKERS.put("xml", XMLTokenMarker.class);
61  TOKEN_MARKERS.put(CROSSFIRE_DIALOG, CrossfireDialogTokenMarker.class);
62  TOKEN_MARKERS.put(DAIMONIN_AI, DaimoninAITokenMarker.class);
63  }
64 
68  private TokenMarkerFactory() {
69  }
70 
76  @NotNull
77  public static TokenMarker createTokenMarker(@Nullable final File file) {
78  if (file == null) {
79  return new EmptyTokenMarker();
80  }
81 
82  final String filename = file.getName();
83  final int dotIndex = filename.lastIndexOf('.');
84  if (dotIndex == -1) {
85  return new EmptyTokenMarker();
86  }
87  return createTokenMarker(filename.substring(dotIndex + 1));
88  }
89 
95  @NotNull
96  public static TokenMarker createTokenMarker(@Nullable final String extension) {
97  if (extension == null) {
98  return new EmptyTokenMarker();
99  }
100 
101  final Class<? extends TokenMarker> tokenMarkerClass = TOKEN_MARKERS.get(extension);
102  if (tokenMarkerClass == null) {
103  return new EmptyTokenMarker();
104  }
105  try {
106  return tokenMarkerClass.newInstance();
107  } catch (final InstantiationException ex) {
108  return new EmptyTokenMarker();
109  } catch (final IllegalAccessException ex) {
110  return new EmptyTokenMarker();
111  }
112  }
113 
114 }
static TokenMarker createTokenMarker(@Nullable final File file)
Creates a suitable TokenMarker for a given file.
static final String DAIMONIN_AI
File extension for DaimoninAITokenMarker.
static final String CROSSFIRE_DIALOG
File extension for CrossfireDialogTokenMarker.
A TokenMarker for the message field of Crossfire objects allowing.
A factory for creatingTokenMarker instances for Files.
static final Map< String, Class<? extends TokenMarker > > TOKEN_MARKERS
Maps file extensions to token marker classes.
static TokenMarker createTokenMarker(@Nullable final String extension)
Creates a TokenMarker for a given file extension.
A TokenMarker for the message field of Daimonin AI objects.
TokenMarkerFactory()
Private constructor to prevent instantiation.
A token marker that splits lines of text into tokens.