package eu.aniketos.serviceruntime.rules;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Kostas Giannakakis
*/
public class RulesParser {
/** Logger */
private static Logger logger = LoggerFactory.getLogger(RulesParser.class);
private String testXmlRulesNew =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<rules>" +
" <rule id=\"[generated by rule-editor to make it easier to edit in SCF?]\">" +
" <service taskId=\"servicetask2\" taskName=\"Process payment\">https://chrispay.com/api/pay</service> <!-- this node text is the serviceID to subscribe to! -->" +
" <type>Threat level change</type>" +
" <description>Malicious service</description>" +
" <value comparison=\"=\">1</value> " +
" <threatId>3dc34eaa-f4bf-4056-b850-51d48ae7100a</threatId>" +
" <scope> <!-- None, one or two of these - so we have freedom in adding several - no scope element means anywhere -->" +
" <before taskId=\"serviceTask3\" /> " +
" <after taskId=\"serviceTask2\" />" +
" <during taskId=\"serviceTask3\" />" +
" </scope>" +
" <action>" +
" <changeRuntime> <!-- Just one of these, right? -->" +
" <recompositionAndReconfiguration allowStop=\"0\"/>" +
" <recomposition allowStop=\"1\"/>" +
" <stopService/>" +
" <reconfiguration allowStop=\"0\"/>" +
" </changeRuntime>" +
" <launchProcess id=\"paymentProviderChange\" /> <!-- Do we have a solution to how Activiti Engine will be able to fetch this process? -->" +
" </action>" +
" </rule> " +
"" +
" <rule id=\"[generated by rule-editor]\">" +
" <service taskId=\"servicetask3\" taskName=\"Send confirmation\">https://mailer.com/sendmail</service>" +
" <type>Trust level change</type>" +
" <value comparison=\"<\">0.5</value>" +
" <action>" +
" <changeRuntime>" +
" <recompositionAndReconfiguration allowStop=\"1\"/>" +
" </changeRuntime>" +
" </action>" +
" </rule> " +
"</rules>";
public RulesParser() {
}
public void test() {
try {
List<Rule> rules = parse(testXmlRulesNew);
for(Rule r: rules) {
System.out.println(r);
}
}
catch (Exception ex) {
logger.error("Error parsing rule: " + ex.getMessage());
}
}
public List<Rule> parse(String xml) throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader =
inputFactory.createXMLStreamReader(new StringReader(xml));
List<Rule> rules = new ArrayList<Rule>();
Rule rule = null;
String tagContent = null;
String localName;
while (reader.hasNext()) {
int event = reader.next();
switch(event){
case XMLStreamConstants.START_DOCUMENT:
break;
case XMLStreamConstants.START_ELEMENT:
localName = reader.getLocalName();
if ("rule".equals(localName)) {
rule = new Rule();
rule.setId(reader.getAttributeValue(0));
}
else if ("service".equals(localName)) {
Service service = new Service();
service.setTaskId(reader.getAttributeValue(null, "taskId"));
service.setTaskName(reader.getAttributeValue(null, "taskName"));
rule.setService(service);
}
else if ("action".equals(localName)) {
Action action = new Action();
rule.setAction(action);
}
// Scope nodes
else if ("scope".equals(localName)) {
Scope scope = new Scope();
rule.setScope(scope);
}
else if ("before".equals(localName)) {
rule.getScope().setBefore(reader.getAttributeValue(0));
}
else if ("after".equals(localName)) {
rule.getScope().setAfter(reader.getAttributeValue(0));
}
else if ("during".equals(localName)) {
rule.getScope().setDuring(reader.getAttributeValue(0));
}
// Event nodes
else if ("value".equals(localName)) {
rule.getEvent().setComparison(reader.getAttributeValue(0));
}
// Action nodes
else if ("recompositionAndReconfiguration".equals(localName)) {
rule.getAction().setActionType(ActionType.RecompositionAndReconfiguration);
String stop = reader.getAttributeValue(0);
rule.getAction().setStop("1".equals(stop));
//System.out.println(localName + ": " + reader.getAttributeValue(0));
}
else if ("recomposition".equals(localName)) {
rule.getAction().setActionType(ActionType.Recomposition);
String stop = reader.getAttributeValue(0);
rule.getAction().setStop("1".equals(stop));
//System.out.println(localName + ": " + reader.getAttributeValue(0));
}
else if ("reconfiguration".equals(localName)) {
rule.getAction().setActionType(ActionType.Reconfiguration);
String stop = reader.getAttributeValue(0);
rule.getAction().setStop("1".equals(stop));
//System.out.println(localName + ": " + reader.getAttributeValue(0));
}
else if ("stopService".equals(localName)) {
rule.getAction().setActionType(ActionType.Stop);
//System.out.println(localName + ": " + reader.getAttributeValue(0));
}
else if ("launchProcess".equals(localName)) {
rule.getAction().setLaunchProcessId(reader.getAttributeValue(0));
}
break;
case XMLStreamConstants.CHARACTERS:
tagContent = reader.getText().trim();
break;
case XMLStreamConstants.END_ELEMENT:
localName = reader.getLocalName();
if ("rule".equals(localName)) {
rules.add(rule);
}
// Service node
else if ("service".equals(localName)) {
rule.getService().setServiceId(tagContent);
}
// Event nodes
else if ("value".equals(localName)) {
rule.getEvent().setValue(tagContent);
}
else if ("type".equals(localName)) {
rule.getEvent().setType(tagContent);
}
else if ("description".equals(localName)) {
rule.getEvent().setDescription(tagContent);
}
else if ("threatId".equals(localName)) {
rule.getEvent().setThreatId(tagContent);
}
break;
}
}
return rules;
}
public static void main(String... args) {
RulesParser rulesParser = new RulesParser();
rulesParser.test();
}
}