public static String freshXML(String ruleStr)
{
Document rule_from = Tools.string2Dom4J(ruleStr);
String resultfreshXML = "0,SyncRule successful.";
String user = rule_from.selectSingleNode(
"//SyncRulesRequest/RequestHeader/User").getText();
String password = rule_from.selectSingleNode(
"//SyncRulesRequest/RequestHeader/Password").getText();
String authorization = FileManager.getProperty("init.properties",
"AUTHORIZATION");
if (authorization.equals("1"))
{
if ((!user.equals(FileManager.getProperty("init.properties",
"USERNAME")))
|| (!password.equals(FileManager.getProperty(
"init.properties", "PASSWORD"))))
{
resultfreshXML = "1,The Username or password is error.";
return resultfreshXML;
}
}
FileListener rulefile = FileManager.getFileListener(RULE_FILE_NAME_XML);
Document rule_destination = getXmlDocument(rulefile.getPath());
Node action = rule_from
.selectSingleNode("//SyncRulesRequest/RuleContents/@Action");
List<?> ruleContent_from = rule_from
.selectNodes("//SyncRulesRequest/RuleContents/Rule");
Element ruleContents_dest = rule_destination.getRootElement().element(
"RuleContents");
String action_value = action.getText();
Node node = null;
Iterator<?> iterator = ruleContent_from.iterator();
String ruleid = null;
String ruleContent = null;
if (action_value.equals("Add"))
{
while (iterator.hasNext())
{
node = (Node) iterator.next();
ruleid = node.selectSingleNode("@RuleId").getText();
if (checkRuleMap(ruleid))
{
resultfreshXML = "2,This rule_id:" + ruleid
+ " was exist so can not add the Rule .";
break;
}
ruleContent = node.selectSingleNode("@RuleContent").getText();
Element element = ruleContents_dest.addElement("Rule");
element.addAttribute("RuleId", ruleid);
element.addAttribute("RuleContent", ruleContent);
ruleMap.put(ruleid, parseRule(ruleContent));
}
}
else if (action_value.equals("Del"))
{
while (iterator.hasNext())
{
node = (Node) iterator.next();
ruleid = node.selectSingleNode("@RuleId").getText();
if (checkRuleMap(ruleid))
{
resultfreshXML = "3,This rule_id:"
+ ruleid
+ " was not exist so can not stick up for the Rule.";
break;
}
// 删除节点
node = ruleContents_dest.selectSingleNode("Rule[@RuleId='"
+ ruleid + "']");
ruleContents_dest.remove(node);
ruleMap.remove(ruleid);
}
}
else if (action_value.equals("Mod"))
{
while (iterator.hasNext())
{
node = (Node) iterator.next();
ruleid = node.selectSingleNode("@RuleId").getText();
if (checkRuleMap(ruleid))
{
resultfreshXML = "3,This rule_id:"
+ ruleid
+ " was not exist so can not stick up for the Rule.";
break;
}
ruleContent = node.selectSingleNode("@RuleContent").getText();
node = ruleContents_dest.selectSingleNode("Rule[@RuleId='"
+ ruleid + "']");
node = node.selectSingleNode("@RuleContent");
node.setText(ruleContent);
ruleMap.remove(ruleid);
ruleMap.put(ruleid, parseRule(ruleContent));
}
}
delete(rulefile.getPath());
saveXmlDocument(rulefile.getPath(), rule_destination);
logger.info("resultfreshXML result:"+resultfreshXML);
return resultfreshXML;
}
{
Document rule_from = Tools.string2Dom4J(ruleStr);
String resultfreshXML = "0,SyncRule successful.";
String user = rule_from.selectSingleNode(
"//SyncRulesRequest/RequestHeader/User").getText();
String password = rule_from.selectSingleNode(
"//SyncRulesRequest/RequestHeader/Password").getText();
String authorization = FileManager.getProperty("init.properties",
"AUTHORIZATION");
if (authorization.equals("1"))
{
if ((!user.equals(FileManager.getProperty("init.properties",
"USERNAME")))
|| (!password.equals(FileManager.getProperty(
"init.properties", "PASSWORD"))))
{
resultfreshXML = "1,The Username or password is error.";
return resultfreshXML;
}
}
FileListener rulefile = FileManager.getFileListener(RULE_FILE_NAME_XML);
Document rule_destination = getXmlDocument(rulefile.getPath());
Node action = rule_from
.selectSingleNode("//SyncRulesRequest/RuleContents/@Action");
List<?> ruleContent_from = rule_from
.selectNodes("//SyncRulesRequest/RuleContents/Rule");
Element ruleContents_dest = rule_destination.getRootElement().element(
"RuleContents");
String action_value = action.getText();
Node node = null;
Iterator<?> iterator = ruleContent_from.iterator();
String ruleid = null;
String ruleContent = null;
if (action_value.equals("Add"))
{
while (iterator.hasNext())
{
node = (Node) iterator.next();
ruleid = node.selectSingleNode("@RuleId").getText();
if (checkRuleMap(ruleid))
{
resultfreshXML = "2,This rule_id:" + ruleid
+ " was exist so can not add the Rule .";
break;
}
ruleContent = node.selectSingleNode("@RuleContent").getText();
Element element = ruleContents_dest.addElement("Rule");
element.addAttribute("RuleId", ruleid);
element.addAttribute("RuleContent", ruleContent);
ruleMap.put(ruleid, parseRule(ruleContent));
}
}
else if (action_value.equals("Del"))
{
while (iterator.hasNext())
{
node = (Node) iterator.next();
ruleid = node.selectSingleNode("@RuleId").getText();
if (checkRuleMap(ruleid))
{
resultfreshXML = "3,This rule_id:"
+ ruleid
+ " was not exist so can not stick up for the Rule.";
break;
}
// 删除节点
node = ruleContents_dest.selectSingleNode("Rule[@RuleId='"
+ ruleid + "']");
ruleContents_dest.remove(node);
ruleMap.remove(ruleid);
}
}
else if (action_value.equals("Mod"))
{
while (iterator.hasNext())
{
node = (Node) iterator.next();
ruleid = node.selectSingleNode("@RuleId").getText();
if (checkRuleMap(ruleid))
{
resultfreshXML = "3,This rule_id:"
+ ruleid
+ " was not exist so can not stick up for the Rule.";
break;
}
ruleContent = node.selectSingleNode("@RuleContent").getText();
node = ruleContents_dest.selectSingleNode("Rule[@RuleId='"
+ ruleid + "']");
node = node.selectSingleNode("@RuleContent");
node.setText(ruleContent);
ruleMap.remove(ruleid);
ruleMap.put(ruleid, parseRule(ruleContent));
}
}
delete(rulefile.getPath());
saveXmlDocument(rulefile.getPath(), rule_destination);
logger.info("resultfreshXML result:"+resultfreshXML);
return resultfreshXML;
}