Search in sources :

Example 1 with XmlChangeSetReader

use of org.drools.core.xml.XmlChangeSetReader in project drools by kiegroup.

the class KnowledgeBuilderImpl method parseChangeSet.

private ChangeSet parseChangeSet(Resource resource) throws IOException, SAXException {
    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();
        return reader.read(resourceReader);
    } finally {
        if (resourceReader != null) {
            resourceReader.close();
        }
    }
}
Also used : XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Reader(java.io.Reader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) StringReader(java.io.StringReader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) ClassPathResource(org.drools.core.io.impl.ClassPathResource)

Example 2 with XmlChangeSetReader

use of org.drools.core.xml.XmlChangeSetReader in project drools by kiegroup.

the class ChangeSetTest method testBasicAuthentication.

@Test
public void testBasicAuthentication() throws SAXException, IOException {
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    XmlChangeSetReader xmlReader = new XmlChangeSetReader(conf.getSemanticModules());
    xmlReader.setClassLoader(ChangeSetTest.class.getClassLoader(), ChangeSetTest.class);
    String str = "";
    str += "<change-set ";
    str += "xmlns='http://drools.org/drools-5.0/change-set' ";
    str += "xmlns:xs='http://www.w3.org/2001/XMLSchema-instance' ";
    str += "xs:schemaLocation='http://drools.org/drools-5.0/change-set http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >";
    str += "    <add> ";
    str += "        <resource source='http://localhost:8081/jboss-brms/org.kie.guvnor.Guvnor/package/defaultPackage/LATEST' type='PKG' basicAuthentication='enabled' username='admin' password='pwd'/>";
    str += "    </add> ";
    str += "</change-set>";
    StringReader reader = new StringReader(str);
    ChangeSet changeSet = xmlReader.read(reader);
    assertEquals(1, changeSet.getResourcesAdded().size());
    UrlResource resource = (UrlResource) ((List) changeSet.getResourcesAdded()).get(0);
    assertEquals("http://localhost:8081/jboss-brms/org.kie.guvnor.Guvnor/package/defaultPackage/LATEST", resource.getURL().toString());
    assertEquals("enabled", resource.getBasicAuthentication());
    assertEquals("admin", resource.getUsername());
    assertEquals("pwd", resource.getPassword());
    assertEquals(ResourceType.PKG, resource.getResourceType());
}
Also used : KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) UrlResource(org.drools.core.io.impl.UrlResource) StringReader(java.io.StringReader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) ChangeSet(org.kie.internal.ChangeSet) Test(org.junit.Test)

Example 3 with XmlChangeSetReader

use of org.drools.core.xml.XmlChangeSetReader in project drools by kiegroup.

the class ChangeSetTest method testXmlParser.

@Test
public void testXmlParser() throws SAXException, IOException {
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    XmlChangeSetReader xmlReader = new XmlChangeSetReader(conf.getSemanticModules());
    xmlReader.setClassLoader(ChangeSetTest.class.getClassLoader(), ChangeSetTest.class);
    String str = "";
    str += "<change-set ";
    str += "xmlns='http://drools.org/drools-5.0/change-set' ";
    str += "xmlns:xs='http://www.w3.org/2001/XMLSchema-instance' ";
    str += "xs:schemaLocation='http://drools.org/drools-5.0/change-set http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >";
    str += "    <add> ";
    str += "        <resource source='http://www.domain.com/test.drl' type='DRL' />";
    str += "        <resource source='http://www.domain.com/test.xls' type='DTABLE' >";
    str += "            <decisiontable-conf worksheet-name='sheet10' input-type='XLS' />";
    str += "        </resource>";
    str += "    </add> ";
    str += "</change-set>";
    StringReader reader = new StringReader(str);
    ChangeSet changeSet = xmlReader.read(reader);
    assertEquals(2, changeSet.getResourcesAdded().size());
    UrlResource resource = (UrlResource) ((List) changeSet.getResourcesAdded()).get(0);
    assertEquals("http://www.domain.com/test.drl", resource.getURL().toString());
    assertEquals(ResourceType.DRL, resource.getResourceType());
    resource = (UrlResource) ((List) changeSet.getResourcesAdded()).get(1);
    assertEquals("http://www.domain.com/test.xls", resource.getURL().toString());
    assertEquals(ResourceType.DTABLE, resource.getResourceType());
    DecisionTableConfiguration dtConf = (DecisionTableConfiguration) resource.getConfiguration();
    assertEquals(DecisionTableInputType.XLS, dtConf.getInputType());
}
Also used : KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) UrlResource(org.drools.core.io.impl.UrlResource) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) ChangeSet(org.kie.internal.ChangeSet) Test(org.junit.Test)

Example 4 with XmlChangeSetReader

use of org.drools.core.xml.XmlChangeSetReader 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);
    }
    try (Reader resourceReader = resource.getReader()) {
        ChangeSet changeSet = reader.read(resourceReader);
        if (changeSet == null) {
            throw new RuntimeException("ChangeSet cannot be read! " + resource);
        } else {
            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());
                }
            }
        }
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) BaseResource(org.drools.core.io.impl.BaseResource) DescrResource(org.drools.core.io.impl.DescrResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Resource(org.kie.api.io.Resource) ReaderResource(org.drools.core.io.impl.ReaderResource) InternalResource(org.drools.core.io.internal.InternalResource) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Reader(java.io.Reader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) StringReader(java.io.StringReader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) ChangeSet(org.kie.internal.ChangeSet) ClassPathResource(org.drools.core.io.impl.ClassPathResource)

Example 5 with XmlChangeSetReader

use of org.drools.core.xml.XmlChangeSetReader in project drools by kiegroup.

the class ChangeSetTest method testBasicAuthentication.

@Test
public void testBasicAuthentication() throws SAXException, IOException {
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    XmlChangeSetReader xmlReader = new XmlChangeSetReader(conf.getSemanticModules());
    xmlReader.setClassLoader(ChangeSetTest.class.getClassLoader(), ChangeSetTest.class);
    String str = "";
    str += "<change-set ";
    str += "xmlns='http://drools.org/drools-5.0/change-set' ";
    str += "xmlns:xs='http://www.w3.org/2001/XMLSchema-instance' ";
    str += "xs:schemaLocation='http://drools.org/drools-5.0/change-set change-set-1.0.0.xsd' >";
    str += "    <add> ";
    str += "        <resource source='http://localhost:8081/jboss-brms/org.kie.guvnor.Guvnor/package/defaultPackage/LATEST' type='PKG' basicAuthentication='enabled' username='admin' password='pwd'/>";
    str += "    </add> ";
    str += "</change-set>";
    StringReader reader = new StringReader(str);
    ChangeSet changeSet = xmlReader.read(reader);
    assertEquals(1, changeSet.getResourcesAdded().size());
    UrlResource resource = (UrlResource) ((List) changeSet.getResourcesAdded()).get(0);
    assertEquals("http://localhost:8081/jboss-brms/org.kie.guvnor.Guvnor/package/defaultPackage/LATEST", resource.getURL().toString());
    assertEquals("enabled", resource.getBasicAuthentication());
    assertEquals("admin", resource.getUsername());
    assertEquals("pwd", resource.getPassword());
    assertEquals(ResourceType.PKG, resource.getResourceType());
}
Also used : KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) UrlResource(org.drools.core.io.impl.UrlResource) StringReader(java.io.StringReader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) ChangeSet(org.kie.internal.ChangeSet) Test(org.junit.Test)

Aggregations

StringReader (java.io.StringReader)6 XmlChangeSetReader (org.drools.core.xml.XmlChangeSetReader)6 ChangeSet (org.kie.internal.ChangeSet)5 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)4 UrlResource (org.drools.core.io.impl.UrlResource)4 Test (org.junit.Test)4 Reader (java.io.Reader)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)2 ClassPathResource (org.drools.core.io.impl.ClassPathResource)2 DecisionTableConfiguration (org.kie.internal.builder.DecisionTableConfiguration)2 BaseResource (org.drools.core.io.impl.BaseResource)1 DescrResource (org.drools.core.io.impl.DescrResource)1 ReaderResource (org.drools.core.io.impl.ReaderResource)1 InternalResource (org.drools.core.io.internal.InternalResource)1 Resource (org.kie.api.io.Resource)1