Gridarta Editor
StringParameterBuilder.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.utils;
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import org.jetbrains.annotations.NotNull;
27 
32 public class StringParameterBuilder {
33 
37  @NotNull
38  private static final Pattern PATTERN = Pattern.compile("\\$\\{([a-zA-Z]+)}");
39 
43  @NotNull
44  private final Map<String, String> values = new HashMap<>();
45 
53  public void addParameter(@NotNull final String key, @NotNull final String value) {
54  values.put(key, value);
55  }
56 
63  @NotNull
64  public String replace(@NotNull final CharSequence spec) throws SyntaxErrorException {
65  final Matcher matcher = PATTERN.matcher(spec);
66  final StringBuffer sb = new StringBuffer();
67  while (matcher.find()) {
68  final String key = matcher.group(1);
69  final String value = values.get(key);
70  if (value == null) {
71  throw new SyntaxErrorException("unknown variable '${" + key + "}");
72  }
73  matcher.appendReplacement(sb, "");
74  sb.append(value);
75  }
76  matcher.appendTail(sb);
77  return sb.toString();
78  }
79 
80 }
net.sf.gridarta.utils.StringParameterBuilder.replace
String replace(@NotNull final CharSequence spec)
Replaces all parameters in a string.
Definition: StringParameterBuilder.java:64
net.sf.gridarta.utils.StringParameterBuilder
Replaces placeholders in strings.
Definition: StringParameterBuilder.java:32
net.sf.gridarta.utils.StringParameterBuilder.PATTERN
static final Pattern PATTERN
The Pattern for parameters.
Definition: StringParameterBuilder.java:38
net.sf.gridarta.utils.StringParameterBuilder.addParameter
void addParameter(@NotNull final String key, @NotNull final String value)
Adds a parameter key/value pair.
Definition: StringParameterBuilder.java:53
net.sf.gridarta.utils.StringParameterBuilder.values
final Map< String, String > values
Maps parameter key to value.
Definition: StringParameterBuilder.java:44
net.sf.gridarta.utils.SyntaxErrorException
Exception thrown for incorrect arguments.
Definition: SyntaxErrorException.java:28