Merge branch 'feature/pool_parser_impl' into develop

This commit is contained in:
Nikita Iryupin 2020-12-25 15:39:05 +01:00
commit d9d15d2428
6 changed files with 115 additions and 16 deletions

View File

@ -1,4 +1,43 @@
package cz.cvut.fel.sit.omo.foodchain.parser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
public class ConfigurationParser {
private static Document activeDocument;
public static void parseDocument(String name) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File(name);
activeDocument = builder.parse(file);
}
public static ParserResult getElements(String tag) {
NodeList elements = activeDocument.getElementsByTagName(tag);
ParserResult resultList = new ParserResult();
for (int i = 0; i < elements.getLength(); i++) {
Node tmp_node = elements.item(i);
ParserResultElement element = new ParserResultElement();
for (int j = 0; j < tmp_node.getAttributes().getLength(); j++) {
element.addAttribute(tmp_node.getAttributes().item(i).getNodeName(), tmp_node.getAttributes().item(i).getTextContent());
}
for (int j = 0; j < tmp_node.getChildNodes().getLength(); j++) {
element.addChild(tmp_node.getChildNodes().item(i).getNodeName(), tmp_node.getChildNodes().item(i).getTextContent());
}
resultList.addElement(element);
}
return resultList;
}
}

View File

@ -0,0 +1,18 @@
package cz.cvut.fel.sit.omo.foodchain.parser;
import org.w3c.dom.Element;
import java.util.LinkedList;
public class ParserResult {
private LinkedList<ParserResultElement> elements;
public ParserResult() {
elements = new LinkedList<>();
}
public void addElement(ParserResultElement element) {
elements.add(element);
}
}

View File

@ -0,0 +1,56 @@
package cz.cvut.fel.sit.omo.foodchain.parser;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
public class ParserResultElement {
private String name;
private Map<String, String> attributes;
private Map<String, String> children;
public ParserResultElement() {
attributes = new HashMap<>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, String> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
public void addAttribute(String key, String value) {
attributes.put(key, value);
}
public String getAttribute(String key) {
return attributes.get(key);
}
public Map<String, String> getChildren() {
return children;
}
public void setChildren(Map<String, String> children) {
this.children = children;
}
public void addChild(String key, String value) {
children.put(key, value);
}
public String getChild(String key) {
return children.get(key);
}
}

View File

@ -1,15 +0,0 @@
package cz.cvut.fel.sit.omo.foodchain.pool;
import java.util.Collection;
public interface IPool<T> {
Collection<T> findAll();
void add(T element);
void remove(T element);
void clear();
boolean contains(T element);
}

View File

@ -3,4 +3,5 @@ package cz.cvut.fel.sit.omo.foodchain.pool;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
public class NodePool extends PoolListBase<NodeBase> {
//TODO Tomas Angelo
}

View File

@ -4,7 +4,7 @@ import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
public abstract class PoolListBase<T> implements IPool<T> {
public abstract class PoolListBase<T> {
List<T> pool;
public PoolListBase() {