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.Map.Entry;
39 import java.util.TreeMap;
40 import javax.swing.JFileChooser;
41 import javax.swing.filechooser.FileFilter;
43 import net.
sf.japi.swing.action.ActionBuilder;
44 import net.
sf.japi.swing.action.ActionBuilderFactory;
45 import net.
sf.japi.util.filter.file.FilenameFileFilter;
46 import org.apache.log4j.Category;
47 import org.apache.log4j.Logger;
48 import org.jetbrains.annotations.NotNull;
49 import org.jetbrains.annotations.Nullable;
71 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
98 public void importSpells(@NotNull
final File dir, @NotNull
final Component parent) {
100 final JFileChooser fileChooser =
new JFileChooser();
101 fileChooser.setDialogTitle(
"Open CF Spell List File");
102 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
103 fileChooser.setMultiSelectionEnabled(
false);
105 final File cd =
new File(System.getProperty(
"user.dir"));
106 final File sd =
new File(cd,
"../server/src/include");
109 final int returnVal = fileChooser.showOpenDialog(parent);
111 if (returnVal == JFileChooser.APPROVE_OPTION) {
113 final File tmpSpellFile = fileChooser.getSelectedFile();
117 ACTION_BUILDER.showMessageDialog(parent,
"importSpellsSuccess", spNum);
133 final Map<String, String> spells =
new TreeMap<>();
134 if (
spellFile.getName().equalsIgnoreCase(
"spellist.h")) {
136 try (InputStream fis =
new FileInputStream(
spellFile.getAbsolutePath())) {
137 try (Reader isr =
new InputStreamReader(fis)) {
138 try (Reader in =
new BufferedReader(isr)) {
146 }
catch (
final EOFException ignored) {
154 spells.put(
name, Integer.toString(counter++));
159 }
catch (
final FileNotFoundException ignored) {
160 LOG.error(
"File '" +
spellFile.getAbsolutePath() +
"' not found!");
161 }
catch (
final IOException ex) {
162 LOG.error(
"Cannot read file '" +
spellFile.getAbsolutePath() +
"': " + ex.getMessage());
167 if (!spells.isEmpty()) {
170 if (!dir.exists() || !dir.isDirectory()) {
174 final File dirFile =
new File(dir, this.spellFile);
177 try (PrintWriter out =
new PrintWriter(
new OutputStreamWriter(
new FileOutputStream(dirFile), StandardCharsets.UTF_8))) {
179 out.println(
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
180 out.println(
"<!DOCTYPE spells SYSTEM \"spells.dtd\">");
182 out.println(
" - ##########################################################");
183 out.println(
" - # You may add new spells to this file, but there's no #");
184 out.println(
" - # need to do it because the file can be auto-generated. #");
185 out.println(
" - # In the editor, select menu \"Resources->Collect Spells\" #");
186 out.println(
" - # to generate a new version of this file. #");
187 out.println(
" - ##########################################################");
189 out.println(
"<!-- Generated on: " +
new Date() +
" -->");
190 out.println(
"<spells>");
192 final String[]
spaces = {
" ",
" ",
"" };
193 for (
final Entry<String, String> entry : spells.entrySet()) {
194 final String
id = entry.getValue();
195 out.println(
" <spell id=\"" +
id +
'\"' +
spaces[
id.length() - 1] +
" name=\"" + entry.getKey() +
"\" />");
197 out.println(
"</spells>");
199 }
catch (
final IOException ex) {
200 LOG.error(
"Cannot write file '" + dirFile.getAbsolutePath() +
"': " + ex.getMessage());
203 return spells.size();
221 private static void readUntil(@NotNull
final Reader stream, @NotNull
final CharSequence tag, @Nullable
final CharSequence abort)
throws IOException {
230 if (c == tag.charAt(t)) {
235 }
while (t < tag.length() && c != -1);
240 if (c == tag.charAt(t)) {
245 if (c == abort.charAt(a)) {
250 }
while (t < tag.length() && a < abort.length() && c != -1);
255 throw new EOFException(
"tag '" + tag +
"' not found");
259 if (abort !=
null && a == abort.length()) {
260 throw new EOFException(
"tag '" + tag +
"' not found before '" + abort +
"'");
275 private static String
readUntil(@NotNull
final Reader stream, @NotNull
final CharSequence tag)
throws IOException {
276 final StringBuilder sb =
new StringBuilder();
286 if (c == tag.charAt(t)) {
291 }
while (t < tag.length() && c != -1 && count++ < maxCount);
294 if (c == -1 || count >= maxCount) {
295 throw new EOFException(
"tag '" + tag +
"' not found");
298 return sb.substring(0, sb.length() - tag.length());