Add multicylinder support for Subsurface CSV import

Signed-off-by: Miika Turkia <miika.turkia@gmail.com>
This commit is contained in:
Miika Turkia 2020-05-21 17:51:33 +03:00 committed by Dirk Hohndel
parent dc829e3487
commit 368719379e

View file

@ -17,11 +17,19 @@
</xsl:variable>
<xsl:template match="/">
<xsl:variable name="cylinders">
<xsl:call-template name="countCylinders">
<xsl:with-param name="line" select="substring-before(substring-after(//SubsurfaceCSV, $lf), $lf)"/>
<xsl:with-param name="count" select="'0'"/>
<xsl:with-param name="index" select="'10'"/>
</xsl:call-template>
</xsl:variable>
<divelog program="subsurface-import" version="2">
<dives>
<xsl:call-template name="printLine">
<xsl:with-param name="line" select="substring-before(substring-after(//SubsurfaceCSV, $lf), $lf)"/>
<xsl:with-param name="remaining" select="substring-after(substring-after(//SubsurfaceCSV, $lf), $lf)"/>
<xsl:with-param name="cylinders" select="$cylinders"/>
</xsl:call-template>
</dives>
</divelog>
@ -30,16 +38,19 @@
<xsl:template name="printLine">
<xsl:param name="line"/>
<xsl:param name="remaining"/>
<xsl:param name="cylinders"/>
<xsl:call-template name="printFields">
<xsl:with-param name="line" select="$line"/>
<xsl:with-param name="remaining" select="$remaining"/>
<xsl:with-param name="cylinders" select="$cylinders"/>
</xsl:call-template>
<xsl:if test="$remaining != ''">
<xsl:call-template name="printLine">
<xsl:with-param name="line" select="substring-before($remaining, $lf)"/>
<xsl:with-param name="remaining" select="substring-after($remaining, $lf)"/>
<xsl:with-param name="cylinders" select="$cylinders"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
@ -47,6 +58,7 @@
<xsl:template name="printFields">
<xsl:param name="line"/>
<xsl:param name="remaining"/>
<xsl:param name="cylinders"/>
<xsl:variable name="number">
<xsl:call-template name="getFieldByIndex">
@ -192,42 +204,185 @@
</xsl:if>
</divetemperature>
<!-- CYLINDER -->
<xsl:call-template name="parseCylinders">
<xsl:with-param name="line" select="$line"/>
<xsl:with-param name="cylinders" select="$cylinders"/>
<xsl:with-param name="count" select="'0'"/>
<xsl:with-param name="index" select="'10'"/>
</xsl:call-template>
<location debug="{$cylinders}">
<xsl:variable name="gps">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="16 + ($cylinders - 1) * 5"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="location">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="15 + ($cylinders - 1) * 5"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$gps != ''">
<xsl:attribute name="gps">
<xsl:value-of select="$gps"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$location != ''">
<xsl:value-of select="$location"/>
</xsl:if>
</location>
<xsl:variable name="dm">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="17 + ($cylinders - 1) * 5"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$dm != ''">
<divemaster>
<xsl:value-of select="$dm"/>
</divemaster>
</xsl:if>
<xsl:variable name="buddy">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="18 + ($cylinders - 1) * 5"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$buddy != ''">
<buddy>
<xsl:value-of select="$buddy"/>
</buddy>
</xsl:if>
<xsl:variable name="suit">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="19 + ($cylinders - 1) * 5"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$suit != ''">
<suit>
<xsl:value-of select="$suit"/>
</suit>
</xsl:if>
<xsl:variable name="notes">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="22 + ($cylinders - 1) * 5"/>
<xsl:with-param name="line" select="$line"/>
<xsl:with-param name="remaining" select="$remaining"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$notes != ''">
<notes>
<xsl:value-of select="$notes"/>
</notes>
</xsl:if>
<xsl:variable name="weight">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="23 + ($cylinders - 1) * 5"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$weight != ''">
<weightsystem description="unknown">
<xsl:attribute name="weight">
<xsl:choose>
<xsl:when test="$units = 0">
<xsl:value-of select="$weight"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(format-number((translate($weight, translate($weight, '0123456789', ''), '') * 0.453592), '#.##'), ' kg')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</weightsystem>
</xsl:if>
</dive>
</xsl:if>
</xsl:template>
<xsl:template name="countCylinders">
<xsl:param name="line"/>
<xsl:param name="count"/>
<xsl:param name="index"/>
<xsl:variable name="field">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="$index"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="substring-before($field, ' ') = 'cylinder'">
<xsl:call-template name="countCylinders">
<xsl:with-param name="line" select="$line"/>
<xsl:with-param name="count" select="$count + 1"/>
<xsl:with-param name="index" select="$index + 5"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$count"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="parseCylinders">
<xsl:param name="line"/>
<xsl:param name="cylinders"/>
<xsl:param name="count"/>
<xsl:param name="index"/>
<xsl:variable name="field">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="$index"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$count &lt; $cylinders">
<cylinder>
<xsl:variable name="size">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="10"/>
<xsl:with-param name="index" select="$index"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="start">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="11"/>
<xsl:with-param name="index" select="$index + 1"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="end">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="12"/>
<xsl:with-param name="index" select="$index + 2"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="o2">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="13"/>
<xsl:with-param name="index" select="$index + 3"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="he">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="14"/>
<xsl:with-param name="index" select="$index + 4"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<!-- ALxxx -> xxx cuft at 3000 psi
LPxxx -> xxx cuft at 2400 psi
HPxxx -> xxx cuft at 3440 psi -->
<xsl:if test="$size != ''">
<xsl:attribute name="size">
<xsl:choose>
@ -294,100 +449,12 @@
</xsl:if>
</cylinder>
<location>
<xsl:variable name="gps">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="16"/>
<xsl:call-template name="parseCylinders">
<xsl:with-param name="line" select="$line"/>
<xsl:with-param name="cylinders" select="$cylinders"/>
<xsl:with-param name="count" select="$count + 1"/>
<xsl:with-param name="index" select="$index + 5"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="location">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="15"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$gps != ''">
<xsl:attribute name="gps">
<xsl:value-of select="$gps"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$location != ''">
<xsl:value-of select="$location"/>
</xsl:if>
</location>
<xsl:variable name="dm">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="17"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$dm != ''">
<divemaster>
<xsl:value-of select="$dm"/>
</divemaster>
</xsl:if>
<xsl:variable name="buddy">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="18"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$buddy != ''">
<buddy>
<xsl:value-of select="$buddy"/>
</buddy>
</xsl:if>
<xsl:variable name="suit">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="19"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$suit != ''">
<suit>
<xsl:value-of select="$suit"/>
</suit>
</xsl:if>
<xsl:variable name="notes">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="22"/>
<xsl:with-param name="line" select="$line"/>
<xsl:with-param name="remaining" select="$remaining"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$notes != ''">
<notes>
<xsl:value-of select="$notes"/>
</notes>
</xsl:if>
<xsl:variable name="weight">
<xsl:call-template name="getFieldByIndex">
<xsl:with-param name="index" select="23"/>
<xsl:with-param name="line" select="$line"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$weight != ''">
<weightsystem description="unknown">
<xsl:attribute name="weight">
<xsl:choose>
<xsl:when test="$units = 0">
<xsl:value-of select="$weight"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(format-number((translate($weight, translate($weight, '0123456789', ''), '') * 0.453592), '#.##'), ' kg')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</weightsystem>
</xsl:if>
</dive>
</xsl:if>
</xsl:template>