CCLXmlToProperties2.xslt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed Materials - Property of IBM
  4. IBM Cognos Products: localizationkit
  5. (C) Copyright IBM Corp. 2005, 2016
  6. US Government Users Restricted Rights - Use, duplication or disclosure restricted
  7. by GSA ADP Schedule Contract with IBM Corp.
  8. -->
  9. <!--
  10. Module: CCLXmlToProperties2.xslt
  11. Purpose:
  12. Transforms a CCL Resource Input File into a Java Properties file, also insert proper prefix into strings
  13. in all Messages sections. The Java Properties file will be generated in the following format:
  14. <key> = <message text>
  15. The ONLY difference between CCLXmlToProperties.xslt and CCLXmlToProperties2.xslt is the escaping of
  16. single quotes in the generated message. This is a requirement when using the Java MessageFormat
  17. class; otherwise a single quote is treated as a special escape to ignore parameter subsitution markers.
  18. Rules:
  19. 1) Each string must defined either or both "id" and "name" attributes, which will be used to generate the <key> identifier.
  20. When both are defined, "id" will takes precedence over "<comp-name>_<sect-name>_<name>".
  21. 2) For string in Messages section with "errorCode" defined, the following format will bs used as prefix:
  22. <component_name>-<section_name>-<errorCode>.
  23. 3) All CCL parameter substitution element (regardless whether defined in Messages section or not) will be converted
  24. into Java parameter substituion format using the {} syntax as follows:
  25. <param name="xxx"/> will be converted to {xxx}.
  26. <param index="<int>"/> will be converted to {<int>-1}.
  27. When both "name" and "index" attributes are defined, the "name" attribute will takes precedence.
  28. 4) To conform with xml specification, non-meaningful whitespaces will be trimmed, and meaningful whitespace must
  29. be kept. The following are some examples:
  30. Example 1:
  31. <string id="ID1">There are
  32. <param name="count"/> items.
  33. </string>
  34. Example 2:
  35. <string id="ID1">
  36. <param name="count"/> items.
  37. </string>
  38. Example 3:
  39. <string id="ID1">
  40. <param name="count"/> items.</string>
  41. All these examples will be transformed into
  42. ID1 = CPN-SEN-xxxx {count} items.
  43. Note: For readibility, linefeed and tab had been added.
  44. -->
  45. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  46. <xsl:output method="text" encoding="UTF-8"/>
  47. <xsl:variable name="singleQuote">
  48. <xsl:text>'</xsl:text>
  49. </xsl:variable>
  50. <xsl:variable name="doubleSingleQuote">
  51. <xsl:text>''</xsl:text>
  52. </xsl:variable>
  53. <xsl:template match="/stringTable">
  54. <!-- Generate Header and Copyright notice. -->
  55. <xsl:text>#&#x0a;</xsl:text>
  56. <xsl:text># Licensed Materials - Property of IBM &#x0a;</xsl:text>
  57. <xsl:text># IBM Cognos Products: localizationkit &#x0a;</xsl:text>
  58. <xsl:text># (C) Copyright IBM Corp. 2005, 2016&#x0a;</xsl:text>
  59. <xsl:text># US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.&#x0a;</xsl:text>
  60. <xsl:text>#&#x0a;</xsl:text>
  61. <xsl:text>&#x0a;</xsl:text>
  62. <xsl:text>## DO NOT EDIT! &#x0a;</xsl:text>
  63. <xsl:text>## Generated using CCLXmlToProperties2.xslt &#x0a;</xsl:text>
  64. <xsl:text>## END OF WARNING &#x0a;</xsl:text><!--CAUTION: Some components may use this line for processing indication.-->
  65. <xsl:for-each select="./component">
  66. <xsl:variable name="compname" select="@name"/>
  67. ##
  68. ## Component: <xsl:value-of select="@name"/>
  69. ##
  70. ## Description: <xsl:value-of select="@usage"/>
  71. ##<xsl:text>&#10;</xsl:text>
  72. <xsl:for-each select="./section">
  73. <xsl:variable name="sectname" select="@name"/>
  74. #
  75. # Section: <xsl:value-of select="@name"/>
  76. #
  77. # Description: <xsl:value-of select="@usage"/>
  78. #<xsl:text>&#10;</xsl:text>
  79. <xsl:for-each select="string">
  80. <!-- generate the key, use either "id" or "<comp-name>_<sect-name>_<name>" -->
  81. <xsl:choose>
  82. <xsl:when test="@id">
  83. <xsl:value-of select="@id"/>
  84. </xsl:when>
  85. <xsl:when test="@name">
  86. <xsl:value-of select="../../@name"/>
  87. <xsl:text disable-output-escaping="yes">_</xsl:text>
  88. <xsl:value-of select="../@name"/>
  89. <xsl:text disable-output-escaping="yes">_</xsl:text>
  90. <xsl:value-of select="@name"/>
  91. </xsl:when>
  92. <xsl:otherwise>
  93. <xsl:message terminate="yes">Key Generation Error: Missing name or id.</xsl:message>
  94. </xsl:otherwise>
  95. </xsl:choose>
  96. <!-- generate the key/value separator (make sure we put a space before the '=') -->
  97. <xsl:text disable-output-escaping="yes"> = </xsl:text>
  98. <!-- generate the prefix based on rule #2 -->
  99. <xsl:if test="@errorCode and (not(../@type) or ../@type='Messages')">
  100. <xsl:value-of select="../../@name"/>
  101. <xsl:text>-</xsl:text>
  102. <xsl:value-of select="../@name"/>
  103. <xsl:text>-</xsl:text>
  104. <xsl:value-of select="@errorCode"/>
  105. <xsl:text>&#32;</xsl:text>
  106. </xsl:if>
  107. <!-- generate the message text based on rule #3 and #4 -->
  108. <xsl:variable name="msgstring">
  109. <xsl:for-each select="child::node()">
  110. <xsl:choose>
  111. <xsl:when test="name() = 'param'">
  112. <xsl:choose>
  113. <xsl:when test="@name">
  114. <xsl:text disable-output-escaping="yes">{</xsl:text>
  115. <xsl:value-of select="@name"/>
  116. <xsl:text disable-output-escaping="yes">}</xsl:text>
  117. </xsl:when>
  118. <xsl:when test="@index">
  119. <xsl:text disable-output-escaping="yes">{</xsl:text>
  120. <xsl:value-of select="@index - 1"/>
  121. <xsl:text disable-output-escaping="yes">}</xsl:text>
  122. </xsl:when>
  123. <xsl:otherwise>
  124. <xsl:message terminate="yes">Parameter Substitution Error: Missing name or index.</xsl:message>
  125. </xsl:otherwise>
  126. </xsl:choose>
  127. </xsl:when>
  128. <xsl:when test="name() = ''">
  129. <xsl:value-of select="."/>
  130. </xsl:when>
  131. <xsl:otherwise>
  132. <xsl:apply-templates select="."/>
  133. </xsl:otherwise>
  134. </xsl:choose>
  135. </xsl:for-each>
  136. </xsl:variable>
  137. <!-- Escape single quotes, creating msgstring2 -->
  138. <xsl:variable name="msgstring2">
  139. <xsl:call-template name="DoReplace">
  140. <xsl:with-param name="text" select="$msgstring"/>
  141. <xsl:with-param name="replace" select="$singleQuote"/>
  142. <xsl:with-param name="by" select="$doubleSingleQuote"/>
  143. </xsl:call-template>
  144. </xsl:variable>
  145. <xsl:choose>
  146. <xsl:when test="@whiteSpace and @whiteSpace='preserve'">
  147. <xsl:text disable-output-escaping="yes">\</xsl:text>
  148. <xsl:value-of select="$msgstring2"/>
  149. </xsl:when>
  150. <xsl:otherwise>
  151. <xsl:value-of select="normalize-space($msgstring2)"/>
  152. </xsl:otherwise>
  153. </xsl:choose>
  154. <xsl:text>&#10;</xsl:text>
  155. </xsl:for-each>
  156. </xsl:for-each>
  157. </xsl:for-each>
  158. </xsl:template>
  159. <!-- copy everything by default -->
  160. <xsl:template match="*">
  161. <xsl:copy>
  162. <xsl:copy-of select="@*"/>
  163. <xsl:apply-templates/>
  164. </xsl:copy>
  165. </xsl:template>
  166. <!-- DoReplace method for escaping single quotes in the message text -->
  167. <xsl:template name="DoReplace">
  168. <xsl:param name="text"/>
  169. <xsl:param name="replace"/>
  170. <xsl:param name="by"/>
  171. <xsl:choose>
  172. <xsl:when test="contains( $text, $replace )">
  173. <xsl:value-of select="substring-before( $text, $replace )"/>
  174. <xsl:value-of select="$by"/>
  175. <xsl:call-template name="DoReplace">
  176. <xsl:with-param name="text" select="substring-after( $text, $replace )"/>
  177. <xsl:with-param name="replace" select="$replace"/>
  178. <xsl:with-param name="by" select="$by"/>
  179. </xsl:call-template>
  180. </xsl:when>
  181. <xsl:otherwise>
  182. <xsl:value-of select="$text"/>
  183. </xsl:otherwise>
  184. </xsl:choose>
  185. </xsl:template>
  186. </xsl:stylesheet>