Skip to content

Commit

Permalink
Merge pull request #38 from joachimvda/master
Browse files Browse the repository at this point in the history
JBRULES-3633 resource encoding
  • Loading branch information
ge0ffrey committed Nov 7, 2012
2 parents 2c4f05e + 277adf2 commit 9f3a7ac
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 2 deletions.
Expand Up @@ -30,6 +30,8 @@ public class DroolsResourceAdapter
private Resource resource;
private ResourceType resourceType;
private ResourceConfiguration resourceConfiguration;
private String resourceName;
private String encoding;

public DroolsResourceAdapter() {

Expand All @@ -45,14 +47,24 @@ public DroolsResourceAdapter(String resource,
}

public void setResource(String resource) {
this.resourceName = resource;
if ( resource.trim().startsWith( "classpath:" ) ) {
this.resource = new ClassPathResource( resource.substring( resource.indexOf( ':' ) + 1 ),
encoding,
ClassPathResource.class.getClassLoader() );
} else {
this.resource = new UrlResource( resource );
}
}

public void setEncoding(String encoding) {
if ( !(resource instanceof ClassPathResource) ) {
throw new IllegalArgumentException( "Encoding attribute is only valid for classpath Resources" );
}
this.encoding = encoding;
setResource(resourceName);
}

public void setBasicAuthenticationEnabled(Boolean enabled) {
if ( enabled && !(this.resource instanceof UrlResource) ) {
throw new IllegalArgumentException( "Authentication Attributes are only valid for URL Resources" );
Expand Down
Expand Up @@ -42,6 +42,7 @@ public class ResourceDefinitionParser extends AbstractBeanDefinitionParser {
private static final String REF = "ref";
private static final String NAME = "name";
private static final String DESCRIPTION = "description";
private static final String ENCODING = "encoding";

@SuppressWarnings("unchecked")
@Override
Expand Down Expand Up @@ -84,11 +85,15 @@ protected AbstractBeanDefinition parseInternal(Element element,
factory.addPropertyValue( "basicAuthenticationPassword",
password );
}


String encoding = element.getAttribute( ENCODING );
factory.addPropertyValue( "encoding",
org.drools.core.util.StringUtils.isEmpty(encoding) ? null : encoding);

String name = element.getAttribute( NAME );
factory.addPropertyValue( "name",
org.drools.core.util.StringUtils.isEmpty(name) ? null : name);

String description = element.getAttribute( DESCRIPTION );
factory.addPropertyValue( "description",
org.drools.core.util.StringUtils.isEmpty(description) ? null : description);
Expand Down
Expand Up @@ -395,6 +395,7 @@
<xsd:attribute name="source" use="optional" type="xsd:string"/>
<xsd:attribute name="type" use="optional" type="resourceTypeEnum"/>
<xsd:attribute name="ref" use="optional" type="xsd:string"/>
<xsd:attribute name="encoding" use="optional" type="xsd:string"/>
</xsd:complexType>

<xsd:complexType name="resourceRefType">
Expand Down
Expand Up @@ -20,6 +20,8 @@
import java.util.List;
import java.util.Map;

import org.drools.KnowledgeBase;
import org.drools.definition.KnowledgePackage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -62,6 +64,18 @@ public void testNoConnection() throws Exception {
assertNotNull( node2 );
}

@Test
public void testEncoding() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/drools/container/spring/resourceWithEncoding.xml" );

KnowledgeBase kbase = (KnowledgeBase) context.getBean("kbase");
assertNotNull( kbase );
for (KnowledgePackage pkg : kbase.getKnowledgePackages()) {
assertEquals("sample acçéntèd rule", pkg.getRules().iterator().next().getName());
}

}

@Test
public void testNoNodeKSessions() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/drools/container/spring/no-node-beans.xml" );
Expand Down
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:drools="http://drools.org/schema/drools-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://drools.org/schema/drools-spring org/drools/container/spring/drools-spring.xsd">

<drools:kbase id="kbase">
<drools:resources>
<drools:resource type="DRL" source="classpath:org/drools/container/spring/sampleUTF8.drl" encoding="UTF-8" />
<drools:resource type="DRL" source="classpath:org/drools/container/spring/sampleISO88591.drl" encoding="ISO-8859-1" />
</drools:resources>
</drools:kbase>


<drools:ksession id="ksession" type="stateless" name="stateless" kbase="kbase"/>

</beans>
@@ -0,0 +1,8 @@
package org.drools.container.spring.i

rule "sample ac��nt�d rule"
@output("\"Hello world!\"")
when
then
System.out.println("\"Hello world!\"");
end
@@ -0,0 +1,8 @@
package org.drools.container.spring.u

rule "sample acçéntèd rule"
@output("\"Hello world!\"")
when
then
System.out.println("\"Hello world!\"");
end

0 comments on commit 9f3a7ac

Please sign in to comment.