Gridarta Editor
AttributeListUtils.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.io;
21 
24 import org.jetbrains.annotations.NotNull;
25 
30 public class AttributeListUtils {
31 
35  private AttributeListUtils() {
36  }
37 
44  public static String removeAttribute(@NotNull final String attributeList, @NotNull final String key) {
45  if (attributeList.length() <= 0) {
46  return attributeList;
47  }
48 
49  final String prefix = key + " ";
50 
51  final String[] lines = StringUtils.PATTERN_END_OF_LINE.split(attributeList, -1);
52 
53  final StringBuilder sb = new StringBuilder();
54  for (final String line : lines) {
55  if (!line.isEmpty() && !line.startsWith(prefix)) {
56  sb.append(line);
57  sb.append('\n');
58  }
59  }
60  return sb.toString();
61  }
62 
71  @NotNull
72  public static String diffArchTextKeys(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final BaseObject<?, ?, ?, ?> archetype) {
73  final CharSequence oldObjectText = gameObject.getObjectText();
74  final StringBuilder result = new StringBuilder();
75  for (final String line : StringUtils.PATTERN_END_OF_LINE.split(archetype.getObjectText(), 0)) {
76  final int spaceIndex = line.indexOf(' ');
77  if (!line.isEmpty() && spaceIndex > 0 && StringUtils.diffTextString(oldObjectText, line.substring(0, spaceIndex + 1), true) == null) {
78  result.append(line).append('\n');
79  }
80  }
81  return result.toString();
82  }
83 
92  @NotNull
93  public static String diffArchTextValues(@NotNull final BaseObject<?, ?, ?, ?> archetype, @NotNull final CharSequence attributes) {
94  final CharSequence oldObjectText = archetype.getObjectText();
95  final StringBuilder result = new StringBuilder();
96  for (final String line : StringUtils.PATTERN_END_OF_LINE.split(attributes, 0)) {
97  try {
98  final CharSequence test = StringUtils.diffTextString(oldObjectText, line, false);
99  final char c = test == null ? '\n' : test.charAt(0);
100  if (!line.isEmpty() && (test == null || c == '\n')) {
101  result.append(line).append('\n');
102  }
103  } catch (final StringIndexOutOfBoundsException ignored) {
104  // ignore
105  }
106  }
107  return result.toString();
108  }
109 
110 }
net.sf.gridarta.model.io.AttributeListUtils.AttributeListUtils
AttributeListUtils()
Private constructor to prevent instantiation.
Definition: AttributeListUtils.java:35
net.sf.gridarta.model.io.AttributeListUtils.removeAttribute
static String removeAttribute(@NotNull final String attributeList, @NotNull final String key)
Removes an attribute from an attribute list.
Definition: AttributeListUtils.java:44
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.model.io.AttributeListUtils
Utility class for archetype attribute related functions.
Definition: AttributeListUtils.java:30
net.sf.gridarta.model.io.AttributeListUtils.diffArchTextKeys
static String diffArchTextKeys(@NotNull final BaseObject<?, ?, ?, ?> gameObject, @NotNull final BaseObject<?, ?, ?, ?> archetype)
Returns all attributes from the given game object that don't exist in an archetype.
Definition: AttributeListUtils.java:72
net
net.sf.gridarta.utils.StringUtils.PATTERN_END_OF_LINE
static final Pattern PATTERN_END_OF_LINE
The pattern to match end of line characters separating lines.
Definition: StringUtils.java:61
net.sf.gridarta.utils.StringUtils.diffTextString
static CharSequence diffTextString(@NotNull final CharSequence base, @NotNull final String str, final boolean ignoreValues)
Helper function for 'diffArchText()': Looks for occurrence of the attribute 'str' in 'base' and if fo...
Definition: StringUtils.java:162
net.sf.gridarta.model.io.AttributeListUtils.diffArchTextValues
static String diffArchTextValues(@NotNull final BaseObject<?, ?, ?, ?> archetype, @NotNull final CharSequence attributes)
Returns all entries from the given attributes that don't exist in an archetype.
Definition: AttributeListUtils.java:93
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.utils.StringUtils
Utility class for string manipulation.
Definition: StringUtils.java:31
net.sf.gridarta.model
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20