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