mirror of
https://github.com/Maschell/JNUSLib.git
synced 2024-12-01 12:14:15 +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;
|
package de.mas.wiiu.jnus.implementations.woomy;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
@ -40,7 +42,7 @@ public final class WoomyMetaParser extends XMLParser {
|
|||||||
private WoomyMetaParser() {
|
private WoomyMetaParser() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static WoomyMeta parseMeta(InputStream data) {
|
public static WoomyMeta parseMeta(InputStream data) throws ParseException {
|
||||||
XMLParser parser = new WoomyMetaParser();
|
XMLParser parser = new WoomyMetaParser();
|
||||||
String resultName = "";
|
String resultName = "";
|
||||||
int resultIcon = 0;
|
int resultIcon = 0;
|
||||||
@ -48,25 +50,18 @@ public final class WoomyMetaParser extends XMLParser {
|
|||||||
parser.loadDocument(data);
|
parser.loadDocument(data);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.info("Error while loading the data into the WoomyMetaParser");
|
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);
|
resultName = parser.getValueOfElement(WOOMY_METADATA_NAME).orElse("");
|
||||||
if (name != null && !name.isEmpty()) {
|
|
||||||
resultName = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
String icon = parser.getValueOfElement(WOOMY_METADATA_ICON);
|
resultIcon = parser.getValueOfElementAsInt(WOOMY_METADATA_ICON).orElse(0);
|
||||||
if (icon != null && !icon.isEmpty()) {
|
|
||||||
int icon_val = Integer.parseInt(icon);
|
|
||||||
resultIcon = icon_val;
|
|
||||||
}
|
|
||||||
|
|
||||||
WoomyMeta result = new WoomyMeta(resultName, resultIcon);
|
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.getChildNodes();
|
NodeList entry_list = entries_node.get().getChildNodes();
|
||||||
for (int i = 0; i < entry_list.getLength(); i++) {
|
for (int i = 0; i < entry_list.getLength(); i++) {
|
||||||
Node node = entry_list.item(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);
|
int entry_count_val = Integer.parseInt(entry_count);
|
||||||
result.addEntry(entry_name, folder, entry_count_val);
|
result.addEntry(entry_name, folder, entry_count_val);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package de.mas.wiiu.jnus.implementations.woomy;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.text.ParseException;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Locale;
|
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();
|
WoomyInfo result = new WoomyInfo();
|
||||||
if (!woomyFile.exists()) {
|
if (!woomyFile.exists()) {
|
||||||
log.info("File does not exist." + woomyFile.getAbsolutePath());
|
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.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
@ -40,56 +41,53 @@ public class XMLParser {
|
|||||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||||
Document document = builder.parse(inputStream);
|
Document document = builder.parse(inputStream);
|
||||||
this.document = document;
|
this.document = document;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getValueOfElementAsInt(String element, int index) {
|
public Optional<Integer> getValueOfElementAsInt(String element) {
|
||||||
return Integer.parseInt(getValueOfElement(element, index));
|
return getValueOfElementAsInt(element, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getValueOfElementAsLong(String element, int index) {
|
public Optional<Integer> getValueOfElementAsInt(String element, int index) {
|
||||||
return Long.parseLong(getValueOfElement(element, 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);
|
return getValueOfElement(element, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getNodeByValue(String element) {
|
public Optional<Node> getNodeByValue(String element) {
|
||||||
return getNodeByValue(element, 0);
|
return getNodeByValue(element, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getNodeByValue(String element, int index) {
|
public Optional<Node> getNodeByValue(String element, int index) {
|
||||||
if (document == null) {
|
if (document == null) {
|
||||||
log.info("Please load the document first.");
|
log.info("Please load the document first.");
|
||||||
}
|
}
|
||||||
NodeList list = document.getElementsByTagName(element);
|
NodeList list = document.getElementsByTagName(element);
|
||||||
if (list == null) {
|
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) {
|
public Optional<String> getValueOfElementAttribute(String element, int index, String attribute) {
|
||||||
Node node = getNodeByValue(element, index);
|
return getNodeByValue(element, index).map(node -> getAttributeValueFromNode(node, attribute));
|
||||||
if (node == null) {
|
|
||||||
// log.info("Node is null");
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return getAttributeValueFromNode(node, attribute);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getAttributeValueFromNode(@NonNull Node element, String attribute) {
|
public static String getAttributeValueFromNode(@NonNull Node element, String attribute) {
|
||||||
return element.getAttributes().getNamedItem(attribute).getTextContent().toString();
|
return element.getAttributes().getNamedItem(attribute).getTextContent().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getValueOfElement(String element, int index) {
|
public Optional<String> getValueOfElement(String element, int index) {
|
||||||
Node node = getNodeByValue(element, index);
|
return getNodeByValue(element, index).map(node -> node.getTextContent().toString());
|
||||||
if (node == null) {
|
|
||||||
// log.info("Node is null");
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return node.getTextContent().toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user