Implemented ConfigurationParser + result, removed IPool
This commit is contained in:
parent
de87d1780d
commit
666abbe2e1
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
@ -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() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user