use of org.drools.core.io.internal.InternalResource in project drools by kiegroup.
the class XmlBifParser method loadBif.
public static Bif loadBif(Resource resource, KnowledgeBuilderErrors errors) {
InputStream is = null;
try {
is = resource.getInputStream();
} catch (IOException e) {
errors.add(new ParserError(resource, "Exception opening Stream:\n" + e.toString(), 0, 0));
return null;
}
try {
String encoding = resource instanceof InternalResource ? ((InternalResource) resource).getEncoding() : null;
XStream xstream = encoding != null ? createTrustingXStream(new DomDriver(encoding)) : createTrustingXStream();
initXStream(xstream);
Bif bif = (Bif) xstream.fromXML(is);
return bif;
} catch (Exception e) {
errors.add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
return null;
}
}
use of org.drools.core.io.internal.InternalResource in project drools by kiegroup.
the class DrlParser method parse.
public PackageDescr parse(final boolean isEditor, final Resource resource, final InputStream is) throws DroolsParserException, IOException {
this.resource = resource;
String encoding = resource instanceof InternalResource ? ((InternalResource) resource).getEncoding() : null;
lexer = buildLexer(is, encoding, languageLevel);
DRLParser parser = buildParser(lexer, languageLevel);
return compile(isEditor, parser);
}
use of org.drools.core.io.internal.InternalResource in project drools by kiegroup.
the class XmlChangeSetReaderTest method testResourceAttributes.
@Test
public void testResourceAttributes() throws Exception {
SemanticModules semanticModules = new SemanticModules();
semanticModules.addSemanticModule(new ChangeSetSemanticModule());
XmlChangeSetReader changeSetReader = new XmlChangeSetReader(semanticModules);
changeSetReader.setClassLoader(XmlChangeSetReaderTest.class.getClassLoader(), null);
ChangeSet changeSet = changeSetReader.read(XmlChangeSetReaderTest.class.getClassLoader().getResourceAsStream("org/drools/core/xml/test-change-set.xml"));
Assert.assertNotNull(changeSet);
Collection<Resource> resourcesAdded = changeSet.getResourcesAdded();
Assert.assertNotNull(resourcesAdded);
Assert.assertEquals(4, resourcesAdded.size());
InternalResource resource1 = null;
InternalResource resource2 = null;
InternalResource resource3 = null;
InternalResource secureResource = null;
for (Resource r : resourcesAdded) {
InternalResource resource = (InternalResource) r;
if (resource.getSourcePath() != null && resource.getSourcePath().equals("resource1")) {
resource1 = resource;
} else if (resource.getSourcePath() != null && resource.getSourcePath().equals("secureResource")) {
secureResource = resource;
} else if (resource.getSourcePath() == null && resource.getDescription() == null) {
resource3 = resource;
} else if (resource.getSourcePath() == null) {
resource2 = resource;
}
}
Assert.assertNotNull(resource1);
Assert.assertNotNull(resource2);
Assert.assertNotNull(resource3);
Assert.assertNotNull(secureResource);
Assert.assertNull(resource1.getDescription());
Assert.assertEquals("another description", resource2.getDescription());
Assert.assertEquals("some useful description", secureResource.getDescription());
Assert.assertEquals(2, changeSetReader.getParser().getAttrs().getLength());
Assert.assertEquals("DRL", changeSetReader.getParser().getAttrs().getValue("type"));
}
use of org.drools.core.io.internal.InternalResource in project drools by kiegroup.
the class KnowledgeBuilderImpl method registerBuildResource.
public void registerBuildResource(final Resource resource, ResourceType type) {
InternalResource ires = (InternalResource) resource;
if (ires.getResourceType() == null) {
ires.setResourceType(type);
} else if (ires.getResourceType() != type) {
addBuilderResult(new ResourceTypeDeclarationWarning(resource, ires.getResourceType(), type));
}
if (ResourceType.CHANGE_SET == type) {
try {
ChangeSet changeSet = parseChangeSet(resource);
List<Resource> resources = new ArrayList<Resource>();
resources.add(resource);
for (Resource addedRes : changeSet.getResourcesAdded()) {
resources.add(addedRes);
}
for (Resource modifiedRes : changeSet.getResourcesModified()) {
resources.add(modifiedRes);
}
for (Resource removedRes : changeSet.getResourcesRemoved()) {
resources.add(removedRes);
}
buildResources.push(resources);
} catch (Exception e) {
results.add(new DroolsError() {
public String getMessage() {
return "Unable to register changeset resource " + resource;
}
public int[] getLines() {
return new int[0];
}
});
}
} else {
buildResources.push(Collections.singletonList(resource));
}
}
use of org.drools.core.io.internal.InternalResource in project drools by kiegroup.
the class KnowledgeBuilderImpl method addPackageFromChangeSet.
void addPackageFromChangeSet(Resource resource) throws SAXException, IOException {
XmlChangeSetReader reader = new XmlChangeSetReader(this.configuration.getSemanticModules());
if (resource instanceof ClassPathResource) {
reader.setClassLoader(((ClassPathResource) resource).getClassLoader(), ((ClassPathResource) resource).getClazz());
} else {
reader.setClassLoader(this.configuration.getClassLoader(), null);
}
Reader resourceReader = null;
try {
resourceReader = resource.getReader();
ChangeSet changeSet = reader.read(resourceReader);
if (changeSet == null) {
// @TODO should log an error
}
for (Resource nestedResource : changeSet.getResourcesAdded()) {
InternalResource iNestedResourceResource = (InternalResource) nestedResource;
if (iNestedResourceResource.isDirectory()) {
for (Resource childResource : iNestedResourceResource.listResources()) {
if (((InternalResource) childResource).isDirectory()) {
// ignore sub directories
continue;
}
((InternalResource) childResource).setResourceType(iNestedResourceResource.getResourceType());
addKnowledgeResource(childResource, iNestedResourceResource.getResourceType(), iNestedResourceResource.getConfiguration());
}
} else {
addKnowledgeResource(iNestedResourceResource, iNestedResourceResource.getResourceType(), iNestedResourceResource.getConfiguration());
}
}
} finally {
if (resourceReader != null) {
resourceReader.close();
}
}
}
Aggregations