PageRenderTime 41ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/uml2dj/xmilib.xsl

http://uml-to-django.googlecode.com/
Extensible Stylesheet Language Transformations | 189 lines | 170 code | 19 blank | 0 comment | 0 complexity | 4c490a0ec76e0dc59ec00a74e6a680fd MD5 | raw file
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. UML to Django
  4. @author: Goffer Looney (glooney)
  5. @doc: http://code.google.com/p/uml-to-django/
  6. -->
  7. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  8. xmlns:ArgoUML="org.omg.xmi.namespace.UML"
  9. xmlns:StarUML="href://org.omg/UML/1.3"
  10. xmlns:UML="org.omg.xmi.namespace.UML"
  11. xmlns:fn="http://www.w3.org/2005/xpath-functions"
  12. xmlns:xmidj="xmi2dj"
  13. >
  14. <xsl:output method="text" omit-xml-declaration="yes" indent="no" />
  15. <!-- ========================== -->
  16. <!-- FUNCTIONS -->
  17. <!-- ========================== -->
  18. <!-- Here's how to do a replace() in xpath 1.0!!! -->
  19. <xsl:template name="getQuotedText">
  20. <xsl:param name="text"/>
  21. <xsl:variable name="dquote">"</xsl:variable>
  22. <xsl:variable name="dquoteEscaped">\"</xsl:variable>
  23. <xsl:choose>
  24. <xsl:when test="contains($text, $dquote)">
  25. <xsl:variable name="bufferBefore" select="substring-before($text,$dquote)"/>
  26. <xsl:variable name="newBuffer" select="substring-after($text,$dquote)"/>
  27. <xsl:value-of select="$bufferBefore"/><xsl:value-of select="$dquoteEscaped"/>
  28. <xsl:call-template name="getQuotedText">
  29. <xsl:with-param name="text" select="$newBuffer"/>
  30. </xsl:call-template>
  31. </xsl:when>
  32. <xsl:otherwise>
  33. <xsl:value-of select="$text"/>
  34. </xsl:otherwise>
  35. </xsl:choose>
  36. </xsl:template>
  37. <xsl:template name="getReplacedText">
  38. <xsl:param name="text"/>
  39. <xsl:param name="source"/>
  40. <xsl:param name="target"/>
  41. <xsl:choose>
  42. <xsl:when test="contains($text, $source)">
  43. <xsl:variable name="bufferBefore" select="substring-before($text,$source)"/>
  44. <xsl:variable name="newBuffer" select="substring-after($text,$source)"/>
  45. <xsl:value-of select="$bufferBefore"/>
  46. <xsl:value-of select="$target"/>
  47. <xsl:call-template name="getReplacedText">
  48. <xsl:with-param name="text" select="$newBuffer"/>
  49. <xsl:with-param name="source" select="$source"/>
  50. <xsl:with-param name="target" select="$target"/>
  51. </xsl:call-template>
  52. </xsl:when>
  53. <xsl:otherwise>
  54. <xsl:value-of select="$text"/>
  55. </xsl:otherwise>
  56. </xsl:choose>
  57. </xsl:template>
  58. <xsl:template name="getPlural">
  59. <xsl:param name="name"/>
  60. <xsl:choose>
  61. <xsl:when test="substring($name, string-length($name), 1) = 'y'"><xsl:value-of select="concat(substring($name, 1, string-length($name) - 1), 'ies')"/></xsl:when>
  62. <xsl:when test="substring($name, string-length($name), 1) = 's'"><xsl:value-of select="concat($name, 'es')"/></xsl:when>
  63. <xsl:otherwise><xsl:value-of select="$name"/>s</xsl:otherwise>
  64. </xsl:choose>
  65. </xsl:template>
  66. <xsl:template name="getDjIdentifier">
  67. <xsl:param name="name"/>
  68. <xsl:value-of select="translate($name, ' ', '_')" />
  69. </xsl:template>
  70. <xsl:template name="getDjIdentifierL">
  71. <xsl:param name="name"/>
  72. <xsl:value-of select="translate($name, ' ABCDEFGHIJKLMNOPQRSTUVWXYZ', '_abcdefghijklmnopqrstuvwxyz')" />
  73. </xsl:template>
  74. <!-- use template instead of a function because we need the calling context (key and select...) -->
  75. <xsl:template name="getParentModel">
  76. <xsl:param name="childid"/>
  77. <xsl:variable name="parentClass" select="//UML:Generalization[UML:Generalization.child/UML:Class/@xmi.idref = $childid]/UML:Generalization.parent/UML:Class" />
  78. <xsl:choose>
  79. <!-- xsl:when test="$parentClass"><xsl:value-of select="xmidj:getDjIdentifier(key('classes', $parentClass/@xmi.idref)/@name)" /></xsl:when -->
  80. <xsl:when test="$parentClass">
  81. <xsl:call-template name="getDjIdentifier">
  82. <xsl:with-param name="name"><xsl:value-of select="key('classes', $parentClass/@xmi.idref)/@name" /></xsl:with-param>
  83. </xsl:call-template>
  84. </xsl:when>
  85. <xsl:otherwise>models.Model</xsl:otherwise>
  86. </xsl:choose>
  87. </xsl:template>
  88. <xsl:template name="getShortDocQuoted">
  89. <xsl:param name="text"/>
  90. <xsl:call-template name="getQuotedText">
  91. <xsl:with-param name="text">
  92. <xsl:call-template name="getShortDoc"><xsl:with-param name="text" select="$text"/></xsl:call-template>
  93. </xsl:with-param>
  94. </xsl:call-template>
  95. </xsl:template>
  96. <xsl:template name="getShortDoc">
  97. <!-- returns the first line of a text. If the first line starts with [, returns nothing. -->
  98. <xsl:param name="text"/>
  99. <xsl:variable name="eol"><xsl:text>&#xA;</xsl:text></xsl:variable>
  100. <xsl:if test="not(starts-with($text, '['))">
  101. <xsl:choose>
  102. <xsl:when test="contains($text, $eol)"><xsl:value-of select="substring-before($text, $eol)" /></xsl:when>
  103. <xsl:otherwise><xsl:value-of select="$text" /></xsl:otherwise>
  104. </xsl:choose>
  105. </xsl:if>
  106. </xsl:template>
  107. <xsl:template name="getLongDoc">
  108. <!-- removes the comments from a text and the first line if its not a comment. -->
  109. <xsl:param name="text"/>
  110. <xsl:variable name="eol"><xsl:text>&#xA;</xsl:text></xsl:variable>
  111. <xsl:variable name="longDoc">
  112. <!-- remove the first line if its not a comment. prepend a newline otherwise. -->
  113. <xsl:choose>
  114. <xsl:when test="starts-with($text, '[')"><xsl:text>&#xA;</xsl:text><xsl:value-of select="$text" /></xsl:when>
  115. <xsl:otherwise><xsl:value-of select="substring-after($text, $eol)" /></xsl:otherwise>
  116. </xsl:choose>
  117. </xsl:variable>
  118. <!-- convert the new lines into br -->
  119. <xsl:call-template name="getReplacedText">
  120. <xsl:with-param name="text">
  121. <!-- remove all the commented lines -->
  122. <xsl:call-template name="getTextWithoutComments"><xsl:with-param name="text" select="$longDoc"/></xsl:call-template>
  123. </xsl:with-param>
  124. <xsl:with-param name="source" select="$eol"/>
  125. <xsl:with-param name="target"><![CDATA[<br/>]]></xsl:with-param>
  126. </xsl:call-template>
  127. </xsl:template>
  128. <xsl:template name="getTextWithoutComments">
  129. <!-- removes all the comments from a text. Comments always starts a line with [ and ends at the next ] -->
  130. <xsl:param name="text"/>
  131. <xsl:variable name="comment"><xsl:text>&#xA;[</xsl:text></xsl:variable>
  132. <xsl:choose>
  133. <xsl:when test="contains($text, $comment)">
  134. <xsl:variable name="bufferBefore" select="substring-before($text,$comment)"/>
  135. <xsl:variable name="newBufferTemp" select="substring-after($text,$comment)"/>
  136. <xsl:variable name="newBuffer" select="substring-after($newBufferTemp,']')"/>
  137. <xsl:value-of select="$bufferBefore"/>
  138. <xsl:call-template name="getTextWithoutComments">
  139. <xsl:with-param name="text" select="$newBuffer"/>
  140. </xsl:call-template>
  141. </xsl:when>
  142. <xsl:otherwise>
  143. <xsl:value-of select="$text"/>
  144. </xsl:otherwise>
  145. </xsl:choose>
  146. </xsl:template>
  147. <xsl:template name="getHelpText">
  148. <!-- returns the help_text attribute for a field in the model. -->
  149. <xsl:param name="text"/>
  150. <xsl:param name="fieldName"/>
  151. <xsl:variable name="fieldNameDj"><xsl:call-template name="getDjIdentifierL"><xsl:with-param name="name" select="$fieldName"/></xsl:call-template></xsl:variable>
  152. <xsl:variable name="longDoc"><xsl:call-template name="getLongDoc"><xsl:with-param name="text" select="$text"/></xsl:call-template></xsl:variable>
  153. <xsl:text>help_text=ur'''</xsl:text>
  154. <xsl:call-template name="getShortDoc"><xsl:with-param name="text" select="$text"/></xsl:call-template>
  155. <xsl:if test="string-length(normalize-space($longDoc)) > 1">
  156. <![CDATA[<img src="/media/img/admin/icon-unknown.gif" class="long-doc-link" onclick="showLongDoc(']]><xsl:value-of select="$fieldName"/><![CDATA[', ']]><xsl:value-of select="$fieldNameDj"/><![CDATA[-doc-id');" /><span style="display:none;" id="]]><xsl:value-of select="$fieldNameDj"/><![CDATA[-doc-id">]]><xsl:value-of select="$longDoc"/><![CDATA[</span>]]>
  157. </xsl:if>
  158. <xsl:text>''', </xsl:text>
  159. </xsl:template>
  160. <xsl:template name="getDefaultValue">
  161. <xsl:param name="associationid" />
  162. <!-- find the id of the default tag -->
  163. <xsl:variable name="tagid"><xsl:value-of select="key('tags', 'default')/@xmi.id" /></xsl:variable>
  164. <xsl:variable name="result"><xsl:value-of select="//*[@xmi.id=$associationid]/UML:ModelElement.taggedValue/UML:TaggedValue[UML:TaggedValue.type/UML:TagDefinition/@xmi.idref = '-119-73-122--119-283b2ec6:122ef60c3b8:-8000:00000000000011EF']/UML:TaggedValue.dataValue/text()"></xsl:value-of></xsl:variable>
  165. <xsl:choose>
  166. <xsl:when test="string-length($result)"><xsl:value-of select="$result" /></xsl:when>
  167. <xsl:otherwise>1</xsl:otherwise>
  168. </xsl:choose>
  169. </xsl:template>
  170. </xsl:stylesheet>