<?xml version="1.0" encoding="UTF-8"?>
<!--

    OpenOffice.org - a multi-platform office productivity suite
 
    $RCSfile: header.xsl,v $
 
    $Revision: 1.7 $
 
    last change: $Author: rt $ $Date: 2005/09/08 22:08:34 $
 
    The Contents of this file are made available subject to
    the terms of GNU Lesser General Public License Version 2.1.
 
 
      GNU Lesser General Public License Version 2.1
      =============================================
      Copyright 2005 by Sun Microsystems, Inc.
      901 San Antonio Road, Palo Alto, CA 94303, USA
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
      License version 2.1, as published by the Free Software Foundation.
 
      This library is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Lesser General Public License for more details.
 
      You should have received a copy of the GNU Lesser General Public
      License along with this library; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place, Suite 330, Boston,
      MA  02111-1307  USA
 
-->
<!--
	For further documentation and updates visit http://xml.openoffice.org/sx2ml
-->
<xsl:stylesheet version="1.0"
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
				xmlns:office="http://openoffice.org/2000/office"
				xmlns:style="http://openoffice.org/2000/style"
				xmlns:text="http://openoffice.org/2000/text"
				xmlns:table="http://openoffice.org/2000/table"
				xmlns:draw="http://openoffice.org/2000/drawing"
				xmlns:fo="http://www.w3.org/1999/XSL/Format"
				xmlns:xlink="http://www.w3.org/1999/xlink"
				xmlns:number="http://openoffice.org/2000/datastyle"
				xmlns:svg="http://www.w3.org/2000/svg"
				xmlns:chart="http://openoffice.org/2000/chart"
				xmlns:dr3d="http://openoffice.org/2000/dr3d"
				xmlns:math="http://www.w3.org/1998/Math/MathML"
				xmlns:form="http://openoffice.org/2000/form"
				xmlns:script="http://openoffice.org/2000/script"
				xmlns:dc="http://purl.org/dc/elements/1.1/"
				xmlns:meta="http://openoffice.org/2000/meta"
				xmlns:config="http://openoffice.org/2001/config"
				xmlns:help="http://openoffice.org/2000/help"
				exclude-result-prefixes="office style text table draw fo xlink number svg chart dr3d math form script dc meta config help"
				xmlns="http://www.w3.org/1999/xhtml">



	<!-- ************** -->
	<!-- *** header *** -->
	<!-- ************** -->

	<xsl:template name="create-header">
		<xsl:param name="globalData" />

		<xsl:element name="head">
			<xsl:if test="$debugEnabled"><xsl:message>CSS helper variable will be created....</xsl:message></xsl:if>
			<xsl:call-template name='xhtml-header-properties'>
				<xsl:with-param name="globalData" select="$globalData" />
			</xsl:call-template>

			<!--+++++ CSS (CASCADING STYLE SHEET) HEADER STYLE WAY +++++-->
			<xsl:if test="$outputType = 'CSS_HEADER'">
				<xsl:if test="$debugEnabled"><xsl:message>CSS variable ready, header will be created....</xsl:message></xsl:if>
				<!-- constructing the css header simulating inheritance of style-families by style order -->
				<xsl:call-template name='create-css-styleheader'>
					<xsl:with-param name="globalData" select="$globalData" />
				</xsl:call-template>
				<xsl:if test="$debugEnabled"><xsl:message>CSS header creation finished!</xsl:message></xsl:if>
			</xsl:if>
		</xsl:element>

	</xsl:template>


	<!-- Creating a CSS style header from the collected styles of the 'globalData' parameter -->
	<xsl:template name='create-css-styleheader'>
		<xsl:param name="globalData" />

		<xsl:element name="style">
			<xsl:attribute name="type">text/css</xsl:attribute>
<xsl:text>
	</xsl:text>
	<xsl:call-template name='create-page-layout'>
		<xsl:with-param name="globalData" select="$globalData" />
	</xsl:call-template>
<xsl:text>table { border-collapse:collapse; border-spacing:0; empty-cells:show }
	</xsl:text>
<xsl:text>td, th { vertical-align:top; }
	</xsl:text>
<xsl:text>h1, h2, h3, h4, h5, h6 { clear:both }
	</xsl:text>
<xsl:text>ol, ul { padding:0; }
	</xsl:text>
<xsl:text>* { margin:0; }
	</xsl:text>
			<xsl:for-each select="$globalData/all-styles/style">
			<!-- NOTE: only the style family in conjunction with the style name, makes the style unambigous -->
		<xsl:text>*.</xsl:text><!--<xsl:value-of select="@style:family" /><xsl:text>:</xsl:text>--><xsl:value-of select="translate(@style:name, '. %()/\+', '')" /><xsl:text> { </xsl:text> <xsl:value-of select="final-properties" /><xsl:text>}
	</xsl:text>
			</xsl:for-each>
		</xsl:element>

	</xsl:template>


	<!-- Creating CSS page layout based on first office master style -->
	<xsl:template name='create-page-layout'>
		<xsl:param name="globalData" />

<xsl:text>@page { </xsl:text>

		<xsl:call-template name="page-size">
			<xsl:with-param name="globalData"   select="$globalData" />
		</xsl:call-template>
		<xsl:call-template name="page-margin">
			<xsl:with-param name="globalData"   select="$globalData" />
		</xsl:call-template>

<xsl:text> }
	</xsl:text>

	</xsl:template>



	<xsl:template name="page-size">
		<xsl:param name="globalData" />

		<xsl:variable name="printOrientation"  select="$globalData/styles-file/*/office:automatic-styles/style:page-master/style:properties/@style:print-orientation" />
		<xsl:variable name="pageWidth"         select="$globalData/styles-file/*/office:automatic-styles/style:page-master/style:properties/@fo:page-width" />
		<xsl:variable name="pageHeight"        select="$globalData/styles-file/*/office:automatic-styles/style:page-master/style:properties/@fo:page-height" />
		<xsl:choose>
			<xsl:when test="$pageWidth and $pageHeight">
				<xsl:text>size: </xsl:text>
				<xsl:value-of select="$pageWidth" />
				<xsl:text> </xsl:text>
				<xsl:value-of select="$pageHeight" />
				<xsl:text>; </xsl:text>
			</xsl:when>
			<xsl:when test="$printOrientation">
				<xsl:text>size: </xsl:text>
				<xsl:value-of select="$printOrientation" />
				<xsl:text>; </xsl:text>
			</xsl:when>
		</xsl:choose>
	</xsl:template>



	<xsl:template name="page-margin">
		<xsl:param name="globalData" />

		<xsl:variable name="marginTop"  select="$globalData/styles-file/*/office:automatic-styles/style:page-master/style:properties/@fo:margin-top" />
		<xsl:if test="$marginTop">
			<xsl:text>margin-top: </xsl:text>
			<xsl:value-of select="$marginTop" />
			<xsl:text>; </xsl:text>
		</xsl:if>
		<xsl:variable name="marginBottom"  select="$globalData/styles-file/*/office:automatic-styles/style:page-master/style:properties/@fo:margin-bottom" />
		<xsl:if test="$marginBottom">
			<xsl:text>margin-bottom: </xsl:text>
			<xsl:value-of select="$marginBottom" />
			<xsl:text>; </xsl:text>
		</xsl:if>
		<xsl:variable name="marginLeft"  select="$globalData/styles-file/*/office:automatic-styles/style:page-master/style:properties/@fo:margin-left" />
		<xsl:if test="$marginLeft">
			<xsl:text>margin-left: </xsl:text>
			<xsl:value-of select="$marginLeft" />
			<xsl:text>; </xsl:text>
		</xsl:if>
		<xsl:variable name="marginRight"  select="$globalData/styles-file/*/office:automatic-styles/style:page-master/style:properties/@fo:margin-right" />
		<xsl:if test="$marginRight">
			<xsl:text>margin-right: </xsl:text>
			<xsl:value-of select="$marginRight" />
		</xsl:if>
	</xsl:template>



	<!-- ********************************************************** -->
	<!-- *** Common XHTML header for CSS_INLINED and CSS_HEADER *** -->
	<!-- ********************************************************** -->

	<xsl:template name='xhtml-header-properties'>
		<xsl:param name="globalData" />

		<!-- explicit output content-type for low-tech browser (e.g. IE6) -->
		<xsl:element name="meta">
			<xsl:attribute name="http-equiv">content-type</xsl:attribute>
			<xsl:attribute name="content">text/html; charset=utf-8</xsl:attribute>
		</xsl:element>

		<!-- title of document for browser frame title -->
		<xsl:element name="title">
			<xsl:choose>
				<xsl:when test="$globalData/meta-file/*/office:meta/dc:title">
					<xsl:value-of select="$globalData/meta-file/*/office:meta/dc:title" />
				</xsl:when>
				<!--<xsl:otherwise>
					<xsl:text>- no title specified</xsl:text>
				</xsl:otherwise>-->
			</xsl:choose>
		</xsl:element>

		<!-- a bit commercial -->
		<xsl:element name="meta">
			<xsl:attribute name="name">generator</xsl:attribute>
			<xsl:attribute name="content">StarOffice/OpenOffice.org XSLT (http://xml.openoffice.org/sx2ml)</xsl:attribute>
		</xsl:element>

		<!-- the author of the input source -->
		<xsl:call-template name="add-meta-tag">
			<xsl:with-param name="meta-name" select="'author'" />
			<xsl:with-param name="meta-data" select="$globalData/meta-file/*/office:meta/meta:initial-creator" />
		</xsl:call-template>

		<!-- creation-date of the input source -->
		<xsl:call-template name="add-meta-tag">
			<xsl:with-param name="meta-name" select="'created'" />
			<xsl:with-param name="meta-data" select="$globalData/meta-file/*/office:meta/meta:creation-date" />
		</xsl:call-template>

		<!-- name of last changing person of the input source -->
		<xsl:call-template name="add-meta-tag">
			<xsl:with-param name="meta-name" select="'changedby'" />
			<xsl:with-param name="meta-data" select="$globalData/meta-file/*/office:meta/dc:creator" />
		</xsl:call-template>

		<!-- last changing date of the input source -->
		<xsl:call-template name="add-meta-tag">
			<xsl:with-param name="meta-name" select="'changed'" />
			<xsl:with-param name="meta-data" select="$globalData/meta-file/*/office:meta/dc:date" />
		</xsl:call-template>


		<!-- short description about the input source -->
		<xsl:call-template name="add-meta-tag">
			<xsl:with-param name="meta-name" select="'subject'" />
			<xsl:with-param name="meta-data" select="$globalData/meta-file/*/office:meta/dc:subject" />
		</xsl:call-template>

		<!-- detailed description about the input source -->
		<xsl:call-template name="add-meta-tag">
			<xsl:with-param name="meta-name" select="'description'" />
			<xsl:with-param name="meta-data" select="$globalData/meta-file/*/office:meta/dc:description" />
		</xsl:call-template>

		<!-- keywords about the input source -->
		<xsl:call-template name="add-meta-keywords">
			<xsl:with-param name="keyWords" select="$globalData/meta-file/*/office:meta/meta:keywords" />
		</xsl:call-template>

		<!-- base URL of document for resolving relative links  -->
		<xsl:element name="base">
			<xsl:attribute name="href">
				<!-- earlier 'targetURL' was used for an absoulte reference of base provided by the Office (file URL)
					<xsl:value-of select="$targetURL" />
					now '.' let relative links work, even if document has been moved -->
				<xsl:text>.</xsl:text>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<!-- generic template for adding common meta tags -->
	<xsl:template name="add-meta-tag">
		<xsl:param name="meta-name" />
		<xsl:param name="meta-data" />

		<xsl:if test="$meta-data">
			<xsl:element name="meta">
				<xsl:attribute name="name">
					<xsl:value-of select="$meta-name" />
				</xsl:attribute>
				<xsl:attribute name="content">
					<xsl:value-of select="$meta-data" />
				</xsl:attribute>
			</xsl:element>
		</xsl:if>
	</xsl:template>

	<!-- helper template to write keyword elements into a tokenized string -->
	<xsl:template name="add-meta-keywords">
		<xsl:param name="keyWords" />

		<xsl:if test="$keyWords">
			<xsl:element name="meta">
				<xsl:attribute name="name">keywords</xsl:attribute>
				<xsl:attribute name="content">
					<xsl:for-each select="$keyWords/meta:keyword">
						<xsl:value-of select="." />
						<xsl:if test="position() != last()">
							<xsl:text>, </xsl:text>
						</xsl:if>
					</xsl:for-each>
				</xsl:attribute>
			</xsl:element>
		</xsl:if>
	</xsl:template>

</xsl:stylesheet>
