Showing posts with label conversion. Show all posts
Showing posts with label conversion. Show all posts

Thursday, April 23, 2009

Drools. How to convert DRL file to XML format and XML file to DRL file format

With the Drools api helper classes, it is very easy to convert a provided drl file to a corresponding xml. Similarly it is very easy to convert from xml file to drl file. Following example code explains how.
public class DroolsConversionHelper {

public static void main(String args[]) throws Exception {
System.out.println(convertDrlFileToXml("/sample.drl"));
System.out.println(convertXmlToDrlFile("/sample.xml"));
}
private static String convertDrlFileToXml(String drlFileName) throws Exception {
Reader source = new InputStreamReader(
DroolsConversionHelper.class.getResourceAsStream(drlFileName));
DrlParser drlParser = new DrlParser();
PackageDescr pkgDesc = drlParser.parse(source);
XmlDumper xmlDumper = new XmlDumper();
String xml = xmlDumper.dump(pkgDesc);
return xml;
}
private static String convertXmlToDrlFile(String xmlFileName) throws Exception {
Reader source = new InputStreamReader(
DroolsConversionHelper.class.getResourceAsStream(xmlFileName));
XmlPackageReader xmlPackageReader = new XmlPackageReader();
PackageDescr pkgDesc = xmlPackageReader.read(source);
DrlDumper drlDumper = new DrlDumper();
String drl = drlDumper.dump(pkgDesc);
return drl;
}
}