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.

188 lines
5.8 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_ICUResourceBundle.cpp 698386 2008-09-23 22:54:08Z amassari $
  19. */
  20. // ---------------------------------------------------------------------------
  21. // Includes
  22. // ---------------------------------------------------------------------------
  23. #include "Xlat.hpp"
  24. // ---------------------------------------------------------------------------
  25. // ICUResBundFormatter: Constructors and Destructor
  26. // ---------------------------------------------------------------------------
  27. ICUResBundFormatter::ICUResBundFormatter()
  28. :fOutFl(0)
  29. ,fTranscoder(0)
  30. {
  31. //
  32. // Try to create a transcoder for the format that we were told
  33. // to output in.
  34. //
  35. // <TBD> Right now we are just using an LCP transcoding, which is not
  36. // really the right thing to do!
  37. //
  38. fTranscoder = XMLPlatformUtils::fgTransService->makeNewLCPTranscoder(XMLPlatformUtils::fgMemoryManager);
  39. if (!fTranscoder)
  40. {
  41. wprintf(L"Could not create LCP transcoder\n");
  42. throw ErrReturn_NoTranscoder;
  43. }
  44. }
  45. ICUResBundFormatter::~ICUResBundFormatter()
  46. {
  47. delete fTranscoder;
  48. }
  49. // ---------------------------------------------------------------------------
  50. // ICUResBundFormatter: Implementation of the formatter interface
  51. // ---------------------------------------------------------------------------
  52. void ICUResBundFormatter::endDomain(const XMLCh* const domainName
  53. , const unsigned int msgCount)
  54. {
  55. fwprintf(fOutFl, L"\t\t} \n\n");
  56. }
  57. void ICUResBundFormatter::endMsgType(const MsgTypes type)
  58. {
  59. #ifndef ICU_RESBUNDLE_IN_TABLE_FORM
  60. fwprintf(fOutFl, L"\t\t\"%s End \" ,\n", xmlStrToPrintable(typePrefixes[type]) );
  61. releasePrintableStr
  62. #endif
  63. }
  64. void ICUResBundFormatter::endOutput()
  65. {
  66. // Close the output files
  67. fwprintf(fOutFl, L"} \n\n");
  68. fclose(fOutFl);
  69. }
  70. void
  71. ICUResBundFormatter::nextMessage(const XMLCh* const msgText
  72. , const XMLCh* const msgId
  73. , const unsigned int messageId
  74. , const unsigned int curId)
  75. {
  76. //
  77. // We have to transcode the message text to UTF-8 in order to be able
  78. // to write it out to a message catalog (which is not Unicode enabled.)
  79. // If the text is just US-ASCII, this won't have any effect, but don't
  80. // bother checking, just do it simple and stupid.
  81. //
  82. //
  83. // array_name {
  84. // "xxx" ,
  85. // "yyy" ,
  86. // }
  87. //
  88. //
  89. // table_name {
  90. // AB { "xxx" }
  91. // CD { "yyy" }
  92. // }
  93. //
  94. #ifdef ICU_RESBUNDLE_IN_TABLE_FORM
  95. fwprintf(fOutFl, L"\t\t %3d { \"%s\" } \n", curId, xmlStrToPrintable(msgText));
  96. releasePrintableStr
  97. // need to print leading 0 if less than 100, not tested yet
  98. #else
  99. fwprintf(fOutFl, L"\t\t\"%s\" ,\n", xmlStrToPrintable(msgText));
  100. releasePrintableStr
  101. // need a space between the last character and the closing "
  102. #endif
  103. }
  104. void ICUResBundFormatter::startDomain(const XMLCh* const domainName
  105. , const XMLCh* const nameSpace)
  106. {
  107. //
  108. // Output:
  109. // // an array
  110. // domainName {
  111. //
  112. int index = XMLString::lastIndexOf(domainName, chForwardSlash);
  113. #ifdef ICU_RESBUNDLE_IN_TABLE_FORM
  114. fwprintf(fOutFl, L"\n\t // a table \n");
  115. #else
  116. fwprintf(fOutFl, L"\n\t // an array \n");
  117. #endif
  118. fwprintf(fOutFl, L"\t%s { \n" , xmlStrToPrintable(&(domainName[index+1])));
  119. releasePrintableStr
  120. }
  121. void ICUResBundFormatter::startMsgType(const MsgTypes type)
  122. {
  123. #ifndef ICU_RESBUNDLE_IN_TABLE_FORM
  124. fwprintf(fOutFl, L"\t\t\"%s Start \" , \n", xmlStrToPrintable(typePrefixes[type]) );
  125. releasePrintableStr
  126. #endif
  127. }
  128. void ICUResBundFormatter::startOutput(const XMLCh* const msgLocale
  129. , const XMLCh* const outPath)
  130. {
  131. //
  132. // Ok, lets try to open the the output file. All of the messages
  133. // for all the domains are put into a single Msg file, which can be
  134. // compiled into the program.
  135. //
  136. // CppErrMsgs_xxxx.Msg
  137. //
  138. // where xxx is the locale suffix passed in.
  139. //
  140. const unsigned int bufSize = 4095;
  141. XMLCh tmpBuf[bufSize + 1];
  142. tmpBuf[0] = 0;
  143. XMLCh *tmpXMLStr = XMLString::transcode(".txt");
  144. // ICU Resource Bundles now uses "root" as locale
  145. XMLCh* locale = XMLString::transcode ("root");
  146. XMLString::catString(tmpBuf, outPath);
  147. XMLString::catString(tmpBuf, locale);
  148. XMLString::catString(tmpBuf, tmpXMLStr );
  149. XMLString::release(&tmpXMLStr);
  150. char *tmpStr = XMLString::transcode(tmpBuf);
  151. fOutFl = fopen(tmpStr, "wt");
  152. XMLString::release(&tmpStr);
  153. if ((!fOutFl) || (fwide(fOutFl, 1) < 0))
  154. {
  155. wprintf(L"Could not open the output file: %s\n\n", xmlStrToPrintable(tmpBuf) );
  156. releasePrintableStr
  157. throw ErrReturn_OutFileOpenFailed;
  158. }
  159. // Set the message delimiter
  160. fwprintf(fOutFl, L"%s { \n", xmlStrToPrintable(locale) );
  161. releasePrintableStr
  162. XMLString::release(&locale);
  163. }