Skip to content

Commit

Permalink
refactor: Apply generics to collections in XMLSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Jun 3, 2024
1 parent 18b3b96 commit 503ce71
Showing 1 changed file with 31 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class XMLSerializer {
/**
* Map of namespaces per element
*/
private Map namespacesPerElement = new TreeMap();
private final Map<String, Map<String, String>> namespacesPerElement = new TreeMap<>();
/**
* the name for an JSONObject Element
*/
Expand All @@ -124,7 +124,7 @@ public class XMLSerializer {
/**
* Map of namespaces for root element
*/
private Map rootNamespace = new TreeMap();
private final Map<String, String> rootNamespace = new TreeMap<>();
/**
* flag for skipping namespaces while reading
*/
Expand Down Expand Up @@ -168,7 +168,7 @@ public class XMLSerializer {
/**
* set of element names that forces its children elements to be in an Array
*/
private Collection<String> forcedArrayElements = new LinkedHashSet<String>();
private final Collection<String> forcedArrayElements = new LinkedHashSet<String>();
/**
* should JSON literals be parsed or not
*/
Expand Down Expand Up @@ -273,28 +273,24 @@ public void addNamespace(String prefix, String uri, String elementName) {
if (StringUtils.isBlank(elementName)) {
rootNamespace.put(prefix.trim(), uri.trim());
} else {
Map nameSpaces = (Map) namespacesPerElement.get(elementName);
if (nameSpaces == null) {
nameSpaces = new TreeMap();
namespacesPerElement.put(elementName, nameSpaces);
}
Map<String, String> nameSpaces = namespacesPerElement.computeIfAbsent(elementName, k -> new TreeMap<>());
nameSpaces.put(prefix, uri);
}
}

/**
* Returns a read-only view of the root name space.
*/
public Map getRootNamespace() {
public Map<String, String> getRootNamespace() {
return Collections.unmodifiableMap(rootNamespace);
}

/**
* Returns a read-only view of the particular element name space if found.
*/
public Map getElementNamespace(String elementName) {
public Map<String, String> getElementNamespace(String elementName) {
if (!StringUtils.isBlank(elementName)) {
Map nameSpaces = (Map) namespacesPerElement.get(elementName);
Map<String, String> nameSpaces = namespacesPerElement.get(elementName);
if (nameSpaces != null) {
return Collections.unmodifiableMap(nameSpaces);
}
Expand Down Expand Up @@ -468,15 +464,15 @@ public void setSkipWhitespace(boolean skipWhitespace) {
}

/**
* Returns whether this serializer will trim leading and trealing whitespace
* Returns whether this serializer will trim leading and trailing whitespace
* from values or not.
*/
public boolean isTrimSpaces() {
return trimSpaces;
}

/**
* Sets if this serializer will trim leading and trealing whitespace from
* Sets if this serializer will trim leading and trailing whitespace from
* values when reading.
*/
public void setTrimSpaces(boolean trimSpaces) {
Expand Down Expand Up @@ -623,7 +619,7 @@ public JSON readFromFile(String path) {
*/
public JSON readFromStream(InputStream stream) {
try {
StringBuffer xml = new StringBuffer();
StringBuilder xml = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String line = null;
while ((line = in.readLine()) != null) {
Expand Down Expand Up @@ -659,7 +655,7 @@ public void removeNamespace(String prefix, String elementName) {
if (StringUtils.isBlank(elementName)) {
rootNamespace.remove(prefix.trim());
} else {
Map nameSpaces = (Map) namespacesPerElement.get(elementName);
Map<String, String> nameSpaces = namespacesPerElement.get(elementName);
nameSpaces.remove(prefix);
}
}
Expand Down Expand Up @@ -695,11 +691,7 @@ public void setNamespace(String prefix, String uri, String elementName) {
rootNamespace.clear();
rootNamespace.put(prefix.trim(), uri.trim());
} else {
Map nameSpaces = (Map) namespacesPerElement.get(elementName);
if (nameSpaces == null) {
nameSpaces = new TreeMap();
namespacesPerElement.put(elementName, nameSpaces);
}
Map<String, String> nameSpaces = namespacesPerElement.computeIfAbsent(elementName, k -> new TreeMap<>());
nameSpaces.clear();
nameSpaces.put(prefix, uri);
}
Expand Down Expand Up @@ -813,14 +805,12 @@ private void addNameSpaceToElement(Element element) {
} else {
elementName = element.getQualifiedName();
}
Map nameSpaces = (Map) namespacesPerElement.get(elementName);
Map<String, String> nameSpaces = namespacesPerElement.get(elementName);
if (nameSpaces != null && !nameSpaces.isEmpty()) {
setNamespaceLenient(true);
for (Iterator entries = nameSpaces.entrySet()
.iterator(); entries.hasNext(); ) {
Map.Entry entry = (Map.Entry) entries.next();
String prefix = (String) entry.getKey();
String uri = (String) entry.getValue();
for (Map.Entry<String, String> entry : nameSpaces.entrySet()) {
String prefix = entry.getKey();
String uri = entry.getValue();
if (StringUtils.isBlank(prefix)) {
element.setNamespaceURI(uri);
} else {
Expand Down Expand Up @@ -1136,8 +1126,7 @@ private Object processElement(Element element, String type) {

private Element processJSONArray(JSONArray array, Element root, String[] expandableProperties) {
int l = array.size();
for (int i = 0; i < l; i++) {
Object value = array.get(i);
for (Object value : array) {
Element element = processJSONValue(value, root, null, expandableProperties);
root.appendChild(element);
}
Expand All @@ -1156,11 +1145,9 @@ private Element processJSONObject(JSONObject jsonObject, Element root,
if (isRoot) {
if (!rootNamespace.isEmpty()) {
setNamespaceLenient(true);
for (Iterator entries = rootNamespace.entrySet()
.iterator(); entries.hasNext(); ) {
Map.Entry entry = (Map.Entry) entries.next();
String prefix = (String) entry.getKey();
String uri = (String) entry.getValue();
for (Map.Entry<String, String> entry : rootNamespace.entrySet()) {
String prefix = entry.getKey();
String uri = entry.getValue();
if (StringUtils.isBlank(prefix)) {
root.setNamespaceURI(uri);
} else {
Expand All @@ -1173,10 +1160,10 @@ private Element processJSONObject(JSONObject jsonObject, Element root,
addNameSpaceToElement(root);

Object[] names = jsonObject.names().toArray();
List unprocessed = new ArrayList();
List<String> unprocessed = new ArrayList<>();
if (isSortPropertyNames()) { Arrays.sort(names); }
for (int i = 0; i < names.length; i++) {
String name = (String) names[i];
for (Object o : names) {
String name = (String) o;
Object value = jsonObject.get(name);
if (name.startsWith("@xmlns")) {
setNamespaceLenient(true);
Expand All @@ -1197,8 +1184,7 @@ private Element processJSONObject(JSONObject jsonObject, Element root,
}
}
Element element = null;
for (int i = 0; i < unprocessed.size(); i++) {
String name = (String) unprocessed.get(i);
for (String name : unprocessed) {
Object value = jsonObject.get(name);
if (name.startsWith("@")) {
int colon = name.indexOf(':');
Expand All @@ -1220,8 +1206,7 @@ private Element processJSONObject(JSONObject jsonObject, Element root,
expandableProperties, name) || (isPerformAutoExpansion && canAutoExpand((JSONArray) value)))) {
JSONArray array = (JSONArray) value;
int l = array.size();
for (int j = 0; j < l; j++) {
Object item = array.get(j);
for (Object item : array) {
element = newElement(name);
root.appendChild(element);
if (item instanceof JSONObject) {
Expand Down Expand Up @@ -1252,8 +1237,8 @@ private Element processJSONObject(JSONObject jsonObject, Element root,
* @return True if all children are objects, false otherwise.
*/
private boolean canAutoExpand(JSONArray array) {
for (int i = 0; i < array.size(); i++) {
if (!(array.get(i) instanceof JSONObject)) {
for (Object o : array) {
if (!(o instanceof JSONObject)) {
return false;
}
}
Expand Down Expand Up @@ -1552,9 +1537,7 @@ private void setValue(JSONObject jsonObject, Element element, String defaultType
private boolean isCData(Element element) {
if (element.getChildCount() == 1) {
final Node child = element.getChild(0);
if (child.toXML().startsWith("<![CDATA[")) {
return true;
}
return child.toXML().startsWith("<![CDATA[");
}
return false;
}
Expand All @@ -1564,8 +1547,7 @@ private Object simplifyValue(JSONObject parent, Object json) {
JSONObject object = (JSONObject) json;
if (parent != null) {
// remove all duplicated @xmlns from child
for (Iterator entries = parent.entrySet()
.iterator(); entries.hasNext(); ) {
for (Iterator entries = parent.entrySet().iterator(); entries.hasNext(); ) {
Map.Entry entry = (Map.Entry) entries.next();
String key = (String) entry.getKey();
Object value = entry.getValue();
Expand Down Expand Up @@ -1609,7 +1591,7 @@ private String writeDocument(Document doc, String encoding) {
}

private static class CustomElement extends Element {
private String prefix;
private final String prefix;

public CustomElement(String name) {
super(CustomElement.getName(name));
Expand Down Expand Up @@ -1651,7 +1633,7 @@ public XomSerializer(OutputStream out, String encoding) throws UnsupportedEncodi
}

private String escape(String text) {
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
final char c = text.charAt(i);
if (c < ' ') {
Expand Down

0 comments on commit 503ce71

Please sign in to comment.