Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JBRULES-3633 resource encoding #38

Merged
merged 1 commit into from Nov 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -30,6 +30,8 @@ public class DroolsResourceAdapter
private Resource resource;
private ResourceType resourceType;
private ResourceConfiguration resourceConfiguration;
private String resourceName;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the resourceName variable pls (unless there's a good reason to keep it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resourceName needs to be stored as there is I see way to change the encoding after creation. So I need to re-create the resource with the correct resource name and encoding.

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is setEncoding() is called before or after setResource() or arbitrary?

After: setEncoding() should assert that resource is a ClassPathResource and do this.resource.setEncoding()
Before: setResource() should assert that resource String starts with "classpath:" if encoding != null
Both: Same as after/before with null-safe checks

}

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