[jboss-jira] [JBoss JIRA] (DROOLS-4662) Resources added through KieHelper get interpreted using the default character set
Mario Fusco (Jira)
issues at jboss.org
Wed Dec 18 10:55:19 EST 2019
[ https://issues.redhat.com/browse/DROOLS-4662?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13937346#comment-13937346 ]
Mario Fusco edited comment on DROOLS-4662 at 12/18/19 10:53 AM:
----------------------------------------------------------------
I tried on master the following test case setting the file.enconding as you suggested but it works for me
{code}
@Test
public void testI18NKieHelper() throws UnsupportedEncodingException {
String testString = "\u2200x. \u2203y. x < y";
String rules =
"rule \"foo\"\n" +
" when\n" +
" not String()\n" +
" then\n" +
" insert(\"" + testString + "\");\n" +
" end\n";
KieServices kieServices = KieServices.Factory.get();
KieHelper kieHelper = new KieHelper();
Resource rulesResource = kieServices.getResources().newByteArrayResource(rules.getBytes("UTF-8"), "UTF-8");
rulesResource.setTargetPath("src/main/resources/rules.drl");
kieHelper.addResource(rulesResource);
KieBase kieBase = kieHelper.build();
KieSession kieSession = kieBase.newKieSession();
kieSession.fireAllRules();
String out = (String) kieSession.getObjects().iterator().next();
System.out.println(out);
assertEquals(out, testString);
}
{code}
and prints
∀x. ∃y. x < y
I also tried the opposite with this slightly modified test case
{code}
@Test
public void testI18NKieHelper() throws UnsupportedEncodingException {
String testString = new String("\u2200x. \u2203y. x < y".getBytes( "latin1" ));
String rules =
"rule \"foo\"\n" +
" when\n" +
" not String()\n" +
" then\n" +
" insert(\"" + testString + "\");\n" +
" end\n";
KieServices kieServices = KieServices.Factory.get();
KieHelper kieHelper = new KieHelper();
Resource rulesResource = kieServices.getResources().newByteArrayResource(rules.getBytes("latin1"), "latin1");
rulesResource.setTargetPath("src/main/resources/rules.drl");
kieHelper.addResource(rulesResource);
KieBase kieBase = kieHelper.build();
KieSession kieSession = kieBase.newKieSession();
kieSession.fireAllRules();
String out = (String) kieSession.getObjects().iterator().next();
System.out.println(out);
assertEquals(out, testString);
}
{code}
e.g. setting both the encoding of string and rule resource to latin1 and this also works adn prints as expected
?x. ?y. x < y
As a final note consider that in all cases the KieHelper class is only intended for internal use and it is not part of our API.
was (Author: mfusco):
I tried on master the following test case setting the file.enconding as you suggested but it works for me
{code}
@Test
public void testI18NKieHelper() throws UnsupportedEncodingException {
String testString = "\u2200x. \u2203y. x < y";
String rules =
"rule \"foo\"\n" +
" when\n" +
" not String()\n" +
" then\n" +
" insert(\"" + testString + "\");\n" +
" end\n";
KieServices kieServices = KieServices.Factory.get();
KieHelper kieHelper = new KieHelper();
Resource rulesResource = kieServices.getResources().newByteArrayResource(rules.getBytes("UTF-8"), "UTF-8");
rulesResource.setTargetPath("src/main/resources/rules.drl");
kieHelper.addResource(rulesResource);
KieBase kieBase = kieHelper.build();
KieSession kieSession = kieBase.newKieSession();
kieSession.fireAllRules();
String out = (String) kieSession.getObjects().iterator().next();
System.out.println(out);
assertEquals(out, testString);
}
{code}
and prints
∀x. ∃y. x < y
I also tried the opposite with this slightly modified test case
{code}
@Test
public void testI18NKieHelper() throws UnsupportedEncodingException {
String testString = new String("\u2200x. \u2203y. x < y".getBytes( "latin1" ));
String rules =
"rule \"foo\"\n" +
" when\n" +
" not String()\n" +
" then\n" +
" insert(\"" + testString + "\");\n" +
" end\n";
KieServices kieServices = KieServices.Factory.get();
KieHelper kieHelper = new KieHelper();
Resource rulesResource = kieServices.getResources().newByteArrayResource(rules.getBytes("latin1"), "latin1");
rulesResource.setTargetPath("src/main/resources/rules.drl");
kieHelper.addResource(rulesResource);
KieBase kieBase = kieHelper.build();
KieSession kieSession = kieBase.newKieSession();
kieSession.fireAllRules();
String out = (String) kieSession.getObjects().iterator().next();
System.out.println(out);
assertEquals(out, testString);
}
{code]
e.g. setting both the encoding of string and rule resource to latin1 and this also works adn prints as expected
?x. ?y. x < y
As a final note consider that in all cases the KieHelper class is only intended for internal use and it is not part of our API.
> Resources added through KieHelper get interpreted using the default character set
> ---------------------------------------------------------------------------------
>
> Key: DROOLS-4662
> URL: https://issues.redhat.com/browse/DROOLS-4662
> Project: Drools
> Issue Type: Bug
> Affects Versions: 7.10.0.Final, 7.20.0.Final, 7.28.0.Final
> Environment: Windows. Can be reproduced on other OSes using "-Dfile.encoding=latin1".
> Reporter: Max Zerzouri
> Assignee: Mario Fusco
> Priority: Major
> Attachments: Test.java
>
>
> When a resource is added using {{KieHelper.addResource}}, any specified encoding is ignored when it is added to the {{MemoryFileSystem}} by {{KieFileSystemImpl.write(Resource)}} (effectively assuming the {{InputStream}} is already encoded in UTF-8).
> Additionally, when the resource is read again by {{AbstractKieModule.getResource}}, it constructs a {{Resource}} object with no encoding specified, whereas it should probably specify an encoding of UTF-8, since it seems that sources are considered to be UTF-8 already at least in the case of the {{MemoryKieModule}}.
> I've attached a fairly minimal test case where the rules bytes are passed in UTF-8 with the encoding explicitly specified. This currently produces an error when the default file encoding is not UTF-8 (eg, by passing {{-Dfile.encoding=latin1}} as a JVM argument, or by using the default arguments on Windows).
--
This message was sent by Atlassian Jira
(v7.13.8#713008)
More information about the jboss-jira
mailing list