BUXXmlToJavascript.xslt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. +========================================================================+
  4. | IBM Confidential
  5. | OCO Source Materials
  6. | IBM Cognos Products: BUX
  7. | (C) Copyright IBM Corp. 2009, 2012
  8. |
  9. | The source code for this program is not published or otherwise
  10. | divested of its trade secrets, irrespective of what has been deposited
  11. | with the U.S. Copyright Office.
  12. +========================================================================+
  13. -->
  14. <!--
  15. Module: CCLXmlToProperties.xslt
  16. Purpose:
  17. Transforms a CCL Resource Input File into a Java Properties file, also insert proper prefix into strings
  18. in all Messages sections. The Java Properties file will be generated in the following format:
  19. <key> = <message text>
  20. Rules:
  21. 1) Each string must defined either or both "id" and "name" attributes, which will be used to generate the <key> identifier.
  22. When both are defined, "id" will takes precedence over "<comp-name>_<sect-name>_<name>".
  23. 2) For string in Messages section with "errorCode" defined, the following format will bs used as prefix:
  24. <component_name>-<section_name>-<errorCode>.
  25. 3) All CCL parameter substitution element (regardless whether defined in Messages section or not) will be converted
  26. into Java parameter substituion format using the {} syntax as follows:
  27. <param name="xxx"/> will be converted to {xxx}.
  28. <param index="<int>"/> will be converted to {<int>-1}.
  29. When both "name" and "index" attributes are defined, the "name" attribute will takes precedence.
  30. 4) To conform with xml specification, non-meaningful whitespaces will be trimmed, and meaningful whitespace must
  31. be kept. The following are some examples:
  32. Example 1:
  33. <string id="ID1">There are
  34. <param name="count"/> items.
  35. </string>
  36. Example 2:
  37. <string id="ID1">
  38. <param name="count"/> items.
  39. </string>
  40. Example 3:
  41. <string id="ID1">
  42. <param name="count"/> items.</string>
  43. All these examples will be transformed into
  44. ID1 = CPN-SEN-xxxx {count} items.
  45. Note: For readibility, linefeed and tab had been added.
  46. -->
  47. <xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  48. <xsl:output method="text" encoding="UTF-8"/>
  49. <xsl:template match="/stringTable">
  50. <xsl:text>/*&#10;</xsl:text>
  51. <xsl:text> *+------------------------------------------------------------------------+&#10;</xsl:text>
  52. <xsl:text> *| IBM Confidential&#10;</xsl:text>
  53. <xsl:text> *| OCO Source Materials&#10;</xsl:text>
  54. <xsl:text> *| IBM Cognos Products: BUX&#10;</xsl:text>
  55. <xsl:text> *| (C) Copyright IBM Corp. 2009, 2012&#10;</xsl:text>
  56. <xsl:text> *|&#10;</xsl:text>
  57. <xsl:text> *| The source code for this program is not published or otherwise&#10;</xsl:text>
  58. <xsl:text> *| divested of its trade secrets, irrespective of what has been deposited&#10;</xsl:text>
  59. <xsl:text> *| with the U.S. Copyright Office.&#10;</xsl:text>
  60. <xsl:text> *+------------------------------------------------------------------------+&#10;</xsl:text>
  61. <xsl:text> */&#10;</xsl:text>
  62. <xsl:text>({</xsl:text>
  63. <xsl:for-each select="./component">
  64. <xsl:variable name="compname" select="@name"/>
  65. <xsl:for-each select="section">
  66. <xsl:if test="preceding-sibling::section">
  67. <xsl:text>,</xsl:text>
  68. </xsl:if>
  69. <xsl:text>&#10; "</xsl:text>
  70. <xsl:value-of select="@name"/>
  71. <xsl:text>": { // </xsl:text>
  72. <xsl:value-of select="@usage"/>
  73. <xsl:text>&#10;</xsl:text>
  74. <xsl:for-each select="string">
  75. <!-- generate the key, use either "id" or "<comp-name>_<sect-name>_<name>" -->
  76. <xsl:if test="preceding-sibling::string">
  77. <xsl:text>,&#10;</xsl:text>
  78. </xsl:if>
  79. <xsl:variable name="var">
  80. <xsl:choose>
  81. <xsl:when test="@id">
  82. <xsl:value-of select="@id"/>
  83. </xsl:when>
  84. <xsl:when test="@name">
  85. <xsl:value-of select="../../@name"/>
  86. <xsl:text disable-output-escaping="yes">_</xsl:text>
  87. <xsl:value-of select="../@name"/>
  88. <xsl:text disable-output-escaping="yes">_</xsl:text>
  89. <xsl:value-of select="@name"/>
  90. </xsl:when>
  91. <xsl:otherwise>
  92. <xsl:message terminate="yes">Key Generation Error: Missing name or id.</xsl:message>
  93. </xsl:otherwise>
  94. </xsl:choose>
  95. </xsl:variable>
  96. <xsl:text> "</xsl:text>
  97. <xsl:value-of select="translate($var, 'abcdefghijklmnopqrstuvwxyz .-', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ___')"/>
  98. <xsl:text>"</xsl:text>
  99. <!-- generate the key/value separator (make sure we put a space before the '=') -->
  100. <xsl:text>: "</xsl:text>
  101. <!-- generate the prefix based on rule #2 -->
  102. <xsl:if test="@errorCode and (not(../@type) or ../@type='Messages')">
  103. <xsl:value-of select="../../@name"/>
  104. <xsl:text>-</xsl:text>
  105. <xsl:value-of select="../@name"/>
  106. <xsl:text>-</xsl:text>
  107. <xsl:value-of select="@errorCode"/>
  108. <xsl:text>&#32;</xsl:text>
  109. </xsl:if>
  110. <!-- generate the message text based on rule #3 and #4 -->
  111. <xsl:variable name="msgstring">
  112. <xsl:for-each select="child::node()">
  113. <xsl:choose>
  114. <xsl:when test="name() = 'param'">
  115. <xsl:choose>
  116. <xsl:when test="@name">
  117. <xsl:text disable-output-escaping="yes">${</xsl:text>
  118. <xsl:value-of select="@name"/>
  119. <xsl:text disable-output-escaping="yes">}</xsl:text>
  120. </xsl:when>
  121. <xsl:when test="@index">
  122. <xsl:text disable-output-escaping="yes">${</xsl:text>
  123. <xsl:value-of select="@index - 1"/>
  124. <xsl:text disable-output-escaping="yes">}</xsl:text>
  125. </xsl:when>
  126. <xsl:otherwise>
  127. <xsl:message terminate="yes">Parameter Substitution Error: Missing name or index.</xsl:message>
  128. </xsl:otherwise>
  129. </xsl:choose>
  130. </xsl:when>
  131. <xsl:when test="name() = ''">
  132. <xsl:value-of select="."/>
  133. </xsl:when>
  134. <xsl:otherwise>
  135. <xsl:apply-templates select="."/>
  136. </xsl:otherwise>
  137. </xsl:choose>
  138. </xsl:for-each>
  139. </xsl:variable>
  140. <xsl:variable name="msgstring_param">
  141. <xsl:choose>
  142. <xsl:when test="@whiteSpace and @whiteSpace='preserve'">
  143. <xsl:value-of select="$msgstring"/>
  144. </xsl:when>
  145. <xsl:otherwise>
  146. <xsl:value-of select="normalize-space($msgstring)"/>
  147. </xsl:otherwise>
  148. </xsl:choose>
  149. </xsl:variable>
  150. <call-template name="replaceChar">
  151. <with-param name="text">
  152. <call-template name="replaceChar">
  153. <with-param name="text" select="$msgstring_param" />
  154. <with-param name="toReplace">\</with-param>
  155. <with-param name="replaceWith">\\</with-param>
  156. </call-template>
  157. </with-param>
  158. <with-param name="toReplace">"</with-param>
  159. <with-param name="replaceWith">\"</with-param>
  160. </call-template>
  161. <xsl:text>"</xsl:text>
  162. </xsl:for-each>
  163. <xsl:text>&#10; }</xsl:text>
  164. </xsl:for-each>
  165. </xsl:for-each>
  166. <xsl:text>&#10;})</xsl:text>
  167. </xsl:template>
  168. <xsl:template name="replaceChar">
  169. <xsl:param name="text" />
  170. <xsl:param name="toReplace" />
  171. <xsl:param name="replaceWith" />
  172. <xsl:choose>
  173. <xsl:when test="contains($text, $toReplace)">
  174. <xsl:variable name="beforeChar" select="substring-before($text,$toReplace)" />
  175. <xsl:variable name="afterChar">
  176. <xsl:call-template name="replaceChar">
  177. <xsl:with-param name="text" select="substring-after($text,$toReplace)" />
  178. <xsl:with-param name="toReplace" select="$toReplace" />
  179. <xsl:with-param name="replaceWith" select="$replaceWith" />
  180. </xsl:call-template>
  181. </xsl:variable>
  182. <xsl:value-of select="concat($beforeChar, $replaceWith, $afterChar)" />
  183. </xsl:when>
  184. <xsl:otherwise>
  185. <xsl:value-of select="$text" />
  186. </xsl:otherwise>
  187. </xsl:choose>
  188. </xsl:template>
  189. <!-- copy everything by default -->
  190. <xsl:template match="*">
  191. <xsl:copy>
  192. <xsl:copy-of select="@*"/>
  193. <xsl:apply-templates/>
  194. </xsl:copy>
  195. </xsl:template>
  196. </xsl:stylesheet>