Gridarta Editor
ArchetypeAttributeParser.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.model.archetypetype;
21 
22 import java.util.regex.Pattern;
25 import nu.xom.Attribute;
26 import nu.xom.Element;
27 import nu.xom.Node;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
36 
40  @NotNull
41  private static final Pattern LINE_BREAK = Pattern.compile("\\s*\n\\s*");
42 
46  @NotNull
47  private static final Pattern UNDERSCORE = Pattern.compile("_");
48 
53  @NotNull
55 
61  this.archetypeAttributeFactory = archetypeAttributeFactory;
62  }
63 
74  @Nullable
75  @SuppressWarnings("FeatureEnvy")
76  public ArchetypeAttribute load(@NotNull final Element attributeElement, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final ArchetypeTypeSet archetypeTypeSet, @NotNull final String typeName) throws MissingAttributeException {
77  final String description = parseText(attributeElement);
78  final int inputLength = getAttributeIntValue(attributeElement, Constants.XML_ATTRIBUTE_LENGTH, 0, typeName, errorViewCollector);
79  final String attributeType = getAttributeValue(attributeElement, Constants.XML_ATTRIBUTE_TYPE);
80  if (attributeType.equals("bool")) {
82  }
83  if (attributeType.equals("bool_special")) {
85  }
86  if (attributeType.equals("int")) {
87  final int minValue = getAttributeIntValue(attributeElement, Constants.XML_ATTRIBUTE_MIN, Integer.MIN_VALUE, typeName, errorViewCollector);
88  final int maxValue = getAttributeIntValue(attributeElement, Constants.XML_ATTRIBUTE_MAX, Integer.MAX_VALUE, typeName, errorViewCollector);
89  final int minCheckValue = getAttributeIntValue(attributeElement, Constants.XML_ATTRIBUTE_CHECK_MIN, minValue, typeName, errorViewCollector);
90  final int maxCheckValue = getAttributeIntValue(attributeElement, Constants.XML_ATTRIBUTE_CHECK_MAX, maxValue, typeName, errorViewCollector);
91 
92  if (minValue > maxValue) {
93  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + " has invalid " + Constants.XML_ATTRIBUTE_MIN + ".." + Constants.XML_ATTRIBUTE_MAX + " range: " + minValue + ".." + maxValue + ".");
94  return null;
95  }
96 
97  if (minCheckValue > maxCheckValue) {
98  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + " has invalid " + Constants.XML_ATTRIBUTE_CHECK_MIN + ".." + Constants.XML_ATTRIBUTE_CHECK_MAX + " range: " + minCheckValue + ".." + maxCheckValue + ".");
99  return null;
100  }
101 
102  if (minCheckValue < minValue) {
103  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + " has invalid " + Constants.XML_ATTRIBUTE_CHECK_MIN + " value: " + minCheckValue + " < " + Constants.XML_ATTRIBUTE_MIN + " = " + minValue + ".");
104  return null;
105  }
106 
107  if (maxCheckValue > maxValue) {
108  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + " has invalid " + Constants.XML_ATTRIBUTE_CHECK_MAX + " value: " + maxCheckValue + " > " + Constants.XML_ATTRIBUTE_MAX + " = " + maxValue + ".");
109  return null;
110  }
111 
112  return archetypeAttributeFactory.newArchetypeAttributeInt(getAttributeValue(attributeElement, Constants.XML_ATTRIBUTE_ARCH), getAttributeValue(attributeElement, Constants.XML_ATTRIBUTE_EDITOR), description, inputLength, minValue, maxValue, minCheckValue, maxCheckValue);
113  }
114  if (attributeType.equals("long")) {
116  }
117  if (attributeType.equals("float")) {
119  }
120  if (attributeType.equals("string")) {
122  }
123  if (attributeType.equals("map_path")) {
125  }
126  if (attributeType.equals("script_file")) {
128  }
129  if (attributeType.equals("facename")) {
131  }
132  if (attributeType.equals("animname")) {
134  }
135  if (attributeType.equals("text")) {
137  }
138  if (attributeType.equals("fixed")) {
140  }
141  if (attributeType.equals("spell")) {
143  }
144  if (attributeType.equals("nz_spell")) {
146  }
147  if (attributeType.equals("inv_spell")) {
149  }
150  if (attributeType.equals("inv_spell_optional")) {
152  }
153  if (attributeType.startsWith("bitmask")) {
154  // got a bitmask attribute
155  final String bitmaskName = attributeType.substring(8).trim();
156  if (archetypeTypeSet.getBitmask(bitmaskName) == null) {
157  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + ": Bitmask \"" + bitmaskName + "\" is undefined.");
158  return null;
159  }
160  return archetypeAttributeFactory.newArchetypeAttributeBitmask(getAttributeValue(attributeElement, Constants.XML_ATTRIBUTE_ARCH), getAttributeValue(attributeElement, Constants.XML_ATTRIBUTE_EDITOR), description, inputLength, bitmaskName);
161  }
162  if (attributeType.startsWith("list_")) {
163  // got a bitmask attribute
164  final String listName = attributeType.substring(5).trim();
165  if (archetypeTypeSet.getList(listName) == null) {
166  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + ": List \"" + listName + "\" is undefined.");
167  return null;
168  }
169  return archetypeAttributeFactory.newArchetypeAttributeList(getAttributeValue(attributeElement, Constants.XML_ATTRIBUTE_ARCH), getAttributeValue(attributeElement, Constants.XML_ATTRIBUTE_EDITOR), description, inputLength, listName);
170  }
171  if (attributeType.startsWith("list2_")) {
172  // got a bitmask attribute
173  final String listNames = attributeType.substring(6).trim();
174  final String[] tmp = UNDERSCORE.split(listNames, 2);
175  if (tmp.length != 2) {
176  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + ": Lists \"" + listNames + "\" are undefined.");
177  return null;
178  }
179  final String listName1 = tmp[0];
180  final String listName2 = tmp[1];
181  if (archetypeTypeSet.getList(listName1) == null) {
182  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + ": List \"" + listName1 + "\" is undefined.");
183  return null;
184  }
185  if (archetypeTypeSet.getList(listName2) == null) {
186  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + ": List \"" + listName2 + "\" is undefined.");
187  return null;
188  }
189  return archetypeAttributeFactory.newArchetypeAttributeList2(getAttributeValue(attributeElement, Constants.XML_ATTRIBUTE_ARCH), getAttributeValue(attributeElement, Constants.XML_ATTRIBUTE_EDITOR), description, inputLength, listName1, listName2);
190  }
191  if (attributeType.equals("treasurelist")) {
193  }
194  // unknown type
195  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + " has an attribute with unknown type: '" + attributeType + "'.");
196  return null;
197  }
198 
206  @NotNull
207  private static String parseText(@NotNull final Node attributeElement) {
208  final String tmp = attributeElement.getValue().trim();
209  return LINE_BREAK.matcher(tmp).replaceAll(tmp.startsWith("<html>") ? " " : "\n");
210  }
211 
219  @NotNull
220  private static String getAttributeValue(@NotNull final Element element, @NotNull final String attributeKey) throws MissingAttributeException {
221  final Attribute attr = element.getAttribute(attributeKey);
222  if (attr == null) {
223  throw new MissingAttributeException(element.getLocalName(), attributeKey);
224  }
225 
226  return attr.getValue().trim();
227  }
228 
236  @Nullable
237  private static String getAttributeValueOptional(@NotNull final Element element, @NotNull final String attributeKey) {
238  final Attribute attr = element.getAttribute(attributeKey);
239  return attr == null ? null : attr.getValue().trim();
240  }
241 
252  private static int getAttributeIntValue(@NotNull final Element element, @NotNull final String attributeKey, final int defaultValue, @NotNull final String typeName, @NotNull final ErrorViewCollector errorViewCollector) {
253  final String value = getAttributeValueOptional(element, attributeKey);
254  if (value == null) {
255  return defaultValue;
256  }
257 
258  try {
259  return Integer.parseInt(value);
260  } catch (final NumberFormatException ignored) {
261  errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "type " + typeName + " has attribute '" + attributeKey + "' with invalid value '" + value + "' (must be a number).");
262  }
263 
264  return defaultValue;
265  }
266 
267 }
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_MAX
static final String XML_ATTRIBUTE_MAX
Definition: Constants.java:271
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_TRUE
static final String XML_ATTRIBUTE_TRUE
Definition: Constants.java:320
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeMapPath
ArchetypeAttribute newArchetypeAttributeMapPath(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeTreasure
ArchetypeAttribute newArchetypeAttributeTreasure(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeBool
ArchetypeAttribute newArchetypeAttributeBool(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeParser.load
ArchetypeAttribute load(@NotNull final Element attributeElement, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final ArchetypeTypeSet archetypeTypeSet, @NotNull final String typeName)
Definition: ArchetypeAttributeParser.java:76
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_MARKER
static final String XML_ATTRIBUTE_MARKER
Definition: Constants.java:334
net.sf.gridarta.model.archetypetype.MissingAttributeException
Definition: MissingAttributeException.java:28
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_ARCH_END
static final String XML_ATTRIBUTE_ARCH_END
Definition: Constants.java:306
net.sf
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_LENGTH
static final String XML_ATTRIBUTE_LENGTH
Definition: Constants.java:313
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeString
ArchetypeAttribute newArchetypeAttributeString(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeBoolSpec
ArchetypeAttribute newArchetypeAttributeBoolSpec(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength, @NotNull String trueValue, @NotNull String falseValue)
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_EDITOR
static final String XML_ATTRIBUTE_EDITOR
Definition: Constants.java:250
net.sf.gridarta.model.archetypetype.ArchetypeAttributeParser.parseText
static String parseText(@NotNull final Node attributeElement)
Definition: ArchetypeAttributeParser.java:207
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeFixed
ArchetypeAttribute newArchetypeAttributeFixed(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeFacename
ArchetypeAttribute newArchetypeAttributeFacename(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet
Definition: ArchetypeTypeSet.java:40
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeZSpell
ArchetypeAttribute newArchetypeAttributeZSpell(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_FALSE
static final String XML_ATTRIBUTE_FALSE
Definition: Constants.java:327
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeBitmask
ArchetypeAttribute newArchetypeAttributeBitmask(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength, @NotNull String bitmaskName)
net
net.sf.gridarta.model.errorview
Definition: ErrorView.java:20
net.sf.gridarta.model.errorview.ErrorViewCollector
Definition: ErrorViewCollector.java:31
net.sf.gridarta.model.errorview.ErrorViewCategory
Definition: ErrorViewCategory.java:28
net.sf.gridarta.model.archetypetype.ArchetypeAttribute
Definition: ArchetypeAttribute.java:28
net.sf.gridarta.model.errorview.ErrorViewCategory.TYPES_ENTRY_INVALID
TYPES_ENTRY_INVALID
Definition: ErrorViewCategory.java:92
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeSpell
ArchetypeAttribute newArchetypeAttributeSpell(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeList
ArchetypeAttribute newArchetypeAttributeList(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength, @NotNull String listName)
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_ARCH
static final String XML_ATTRIBUTE_ARCH
Definition: Constants.java:243
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeFloat
ArchetypeAttribute newArchetypeAttributeFloat(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeLong
ArchetypeAttribute newArchetypeAttributeLong(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeParser.UNDERSCORE
static final Pattern UNDERSCORE
Definition: ArchetypeAttributeParser.java:47
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeText
ArchetypeAttribute newArchetypeAttributeText(@NotNull String archetypeAttributeName, @NotNull String endingOld, @NotNull String attributeName, @NotNull String description, int inputLength, @Nullable String fileExtension)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeInvSpellOptional
ArchetypeAttribute newArchetypeAttributeInvSpellOptional(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeParser.getAttributeIntValue
static int getAttributeIntValue(@NotNull final Element element, @NotNull final String attributeKey, final int defaultValue, @NotNull final String typeName, @NotNull final ErrorViewCollector errorViewCollector)
Definition: ArchetypeAttributeParser.java:252
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_MIN
static final String XML_ATTRIBUTE_MIN
Definition: Constants.java:264
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory
Definition: ArchetypeAttributeFactory.java:29
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeInt
ArchetypeAttribute newArchetypeAttributeInt(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength, int minValue, int maxValue, int minCheckValue, int maxCheckValue)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeList2
ArchetypeAttribute newArchetypeAttributeList2(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength, @NotNull String listName1, @NotNull String listName2)
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_CHECK_MAX
static final String XML_ATTRIBUTE_CHECK_MAX
Definition: Constants.java:285
net.sf.gridarta.model.archetypetype.ArchetypeAttributeParser.getAttributeValue
static String getAttributeValue(@NotNull final Element element, @NotNull final String attributeKey)
Definition: ArchetypeAttributeParser.java:220
net.sf.gridarta.model
net.sf.gridarta.model.archetypetype.ArchetypeAttributeParser.LINE_BREAK
static final Pattern LINE_BREAK
Definition: ArchetypeAttributeParser.java:41
net.sf.gridarta.model.archetypetype.ArchetypeAttributeParser
Definition: ArchetypeAttributeParser.java:35
net.sf.gridarta.model.archetypetype.Constants
Definition: Constants.java:28
net.sf.gridarta.model.archetypetype.ArchetypeAttributeParser.ArchetypeAttributeParser
ArchetypeAttributeParser(@NotNull final ArchetypeAttributeFactory archetypeAttributeFactory)
Definition: ArchetypeAttributeParser.java:60
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_VALUE
static final String XML_ATTRIBUTE_VALUE
Definition: Constants.java:257
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeAnimname
ArchetypeAttribute newArchetypeAttributeAnimname(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeScriptFile
ArchetypeAttribute newArchetypeAttributeScriptFile(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.ArchetypeAttributeParser.getAttributeValueOptional
static String getAttributeValueOptional(@NotNull final Element element, @NotNull final String attributeKey)
Definition: ArchetypeAttributeParser.java:237
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_ARCH_BEGIN
static final String XML_ATTRIBUTE_ARCH_BEGIN
Definition: Constants.java:299
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_CHECK_MIN
static final String XML_ATTRIBUTE_CHECK_MIN
Definition: Constants.java:278
net.sf.gridarta.model.archetypetype.ArchetypeAttributeFactory.newArchetypeAttributeInvSpell
ArchetypeAttribute newArchetypeAttributeInvSpell(@NotNull String archetypeAttributeName, @NotNull String attributeName, @NotNull String description, int inputLength)
net.sf.gridarta.model.archetypetype.Constants.XML_ATTRIBUTE_TYPE
static final String XML_ATTRIBUTE_TYPE
Definition: Constants.java:292
net.sf.gridarta.model.archetypetype.ArchetypeAttributeParser.archetypeAttributeFactory
final ArchetypeAttributeFactory archetypeAttributeFactory
Definition: ArchetypeAttributeParser.java:54