20 package net.sf.gridarta.model.match;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Locale;
27 import nu.xom.Element;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
138 final String currentLanguage = Locale.getDefault().getLanguage();
139 final String localName = el.getLocalName();
140 if (localName == null || !localName.equals(
"GameObjectMatcher")) {
141 throw new ParsingException(
"wrong local element name: expected \"GameObjectMatcher\" but got \"" + localName +
"\"");
143 final String
id = el.getAttribute(XML_MATCHER_ATTRIBUTE_ID).getValue();
145 if (title == null || title.isEmpty()) {
148 if (title == null || title.isEmpty()) {
151 final boolean systemMatcher = el.getAttribute(XML_MATCHER_ATTRIBUTE_SYSTEM).getValue().equals(
"true");
153 assert content != null;
157 if (env != null && env instanceof Element) {
161 envGameObjectMatcher = null;
166 return new NamedGameObjectMatcher(systemMatcher ? 0 : editType,
id, title, systemMatcher, envGameObjectMatcher, matcher);
176 private static String
getLanguageTitle(@NotNull
final Node el, @NotNull
final String language) {
177 final Nodes nodes = el.query(
"title[lang('" + language +
"')]");
178 return nodes.size() == 0 ? null : nodes.get(0).getValue();
189 final String localName = el.getLocalName();
190 if (localName == null) {
192 }
else if (localName.equals(XML_ELEMENT_TYPENRS)) {
194 }
else if (localName.equals(XML_ELEMENT_ATTRIB)) {
196 }
else if (localName.equals(XML_ELEMENT_OR)) {
198 }
else if (localName.equals(XML_ELEMENT_AND)) {
201 throw new ParsingException(
"expected element node name to be one of \"" + XML_ELEMENT_TYPENRS +
"\", \"" + XML_ELEMENT_ATTRIB +
"\", \"" + XML_ELEMENT_OR +
"\", \"" + XML_ELEMENT_AND +
"\".");
237 final Collection<GameObjectMatcher> childMatchers =
new ArrayList<>();
238 for (
int i = 0; i < el.getChildCount(); i++) {
239 final Node childNode = el.getChild(i);
240 if (childNode instanceof Element) {
244 return childMatchers;
255 final String type = el.getAttribute(XML_ATTRIB_ATTRIBUTE_TYPE).getValue();
256 if (type.equals(
"string")) {
257 return new StringAttributeGameObjectMatcher(el.getAttribute(XML_ATTRIB_ATTRIBUTE_NAME).getValue(), Enum.valueOf(
Operation.class, el.getAttribute(XML_ATTRIB_ATTRIBUTE_OP).getValue()), el.getAttribute(XML_ATTRIB_ATTRIBUTE_VALUE).getValue(), el.getAttribute(XML_ATTRIB_ATTRIBUTE_USEARCHETYPE).getValue().equals(
"true"));
258 }
else if (type.equals(
"int")) {
259 return new IntAttributeGameObjectMatcher(el.getAttribute(XML_ATTRIB_ATTRIBUTE_NAME).getValue(), Enum.valueOf(
Operation.class, el.getAttribute(XML_ATTRIB_ATTRIBUTE_OP).getValue()), el.getAttribute(XML_ATTRIB_ATTRIBUTE_VALUE).getValue(), el.getAttribute(XML_ATTRIB_ATTRIBUTE_USEARCHETYPE).getValue().equals(
"true"));
261 throw new AssertionError(
"impossible value '" + type +
"' for XML_ATTRIB_ATTRIBUTE_TYPE");
273 final CharSequence numbers = el.getAttribute(XML_TYPENRS_ATTRIBUTE_NUMBERS).getValue();
275 final int[] types =
new int[typeNrs.length];
276 for (
int i = 0; i < types.length; i++) {
278 types[i] = Integer.parseInt(typeNrs[i]);
279 }
catch (
final NumberFormatException ignored) {
294 private static Node
xpathEvaluate(@NotNull
final Node el, @NotNull
final String xpathExpression) {
295 final Nodes nodes = el.query(xpathExpression);
296 assert nodes.size() <= 1;
297 return nodes.size() == 0 ? null : nodes.get(0);
static final String XML_TYPENRS_ATTRIBUTE_NUMBERS
The name of the "numbers" attribute within XML_ELEMENT_TYPENRS elements.
static GameObjectMatcher createAttributeArchObjectMatcher(@NotNull final Element el)
Creates an AttributeGameObjectMatcher.
Utility class for string manipulation.
Interface for classes that match GameObjects.
GameObjectMatcherParser()
Private constructor to prevent instantiation.
static final String XML_ATTRIB_ATTRIBUTE_TYPE
The name of the "type" attribute within XML_ELEMENT_ATTRIB elements.
Thrown when a parsing error occurs.
static final String XML_ATTRIB_ATTRIBUTE_VALUE
The name of the "value" attribute within XML_ELEMENT_ATTRIB elements.
A GameObjectMatcher that And-combines other.
Class for some default GameObjectMatchers.
static GameObjectMatcher createTypeNrsArchObjectMatcher(@NotNull final Element el)
Creates a TypeNrsGameObjectMatcher.
static Node xpathEvaluate(@NotNull final Node el, @NotNull final String xpathExpression)
Evaluates an XPath expression and returns the result Node.
Decorates an arbitrary GameObjectMatcher with a localized name that is suitable for the user interfac...
static final String XML_ATTRIB_ATTRIBUTE_USEARCHETYPE
The name of the "useArchetype" attribute within XML_ELEMENT_ATTRIB elements.
Base package of all Gridarta classes.
static NamedGameObjectMatcher parseMatcher(@NotNull final Element el, final int editType)
Creates a NamedGameObjectMatcher from XML.
static final String XML_ATTRIB_ATTRIBUTE_OP
The name of the "op" attribute within XML_ELEMENT_ATTRIB elements.
A AttributeGameObjectMatcher that compares attributes values using "string" type. ...
static final String XML_ELEMENT_ATTRIB
The name of the "Attrib" element.
static final String XML_MATCHER_ATTRIBUTE_SYSTEM
The name of the "system" attribute within matcher elements.
static Collection< GameObjectMatcher > getChildMatchers(@NotNull final Node el)
Gets the matchers that are found as children of an XML element.
static final String XML_ELEMENT_OR
The name of the "Or" element.
static final Pattern PATTERN_WHITESPACE
Pattern to match whitespace excluding NL and CR.
static final String XML_ELEMENT_TYPENRS
The name of the "TypeNrs" element.
static GameObjectMatcher createOrMatcher(@NotNull final Node el)
Creates an OrGameObjectMatcher.
static GameObjectMatcher createMatcher(@NotNull final Element el)
Creates a GameObjectMatcher from XML.
static final String XML_MATCHER_ATTRIBUTE_ID
The name of the "id" attribute within matcher elements.
An GameObjectMatcher matching certain archetype types.
static String getLanguageTitle(@NotNull final Node el, @NotNull final String language)
Returns the title for a given language.
static final String XML_MATCHER_ELEMENT_ENV
The name of the "Env" element within matcher elements.
static final String XML_ATTRIB_ATTRIBUTE_NAME
The name of the "name" attribute within XML_ELEMENT_ATTRIB elements.
A GameObjectMatcher that Or-combines other.
static GameObjectMatcher createAndMatcher(@NotNull final Node el)
Creates an AndGameObjectMatcher.
static final String XML_ELEMENT_AND
The name of the "And" element.
A AttributeGameObjectMatcher that compares attributes values using "int" type.