在层次遍历的基础上,保存层数信息即可
public void RTParseDirective(LinkedList rts){
for(RuleTree rt: rts) {
Directive dir = new Directive();
//场景头
dir.setId("16");
dir.setName("NAME:XXXXXXX");
dir.setPriority("2");
//节点的信息和层数
Stack st = new Stack<>();
st.push(new TwoTuple<>(rt, 0));
System.out.println(st.size());
LinkedList> Rule = dir.getXMLRule();
while (st.size() != 0) {
if ((int) st.firstElement().second > Rule.size() - 1) {
LinkedList newLevel = new LinkedList<>();
Rule.addLast(newLevel);
}
for (int i = 0; i < ((RuleTree) st.firstElement().first).getChildren().size(); i++) {
System.out.println(i);
st.push(new TwoTuple(((RuleTree) st.firstElement().first).getChildren().get(i), (int) st.firstElement().second + 1));
}
Rule.get((int) st.firstElement().second).addLast(this.DirectiveNewRuleFromAlertInfo(((RuleTree) (st.firstElement().first)).getAlertInfo()));
st.remove(0);
}
this.directives.add(dir);
}
}
层次结构转换为树形结构(下一次链接在上一层的最后一个节点的特殊情况)
package hznu.sun.ruleming.function.rule.updateDirective;
import hznu.sun.ruleming.pojo.Directive;
import hznu.sun.ruleming.pojo.XMLRuleNode;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.LinkedList;
/**
* Created by sun on 2016/7/11.
*/
public class writeDirectiveToXML {
private final static Integer UPDATE = 0;//update rule
private final static Integer NEW = 1;//new rule
private final String needUpdatefilename = "Rule/newRulematched.xml";
private final String newRulefilename = "Rule/newRuleNotmatched.xml";
private Document doc;
/**
* write Directive to *.xml
* 1、add directive
* 2、add next level to last level's last elemt
* 3、write to *.xml by NEWorUPDAT
* @param directive
* @param NEWorUPDATE write to diffirent xml
*/
public void create(Directive directive,Integer NEWorUPDATE){
try {
SAXReader reader = new SAXReader();
if(NEWorUPDATE.equals(NEW))
doc = reader.read(newRulefilename);
else
doc = reader.read(needUpdatefilename);
Element root = doc.getRootElement();//get root
//get classification
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
if (element.getName().equals("classification")) {
//add first level
Element edirective = element.addElement("directive").
addAttribute("id", directive.getId()).
addAttribute("name", directive.getName()).
addAttribute("priority", directive.getPriority());
LinkedList> xnodes = directive.getXMLRule();
for (int ifir = 0 ; ifir if(ifir == xnodes.getFirst().size() - 1){
// the Element 'child' is used for record the last ruleElement
Element child = this.addruleInfo(edirective, xnodes.getFirst().get(ifir));
//add 'rules' (the next levels)
for(int ixnodes = 1 ; ixnodes < xnodes.size(); ixnodes ++){
child = this.addrulesinfo(child);
for(int ix = 0 ; ix if(ix == xnodes.get(ixnodes).size() - 1){
child = this.addruleInfo(child, xnodes.get(ixnodes).get(ix));
}else {
this.addruleInfo(child, xnodes.get(ixnodes).get(ix));
}
}
}
}else {
this.addruleInfo(edirective, xnodes.getFirst().get(ifir));
}
}
}
}
OutputFormat format = new OutputFormat();
format.setNewlines(true);
XMLWriter output ;
if(NEW.equals(NEWorUPDATE)){
output = new XMLWriter(new FileWriter(newRulefilename),format);
} else{
output = new XMLWriter(new FileWriter(needUpdatefilename));
}
output.write(doc);
output.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println("could not parse the file!");
}
}
/**
* add child element 'rule' to the element
* @param e
* @param xmlRuleNode
* @return Element return the child element 'rule'
*/
private Element addruleInfo(Element e, XMLRuleNode xmlRuleNode) {
Element element = DocumentHelper.createElement("rule");
element.addAttribute("id", xmlRuleNode.getId()).
addAttribute("type", xmlRuleNode.getType()).
addAttribute("name", xmlRuleNode.getName()).
addAttribute("reliability", xmlRuleNode.getReliability()).
addAttribute("from", xmlRuleNode.getFrom()).
addAttribute("to", xmlRuleNode.getTo()).
addAttribute("port_from", xmlRuleNode.getPort_from()).
addAttribute("port_to", xmlRuleNode.getPort_to()).
addAttribute("plugin_id", xmlRuleNode.getPlugin_id()).
addAttribute("plugin_sid", xmlRuleNode.getPlugin_sid()).
addAttribute("protocol", xmlRuleNode.getProtocol());
e.add(element);
return element;
}
/**
* add 'rules'
* @param e
* @return get the child 'rules'
*/
private Element addrulesinfo(Element e) {
Element element = DocumentHelper.createElement("rules");
e.add(element);
return element;
}
public static void main(String[] args) {
Directive directive = new Directive();
directive.setName("nihao");
directive.setId("9999");
directive.setPriority("9999");
LinkedList> rule = new LinkedList<>();
LinkedList l1 = new LinkedList<>();
LinkedList l2 = new LinkedList<>();
LinkedList l3 = new LinkedList<>();
XMLRuleNode xmlRuleNode = new XMLRuleNode();
xmlRuleNode.setId("11");
xmlRuleNode.setName("xx");
xmlRuleNode.setPlugin_sid("ss");
xmlRuleNode.setFrom("ss");
l1.add(xmlRuleNode);
l2.add(xmlRuleNode);
l2.add(xmlRuleNode);
l3.add(xmlRuleNode);
l3.add(xmlRuleNode);
l3.add(xmlRuleNode);
rule.addFirst(l1);
rule.addLast(l2);
rule.addLast(l2);
directive.setXMLRule(rule);
new writeDirectiveToXML().create(directive ,UPDATE);
}
}