<?xml version="1.0" encoding="utf-8"?>
<!--  Copyright (C) 2009, Earl Hood <earl AT earlhood DOT com>

      Permission is hereby granted, free of charge, to any person
      obtaining a copy of this software and associated documentation
      files (the "Software"), to deal in the Software without restriction,
      including without limitation the rights to use, copy, modify, merge,
      publish, distribute, sublicense, and/or sell copies of the Software,
      and to permit persons to whom the Software is furnished to do so,
      subject to the following conditions:

      The above copyright notice and this permission notice shall be
      included in all copies or substantial portions of the Software.

      THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY
      KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
      WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
      AND NONINFRINGEMENT.  IN NO EVENT SHALL EARL HOOD BE LIABLE FOR
      ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
      CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
      WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  -->
<!-- =====================================================================
     $Id: xmlcat-to-opencat.xsl,v 1.3 2009/10/28 17:03:13 ehood Exp $

     Description:
        XSLT to transform an XML Catalog to a TR9401 Catalog.
        Handy in cases where one has to use (old) software that
        only supports the TR9401 catalog format.

        XML Catalog defined:
          <http://www.oasis-open.org/committees/download.php/14809/xml-catalogs.html>
        TR9401 Catalog defined:
          <http://www.oasis-open.org/specs/tr9401.html>

   - ===================================================================== -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xcat="urn:oasis:names:tc:entity:xmlns:xml:catalog"
                exclude-result-prefixes="#all"
                version="2.0">

<xsl:output method="text"
            encoding="utf-8"
            byte-order-mark="no"/>

<xsl:param name="EOL" select="'&#x0A;'"/>

<xsl:template match="*:catalog">
  <xsl:choose>
    <xsl:when test="@prefer = 'public'">
      <xsl:text>OVERRIDE YES</xsl:text>
      <xsl:value-of select="$EOL"/>
    </xsl:when>
    <xsl:when test="@prefer = 'system'">
      <xsl:text>OVERRIDE NO</xsl:text>
      <xsl:value-of select="$EOL"/>
    </xsl:when>
    <xsl:otherwise/>
  </xsl:choose>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="*:public">
  <xsl:text>PUBLIC "</xsl:text>
  <xsl:value-of select="@publicId"/>
  <xsl:text>" "</xsl:text>
  <xsl:value-of select="@uri"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

<xsl:template match="*:system">
  <xsl:text>SYSTEM "</xsl:text>
  <xsl:value-of select="@systemId"/>
  <xsl:text>" "</xsl:text>
  <xsl:value-of select="@uri"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

<xsl:template match="*:rewriteSystem">
  <xsl:message>WARN: rewriteSystem not supported</xsl:message>
</xsl:template>

<xsl:template match="*:systemSuffix">
  <xsl:message>WARN: systemSuffix not supported</xsl:message>
</xsl:template>

<xsl:template match="*:delegatePublic">
  <xsl:text>DELEGATE "</xsl:text>
  <xsl:value-of select="@publicIdStartString"/>
  <xsl:text>" "</xsl:text>
  <xsl:value-of select="@catalog"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

<xsl:template match="*:delegateSystem">
  <xsl:message>WARN: delegateSystem not supported</xsl:message>
</xsl:template>

<xsl:template match="*:uri">
  <xsl:message>WARN: uri not supported</xsl:message>
</xsl:template>

<xsl:template match="*:rewriteURI">
  <xsl:message>WARN: rewriteURI not supported</xsl:message>
</xsl:template>

<xsl:template match="*:uriSuffix">
  <xsl:message>WARN: uriSuffix not supported</xsl:message>
</xsl:template>

<xsl:template match="*:delegateURI">
  <xsl:message>WARN: delegateURI not supported</xsl:message>
</xsl:template>

<xsl:template match="*:nextCatalog">
  <xsl:text>CATALOG "</xsl:text>
  <xsl:value-of select="@catalog"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

<xsl:template match="*:doctype">
  <xsl:text>DOCTYPE "</xsl:text>
  <xsl:value-of select="@name"/>
  <xsl:text>" "</xsl:text>
  <xsl:value-of select="@uri"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

<xsl:template match="*:document">
  <xsl:text>DOCUMENT "</xsl:text>
  <xsl:value-of select="@uri"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

<xsl:template match="*:dtddecl">
  <xsl:text>DTDDECL "</xsl:text>
  <xsl:value-of select="@publicId"/>
  <xsl:text>" "</xsl:text>
  <xsl:value-of select="@uri"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

<xsl:template match="*:entity">
  <xsl:text>ENTITY "</xsl:text>
  <xsl:value-of select="@name"/>
  <xsl:text>" "</xsl:text>
  <xsl:value-of select="@uri"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

<xsl:template match="*:linktype">
  <xsl:text>LINKTYPE "</xsl:text>
  <xsl:value-of select="@name"/>
  <xsl:text>" "</xsl:text>
  <xsl:value-of select="@uri"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

<xsl:template match="*:notation">
  <xsl:text>NOTATION "</xsl:text>
  <xsl:value-of select="@name"/>
  <xsl:text>" "</xsl:text>
  <xsl:value-of select="@uri"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

<xsl:template match="*:sgmldecl">
  <xsl:text>SGMLDECL "</xsl:text>
  <xsl:value-of select="@uri"/>
  <xsl:text>"</xsl:text>
  <xsl:value-of select="$EOL"/>
</xsl:template>

</xsl:stylesheet>
