| | | | Browse by category |
When parsing non-UTF-8 XML documents, LEIF automatically converts the string data to UTF-8 before unmarshaling and on marshaling, the string data remains in UTF-8 format.
Cause
This article follows on from section 9.6 Internationalizing your Applications in the XML Object LinkUser's Guide and shows all the necessary steps required to set up custom character conversions and also provides a working example for Windows.
Action
These are the basic steps required to convert the string data in your C++ classes to and from any character encoding:
- Edit or create a configuration file identifying the character encoding of your choice and the conversion routine to use. This allows LEIF to perform conversions automatically while marshaling and unmarshaling.
- Link in the International Components of Unicode (ICU) libraries.
- Generate code.
- Edit the main() method to initialize the LEIF ICU libraries (if required).
The following working example (also attached) shows how to convert from UTF-8 to ISO-8859-1 on a windows environment. For Unix environments, please edit the properties file accordingly.
The following configuration file will convert from UTF-8 to ISO-8895-1:
<config xmlns="http://www.roguewave.com/ratchet">
<mappings>
<type xsdType="string" cppType="std::string"/>
<<typeinfo cppType="std::string" include="rw/leif/core/XmlUtils.h" returnConstRefOnly="true">
<fromString>LEIF::XmlUtils::convertCharset($value$, "UTF-8","ISO-8859-1").data()</fromString>
<toString>LEIF::XmlUtils::convertCharset($value$.data(),"ISO-8859-1","UTF-8")</toString>
<defaultValue>"$value$"</defaultValue>
<initialValue></initialValue>
</typeinfo>
</mappings>
</config>
The following properties file includes the required LEIF ICU libraries and their location along with the name of the configuration file:
additionalLibs=/LIBPATH:$(RW_HOME)/3rdparty/icu-2.4/windows/ia32-msvc6/lib leificu$(TAG).lib tls$(TAG).lib i18n$(TAG).lib icuuc.lib
additionalIncludes=-I$(RW_HOME)/3rdparty/icu-2.4/windows/ia32-msvc6/include
config=conversion.cfg
outdir=out21
Once the code has been generated add the following to the main() method:
LEIF_REGISTER_DEFAULT_UCONVERTER();
Please include the following header files:
#include <rw/leif/core/UConverter.h>
#include <rw/leif/core/UConverterFactory.h>
The attached test case i18nTest.zip contains all the necessary files and a set of instructions to demonstrate this on Windows with LEIF 2.1 and LEIF 2.2.