Update the XMLParser to return Optionals instead of null values.

This commit is contained in:
Maschell 2019-04-10 17:56:36 +02:00
parent e3dbf81a8e
commit 9cc1a8c948
3 changed files with 42 additions and 47 deletions

View File

@ -17,6 +17,8 @@
package de.mas.wiiu.jnus.implementations.woomy;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Optional;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@ -40,7 +42,7 @@ public final class WoomyMetaParser extends XMLParser {
private WoomyMetaParser() {
}
public static WoomyMeta parseMeta(InputStream data) {
public static WoomyMeta parseMeta(InputStream data) throws ParseException {
XMLParser parser = new WoomyMetaParser();
String resultName = "";
int resultIcon = 0;
@ -48,25 +50,18 @@ public final class WoomyMetaParser extends XMLParser {
parser.loadDocument(data);
} catch (Exception e) {
log.info("Error while loading the data into the WoomyMetaParser");
return null;
throw new ParseException("Error while loading the data into the WoomyMetaParser", 0);
}
String name = parser.getValueOfElement(WOOMY_METADATA_NAME);
if (name != null && !name.isEmpty()) {
resultName = name;
}
resultName = parser.getValueOfElement(WOOMY_METADATA_NAME).orElse("");
String icon = parser.getValueOfElement(WOOMY_METADATA_ICON);
if (icon != null && !icon.isEmpty()) {
int icon_val = Integer.parseInt(icon);
resultIcon = icon_val;
}
resultIcon = parser.getValueOfElementAsInt(WOOMY_METADATA_ICON).orElse(0);
WoomyMeta result = new WoomyMeta(resultName, resultIcon);
Node entries_node = parser.getNodeByValue(WOOMY_METADATA_ENTRIES);
NodeList entry_list = entries_node.getChildNodes();
Optional<Node> entries_node = parser.getNodeByValue(WOOMY_METADATA_ENTRIES);
if (entries_node.isPresent()) {
NodeList entry_list = entries_node.get().getChildNodes();
for (int i = 0; i < entry_list.getLength(); i++) {
Node node = entry_list.item(i);
@ -76,6 +71,7 @@ public final class WoomyMetaParser extends XMLParser {
int entry_count_val = Integer.parseInt(entry_count);
result.addEntry(entry_name, folder, entry_count_val);
}
}
return result;
}

View File

@ -18,6 +18,7 @@ package de.mas.wiiu.jnus.implementations.woomy;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
@ -48,7 +49,7 @@ public final class WoomyParser {
//
}
public static WoomyInfo createWoomyInfo(File woomyFile) throws IOException, ParserConfigurationException, SAXException {
public static WoomyInfo createWoomyInfo(File woomyFile) throws IOException, ParserConfigurationException, SAXException, ParseException {
WoomyInfo result = new WoomyInfo();
if (!woomyFile.exists()) {
log.info("File does not exist." + woomyFile.getAbsolutePath());

View File

@ -18,6 +18,7 @@ package de.mas.wiiu.jnus.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -40,56 +41,53 @@ public class XMLParser {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inputStream);
this.document = document;
}
public long getValueOfElementAsInt(String element, int index) {
return Integer.parseInt(getValueOfElement(element, index));
public Optional<Integer> getValueOfElementAsInt(String element) {
return getValueOfElementAsInt(element, 0);
}
public long getValueOfElementAsLong(String element, int index) {
return Long.parseLong(getValueOfElement(element, index));
public Optional<Integer> getValueOfElementAsInt(String element, int index) {
return getValueOfElement(element, index).map(intStr -> Integer.parseInt(intStr));
}
public String getValueOfElement(String element) {
public Optional<Long> getValueOfElementAsLong(String element, int index) {
return getValueOfElement(element, index).map(longStr -> Long.parseLong(longStr));
}
public Optional<String> getValueOfElement(String element) {
return getValueOfElement(element, 0);
}
public Node getNodeByValue(String element) {
public Optional<Node> getNodeByValue(String element) {
return getNodeByValue(element, 0);
}
public Node getNodeByValue(String element, int index) {
public Optional<Node> getNodeByValue(String element, int index) {
if (document == null) {
log.info("Please load the document first.");
}
NodeList list = document.getElementsByTagName(element);
if (list == null) {
return null;
return Optional.empty();
}
return list.item(index);
Node res = list.item(index);
if (res == null) {
return Optional.empty();
}
return Optional.of(res);
}
public String getValueOfElementAttribute(String element, int index, String attribute) {
Node node = getNodeByValue(element, index);
if (node == null) {
// log.info("Node is null");
return "";
}
return getAttributeValueFromNode(node, attribute);
public Optional<String> getValueOfElementAttribute(String element, int index, String attribute) {
return getNodeByValue(element, index).map(node -> getAttributeValueFromNode(node, attribute));
}
public static String getAttributeValueFromNode(@NonNull Node element, String attribute) {
return element.getAttributes().getNamedItem(attribute).getTextContent().toString();
}
public String getValueOfElement(String element, int index) {
Node node = getNodeByValue(element, index);
if (node == null) {
// log.info("Node is null");
return "";
public Optional<String> getValueOfElement(String element, int index) {
return getNodeByValue(element, index).map(node -> node.getTextContent().toString());
}
return node.getTextContent().toString();
}
}