svghmi/gen_dnd_widget_svg.xslt
changeset 3302 c89fc366bebd
parent 3264 51645afeded9
child 3410 eac5832a1489
equal deleted inserted replaced
2744:577118ebd179 3302:c89fc366bebd
       
     1 <?xml version="1.0"?>
       
     2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:str="http://exslt.org/strings" xmlns:func="http://exslt.org/functions" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:ns="beremiz" version="1.0" extension-element-prefixes="ns func exsl regexp str dyn" exclude-result-prefixes="ns func exsl regexp str dyn">
       
     3   <xsl:output method="xml"/>
       
     4   <xsl:variable name="hmi_elements" select="//svg:*[starts-with(@inkscape:label, 'HMI:')]"/>
       
     5   <xsl:variable name="widgetparams" select="ns:GetWidgetParams()"/>
       
     6   <xsl:variable name="indexed_hmitree" select="/.."/>
       
     7   <xsl:variable name="pathregex" select="'^([^\[,]+)(\[[^\]]+\])?([\d,]*)$'"/>
       
     8   <xsl:template mode="parselabel" match="*">
       
     9     <xsl:variable name="label" select="@inkscape:label"/>
       
    10     <xsl:variable name="id" select="@id"/>
       
    11     <xsl:variable name="description" select="substring-after($label,'HMI:')"/>
       
    12     <xsl:variable name="_args" select="substring-before($description,'@')"/>
       
    13     <xsl:variable name="args">
       
    14       <xsl:choose>
       
    15         <xsl:when test="$_args">
       
    16           <xsl:value-of select="$_args"/>
       
    17         </xsl:when>
       
    18         <xsl:otherwise>
       
    19           <xsl:value-of select="$description"/>
       
    20         </xsl:otherwise>
       
    21       </xsl:choose>
       
    22     </xsl:variable>
       
    23     <xsl:variable name="_type" select="substring-before($args,':')"/>
       
    24     <xsl:variable name="type">
       
    25       <xsl:choose>
       
    26         <xsl:when test="$_type">
       
    27           <xsl:value-of select="$_type"/>
       
    28         </xsl:when>
       
    29         <xsl:otherwise>
       
    30           <xsl:value-of select="$args"/>
       
    31         </xsl:otherwise>
       
    32       </xsl:choose>
       
    33     </xsl:variable>
       
    34     <xsl:if test="$type">
       
    35       <widget>
       
    36         <xsl:attribute name="id">
       
    37           <xsl:value-of select="$id"/>
       
    38         </xsl:attribute>
       
    39         <xsl:attribute name="type">
       
    40           <xsl:value-of select="$type"/>
       
    41         </xsl:attribute>
       
    42         <xsl:for-each select="str:split(substring-after($args, ':'), ':')">
       
    43           <arg>
       
    44             <xsl:attribute name="value">
       
    45               <xsl:value-of select="."/>
       
    46             </xsl:attribute>
       
    47           </arg>
       
    48         </xsl:for-each>
       
    49         <xsl:variable name="paths" select="substring-after($description,'@')"/>
       
    50         <xsl:for-each select="str:split($paths, '@')">
       
    51           <xsl:if test="string-length(.) &gt; 0">
       
    52             <path>
       
    53               <xsl:variable name="path_match" select="regexp:match(.,$pathregex)"/>
       
    54               <xsl:variable name="pathminmax" select="str:split($path_match[4],',')"/>
       
    55               <xsl:variable name="path" select="$path_match[2]"/>
       
    56               <xsl:variable name="path_accepts" select="$path_match[3]"/>
       
    57               <xsl:variable name="pathminmaxcount" select="count($pathminmax)"/>
       
    58               <xsl:attribute name="value">
       
    59                 <xsl:value-of select="$path"/>
       
    60               </xsl:attribute>
       
    61               <xsl:if test="string-length($path_accepts)">
       
    62                 <xsl:attribute name="accepts">
       
    63                   <xsl:value-of select="$path_accepts"/>
       
    64                 </xsl:attribute>
       
    65               </xsl:if>
       
    66               <xsl:choose>
       
    67                 <xsl:when test="$pathminmaxcount = 2">
       
    68                   <xsl:attribute name="min">
       
    69                     <xsl:value-of select="$pathminmax[1]"/>
       
    70                   </xsl:attribute>
       
    71                   <xsl:attribute name="max">
       
    72                     <xsl:value-of select="$pathminmax[2]"/>
       
    73                   </xsl:attribute>
       
    74                 </xsl:when>
       
    75                 <xsl:when test="$pathminmaxcount = 1 or $pathminmaxcount &gt; 2">
       
    76                   <xsl:message terminate="yes">
       
    77                     <xsl:text>Widget id:</xsl:text>
       
    78                     <xsl:value-of select="$id"/>
       
    79                     <xsl:text> label:</xsl:text>
       
    80                     <xsl:value-of select="$label"/>
       
    81                     <xsl:text> has wrong syntax of path section </xsl:text>
       
    82                     <xsl:value-of select="$pathminmax"/>
       
    83                   </xsl:message>
       
    84                 </xsl:when>
       
    85               </xsl:choose>
       
    86               <xsl:if test="$indexed_hmitree">
       
    87                 <xsl:choose>
       
    88                   <xsl:when test="regexp:test($path,'^\.[a-zA-Z0-9_]+$')">
       
    89                     <xsl:attribute name="type">
       
    90                       <xsl:text>PAGE_LOCAL</xsl:text>
       
    91                     </xsl:attribute>
       
    92                   </xsl:when>
       
    93                   <xsl:when test="regexp:test($path,'^[a-zA-Z0-9_]+$')">
       
    94                     <xsl:attribute name="type">
       
    95                       <xsl:text>HMI_LOCAL</xsl:text>
       
    96                     </xsl:attribute>
       
    97                   </xsl:when>
       
    98                   <xsl:otherwise>
       
    99                     <xsl:variable name="item" select="$indexed_hmitree/*[@hmipath = $path]"/>
       
   100                     <xsl:variable name="pathtype" select="local-name($item)"/>
       
   101                     <xsl:if test="$pathminmaxcount = 3 and not($pathtype = 'HMI_INT' or $pathtype = 'HMI_REAL')">
       
   102                       <xsl:message terminate="yes">
       
   103                         <xsl:text>Widget id:</xsl:text>
       
   104                         <xsl:value-of select="$id"/>
       
   105                         <xsl:text> label:</xsl:text>
       
   106                         <xsl:value-of select="$label"/>
       
   107                         <xsl:text> path section </xsl:text>
       
   108                         <xsl:value-of select="$pathminmax"/>
       
   109                         <xsl:text> use min and max on non mumeric value</xsl:text>
       
   110                       </xsl:message>
       
   111                     </xsl:if>
       
   112                     <xsl:if test="count($item) = 1">
       
   113                       <xsl:attribute name="index">
       
   114                         <xsl:value-of select="$item/@index"/>
       
   115                       </xsl:attribute>
       
   116                       <xsl:attribute name="type">
       
   117                         <xsl:value-of select="$pathtype"/>
       
   118                       </xsl:attribute>
       
   119                     </xsl:if>
       
   120                   </xsl:otherwise>
       
   121                 </xsl:choose>
       
   122               </xsl:if>
       
   123             </path>
       
   124           </xsl:if>
       
   125         </xsl:for-each>
       
   126         <xsl:if test="svg:desc">
       
   127           <desc>
       
   128             <xsl:value-of select="svg:desc/text()"/>
       
   129           </desc>
       
   130         </xsl:if>
       
   131       </widget>
       
   132     </xsl:if>
       
   133   </xsl:template>
       
   134   <xsl:template mode="genlabel" match="arg">
       
   135     <xsl:text>:</xsl:text>
       
   136     <xsl:value-of select="@value"/>
       
   137   </xsl:template>
       
   138   <xsl:template mode="genlabel" match="path">
       
   139     <xsl:text>@</xsl:text>
       
   140     <xsl:value-of select="@value"/>
       
   141     <xsl:if test="string-length(@min)&gt;0 or string-length(@max)&gt;0">
       
   142       <xsl:text>,</xsl:text>
       
   143       <xsl:value-of select="@min"/>
       
   144       <xsl:text>,</xsl:text>
       
   145       <xsl:value-of select="@max"/>
       
   146     </xsl:if>
       
   147   </xsl:template>
       
   148   <xsl:template mode="genlabel" match="widget">
       
   149     <xsl:text>HMI:</xsl:text>
       
   150     <xsl:value-of select="@type"/>
       
   151     <xsl:apply-templates mode="genlabel" select="arg"/>
       
   152     <xsl:apply-templates mode="genlabel" select="path"/>
       
   153   </xsl:template>
       
   154   <xsl:variable name="_parsed_widgets">
       
   155     <xsl:apply-templates mode="parselabel" select="$hmi_elements"/>
       
   156   </xsl:variable>
       
   157   <xsl:variable name="parsed_widgets" select="exsl:node-set($_parsed_widgets)"/>
       
   158   <xsl:variable name="svg_widget" select="$parsed_widgets/widget[1]"/>
       
   159   <xsl:variable name="svg_widget_type" select="$svg_widget/@type"/>
       
   160   <xsl:variable name="svg_widget_path" select="$svg_widget/@path"/>
       
   161   <xsl:variable name="svg_widget_count" select="count($parsed_widgets/widget)"/>
       
   162   <xsl:template mode="replace_params" match="@* | node()">
       
   163     <xsl:copy>
       
   164       <xsl:apply-templates mode="replace_params" select="@* | node()"/>
       
   165     </xsl:copy>
       
   166   </xsl:template>
       
   167   <xsl:template mode="replace_params" match="arg"/>
       
   168   <xsl:template mode="replace_params" match="path"/>
       
   169   <xsl:template mode="replace_params" match="widget">
       
   170     <xsl:copy>
       
   171       <xsl:apply-templates mode="replace_params" select="@* | node()"/>
       
   172       <xsl:copy-of select="$widgetparams/*"/>
       
   173     </xsl:copy>
       
   174   </xsl:template>
       
   175   <xsl:template xmlns="http://www.w3.org/2000/svg" mode="inline_svg" match="@*">
       
   176     <xsl:copy/>
       
   177   </xsl:template>
       
   178   <xsl:template xmlns="http://www.w3.org/2000/svg" mode="inline_svg" match="@inkscape:label[starts-with(., 'HMI:')]"/>
       
   179   <xsl:template mode="inline_svg" match="node()">
       
   180     <xsl:copy>
       
   181       <xsl:if test="@id = $svg_widget/@id">
       
   182         <xsl:variable name="substituted_widget">
       
   183           <xsl:apply-templates mode="replace_params" select="$svg_widget"/>
       
   184         </xsl:variable>
       
   185         <xsl:variable name="substituted_widget_ns" select="exsl:node-set($substituted_widget)"/>
       
   186         <xsl:variable name="new_label">
       
   187           <xsl:apply-templates mode="genlabel" select="$substituted_widget_ns"/>
       
   188         </xsl:variable>
       
   189         <xsl:attribute name="inkscape:label">
       
   190           <xsl:value-of select="$new_label"/>
       
   191         </xsl:attribute>
       
   192       </xsl:if>
       
   193       <xsl:apply-templates mode="inline_svg" select="@* | node()"/>
       
   194     </xsl:copy>
       
   195   </xsl:template>
       
   196   <xsl:template match="/">
       
   197     <xsl:comment>
       
   198       <xsl:text>Widget dropped in Inkscape from Beremiz</xsl:text>
       
   199     </xsl:comment>
       
   200     <xsl:choose>
       
   201       <xsl:when test="$svg_widget_count &lt; 1">
       
   202         <xsl:message terminate="yes">
       
   203           <xsl:text>No widget detected on selected SVG</xsl:text>
       
   204         </xsl:message>
       
   205       </xsl:when>
       
   206       <xsl:when test="$svg_widget_count &gt; 1">
       
   207         <xsl:message terminate="yes">
       
   208           <xsl:text>Multiple widget DnD not yet supported</xsl:text>
       
   209         </xsl:message>
       
   210       </xsl:when>
       
   211     </xsl:choose>
       
   212     <xsl:apply-templates mode="inline_svg" select="/"/>
       
   213   </xsl:template>
       
   214 </xsl:stylesheet>