20 package net.sf.gridarta.gui.spells;
22 import java.awt.Component;
23 import java.io.BufferedReader;
24 import java.io.EOFException;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.io.OutputStreamWriter;
33 import java.io.PrintWriter;
34 import java.io.Reader;
35 import java.nio.charset.StandardCharsets;
36 import java.util.Date;
38 import java.util.TreeMap;
39 import javax.swing.JFileChooser;
40 import javax.swing.filechooser.FileFilter;
42 import net.
sf.japi.swing.action.ActionBuilder;
43 import net.
sf.japi.swing.action.ActionBuilderFactory;
44 import net.
sf.japi.util.filter.file.FilenameFileFilter;
45 import org.apache.log4j.Category;
46 import org.apache.log4j.Logger;
47 import org.jetbrains.annotations.NotNull;
48 import org.jetbrains.annotations.Nullable;
70 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
97 public void importSpells(@NotNull
final File dir, @NotNull
final Component parent) {
99 final JFileChooser fileChooser =
new JFileChooser();
100 fileChooser.setDialogTitle(
"Open CF Spell List File");
101 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
102 fileChooser.setMultiSelectionEnabled(
false);
103 fileChooser.setFileFilter(SPELL_LIST_H_FILE_FILTER);
104 final File cd =
new File(System.getProperty(
"user.dir"));
105 final File sd =
new File(cd,
"../server/src/include");
108 final int returnVal = fileChooser.showOpenDialog(parent);
110 if (returnVal == JFileChooser.APPROVE_OPTION) {
112 final File tmpSpellFile = fileChooser.getSelectedFile();
116 ACTION_BUILDER.showMessageDialog(parent,
"importSpellsSuccess", spNum);
119 ACTION_BUILDER.showMessageDialog(parent,
"importSpellsFailed");
131 private int importSpells(
final File spellFile, @NotNull
final File dir) {
132 final Map<String, String> spells =
new TreeMap<>();
133 if (spellFile.getName().equalsIgnoreCase(
"spellist.h")) {
135 try (InputStream fis =
new FileInputStream(spellFile.getAbsolutePath())) {
136 try (Reader isr =
new InputStreamReader(fis)) {
137 try (Reader in =
new BufferedReader(isr)) {
145 }
catch (
final EOFException ignored) {
150 final String name =
readUntil(in,
"\"").trim();
153 spells.put(name, Integer.toString(counter++));
158 }
catch (
final FileNotFoundException ignored) {
159 LOG.error(
"File '" + spellFile.getAbsolutePath() +
"' not found!");
160 }
catch (
final IOException ex) {
161 LOG.error(
"Cannot read file '" + spellFile.getAbsolutePath() +
"': " + ex.getMessage());
166 if (!spells.isEmpty()) {
169 if (!dir.exists() || !dir.isDirectory()) {
173 final File dirFile =
new File(dir, this.spellFile);
176 try (PrintWriter out =
new PrintWriter(
new OutputStreamWriter(
new FileOutputStream(dirFile), StandardCharsets.UTF_8))) {
178 out.println(
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
179 out.println(
"<!DOCTYPE spells SYSTEM \"spells.dtd\">");
181 out.println(
" - ##########################################################");
182 out.println(
" - # You may add new spells to this file, but there's no #");
183 out.println(
" - # need to do it because the file can be auto-generated. #");
184 out.println(
" - # In the editor, select menu \"Resources->Collect Spells\" #");
185 out.println(
" - # to generate a new version of this file. #");
186 out.println(
" - ##########################################################");
188 out.println(
"<!-- Generated on: " +
new Date() +
" -->");
189 out.println(
"<spells>");
191 final String[] spaces = {
" ",
" ",
"" };
192 for (
final Map.Entry<String, String> entry : spells.entrySet()) {
193 final String
id = entry.getValue();
194 out.println(
" <spell id=\"" +
id +
'\"' + spaces[
id.length() - 1] +
" name=\"" + entry.getKey() +
"\" />");
196 out.println(
"</spells>");
198 }
catch (
final IOException ex) {
199 LOG.error(
"Cannot write file '" + dirFile.getAbsolutePath() +
"': " + ex.getMessage());
202 return spells.size();
220 private static void readUntil(@NotNull
final Reader stream, @NotNull
final CharSequence tag, @Nullable
final CharSequence abort)
throws IOException {
229 if (c == tag.charAt(t)) {
234 if (c == abort.charAt(a)) {
239 }
while (t < tag.length() && a < abort.length() && c != -1);
244 if (c == tag.charAt(t)) {
249 }
while (t < tag.length() && c != -1);
254 throw new EOFException();
258 if (abort != null && a == abort.length()) {
259 throw new EOFException();
274 private static String
readUntil(@NotNull
final Reader stream, @NotNull
final CharSequence tag)
throws IOException {
275 final StringBuilder sb =
new StringBuilder();
285 if (c == tag.charAt(t)) {
290 }
while (t < tag.length() && c != -1 && count++ < maxCount);
293 if (c == -1 || count >= maxCount) {
294 throw new EOFException();
297 return sb.substring(0, sb.length() - tag.length());
void importSpells(@NotNull final File dir, @NotNull final Component parent)
Opens a file chooser to select the spell list file, then import spells.
Utility class for JFileChooser related functions.
static String readUntil(@NotNull final Reader stream, @NotNull final CharSequence tag)
Reads characters from the BufferedReader stream till 'tag' is found.
static final Category LOG
The Logger for printing log messages.
Base package of all Gridarta classes.
static void setCurrentDirectory(@NotNull final JFileChooser fileChooser, @Nullable final File dir)
Calls JFileChooser#setCurrentDirectory(File).
static void readUntil(@NotNull final Reader stream, @NotNull final CharSequence tag, @Nullable final CharSequence abort)
Reads characters from the BufferedReader stream till 'tag' is found.
final String spellFile
The spell file name.
int importSpells(final File spellFile, @NotNull final File dir)
Reads all spells from a Crossfire or Daimonin spell list file and write an alphabetical list into "sp...
static final long READ_MAX
Maximum number of characters to read in readUntil.
SpellsUtils(@NotNull final String spellFile)
Creates a new instance.
static final ActionBuilder ACTION_BUILDER
Action Builder.
static final FileFilter SPELL_LIST_H_FILE_FILTER
File filter for filtering spellist.h files.