Gridarta Editor
HtmlUtils.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 org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
24 
28 public class HtmlUtils {
29 
33  private HtmlUtils() {
34  }
35 
42  @Nullable
43  public static String encodeOptional(@Nullable final String str) {
44  return str == null ? null : encode(str);
45  }
46 
52  @NotNull
53  public static String encode(@NotNull final String str) {
54  final StringBuilder sb = new StringBuilder(str.length());
55  for (final char ch : str.toCharArray()) {
56  switch (ch) {
57  case '<':
58  sb.append("&lt;");
59  break;
60 
61  case '>':
62  sb.append("&gt;");
63  break;
64 
65  case '&':
66  sb.append("&amp;");
67  break;
68 
69  default:
70  sb.append(ch);
71  break;
72  }
73  }
74  return sb.toString();
75  }
76 
77 }
net.sf.gridarta.utils.HtmlUtils
Utility class for HTML related functions.
Definition: HtmlUtils.java:28
net.sf.gridarta.utils.HtmlUtils.encode
static String encode(@NotNull final String str)
Encodes a string so that the result can be embedded into HTML.
Definition: HtmlUtils.java:53
net.sf.gridarta.utils.HtmlUtils.encodeOptional
static String encodeOptional(@Nullable final String str)
Encodes an optional string so that the result can be embedded into HTML.
Definition: HtmlUtils.java:43
net.sf.gridarta.utils.HtmlUtils.HtmlUtils
HtmlUtils()
Private constructor to prevent instantiation.
Definition: HtmlUtils.java:33