com.github.tsijercic1.Node Maven / Gradle / Ivy
The newest version!
package com.github.tsijercic1;
import java.util.*;
public class Node {
private String name;
private String content;
private Map attributes;
private ArrayList childNodes;
Node() {
attributes = new TreeMap<>();
childNodes = new ArrayList<>();
}
Node(String name) {
this();
this.name = name;
}
void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
private String getActualContent(){
return content;
}
public String getContent() {
StringBuilder textContent = new StringBuilder();
for (Node child : childNodes) {
if(child.getName().equals("")){
textContent.append(child.getActualContent());
}
}
return textContent.toString();
}
void setContent(String content) {
this.content = content;
}
void addAttribute(String key, String value) {
attributes.put(key, value);
}
void addChildNode(Node node) {
childNodes.add(node);
}
public Map getAttributes() {
return new HashMap<>(attributes);
}
public ArrayList getChildNodes() {
ArrayList nodes = new ArrayList<>(childNodes);
nodes.removeIf(node -> node.getName().equals(""));
return nodes;
}
public ArrayList getChildNodes(String name){
ArrayList nodes = new ArrayList<>(childNodes);
nodes.removeIf(node -> !node.getName().equals(name) || name.equals(""));
return nodes;
}
public Node getChildNode(String name){
return getNthChildNode(name, 0);
}
public Node getNthChildNode(String name, int i){
ArrayList nodes = new ArrayList<>(childNodes);
nodes.removeIf(node -> !node.getName().equals(name) || name.equals(""));
if(nodes.size()<=i)return null;
return nodes.get(i);
}
public Set getAttributeKeys(){
return new TreeSet<>(attributes.keySet());
}
public Collection getAttributeValues(){
return new ArrayList<>(attributes.values());
}
}