Skip to content

Commit

Permalink
Remove some usages of StringUtils (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Apr 26, 2024
1 parent 7001fc2 commit 518da7a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/main/java/net/sf/json/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import net.sf.json.util.PropertyFilter;
import net.sf.json.util.PropertySetStrategy;
import org.apache.commons.collections.map.MultiKeyMap;
import org.apache.commons.lang.StringUtils;

/**
* Utility class that helps configuring the serialization process.
Expand Down Expand Up @@ -604,7 +603,7 @@ public Collection getMergedExcludes() {
Collection exclusions = new HashSet();
for (int i = 0; i < excludes.length; i++) {
String exclusion = excludes[i];
if (!StringUtils.isBlank(excludes[i])) {
if (exclusion != null && !exclusion.isBlank()) {
exclusions.add(exclusion.trim());
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/sf/json/test/JSONAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.util.JSONUtils;
import org.apache.commons.lang.StringUtils;

/**
* Provides assertions on equality for JSON strings and JSON types.
Expand Down Expand Up @@ -388,7 +387,7 @@ private static String missingExpectedNames(JSONObject expected, JSONObject actua
if (keysInExpectedButNotInActual.isEmpty()) {
return null;
} else {
return "missing expected names: [" + StringUtils.join(keysInExpectedButNotInActual, ", ") + "]";
return "missing expected names: [" + String.join(", ", keysInExpectedButNotInActual) + "]";
}
}

Expand All @@ -398,7 +397,7 @@ private static String unexpectedNames(JSONObject expected, JSONObject actual) {
if (keysInActualButNotInExpected.isEmpty()) {
return null;
} else {
return "unexpected names: [" + StringUtils.join(keysInActualButNotInExpected, ", ") + "]";
return "unexpected names: [" + String.join(", ", keysInActualButNotInExpected) + "]";
}
}

Expand Down
48 changes: 26 additions & 22 deletions src/main/java/net/sf/json/xml/XMLSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ public void addNamespace(String prefix, String uri) {
* @param elementName name of target element
*/
public void addNamespace(String prefix, String uri, String elementName) {
if (StringUtils.isBlank(uri)) {
if (uri == null || uri.isBlank()) {
return;
}
if (prefix == null) {
prefix = "";
}
if (StringUtils.isBlank(elementName)) {
if (elementName == null || elementName.isBlank()) {
rootNamespace.put(prefix.trim(), uri.trim());
} else {
Map nameSpaces = (Map) namespacesPerElement.get(elementName);
Expand Down Expand Up @@ -195,7 +195,7 @@ public void clearNamespaces() {
* @param elementName name of target element
*/
public void clearNamespaces(String elementName) {
if (StringUtils.isBlank(elementName)) {
if (elementName == null || elementName.isBlank()) {
rootNamespace.clear();
} else {
namespacesPerElement.remove(elementName);
Expand Down Expand Up @@ -412,7 +412,7 @@ public void removeNamespace(String prefix, String elementName) {
if (prefix == null) {
prefix = "";
}
if (StringUtils.isBlank(elementName)) {
if (elementName == null || elementName.isBlank()) {
rootNamespace.remove(prefix.trim());
} else {
Map nameSpaces = (Map) namespacesPerElement.get(elementName);
Expand All @@ -425,15 +425,15 @@ public void removeNamespace(String prefix, String elementName) {
* Default is 'a'.
*/
public void setArrayName(String arrayName) {
this.arrayName = StringUtils.isBlank(arrayName) ? "a" : arrayName;
this.arrayName = arrayName == null || arrayName.isBlank() ? "a" : arrayName;
}

/**
* Sets the name used for JSONArray elements.<br>
* Default is 'e'.
*/
public void setElementName(String elementName) {
this.elementName = StringUtils.isBlank(elementName) ? "e" : elementName;
this.elementName = elementName == null || elementName.isBlank() ? "e" : elementName;
}

/**
Expand Down Expand Up @@ -468,13 +468,13 @@ public void setNamespace(String prefix, String uri) {
* @param elementName name of target element
*/
public void setNamespace(String prefix, String uri, String elementName) {
if (StringUtils.isBlank(uri)) {
if (uri == null || uri.isBlank()) {
return;
}
if (prefix == null) {
prefix = "";
}
if (StringUtils.isBlank(elementName)) {
if (elementName == null || elementName.isBlank()) {
rootNamespace.clear();
rootNamespace.put(prefix.trim(), uri.trim());
} else {
Expand All @@ -500,7 +500,7 @@ public void setNamespaceLenient(boolean namespaceLenient) {
* Default is 'o'.
*/
public void setObjectName(String objectName) {
this.objectName = StringUtils.isBlank(objectName) ? "o" : objectName;
this.objectName = objectName == null || objectName.isBlank() ? "o" : objectName;
}

/**
Expand All @@ -515,7 +515,7 @@ public void setRemoveNamespacePrefixFromElements(boolean removeNamespacePrefixFr
* Sets the name used for the root element.
*/
public void setRootName(String rootName) {
this.rootName = StringUtils.isBlank(rootName) ? null : rootName;
this.rootName = rootName == null || rootName.isBlank() ? null : rootName;
}

/**
Expand Down Expand Up @@ -631,7 +631,7 @@ private void addNameSpaceToElement(Element element) {
Map.Entry entry = (Map.Entry) entries.next();
String prefix = (String) entry.getKey();
String uri = (String) entry.getValue();
if (StringUtils.isBlank(prefix)) {
if (prefix == null || prefix.isBlank()) {
element.setNamespaceURI(uri);
} else {
element.addNamespaceDeclaration(prefix, uri);
Expand Down Expand Up @@ -667,7 +667,8 @@ private boolean checkChildElements(Element element, boolean isTopLevel) {
Node node = element.getChild(i);
if (node instanceof Text) {
Text text = (Text) node;
if (StringUtils.isNotBlank(StringUtils.strip(text.getValue())) && !skipWhitespace) {
String stripped = text.getValue() != null ? text.getValue().strip() : null;
if (stripped != null && !stripped.isBlank() && !skipWhitespace) {
return false;
}
}
Expand Down Expand Up @@ -738,7 +739,7 @@ private boolean hasNamespaces(Element element) {
for (int i = 0; i < element.getNamespaceDeclarationCount(); i++) {
String prefix = element.getNamespacePrefix(i);
String uri = element.getNamespaceURI(prefix);
if (StringUtils.isBlank(uri)) {
if (uri == null || uri.isBlank()) {
continue;
}
namespaces++;
Expand Down Expand Up @@ -768,7 +769,7 @@ private boolean isArray(Element element, boolean isTopLevel) {
for (int j = 0; j < element.getNamespaceDeclarationCount(); j++) {
String prefix = element.getNamespacePrefix(j);
String uri = element.getNamespaceURI(prefix);
if (!StringUtils.isBlank(uri)) {
if (uri != null && !uri.isBlank()) {
return false;
}
}
Expand Down Expand Up @@ -876,7 +877,8 @@ private JSON processArrayElement(Element element, String defaultType) {
Node child = element.getChild(i);
if (child instanceof Text) {
Text text = (Text) child;
if (StringUtils.isNotBlank(StringUtils.strip(text.getValue()))) {
String stripped = text.getValue() != null ? text.getValue().strip() : null;
if (stripped != null && !stripped.isBlank()) {
jsonArray.element(text.getValue());
}
} else if (child instanceof Element) {
Expand Down Expand Up @@ -924,7 +926,7 @@ private Element processJSONObject(
Map.Entry entry = (Map.Entry) entries.next();
String prefix = (String) entry.getKey();
String uri = (String) entry.getValue();
if (StringUtils.isBlank(prefix)) {
if (prefix == null || prefix.isBlank()) {
root.setNamespaceURI(uri);
} else {
root.addNamespaceDeclaration(prefix, uri);
Expand All @@ -946,12 +948,13 @@ private Element processJSONObject(
int colon = name.indexOf(':');
if (colon == -1) {
// do not override if already defined by nameSpaceMaps
if (StringUtils.isBlank(root.getNamespaceURI())) {
if (root.getNamespaceURI() == null || root.getNamespaceURI().isBlank()) {
root.setNamespaceURI(String.valueOf(value));
}
} else {
String prefix = name.substring(colon + 1);
if (StringUtils.isBlank(root.getNamespaceURI(prefix))) {
if (root.getNamespaceURI(prefix) == null
|| root.getNamespaceURI(prefix).isBlank()) {
root.addNamespaceDeclaration(prefix, String.valueOf(value));
}
}
Expand Down Expand Up @@ -1051,10 +1054,10 @@ private JSON processObjectElement(Element element, String defaultType) {
for (int j = 0; j < element.getNamespaceDeclarationCount(); j++) {
String prefix = element.getNamespacePrefix(j);
String uri = element.getNamespaceURI(prefix);
if (StringUtils.isBlank(uri)) {
if (uri == null || uri.isBlank()) {
continue;
}
if (!StringUtils.isBlank(prefix)) {
if (prefix != null && !prefix.isBlank()) {
prefix = ":" + prefix;
}
setOrAccumulate(jsonObject, "@xmlns" + prefix, trimSpaceFromValue(uri));
Expand All @@ -1081,7 +1084,8 @@ private JSON processObjectElement(Element element, String defaultType) {
Node child = element.getChild(i);
if (child instanceof Text) {
Text text = (Text) child;
if (StringUtils.isNotBlank(StringUtils.strip(text.getValue()))) {
String stripped = text.getValue() != null ? text.getValue().strip() : null;
if (stripped != null && !stripped.isBlank()) {
setOrAccumulate(jsonObject, "#text", trimSpaceFromValue(text.getValue()));
}
} else if (child instanceof Element) {
Expand Down Expand Up @@ -1390,7 +1394,7 @@ protected void writeEndTag(Element element) throws IOException {

@Override
protected void writeNamespaceDeclaration(String prefix, String uri) throws IOException {
if (!StringUtils.isBlank(uri)) {
if (uri != null && !uri.isBlank()) {
super.writeNamespaceDeclaration(prefix, uri);
}
}
Expand Down

0 comments on commit 518da7a

Please sign in to comment.