|
Rulesets are just ZIP files.
The Java code to load a ruleset looks almost exactly like the code to load a ZIP file.
The first step is knowing the name of the ruleset file and knowing where that file is located.
You can use either a relative file name or an absolute file name. We use a relative name.
String rulesetPath = "ruleArchive1.jar";
We open this binary file using a very standard Java programming technique.
We create a File from our ruleset file name. We use that File object to create
a FileInputStream. We use that FileInputStream to create a JarInputStream.
In your code, you have to remember to surround this line with a try / catch block.
Any attempt to open a ruleset has the potential to throw an IOException.
is = new JarInputStream( new FileInputStream( new File(rulesetPath) ) );
Once you have opened -- created -- a JarInputStream, you pass that stream
to some JRules code -- an IlrRulesetArchiveLoader. You will not do anything with
the IlrRulesetArchiveLoader object. Instead, you will pass this object
to the code that actually parses the ruleset. (See the IlrRulesetArchiveParser below.)
IlrRulesetArchiveLoader rulesetloader = new IlrJarArchiveLoader(is);
Once you have created a ruleset loader, the next step is to attempt to parse the ruleset.
You need three objects to successfully parse a ruleset.
- The IlrRulesetArchiveLoader that you just created.
- An IlrRuleset - a wrapper object for the ruleset itself.
- An IlrRulesetArchiveParser that does the actual parsing.
Create the ruleset parser and the ruleset wrapper class. Associate the ruleset wrapper class
with the ruleset parser. Neither of these objects throw any checked exceptions,
so this code does not have to be in a try / catch block in order to compile. At this point, the IlrRuleset
object is essentially empty. It will not have anything interesting inside it
until you actually parse the ruleset archive.
IlrRulesetArchiveParser rulesetparser = new IlrRulesetArchiveParser();
IlrRuleset ruleset = new IlrRuleset();
rulesetparser.setRuleset(ruleset);
Attempt to parse the ruleset archive. Once again, this code does not throw any checked exceptions.
You should always check the return value of the parseArchive() method. If it did not return true,
you should probably exit with some kind of error message.
boolean parsed = rulesetparser.parseArchive(rulesetloader);
Once you have successfully parsed a ruleset archive, the IlrRuleset object has some interesting things in it.
One of those things is an array of all the rules it found in the ruleset archive.
Sometimes, this has a value when you are trying to troubleshoot a problem.
IlrRule[] ruleArray = ruleset.getAllRules();
|