use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.
the class XmlPackageReaderTest method testParseCollect.
@Test
public void testParseCollect() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseCollect.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
String expected = StringUtils.readFileAsString(new InputStreamReader(getClass().getResourceAsStream("test_ParseCollect.drl")));
String expectedWithoutHeader = removeLicenseHeader(expected);
String actual = new DrlDumper().dump(packageDescr);
Assertions.assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual);
}
use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.
the class DumperTestHelper method XmlFile.
public static void XmlFile(String filename) throws Exception {
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
XmlPackageReader xmlPackageReader = new XmlPackageReader(conf.getSemanticModules());
xmlPackageReader.getParser().setClassLoader(DumperTestHelper.class.getClassLoader());
xmlPackageReader.read(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
final PackageDescr pkgOriginal = xmlPackageReader.getPackageDescr();
final XmlDumper dumper = new XmlDumper();
final String result = dumper.dump(pkgOriginal);
String buffer = readFile(filename);
System.out.println(buffer);
System.out.println(result);
Assertions.assertThat(buffer).isEqualToIgnoringWhitespace(result);
assertNotNull(result);
}
use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.
the class KnowledgeBuilderImpl method addPackageFromXml.
/**
* Load a rule package from XML source.
*
* @param reader
* @throws DroolsParserException
* @throws IOException
*/
public void addPackageFromXml(final Reader reader) throws DroolsParserException, IOException {
this.resource = new ReaderResource(reader, ResourceType.XDRL);
final XmlPackageReader xmlReader = new XmlPackageReader(this.configuration.getSemanticModules());
xmlReader.getParser().setClassLoader(this.rootClassLoader);
try {
xmlReader.read(reader);
} catch (final SAXException e) {
throw new DroolsParserException(e.toString(), e.getCause());
}
addPackage(xmlReader.getPackageDescr());
this.resource = null;
}
use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.
the class KnowledgeBuilderImpl method xmlToPackageDescr.
PackageDescr xmlToPackageDescr(Resource resource) throws DroolsParserException, IOException {
final XmlPackageReader xmlReader = new XmlPackageReader(this.configuration.getSemanticModules());
xmlReader.getParser().setClassLoader(this.rootClassLoader);
Reader reader = null;
try {
reader = resource.getReader();
xmlReader.read(reader);
} catch (final SAXException e) {
throw new DroolsParserException(e.toString(), e.getCause());
} finally {
if (reader != null) {
reader.close();
}
}
return xmlReader.getPackageDescr();
}
use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.
the class XmlPackageReaderTest method testParseGlobal.
@Test
public void testParseGlobal() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseGlobal.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
assertEquals("com.sample", packageDescr.getName());
final List imports = packageDescr.getImports();
assertEquals(2, imports.size());
assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
assertEquals("org.drools.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
final List globals = packageDescr.getGlobals();
assertEquals(2, globals.size());
final GlobalDescr x = (GlobalDescr) globals.get(0);
final GlobalDescr yada = (GlobalDescr) globals.get(1);
assertEquals("com.sample.X", x.getType());
assertEquals("x", x.getIdentifier());
assertEquals("com.sample.Yada", yada.getType());
assertEquals("yada", yada.getIdentifier());
}
Aggregations