Create a Customized Mapping
The mappings element in a HydraExpress project file defines a mapping between each XML element and a C++ type to be used during unmarshaling. For more complete information on the required and optional parts of a <mappings> element of a HydraExpress project file, see “Creating Customized Mappings,” in the HydraExpress XML Binding Development Guide.
The mapping must provide three items:
*The C++ type
*The code to be used to convert that type to and from the basic string type used (either string or RWCString). If you don’t specify any particular conversion code or information, the default is UTF-8.
*The header file that provides the conversion code
The following HydraExpress project file maps XML Schema elements to C++ types and identifies two converters, one that converts from UTF-8 to the desired encoding, and one that converts back from the desired encoding to UTF-8.
<option name='project-name' value='CharConvert'/>
<type xsdType="string" cppType="std::string"/> 1
<typeinfo cppType="std::string" include="international/myconverter.h"> 2
<fromString>convertToShiftJIS($value$)</fromString> 3
<toString>convertFromShiftJIS($value$)</toString> 4
<defaultValue>std::string("$value$")</defaultValue> 5
//1 The attribute xsdtype maps the XML Schema type “string” to the desired C++ type, in this case the default “std::string”.
//2 Each cppType attribute under the type element requires the existence of a typeinfo element to define the C++ type that HydraExpress should generate. In this case, the typeinfo element contains an include attribute to specify the header file, myconverter.h, to perform the character conversion. See below for the possible contents of such a file.
Note that the mapping includes the path to the include file, given a project titled international.
//3 The fromString subelement provides the logic to convert the parent element type from a std::string instance to a user-specified type instance when unmarshaling. The $value$ parameter represents the std::string variable to be converted.
In this case, this line sets up a conversion from UTF-8 to ShiftJIS, provided by the referenced header file myconverter.h.
//4 The toString subelement provides the logic to convert the parent element type from a user-specified type instance to a string instance. The $value$ parameter represents the user-defined type instance to convert to a string.
In this case, this line sets up a conversion from ShiftJIS to UTF-8, provided by the referenced header file myconverter.h.
//5 Subelements <defaultValue> and <initialValue> required for mapping.
Following is one possible way to create a character converter. This example uses the conversion methods of utility class rwsf::XmlUtils.
// myconverter.h
#include <rwsf/core/XmlUtils.h> //1
#include <rwsf/core/CString.h>
#include <string>
class MyConverter
static std::string convertFromShiftJIS(const std::string& str) { //2
return rwsf::XmlUtils::convertCharset(str, "Shift_JIS", "UTF-8");
static std::string convertToShiftJIS(const std::string& str) { //3
return rwsf::XmlUtils::convertCharset(str, "UTF-8", "Shift_JIS");
//1 Necessary includes
//2 Converts str from ShiftJIS to UTF-8.
//3 Converts str from UTF-8 to ShiftJIS.