I want to get a date from a website, which I get via the HTTP-binding. The problem is that the encoding isn’t correctly interpreted by the binding and so the umlauts aren’t correctly shown. The Basic UI just show a ? instead of the “ä” in März. How can I fix this?
org.openhab.core.transform.TransformationException: transformation throws exception
at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:67) [227:org.openhab.core.compat1x:2.2.0]
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:194) [221:org.openhab.binding.http:1.11.0]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:144) [227:org.openhab.core.compat1x:2.2.0]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:166) [227:org.openhab.core.compat1x:2.2.0]
2018-03-09 20:43:13.890 [ERROR] [t.internal.XsltTransformationService] - transformation throws exception
javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Markup im Dokument vor dem Root-Element muss ordnungsgemäß formatiert sein.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:740) [?:?]
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:343) [?:?]
at org.eclipse.smarthome.transform.xslt.internal.XsltTransformationService.transform(XsltTransformationService.java:83) [213:org.eclipse.smarthome.transform.xslt:0.10.0.b1]
at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:65) [227:org.openhab.core.compat1x:2.2.0]
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:194) [221:org.openhab.binding.http:1.11.0]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:144) [227:org.openhab.core.compat1x:2.2.0]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:166) [227:org.openhab.core.compat1x:2.2.0]
Caused by: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Markup im Dokument vor dem Root-Element muss ordnungsgemäß formatiert sein.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getDOM(TransformerImpl.java:570) ~[?:?]
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:730) ~[?:?]
... 6 more
Caused by: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Markup im Dokument vor dem Root-Element muss ordnungsgemäß formatiert sein.
at com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager.getDTM(XSLTCDTMManager.java:427) ~[?:?]
at com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager.getDTM(XSLTCDTMManager.java:215) ~[?:?]
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getDOM(TransformerImpl.java:548) ~[?:?]
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:730) ~[?:?]
... 6 more
2018-03-09 20:43:13.893 [WARN ] [ab.binding.http.internal.HttpBinding] - Transformation 'XSLT(temp-date.xsl)' threw an exception. [response=<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
...
import java.nio.charset.Charset
import java.nio.ByteBuffer
import java.nio.CharBuffer
rule "Test Encoding"
when
Item Test changed
then
var String input = new String("Bär".getBytes, "ISO-8859-1")
var Charset utf8charset = Charset.forName("UTF-8");
var ByteBuffer inputBuffer = ByteBuffer.wrap(input.getBytes("ISO-8859-1") );
var CharBuffer fixed = utf8charset.decode(inputBuffer)
logInfo("Test Encoding", input +" "+ fixed.toString )
end
var String input = Temp_Date.state
var byte[] bytes = input.getBytes("ISO-8859-1")
var String converted = new String(bytes, "UTF-8")
sendCommand(FinalDate, converted)
2018-03-10 22:50:42.409 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Test Encoding': An error occurred during the script execution: Could not invoke method: java.lang.String.getBytes(java.lang.String) on instance: 10. M�rz 2018
So with this I get the following output for input:
11. M�rz 2018
When I use your rule:
import java.nio.charset.Charset
import java.nio.ByteBuffer
import java.nio.CharBuffer
rule "Change encoding of weatherstation date"
when
Item Temp_Date received update
then
var String input = new String(Temp_Date.state.toString.getBytes, "ISO-8859-1")
var Charset utf8charset = Charset.forName("UTF-8");
var ByteBuffer inputBuffer = ByteBuffer.wrap(input.getBytes("ISO-8859-1") );
var CharBuffer fixed = utf8charset.decode(inputBuffer)
logInfo("Test Encoding", input +" "+ fixed )
end
I get this output:
11. M�rz 2018 11. M�rz 2018
With that rule:
rule "Change encoding of weatherstation date"
when
Item Temp_Date_str received update
then
var input = Temp_Date_str.state.toString
var byte[] bytes = input.getBytes("ISO-8859-1")
var String converted = new String(bytes, "UTF-8")
Temp_Date.postUpdate(converted.replace('?','ä'))
logInfo("Test Encoding", input +" "+ Temp_Date.state )
end
So after thinking about this, i think: it is a bug.
The HTTP binding does not consider the encoding and stuffs the result into a java string, which should be utf-16, and with that the encoding informations is allready lost. It can not be restored.
The “�” is the replacement character for all unknown character which means there is no valid solution to restore the encoding as all “äüöß” and so on just gets replaced with it.