Gridarta Editor
XMLSpellLoader.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.spells;
21 
25 import nu.xom.Document;
26 import nu.xom.Element;
27 import org.apache.log4j.Category;
28 import org.apache.log4j.Logger;
29 import org.jetbrains.annotations.NotNull;
30 
35 public class XMLSpellLoader {
36 
40  @NotNull
41  private static final Category LOG = Logger.getLogger(XMLSpellLoader.class);
42 
46  private XMLSpellLoader() {
47  }
48 
55  public static void load(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final Document document, @NotNull final Spells<NumberSpell> spells) {
56  // retrieve the spell data from the xml
57  final Element root = document.getRootElement();
58  if (root == null || !root.getLocalName().equals("spells")) {
59  errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "root element 'spells' is missing");
60  } else {
61  // initialize array with appropriate size
62  int numSpells = 0;
63  for (final Element spellElem : new ElementsIterable(root.getChildElements("spell"))) {
64  if (spellElem.getAttribute("id") == null) {
65  errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "found 'spell' element without 'id'");
66  } else if (spellElem.getAttribute("name") == null) {
67  errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "found 'spell' element without 'name'");
68  } else {
69  try {
70  // parse spell number and -name
71  spells.add(new NumberSpell(spellElem.getAttribute("name").getValue().trim(), Integer.parseInt(spellElem.getAttribute("id").getValue())));
72  numSpells++;
73  } catch (final NumberFormatException ignored) {
74  errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "parsing error: spell id '" + spellElem.getAttribute("id") + "' is not an integer.");
75  }
76  }
77  }
78 
79  // loading successful
80  if (LOG.isInfoEnabled()) {
81  LOG.info("Loaded " + numSpells + " defined spells.");
82  }
83  if (numSpells == 0) {
84  errorViewCollector.addWarning(ErrorViewCategory.SPELLS_FILE_INVALID, "no content");
85  }
86  }
87  }
88 
89 }
net.sf.gridarta.model.spells.XMLSpellLoader
Load spell definitions from an XML file.
Definition: XMLSpellLoader.java:35
net.sf.gridarta.model.errorview.ErrorViewCategory.SPELLS_ENTRY_INVALID
SPELLS_ENTRY_INVALID
Definition: ErrorViewCategory.java:84
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.model.spells.XMLSpellLoader.XMLSpellLoader
XMLSpellLoader()
Private constructor to prevent instantiation.
Definition: XMLSpellLoader.java:46
net.sf.gridarta.model.spells.XMLSpellLoader.LOG
static final Category LOG
The Logger for printing log messages.
Definition: XMLSpellLoader.java:41
net.sf.gridarta.model.errorview.ErrorViewCategory.SPELLS_FILE_INVALID
SPELLS_FILE_INVALID
Definition: ErrorViewCategory.java:86
net
net.sf.gridarta.model.errorview
Definition: ErrorView.java:20
net.sf.gridarta.model.errorview.ErrorViewCollector
Convenience class for adding messages to a ErrorView instance using a fixed category name.
Definition: ErrorViewCollector.java:31
net.sf.gridarta.model.errorview.ErrorViewCategory
Defines possible error categories for ErrorView instances.
Definition: ErrorViewCategory.java:28
net.sf.gridarta.utils.xml.ElementsIterable
Definition: ElementsIterable.java:30
net.sf.gridarta.model.spells.NumberSpell
Describes a numbered in-game spell.
Definition: NumberSpell.java:28
net.sf.gridarta.model
net.sf.gridarta.utils.xml
Definition: ElementsIterable.java:20
net.sf.gridarta.model.spells.Spells
Common base class for spells and spell lists.
Definition: Spells.java:33
net.sf.gridarta.model.spells.XMLSpellLoader.load
static void load(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final Document document, @NotNull final Spells< NumberSpell > spells)
Load an XML spell definitions file.
Definition: XMLSpellLoader.java:55
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20