You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.9 KiB

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * $Id: Xlat_CppSrc.cpp 470094 2006-11-01 20:41:30Z amassari $
  19. */
  20. // ---------------------------------------------------------------------------
  21. // Includes
  22. // ---------------------------------------------------------------------------
  23. #include "Xlat.hpp"
  24. // ---------------------------------------------------------------------------
  25. // CppSrcFormatter: Implementation of the formatter interface
  26. // ---------------------------------------------------------------------------
  27. void CppSrcFormatter::endDomain(const XMLCh* const domainName
  28. , const unsigned int msgCount)
  29. {
  30. // And close out the array declaration
  31. fwprintf(fOutFl, L"\n};\n");
  32. // Output the const size value
  33. fwprintf(fOutFl, L"const unsigned int %s%s = %d;\n\n", xmlStrToPrintable(fCurDomainName), longChars("Size"), msgCount);
  34. releasePrintableStr
  35. }
  36. void CppSrcFormatter::endMsgType(const MsgTypes type)
  37. {
  38. if (fFirst)
  39. {
  40. fwprintf(fOutFl, L" { ");
  41. fFirst = false;
  42. }
  43. else
  44. {
  45. fwprintf(fOutFl, L" , { ");
  46. }
  47. XMLCh* rawData = typePrefixes[type];
  48. while (*rawData)
  49. fwprintf(fOutFl, L"0x%04lX,", *rawData++);
  50. XMLCh* tmpXMLStr = rawData = XMLString::transcode("End");
  51. while (*rawData)
  52. fwprintf(fOutFl, L"0x%04lX,", *rawData++);
  53. XMLString::release(&tmpXMLStr);
  54. fwprintf(fOutFl, L"0x00 }\n");
  55. }
  56. void CppSrcFormatter::endOutput()
  57. {
  58. fwprintf
  59. (
  60. fOutFl,
  61. L"XERCES_CPP_NAMESPACE_END\n\n"
  62. );
  63. // Close the output file
  64. fclose(fOutFl);
  65. }
  66. void
  67. CppSrcFormatter::nextMessage(const XMLCh* const msgText
  68. , const XMLCh* const msgId
  69. , const unsigned int messageId
  70. , const unsigned int curId)
  71. {
  72. //
  73. // We do not transcode to the output format in this case. Instead we
  74. // just store the straight Unicode format. Because we cannot assume 'L'
  75. // type prefix support, we have to put them out as numeric character
  76. // values.
  77. //
  78. const XMLCh* rawData = msgText;
  79. if (fFirst)
  80. {
  81. fwprintf(fOutFl, L" { ");
  82. fFirst = false;
  83. }
  84. else
  85. {
  86. fwprintf(fOutFl, L" , { ");
  87. }
  88. unsigned int i = 0;
  89. while (*rawData) {
  90. fwprintf(fOutFl, L"0x%04lX,", *rawData++);
  91. if (++i == 35) {
  92. i = 0;
  93. fwprintf(fOutFl, L"\n");
  94. fwprintf(fOutFl, L" ");
  95. }
  96. }
  97. fwprintf(fOutFl, L"0x00 }\n");
  98. }
  99. void CppSrcFormatter::startDomain( const XMLCh* const domainName
  100. , const XMLCh* const)
  101. {
  102. //
  103. // We have a different array name for each domain, so store that for
  104. // later use and for use below.
  105. //
  106. XMLString::release(&fCurDomainName);
  107. if (!XMLString::compareString(XMLUni::fgXMLErrDomain, domainName))
  108. {
  109. fCurDomainName = XMLString::transcode("gXMLErrArray");
  110. }
  111. else if (!XMLString::compareString(XMLUni::fgExceptDomain, domainName))
  112. {
  113. fCurDomainName = XMLString::transcode("gXMLExceptArray");
  114. }
  115. else if (!XMLString::compareString(XMLUni::fgValidityDomain, domainName))
  116. {
  117. fCurDomainName = XMLString::transcode("gXMLValidityArray");
  118. }
  119. else if (!XMLString::compareString(XMLUni::fgXMLDOMMsgDomain, domainName))
  120. {
  121. fCurDomainName = XMLString::transcode("gXMLDOMMsgArray");
  122. }
  123. else
  124. {
  125. wprintf(L"Unknown message domain: %s\n", domainName);
  126. throw ErrReturn_SrcFmtError;
  127. }
  128. //
  129. // Output the leading part of the array declaration. Its just an
  130. // array of pointers to Unicode chars.
  131. //
  132. fwprintf(fOutFl, L"const XMLCh %s[][128] = \n{\n", xmlStrToPrintable(fCurDomainName));
  133. releasePrintableStr
  134. // Reset the first message trigger
  135. fFirst = true;
  136. }
  137. void CppSrcFormatter::startMsgType(const MsgTypes type)
  138. {
  139. if (fFirst)
  140. {
  141. fwprintf(fOutFl, L" { ");
  142. fFirst = false;
  143. }
  144. else
  145. {
  146. fwprintf(fOutFl, L" , { ");
  147. }
  148. XMLCh* rawData = typePrefixes[type];
  149. while (*rawData)
  150. fwprintf(fOutFl, L"0x%04lX,", *rawData++);
  151. XMLCh *tmpXMLStr = rawData = XMLString::transcode("Start");
  152. while (*rawData)
  153. fwprintf(fOutFl, L"0x%04lX,", *rawData++);
  154. XMLString::release(&tmpXMLStr);
  155. fwprintf(fOutFl, L"0x00 }\n");
  156. }
  157. void CppSrcFormatter::startOutput( const XMLCh* const locale
  158. , const XMLCh* const outPath)
  159. {
  160. //
  161. // Ok, lets try to open the the output file. All of the messages
  162. // for all the domains are put into a single Cpp file, which can be
  163. // compiled into the program.
  164. //
  165. // CppErrMsgs_xxxx.cpp
  166. //
  167. // where xxx is the locale suffix passed in.
  168. //
  169. const unsigned int bufSize = 4095;
  170. XMLCh tmpBuf[bufSize + 1];
  171. tmpBuf[0] = 0;
  172. XMLCh *tmpXMLStr = XMLString::transcode("XercesMessages_");
  173. XMLCh *tmpXMLStr2 = XMLString::transcode(".hpp");
  174. XMLString::catString(tmpBuf, outPath);
  175. XMLString::catString(tmpBuf, tmpXMLStr );
  176. XMLString::catString(tmpBuf, locale);
  177. XMLString::catString(tmpBuf, tmpXMLStr2 );
  178. XMLString::release(&tmpXMLStr);
  179. XMLString::release(&tmpXMLStr2);
  180. char *tmpStr = XMLString::transcode(tmpBuf);
  181. fOutFl = fopen(tmpStr, "wt");
  182. XMLString::release(&tmpStr);
  183. if ((!fOutFl) || (fwide(fOutFl, 1) < 0))
  184. {
  185. wprintf(L"Could not open the output file: %s\n\n", tmpBuf);
  186. throw ErrReturn_OutFileOpenFailed;
  187. }
  188. //
  189. // Ok, lets output the grunt data at the start of the file. We put out a
  190. // comment that indicates its a generated file, and the title string.
  191. //
  192. fwprintf
  193. (
  194. fOutFl
  195. , L"// ----------------------------------------------------------------\n"
  196. L"// This file was generated from the XML error message source.\n"
  197. L"// so do not edit this file directly!!\n"
  198. L"// ----------------------------------------------------------------\n\n"
  199. L"#include <xercesc/util/XercesDefs.hpp>\n\n"
  200. L"XERCES_CPP_NAMESPACE_BEGIN\n\n"
  201. );
  202. }