2018-03-10 12:22:10 +01:00
|
|
|
/****************************************************************************
|
2019-04-10 20:13:38 +02:00
|
|
|
* Copyright (C) 2016-2019 Maschell
|
2018-03-10 12:22:10 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
2017-05-17 19:20:42 +02:00
|
|
|
package de.mas.wiiu.jnus.utils;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2019-04-10 17:56:36 +02:00
|
|
|
import java.util.Optional;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
|
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Node;
|
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
import lombok.NonNull;
|
|
|
|
import lombok.extern.java.Log;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
@Log
|
2016-12-12 21:01:12 +01:00
|
|
|
public class XMLParser {
|
|
|
|
private Document document;
|
2018-03-10 12:22:10 +01:00
|
|
|
|
|
|
|
public void loadDocument(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
|
2016-12-12 21:01:12 +01:00
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
|
Document document = builder.parse(inputStream);
|
|
|
|
this.document = document;
|
2019-04-10 17:56:36 +02:00
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
|
2019-04-10 17:56:36 +02:00
|
|
|
public Optional<Integer> getValueOfElementAsInt(String element) {
|
|
|
|
return getValueOfElementAsInt(element, 0);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
|
2019-04-10 17:56:36 +02:00
|
|
|
public Optional<Integer> getValueOfElementAsInt(String element, int index) {
|
|
|
|
return getValueOfElement(element, index).map(intStr -> Integer.parseInt(intStr));
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
|
2019-04-10 17:56:36 +02:00
|
|
|
public Optional<Long> getValueOfElementAsLong(String element, int index) {
|
|
|
|
return getValueOfElement(element, index).map(longStr -> Long.parseLong(longStr));
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
|
2019-04-10 17:56:36 +02:00
|
|
|
public Optional<String> getValueOfElement(String element) {
|
2018-03-10 12:22:10 +01:00
|
|
|
return getValueOfElement(element, 0);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
|
2019-04-10 17:56:36 +02:00
|
|
|
public Optional<Node> getNodeByValue(String element) {
|
2016-12-12 21:01:12 +01:00
|
|
|
return getNodeByValue(element, 0);
|
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
|
2019-04-10 17:56:36 +02:00
|
|
|
public Optional<Node> getNodeByValue(String element, int index) {
|
2018-03-10 12:22:10 +01:00
|
|
|
if (document == null) {
|
2017-05-17 19:20:42 +02:00
|
|
|
log.info("Please load the document first.");
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
NodeList list = document.getElementsByTagName(element);
|
|
|
|
if (list == null) {
|
2019-04-10 17:56:36 +02:00
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
Node res = list.item(index);
|
|
|
|
if (res == null) {
|
|
|
|
return Optional.empty();
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2019-04-10 17:56:36 +02:00
|
|
|
return Optional.of(res);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
|
2019-04-10 17:56:36 +02:00
|
|
|
public Optional<String> getValueOfElementAttribute(String element, int index, String attribute) {
|
|
|
|
return getNodeByValue(element, index).map(node -> getAttributeValueFromNode(node, attribute));
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
|
|
|
|
public static String getAttributeValueFromNode(@NonNull Node element, String attribute) {
|
2016-12-12 21:01:12 +01:00
|
|
|
return element.getAttributes().getNamedItem(attribute).getTextContent().toString();
|
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
|
2019-04-10 17:56:36 +02:00
|
|
|
public Optional<String> getValueOfElement(String element, int index) {
|
|
|
|
return getNodeByValue(element, index).map(node -> node.getTextContent().toString());
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2019-04-10 17:56:36 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|