Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
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.

200 lines
7.8 KiB

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Downloaded on 12/6/2012 from http://www.gerixsoft.com/blog/xslt/json2xml -->
  3. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4. <xsl:template match="json">
  5. <xsl:copy>
  6. <xsl:copy-of select="@*"/>
  7. <xsl:call-template name="json2xml">
  8. <xsl:with-param name="text" select="."/>
  9. </xsl:call-template>
  10. </xsl:copy>
  11. </xsl:template>
  12. <xsl:template name="json2xml">
  13. <xsl:param name="text"/>
  14. <xsl:variable name="mode0">
  15. <xsl:variable name="regexps" select="'//(.*?)\n', '/\*(.*?)\*/', '(''|&quot;)(([^\\]|\\[\\&quot;''/btnvfr])*?)\3', '(-?\d+(\.\d+([eE][+-]?\d+)?|[eE][+-]?\d+))', '(-?[1-9]\d*)', '(-?0[0-7]+)', '(-?0x[0-9a-fA-F]+)', '([:,\{\}\[\]])', '(true|false)', '(null)'"/>
  16. <xsl:analyze-string select="$text" regex="{string-join($regexps,'|')}" flags="s">
  17. <xsl:matching-substring>
  18. <xsl:choose>
  19. <!-- single line comment -->
  20. <xsl:when test="regex-group(1)">
  21. <xsl:comment>
  22. <xsl:value-of select="regex-group(1)"/>
  23. </xsl:comment>
  24. <xsl:text>&#10;</xsl:text>
  25. </xsl:when>
  26. <!-- multi line comment -->
  27. <xsl:when test="regex-group(2)">
  28. <xsl:comment>
  29. <xsl:value-of select="regex-group(2)"/>
  30. </xsl:comment>
  31. </xsl:when>
  32. <!-- string -->
  33. <xsl:when test="regex-group(3)">
  34. <string>
  35. <xsl:analyze-string select="regex-group(4)" regex="\\([\\&quot;'/btnvfr])" flags="s">
  36. <xsl:matching-substring>
  37. <xsl:variable name="s" select="regex-group(1)"/>
  38. <xsl:choose>
  39. <xsl:when test="$s=('\', '&quot;', '''', '/')">
  40. <xsl:value-of select="regex-group(1)"/>
  41. </xsl:when>
  42. <xsl:when test="$s='b'">
  43. <!--xsl:text>&#8;</xsl:text-->
  44. <xsl:message select="'escape sequense \b is not supported by XML'"/>
  45. <xsl:text>\b</xsl:text>
  46. </xsl:when>
  47. <xsl:when test="$s='t'">
  48. <xsl:text>&#9;</xsl:text>
  49. </xsl:when>
  50. <xsl:when test="$s='n'">
  51. <xsl:text>&#10;</xsl:text>
  52. </xsl:when>
  53. <xsl:when test="$s='v'">
  54. <!--xsl:text>&#11;</xsl:text-->
  55. <xsl:message select="'escape sequence \v is not supported by XML'"/>
  56. <xsl:text>\v</xsl:text>
  57. </xsl:when>
  58. <xsl:when test="$s='f'">
  59. <!--xsl:text>&#12;</xsl:text-->
  60. <xsl:message select="'escape sequence \f is not supported by XML'"/>
  61. <xsl:text>\f</xsl:text>
  62. </xsl:when>
  63. <xsl:when test="$s='r'">
  64. <xsl:text>&#13;</xsl:text>
  65. </xsl:when>
  66. <xsl:otherwise>
  67. <xsl:message terminate="yes" select="'internal error'"/>
  68. </xsl:otherwise>
  69. </xsl:choose>
  70. </xsl:matching-substring>
  71. <xsl:non-matching-substring>
  72. <xsl:value-of select="."/>
  73. </xsl:non-matching-substring>
  74. </xsl:analyze-string>
  75. </string>
  76. </xsl:when>
  77. <!-- double -->
  78. <xsl:when test="regex-group(6)">
  79. <double>
  80. <xsl:value-of select="regex-group(6)"/>
  81. </double>
  82. </xsl:when>
  83. <!-- integer -->
  84. <xsl:when test="regex-group(9)">
  85. <integer>
  86. <xsl:value-of select="regex-group(9)"/>
  87. </integer>
  88. </xsl:when>
  89. <!-- octal -->
  90. <xsl:when test="regex-group(10)">
  91. <integer>
  92. <xsl:value-of xmlns:Integer="java:java.lang.Integer" select="Integer:parseInt(regex-group(10), 8)"/>
  93. </integer>
  94. </xsl:when>
  95. <!-- hex -->
  96. <xsl:when test="regex-group(11)">
  97. <integer>
  98. <xsl:value-of xmlns:Integer="java:java.lang.Integer" select="Integer:parseInt(replace(regex-group(11), '0x', ''), 16)"/>
  99. </integer>
  100. </xsl:when>
  101. <!-- symbol -->
  102. <xsl:when test="regex-group(12)">
  103. <symbol>
  104. <xsl:value-of select="regex-group(12)"/>
  105. </symbol>
  106. </xsl:when>
  107. <!-- boolean -->
  108. <xsl:when test="regex-group(13)">
  109. <boolean>
  110. <xsl:value-of select="regex-group(13)"/>
  111. </boolean>
  112. </xsl:when>
  113. <!-- null -->
  114. <xsl:when test="regex-group(14)">
  115. <null />
  116. </xsl:when>
  117. <xsl:otherwise>
  118. <xsl:message terminate="yes" select="'internal error'"/>
  119. </xsl:otherwise>
  120. </xsl:choose>
  121. </xsl:matching-substring>
  122. <xsl:non-matching-substring>
  123. <xsl:if test="normalize-space()!=''">
  124. <xsl:message select="concat('unknown token: ', .)"/>
  125. <xsl:value-of select="."/>
  126. </xsl:if>
  127. </xsl:non-matching-substring>
  128. </xsl:analyze-string>
  129. </xsl:variable>
  130. <xsl:variable name="mode1">
  131. <xsl:apply-templates mode="json2xml1" select="$mode0/node()[1]"/>
  132. </xsl:variable>
  133. <xsl:variable name="mode2">
  134. <xsl:apply-templates mode="json2xml2" select="$mode1"/>
  135. </xsl:variable>
  136. <xsl:variable name="mode3">
  137. <xsl:apply-templates mode="json2xml3" select="$mode2"/>
  138. </xsl:variable>
  139. <xsl:copy-of select="$mode3"/> <!-- change $mode3 to $mode[0-2] for easy debug -->
  140. </xsl:template>
  141. <!-- json2xml1 mode: group content between {} and [] into object and array elements -->
  142. <xsl:template mode="json2xml1" match="node()" priority="-9">
  143. <xsl:copy-of select="."/>
  144. <xsl:apply-templates mode="json2xml1" select="following-sibling::node()[1]"/>
  145. </xsl:template>
  146. <xsl:template mode="json2xml1" match="symbol[.=('}',']')]"/>
  147. <xsl:template mode="json2xml1" match="symbol[.=('{','[')]">
  148. <xsl:element name="{if (.='{') then 'object' else 'array'}">
  149. <xsl:apply-templates mode="json2xml1" select="following-sibling::node()[1]"/>
  150. </xsl:element>
  151. <xsl:variable name="level" select="count(preceding-sibling::symbol[.=('{','[')])-count(preceding-sibling::symbol[.=('}',']')])+1"/>
  152. <xsl:variable name="ender"
  153. select="following-sibling::symbol[.=('}',']') and count(preceding-sibling::symbol[.=('{','[')])-count(preceding-sibling::symbol[.=('}',']')])=$level][1]"/>
  154. <xsl:apply-templates mode="json2xml1" select="$ender/following-sibling::node()[1]"/>
  155. </xsl:template>
  156. <!-- json2xml2 mode: group <string>:<string|integer|double|object|array> into field element -->
  157. <xsl:template priority="-9" mode="json2xml2" match="@*|node()">
  158. <xsl:copy>
  159. <xsl:apply-templates mode="json2xml2" select="@*|node()"/>
  160. </xsl:copy>
  161. </xsl:template>
  162. <xsl:template mode="json2xml2"
  163. match="string[following-sibling::*[1]/self::symbol[.=':'] and following-sibling::*[2]/(self::string|self::integer|self::double|self::boolean|self::object|self::array|self::null)]"/>
  164. <xsl:template mode="json2xml2"
  165. match="symbol[.=':'][preceding-sibling::*[1]/self::string and following-sibling::*[1]/(self::string|self::integer|self::double|self::boolean|self::object|self::array|self::null)]">
  166. <field name="{preceding-sibling::*[1]}">
  167. <xsl:for-each select="following-sibling::*[1]">
  168. <xsl:copy>
  169. <xsl:apply-templates mode="json2xml2" select="@*|node()"/>
  170. </xsl:copy>
  171. </xsl:for-each>
  172. </field>
  173. </xsl:template>
  174. <xsl:template mode="json2xml2"
  175. match="*[self::string|self::integer|self::double|self::boolean|self::object|self::array|self::null][preceding-sibling::*[2]/self::string and preceding-sibling::*[1]/self::symbol[.=':']]"/>
  176. <!-- json2xml3 mode: drop comma between consecutive field and object elements -->
  177. <xsl:template priority="-9" mode="json2xml3" match="@*|node()">
  178. <xsl:copy>
  179. <xsl:apply-templates mode="json2xml3" select="@*|node()"/>
  180. </xsl:copy>
  181. </xsl:template>
  182. <xsl:template mode="json2xml3" match="object/symbol[.=','][preceding-sibling::*[1]/self::field and following-sibling::*[1]/self::field]"/>
  183. <xsl:template mode="json2xml3" match="array/symbol[.=','][preceding-sibling::*[1]/(self::string|self::integer|self::double|self::boolean|self::object|self::array|self::null) and following-sibling::*[1]/(self::string|self::integer|self::double|self::boolean|self::object|self::array|self::null)]"/>
  184. </xsl:stylesheet>