Skip to content

Commit

Permalink
Merge pull request #855 from ashishbhasin45/refactor/remove-implement…
Browse files Browse the repository at this point in the history
…ation-smells

Refactored to fix 3 implementation smells
  • Loading branch information
rsoika committed Apr 2, 2024
2 parents 66c9748 + c31fe2d commit dec3110
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,32 +358,14 @@ public static ItemCollection parseTag(String xmlContent, String tag) throws Plug

// we expect the tag name as the root tag of the xml structure!
if (node != null && node.getNodeName().equals(tag)) {
// collect all child nodes...
DocumentFragment docfrag = doc.createDocumentFragment();
while (node.hasChildNodes()) {
docfrag.appendChild(node.removeChild(node.getFirstChild()));
}

// append all items into the evalItemCollection...
NodeList childs = docfrag.getChildNodes();
int itemCount = childs.getLength();
for (int i = 0; i < itemCount; i++) {
Node childNode = childs.item(i);
if (childNode instanceof Element && childNode.getFirstChild() != null) {
String name = childNode.getNodeName();
// String value =
// childNode.getFirstChild().getNodeValue();
String value = innerXml(childNode);

result.appendItemValue(name, value);
// result.replaceItemValue(name, value);
if (debug) {
logger.log(Level.FINEST, "......parsing item ''{0}'' value={1}",
new Object[] { name, value });
}
}
}

NodeList children = docfrag.getChildNodes();
parseAndAppendChildNodes(children, result, debug);
}

} catch (ParserConfigurationException | TransformerFactoryConfigurationError | SAXException
Expand All @@ -396,6 +378,29 @@ public static ItemCollection parseTag(String xmlContent, String tag) throws Plug
return result;
}

/**
* This helper method parses all child elements and creates / or appends the tag value as an Item value into the
result ItemCollection.
* The method is called from parseTag.
**/
private static void parseAndAppendChildNodes(NodeList children, ItemCollection result, boolean debug){
// collect all child nodes...
int itemCount = children.getLength();
for (int i = 0; i < itemCount; i++) {
Node childNode = children.item(i);
if (childNode instanceof Element && childNode.getFirstChild() != null) {
String name = childNode.getNodeName();
String value = innerXml(childNode);
result.appendItemValue(name, value);

if (debug) {
logger.log(Level.FINEST, "......parsing item ''{0}'' value={1}",
new Object[] { name, value });
}
}
}
}

/**
* This method parses the xml content and returns a list of
* ItemCollection elements for each tag with the given tag name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ private String stripNonValidXMLCharacters(String itemValue) {
for (int i = 0; i < itemValue.length(); i++) {
current = itemValue.charAt(i); // NOTE: No IndexOutOfBoundsException caught here; it should
// not happen.
if ((current == 0x9) || (current == 0xA) || (current == 0xD) || ((current >= 0x20) && (current <= 0xD7FF))
|| ((current >= 0xE000) && (current <= 0xFFFD))
|| ((current >= 0x10000) && (current <= 0x10FFFF))) {
if (isValidXmlCharacter(current)) {
out.append(current);
} else {
logger.log(Level.WARNING, "invalid xml character at position {0} in item ''{1}''", new Object[]{i, name});
Expand All @@ -181,6 +179,17 @@ private String stripNonValidXMLCharacters(String itemValue) {
return out.toString();
}

/**
* checks if current character is a valid xml character
* @param current character to check
* @return boolean based on character match
*/
private boolean isValidXmlCharacter(char current){
return current == 0x9 || current == 0xA || current == 0xD || (current >= 0x20 && current <= 0xD7FF)
|| (current >= 0xE000 && current <= 0xFFFD)
|| (current >= 0x10000 && current <= 0x10FFFF);
}

/**
* This method returns a transformed version of the XMLItem value array.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,9 @@ private ItemCollection updateReport(ItemCollection newReport, ItemCollection old
while (iter.hasNext()) {
Map.Entry mapEntry = (Map.Entry) iter.next();
String sName = mapEntry.getKey().toString();
Object o = mapEntry.getValue();
Object currentReportItemValue = mapEntry.getValue();
if (isValidAttributeName(sName)) {
oldReport.replaceItemValue(sName, o);
oldReport.replaceItemValue(sName, currentReportItemValue);
}
}
return oldReport;
Expand Down

0 comments on commit dec3110

Please sign in to comment.