Skip to content

Commit

Permalink
Stop using StringUtils (#946)
Browse files Browse the repository at this point in the history
There is no need for using StringUtils to replace or join strings.

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored Mar 10, 2021
1 parent 5012ed3 commit eb7596c
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.i18n.I18nUtil;
Expand Down Expand Up @@ -95,22 +94,21 @@ public ItemUIRegistry getItemUIRegistry() {
*/
protected String preprocessSnippet(String originalSnippet, Widget w) {
String snippet = originalSnippet;
snippet = StringUtils.replace(snippet, "%widget_id%", itemUIRegistry.getWidgetId(w));
snippet = StringUtils.replace(snippet, "%icon_type%", ICON_TYPE);
snippet = StringUtils.replace(snippet, "%item%", w.getItem() != null ? w.getItem() : "");
snippet = snippet.replace("%widget_id%", itemUIRegistry.getWidgetId(w));
snippet = snippet.replace("%icon_type%", ICON_TYPE);
snippet = snippet.replace("%item%", w.getItem() != null ? w.getItem() : "");
// Optimization: avoid calling 3 times itemUIRegistry.getLabel(w)
String text = itemUIRegistry.getLabel(w);
snippet = StringUtils.replace(snippet, "%label%", getLabel(text));
snippet = StringUtils.replace(snippet, "%value%", getValue(text));
snippet = StringUtils.replace(snippet, "%has_value%", Boolean.valueOf(hasValue(text)).toString());
snippet = StringUtils.replace(snippet, "%visibility_class%",
itemUIRegistry.getVisiblity(w) ? "" : "mdl-form__row--hidden");
snippet = snippet.replace("%label%", getLabel(text));
snippet = snippet.replace("%value%", getValue(text));
snippet = snippet.replace("%has_value%", Boolean.valueOf(hasValue(text)).toString());
snippet = snippet.replace("%visibility_class%", itemUIRegistry.getVisiblity(w) ? "" : "mdl-form__row--hidden");

String state = getState(w);
snippet = StringUtils.replace(snippet, "%state%", escapeURL(state));
snippet = snippet.replace("%state%", escapeURL(state));

String category = getCategory(w);
snippet = StringUtils.replace(snippet, "%category%", escapeURL(category));
snippet = snippet.replace("%category%", escapeURL(category));

return snippet;
}
Expand Down Expand Up @@ -263,15 +261,15 @@ protected String processColor(Widget w, String originalSnippet) {
if (color != null) {
style = "style=\"color:" + color + "\"";
}
snippet = StringUtils.replace(snippet, "%labelstyle%", style);
snippet = snippet.replace("%labelstyle%", style);

style = "";
color = itemUIRegistry.getValueColor(w);

if (color != null) {
style = "style=\"color:" + color + "\"";
}
snippet = StringUtils.replace(snippet, "%valuestyle%", style);
snippet = snippet.replace("%valuestyle%", style);

return snippet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -115,16 +114,16 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
snippet = preprocessSnippet(snippet, w);

if (chart.getRefresh() > 0) {
snippet = StringUtils.replace(snippet, "%update_interval%", Integer.toString(chart.getRefresh()));
snippet = snippet.replace("%update_interval%", Integer.toString(chart.getRefresh()));
} else {
snippet = StringUtils.replace(snippet, "%update_interval%", "0");
snippet = snippet.replace("%update_interval%", "0");
}

snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
snippet = StringUtils.replace(snippet, "%proxied_url%", chartUrl);
snippet = StringUtils.replace(snippet, "%valid_url%", "true");
snippet = StringUtils.replace(snippet, "%ignore_refresh%", ignoreRefresh ? "true" : "false");
snippet = StringUtils.replace(snippet, "%url%", url);
snippet = snippet.replace("%id%", itemUIRegistry.getWidgetId(w));
snippet = snippet.replace("%proxied_url%", chartUrl);
snippet = snippet.replace("%valid_url%", "true");
snippet = snippet.replace("%ignore_refresh%", ignoreRefresh ? "true" : "false");
snippet = snippet.replace("%url%", url);

sb.append(snippet);
} catch (ItemNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.ui.basic.internal.render;

import org.apache.commons.lang.StringUtils;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -80,27 +79,29 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
}

// Should be called before preprocessSnippet
snippet = StringUtils.replace(snippet, "%state%", hexValue);
snippet = StringUtils.replace(snippet, "%icon_state%", escapeURL(hexValue));
snippet = snippet.replace("%state%", hexValue);
snippet = snippet.replace("%icon_state%", escapeURL(hexValue));

snippet = preprocessSnippet(snippet, w);
snippet = StringUtils.replace(snippet, "%purelabel%", purelabel);
snippet = StringUtils.replace(snippet, "%frequency%", frequency);
snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);
if (purelabel != null) {
snippet = snippet.replace("%purelabel%", purelabel);
}
snippet = snippet.replace("%frequency%", frequency);
snippet = snippet.replace("%servletname%", WebAppServlet.SERVLET_NAME);

String style = "";
String color = itemUIRegistry.getLabelColor(w);
if (color != null) {
style = "color:" + color;
}
snippet = StringUtils.replace(snippet, "%labelstyle%", style);
snippet = snippet.replace("%labelstyle%", style);

style = "";
color = itemUIRegistry.getValueColor(w);
if (color != null) {
style = "color:" + color;
}
snippet = StringUtils.replace(snippet, "%valuestyle%", style);
snippet = snippet.replace("%valuestyle%", style);

sb.append(snippet);
return ECollections.emptyEList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.i18n.LocaleProvider;
Expand Down Expand Up @@ -59,8 +58,8 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
String label = StringEscapeUtils.escapeHtml(itemUIRegistry.getLabel(w));
List<String> frameClassList = new ArrayList<>();

snippet = StringUtils.replace(snippet, "%label%", label);
snippet = StringUtils.replace(snippet, "%widget_id%", itemUIRegistry.getWidgetId(w));
snippet = snippet.replace("%label%", label);
snippet = snippet.replace("%widget_id%", itemUIRegistry.getWidgetId(w));

if (label.isEmpty()) {
frameClassList.add("mdl-form--no-label");
Expand All @@ -70,8 +69,8 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
frameClassList.add("mdl-form--hidden");
}

String frameClass = StringUtils.join(frameClassList, ' ');
snippet = StringUtils.replace(snippet, "%frame_class%", frameClass);
String frameClass = String.join(" ", frameClassList);
snippet = snippet.replace("%frame_class%", frameClass);

// Process the color tags
snippet = processColor(w, snippet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.ui.basic.internal.render;

import org.apache.commons.lang.StringUtils;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -55,7 +54,7 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
String snippet = getSnippet("group");

snippet = preprocessSnippet(snippet, w);
snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
snippet = snippet.replace("%id%", itemUIRegistry.getWidgetId(w));

// Process the color tags
snippet = processColor(w, snippet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -63,13 +62,13 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
String snippet = (image.getChildren().size() > 0) ? getSnippet("image_link") : getSnippet("image");

if (image.getRefresh() > 0) {
snippet = StringUtils.replace(snippet, "%update_interval%", Integer.toString(image.getRefresh()));
snippet = snippet.replace("%update_interval%", Integer.toString(image.getRefresh()));
} else {
snippet = StringUtils.replace(snippet, "%update_interval%", "0");
snippet = snippet.replace("%update_interval%", "0");
}

String widgetId = itemUIRegistry.getWidgetId(w);
snippet = StringUtils.replace(snippet, "%id%", widgetId);
snippet = snippet.replace("%id%", widgetId);
snippet = preprocessSnippet(snippet, w);

boolean validUrl = isValidURL(image.getUrl());
Expand All @@ -90,10 +89,10 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
url = URL_NONE_ICON;
ignoreRefresh = true;
}
snippet = StringUtils.replace(snippet, "%valid_url%", validUrl ? "true" : "false");
snippet = StringUtils.replace(snippet, "%proxied_url%", proxiedUrl);
snippet = StringUtils.replace(snippet, "%ignore_refresh%", ignoreRefresh ? "true" : "false");
snippet = StringUtils.replace(snippet, "%url%", url);
snippet = snippet.replace("%valid_url%", validUrl ? "true" : "false");
snippet = snippet.replace("%proxied_url%", proxiedUrl);
snippet = snippet.replace("%ignore_refresh%", ignoreRefresh ? "true" : "false");
snippet = snippet.replace("%url%", url);

sb.append(snippet);
return ECollections.emptyEList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.ui.basic.internal.render;

import org.apache.commons.lang.StringUtils;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -63,11 +62,11 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th

if (state != null) {
for (String row : state.toString().split(((List) w).getSeparator())) {
rowSB.append(StringUtils.replace(rowSnippet, "%title%", escapeHtml(row)));
rowSB.append(rowSnippet.replace("%title%", escapeHtml(row)));
}
}

snippet = StringUtils.replace(snippet, "%rows%", rowSB.toString());
snippet = snippet.replace("%rows%", rowSB.toString());

// Process the color tags
snippet = processColor(w, snippet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.ui.basic.internal.render;

import org.apache.commons.lang.StringUtils;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -67,26 +66,26 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
PointType pointState = (PointType) state;
double latitude = pointState.getLatitude().doubleValue();
double longitude = pointState.getLongitude().doubleValue();
snippet = StringUtils.replace(snippet, "%url%", MAP_URL);
snippet = StringUtils.replace(snippet, "%lat%", Double.toString(latitude));
snippet = StringUtils.replace(snippet, "%lon%", Double.toString(longitude));
snippet = StringUtils.replace(snippet, "%lonminus%", Double.toString(longitude - MAP_ZOOM));
snippet = StringUtils.replace(snippet, "%lonplus%", Double.toString(longitude + MAP_ZOOM));
snippet = StringUtils.replace(snippet, "%latminus%", Double.toString(latitude - MAP_ZOOM));
snippet = StringUtils.replace(snippet, "%latplus%", Double.toString(latitude + MAP_ZOOM));
snippet = snippet.replace("%url%", MAP_URL);
snippet = snippet.replace("%lat%", Double.toString(latitude));
snippet = snippet.replace("%lon%", Double.toString(longitude));
snippet = snippet.replace("%lonminus%", Double.toString(longitude - MAP_ZOOM));
snippet = snippet.replace("%lonplus%", Double.toString(longitude + MAP_ZOOM));
snippet = snippet.replace("%latminus%", Double.toString(latitude - MAP_ZOOM));
snippet = snippet.replace("%latplus%", Double.toString(latitude + MAP_ZOOM));
} else {
snippet = StringUtils.replace(snippet, "%url%", "images/map-marker-off.png");
snippet = snippet.replace("%url%", "images/map-marker-off.png");
}

snippet = StringUtils.replace(snippet, "%map_url%", MAP_URL);
snippet = StringUtils.replace(snippet, "%map_zoom%", Double.toString(MAP_ZOOM));
snippet = snippet.replace("%map_url%", MAP_URL);
snippet = snippet.replace("%map_zoom%", Double.toString(MAP_ZOOM));

int height = mapview.getHeight();
if (height == 0) {
height = 4; // set default height to something viewable
}
height = height * 36;
snippet = StringUtils.replace(snippet, "%height%", Integer.toString(height));
snippet = snippet.replace("%height%", Integer.toString(height));

sb.append(snippet);
return ECollections.emptyEList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
Expand Down Expand Up @@ -109,12 +108,12 @@ public StringBuilder processPage(String id, String sitemap, String label, EList<
if (labelPlain.contains("[") && labelPlain.endsWith("]")) {
labelPlain = labelPlain.replace("[", "").replace("]", "");
}
snippet = StringUtils.replace(snippet, "%label%", escapeHtml(labelPlain));
snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);
snippet = StringUtils.replace(snippet, "%sitemap%", sitemap);
snippet = StringUtils.replace(snippet, "%htmlclass%", config.getCssClassList());
snippet = StringUtils.replace(snippet, "%icon_type%", ICON_TYPE);
snippet = StringUtils.replace(snippet, "%theme%", config.getTheme());
snippet = snippet.replace("%label%", escapeHtml(labelPlain));
snippet = snippet.replace("%servletname%", WebAppServlet.SERVLET_NAME);
snippet = snippet.replace("%sitemap%", sitemap);
snippet = snippet.replace("%htmlclass%", config.getCssClassList());
snippet = snippet.replace("%icon_type%", ICON_TYPE);
snippet = snippet.replace("%theme%", config.getTheme());

String[] parts = snippet.split("%children%");

Expand All @@ -139,9 +138,9 @@ private void processChildren(StringBuilder sb_pre, StringBuilder sb_post, EList<
if (!(firstChild instanceof Frame || parent instanceof Frame || parent instanceof Sitemap
|| parent instanceof org.openhab.core.model.sitemap.sitemap.List)) {
String frameSnippet = getSnippet("frame");
frameSnippet = StringUtils.replace(frameSnippet, "%widget_id%", "");
frameSnippet = StringUtils.replace(frameSnippet, "%label%", "");
frameSnippet = StringUtils.replace(frameSnippet, "%frame_class%", "mdl-form--no-label");
frameSnippet = frameSnippet.replace("%widget_id%", "");
frameSnippet = frameSnippet.replace("%label%", "");
frameSnippet = frameSnippet.replace("%frame_class%", "mdl-form--no-label");

String[] parts = frameSnippet.split("%children%");
if (parts.length > 1) {
Expand Down Expand Up @@ -230,26 +229,33 @@ public CharSequence renderSitemapList(Set<SitemapProvider> sitemapProviders) thr
StringBuilder sb = new StringBuilder();
if (sitemapList.isEmpty()) {
String listEmptySnippet = getSnippet("sitemaps_list_empty");
listEmptySnippet = StringUtils.replace(listEmptySnippet, "%sitemaps-list-empty.info%",
localizeText("@text/sitemaps-list-empty.info"));
String text = localizeText("@text/sitemaps-list-empty.info");
if (text != null) {
listEmptySnippet = listEmptySnippet.replace("%sitemaps-list-empty.info%", text);
}
sb.append(listEmptySnippet);
} else {
for (String sitemap : sitemapList) {
sb.append(StringUtils.replace(sitemapSnippet, "%sitemap%", sitemap));
sb.append(sitemapSnippet.replace("%sitemap%", sitemap));
}
}

listSnippet = StringUtils.replace(listSnippet, "%sitemaps-list.welcome%",
localizeText("@text/sitemaps-list.welcome"));
listSnippet = StringUtils.replace(listSnippet, "%sitemaps-list.available-sitemaps%",
localizeText("@text/sitemaps-list.available-sitemaps"));
listSnippet = StringUtils.replace(listSnippet, "%items%", sb.toString());
String text = localizeText("@text/sitemaps-list.welcome");
if (text != null) {
listSnippet = listSnippet.replace("%sitemaps-list.welcome%", text);
}

text = localizeText("@text/sitemaps-list.available-sitemaps");
if (text != null) {
listSnippet = listSnippet.replace("%sitemaps-list.available-sitemaps%", text);
}

listSnippet = listSnippet.replace("%items%", sb.toString());

pageSnippet = StringUtils.replace(pageSnippet, "%title%", "BasicUI");
pageSnippet = StringUtils.replace(pageSnippet, "%htmlclass%",
config.getCssClassList() + " page-welcome-sitemaps");
pageSnippet = StringUtils.replace(pageSnippet, "%theme%", config.getTheme());
pageSnippet = StringUtils.replace(pageSnippet, "%content%", listSnippet);
pageSnippet = pageSnippet.replace("%title%", "BasicUI");
pageSnippet = pageSnippet.replace("%htmlclass%", config.getCssClassList() + " page-welcome-sitemaps");
pageSnippet = pageSnippet.replace("%theme%", config.getTheme());
pageSnippet = pageSnippet.replace("%content%", listSnippet);

return pageSnippet;
}
Expand Down
Loading

0 comments on commit eb7596c

Please sign in to comment.