<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:exslt="http://exslt.org/common"
xmlns:math="http://exslt.org/math"
xmlns:str="http://exslt.org/strings"
>
	
	<xsl:output method="text" encoding="UTF-8" indent="no"/>
	<xsl:strip-space elements="*"/>

	<xsl:template match="textitem">
		<xsl:element name="textlength"><xsl:value-of select="string-length(.)"/></xsl:element>
	</xsl:template>
	
	<xsl:variable name="g_c1width">
		<xsl:variable name="textitems">
			<textitem>QT_TRANSLATE_NOOP("QObject", "Hard Disk")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Serial Number")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Capacity")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Object")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Free Space Only")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Start Time")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "End Time")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Name")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Description")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Write Pass Count")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Verify Pass Count")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Verify Percent")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "Total Bad Blocks")</textitem>
			<textitem>QT_TRANSLATE_NOOP("QObject", "List")</textitem>
		</xsl:variable>
		
		<xsl:variable name="textlengths">
			<xsl:apply-templates select="exslt:node-set($textitems)/textitem"/>
		</xsl:variable>
		
		<xsl:value-of select="math:max(exslt:node-set($textlengths)/textlength)"/>
	</xsl:variable>
	
	<xsl:template name="substring-before-last">
		<xsl:param name="input" />
		<xsl:param name="substr" />

		<xsl:if test="$substr">
			<xsl:choose>
				<xsl:when test="contains($input, $substr)">
					<xsl:variable name="temp" select="substring-after($input, $substr)" />
					<xsl:value-of select="substring-before($input, $substr)" />
					<xsl:if test="contains($temp, $substr)">
						<xsl:value-of select="$substr" />
						<xsl:call-template name="substring-before-last">
							<xsl:with-param name="input" select="$temp" />
							<xsl:with-param name="substr" select="$substr" />
						</xsl:call-template>
					</xsl:if>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$input"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="text_justify">
		<xsl:param name="value" /> 
		<xsl:param name="width" select="10"/>
		<xsl:param name="align" select=" 'left' "/>

		<xsl:value-of select="str:align($value, str:padding($width, ' '), $align)"/>
	</xsl:template>
	
	<xsl:template name="list2string">
		<xsl:param name="items"/>
		<xsl:param name="separator" select="', '"/>
		<xsl:choose>
			<xsl:when test="count($items) > 1">
				<xsl:variable name="head" select="$items[1]"/>
				<xsl:variable name="tail">
					<xsl:call-template name="list2string">
						<xsl:with-param name="items" select="$items[position() != 1]"/>
						<xsl:with-param name="separator" select="$separator"/>
					</xsl:call-template>
				</xsl:variable>
				<xsl:choose>
					<xsl:when test="$tail">
						<xsl:value-of select="concat($head, $separator, $tail)"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$head"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="count($items) = 1">
				<xsl:value-of select="$items[1]"/>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="separator">
		<xsl:param name="width" select="80"/>
	
		<xsl:value-of select="str:padding($width, '-')"/><xsl:text>&#xa;</xsl:text>
	</xsl:template>

	<xsl:template name="section">
		<xsl:param name="desc"/>

		<xsl:call-template name="separator"/>

		<xsl:call-template name="text_justify">
			<xsl:with-param name="value" select="$desc"/>
			<xsl:with-param name="width" select="80"/>
			<xsl:with-param name="align" select=" 'center' "/>
		</xsl:call-template>
		<xsl:text>&#xa;</xsl:text>

		<xsl:call-template name="separator"/>
	</xsl:template>

	<xsl:template name="row">
		<xsl:param name="v1"/>
		<xsl:param name="v2"/>
		<xsl:param name="c1width"/>
		<xsl:param name="totalwidth" select="80"/>
		
		<xsl:if test="$v1 or $v2">
			<!-- Cell 1 -->
			<xsl:variable name="c1">
				<xsl:choose>
					<xsl:when test="string-length($v1) > $c1width">
						<xsl:call-template name="substring-before-last">
							<xsl:with-param name="input" select="substring($v1, 1, $c1width)"/>
							<xsl:with-param name="substr" select=" ' ' "/>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$v1"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>

			<!-- Cell 2 -->
			<xsl:variable name="c2">
				<xsl:choose>
					<xsl:when test="string-length($v2) > ($totalwidth - $c1width - 1)">
						<xsl:call-template name="substring-before-last">
							<xsl:with-param name="input" select="substring($v2, 1, $totalwidth - $c1width - 1)"/>
							<xsl:with-param name="substr" select=" ' ' "/>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$v2"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>

			<xsl:call-template name="text_justify">
				<xsl:with-param name="value" select="$c1"/>
				<xsl:with-param name="width" select="$c1width"/>
			</xsl:call-template>
			<xsl:text> </xsl:text>
			<xsl:value-of select="$c2"/>
			<xsl:text>&#xa;</xsl:text>

			<xsl:call-template name="row">
				<xsl:with-param name="v1" select="substring($v1, string-length($c1) + 2)"/>
				<xsl:with-param name="v2" select="substring($v2, string-length($c2) + 2)"/>
				<xsl:with-param name="c1width" select="$c1width"/>
				<xsl:with-param name="totalwidth" select="$totalwidth"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="simplerow">
		<xsl:param name="v1"/>
		<xsl:param name="width"/>
		<xsl:param name="totalwidth" select="80"/>
		
		<xsl:if test="$v1">
			<xsl:variable name="c1">
				<xsl:choose>
					<xsl:when test="string-length($v1) > $width">
						<xsl:call-template name="substring-before-last">
							<xsl:with-param name="input" select="substring($v1, 1, $width)"/>
							<xsl:with-param name="substr" select=" ' ' "/>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$v1"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>

			<xsl:value-of select="$c1"/>
			<xsl:text>&#xa;</xsl:text>

			<xsl:call-template name="simplerow">
				<xsl:with-param name="v1" select="substring($v1, string-length($c1) + 2)"/>
				<xsl:with-param name="width" select="$width"/>
				<xsl:with-param name="totalwidth" select="$totalwidth"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="/wipereport">
		<xsl:text><![CDATA[QT_TRANSLATE_NOOP("QObject", "Data Sanitization Report (<WiperProductFullNameWithVersion>)")]]>&#xa;</xsl:text>
		<xsl:text>QT_TRANSLATE_NOOP("QObject", "Check the data erased according to the following requirements:")&#xa;&#xa;</xsl:text>

		<!-- HDD Info -->
		<xsl:apply-templates select="hddinfo"/>
		<!-- General Info -->
		<xsl:apply-templates select="operation"/>
		<!-- Algorithm Info -->
		<xsl:apply-templates select="algorithm"/>
		<!-- Bad Blocks Info -->
		<xsl:apply-templates select="badblocks"/>
		<!-- Dump -->
		<xsl:apply-templates select="sectors"/>
		<!-- Last Line -->
		<xsl:call-template name="separator"/>
	</xsl:template>
	
	<!-- Hard Drive Information -->
	<xsl:template match="hddinfo">
		<xsl:call-template name="section">
			<xsl:with-param name="desc">QT_TRANSLATE_NOOP("QObject", "Hard Disk Info")</xsl:with-param>
		</xsl:call-template>
		
		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Hard Disk")</xsl:with-param>
			<xsl:with-param name="v2" select="name"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>

		<xsl:if test="string-length(serial) != 0">
			<xsl:call-template name="row">
				<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Serial Number")</xsl:with-param>
				<xsl:with-param name="v2" select="serial"/>
				<xsl:with-param name="c1width" select="$g_c1width"/>
			</xsl:call-template>
		</xsl:if>

		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Capacity")</xsl:with-param>
			<xsl:with-param name="v2" select="capasity"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>
	</xsl:template>

	<!-- General Information -->
	<xsl:template match="operation">
		<xsl:call-template name="section">
			<xsl:with-param name="desc">QT_TRANSLATE_NOOP("QObject", "General Info")</xsl:with-param>
		</xsl:call-template>
		
		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Object")</xsl:with-param>
			<xsl:with-param name="v2" select="object"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>

		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Free Space Only")</xsl:with-param>
			<xsl:with-param name="v2">
				<xsl:choose>
					<xsl:when test="object/@onlyFreeSpace=1">
						<xsl:text>QT_TRANSLATE_NOOP("QObject", "Yes")</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>QT_TRANSLATE_NOOP("QObject", "No")</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:with-param>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>

		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Start Time")</xsl:with-param>
			<xsl:with-param name="v2" select="starttime"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>

		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "End Time")</xsl:with-param>
			<xsl:with-param name="v2" select="finishtime"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>
	</xsl:template>
	
	<!-- Algorithm Information -->
	<xsl:template match="algorithm">
		<xsl:call-template name="section">
			<xsl:with-param name="desc">QT_TRANSLATE_NOOP("QObject", "Algorithm Info")</xsl:with-param>
		</xsl:call-template>

		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Name")</xsl:with-param>
			<xsl:with-param name="v2" select="name"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>
		
		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Description")</xsl:with-param>
			<xsl:with-param name="v2" select="description"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>
		
		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Write Pass Count")</xsl:with-param>
			<xsl:with-param name="v2" select="writepass"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>
		
		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Verify Pass Count")</xsl:with-param>
			<xsl:with-param name="v2" select="verifypass"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>
		
		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Verify Percent")</xsl:with-param>
			<xsl:with-param name="v2" select="verifyPercent"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>
	</xsl:template>
	
	<!-- Bad Blocks Information -->
	<xsl:template match="badblocks">
		<xsl:call-template name="section">
			<xsl:with-param name="desc">QT_TRANSLATE_NOOP("QObject", "Bad Blocks Info")</xsl:with-param>
		</xsl:call-template>
		
		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Total Bad Blocks")</xsl:with-param>
			<xsl:with-param name="v2" select="count"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>

		<xsl:variable name="badblocks" select="list/badblock"/>
		<xsl:choose>
			<xsl:when test="count($badblocks) > 0">
				<xsl:call-template name="row">
					<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "List")</xsl:with-param>
					<xsl:with-param name="v2">
						<xsl:call-template name="list2string">
							<xsl:with-param name="items" select="$badblocks"/>
						</xsl:call-template>
					</xsl:with-param>
					<xsl:with-param name="c1width" select="$g_c1width"/>
				</xsl:call-template>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<!-- Dump -->
	<xsl:template match="sectors">
		<xsl:apply-templates select="sector"/>
	</xsl:template>
	
	<!-- single sector dump  -->
	<xsl:template match="sector">
		<xsl:call-template name="separator"/>
		<xsl:call-template name="row">
			<xsl:with-param name="v1">QT_TRANSLATE_NOOP("QObject", "Sector")</xsl:with-param>
			<xsl:with-param name="v2" select="@address"/>
			<xsl:with-param name="c1width" select="$g_c1width"/>
		</xsl:call-template>
		<xsl:call-template name="separator"/>
		
		<xsl:call-template name="simplerow">
			<xsl:with-param name="v1">
				<xsl:for-each select="byte"><xsl:value-of select="."/><xsl:text> </xsl:text></xsl:for-each>
			</xsl:with-param>
			<xsl:with-param name="width" select="48"/>
		</xsl:call-template>
	</xsl:template>
	
</xsl:stylesheet>
