Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored to fix 3 implementation smells #855

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,28 @@ public static ItemCollection parseTag(String xmlContent, String tag) throws Plug
return result;
}

ashishbhasin45 marked this conversation as resolved.
Show resolved Hide resolved
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 =
// 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 });
}
}
}
}

/**
* 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