mirror of
https://github.com/Maschell/JNUSLib.git
synced 2024-11-25 17:36:55 +01:00
Update the XMLParser to return Optionals instead of null values.
This commit is contained in:
parent
e3dbf81a8e
commit
9cc1a8c948
@ -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,33 +50,27 @@ 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);
|
||||
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);
|
||||
|
||||
NodeList entry_list = entries_node.getChildNodes();
|
||||
for (int i = 0; i < entry_list.getLength(); i++) {
|
||||
Node node = entry_list.item(i);
|
||||
|
||||
String folder = getAttributeValueFromNode(node, WOOMY_METADATA_ENTRY_FOLDER);
|
||||
String entry_name = getAttributeValueFromNode(node, WOOMY_METADATA_ENTRY_NAME);
|
||||
String entry_count = getAttributeValueFromNode(node, WOOMY_METADATA_ENTRY_ENTRIES);
|
||||
int entry_count_val = Integer.parseInt(entry_count);
|
||||
result.addEntry(entry_name, folder, entry_count_val);
|
||||
String folder = getAttributeValueFromNode(node, WOOMY_METADATA_ENTRY_FOLDER);
|
||||
String entry_name = getAttributeValueFromNode(node, WOOMY_METADATA_ENTRY_NAME);
|
||||
String entry_count = getAttributeValueFromNode(node, WOOMY_METADATA_ENTRY_ENTRIES);
|
||||
int entry_count_val = Integer.parseInt(entry_count);
|
||||
result.addEntry(entry_name, folder, entry_count_val);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -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());
|
||||
|
@ -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 "";
|
||||
}
|
||||
|
||||
return node.getTextContent().toString();
|
||||
public Optional<String> getValueOfElement(String element, int index) {
|
||||
return getNodeByValue(element, index).map(node -> node.getTextContent().toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user