Rewriting resources with no archived out links

This commit is contained in:
Eric van der Vlist 2012-05-09 19:38:21 +02:00
parent 4473ad6e15
commit 5acb10101f
2 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" xmlns:owk="http://owark.org/xslt/"
exclude-result-prefixes="xs xd owk" version="2.0">
<xd:doc scope="stylesheet">
<xd:desc>
<xd:p><xd:b>Created on:</xd:b> May 4, 2012</xd:p>
<xd:p><xd:b>Author:</xd:b> vdv</xd:p>
<xd:p>Common functions and template for URL rewriting</xd:p>
</xd:desc>
</xd:doc>
<xsl:function name="owk:is-relative" as="xs:boolean">
<xsl:param name="url" as="xs:string"/>
<xsl:sequence select="not(substring-before($url, ':') = ('http', 'https'))"/>
</xsl:function>
<xsl:function name="owk:safer-resolve-uri" as="xs:string">
<xsl:param name="relative" as="xs:string"/>
<xsl:param name="hbase" as="xs:string"/>
<xsl:sequence select="if (owk:is-relative($relative)) then resolve-uri($relative, $hbase) else $relative"/>
</xsl:function>
<xsl:function name="owk:url-rewrite" as="xs:string">
<xsl:param name="url" as="xs:string"/>
<xsl:variable name="no-fragment" select="substring-before(concat($url, '#'), '#')"/>
<xsl:variable name="abs" select="owk:safer-resolve-uri($no-fragment, $base) cast as xs:string"/>
<xsl:variable name="local-name" select="$index/resource[(for $u in (uri, same-as) return $u cast as xs:string) = $abs][1]/local-name"/>
<xsl:message>local-name: <xsl:value-of select="$local-name"/></xsl:message>
<xsl:sequence select="if ($local-name) then concat(if ($resource/uri/@seed = 'false') then '../' else '', $local-name) else owk:safer-resolve-uri($url, $base)"/>
</xsl:function>
<xsl:variable name="index" select="doc('input:index')/*"/>
<xsl:variable name="resource" select="doc('input:index-entry')/resource"/>
<xsl:variable name="base" select="$resource/uri"/>
</xsl:stylesheet>

View File

@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl" xmlns:xsltu="http://xsltunit.org/0/"
xmlns:owk="http://owark.org/xslt/" exclude-result-prefixes="exsl">
<xsl:import href="../actions/mediatypes/common-rewrite.xsl"/>
<xsl:import href="xsltunit.xsl"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="index" select="doc('local-names.xml')/index"/>
<xsl:variable name="resource" select="$index/resource[uri='http://gmpg.org/xfn/11']"/>
<xsl:key name="log-by-uri" match="/log/entry" use="uri"/>
<xsl:template match="/">
<xsltu:tests>
<xsltu:test id="is-relative1">
<xsl:call-template name="xsltu:assertEqual">
<xsl:with-param name="id" select="'is-relative'"/>
<xsl:with-param name="nodes1">
<is-relative>true</is-relative>
</xsl:with-param>
<xsl:with-param name="nodes2">
<is-relative>
<xsl:value-of select="owk:is-relative('/foo')"/>
</is-relative>
</xsl:with-param>
</xsl:call-template>
</xsltu:test>
<xsltu:test id="is-relative2">
<xsl:call-template name="xsltu:assertEqual">
<xsl:with-param name="id" select="'is-relative'"/>
<xsl:with-param name="nodes1">
<is-relative>false</is-relative>
</xsl:with-param>
<xsl:with-param name="nodes2">
<is-relative>
<xsl:value-of select="owk:is-relative('http://example.com/foo')"/>
</is-relative>
</xsl:with-param>
</xsl:call-template>
</xsltu:test>
<xsltu:test id="safer-resolve-uri1">
<xsl:call-template name="xsltu:assertEqual">
<xsl:with-param name="id" select="'is-relative'"/>
<xsl:with-param name="nodes1">
<uri>http://example.com/foo</uri>
</xsl:with-param>
<xsl:with-param name="nodes2">
<uri>
<xsl:value-of select="owk:safer-resolve-uri('/foo', 'http://example.com/')"/>
</uri>
</xsl:with-param>
</xsl:call-template>
</xsltu:test>
<xsltu:test id="safer-resolve-uri2">
<xsl:call-template name="xsltu:assertEqual">
<xsl:with-param name="id" select="'is-relative'"/>
<xsl:with-param name="nodes1">
<uri>http://owark.org/foo</uri>
</xsl:with-param>
<xsl:with-param name="nodes2">
<uri>
<xsl:value-of select="owk:safer-resolve-uri('http://owark.org/foo', 'http://example.com/')"/>
</uri>
</xsl:with-param>
</xsl:call-template>
</xsltu:test>
<xsltu:test id="safer-resolve-uri3">
<xsl:call-template name="xsltu:assertEqual">
<xsl:with-param name="id" select="'is-relative'"/>
<xsl:with-param name="nodes1">
<uri>http://owark.org/foo{{{{}}}}</uri>
</xsl:with-param>
<xsl:with-param name="nodes2">
<uri>
<xsl:value-of select="owk:safer-resolve-uri('http://owark.org/foo{{{{}}}}', 'http://example.com/')"/>
</uri>
</xsl:with-param>
</xsl:call-template>
</xsltu:test>
<xsltu:test id="url-rewrite">
<xsl:call-template name="xsltu:assertEqual">
<xsl:with-param name="id" select="'rewrite1'"/>
<xsl:with-param name="nodes1">
<uri>http://gmpg.org/foo</uri>
</xsl:with-param>
<xsl:with-param name="nodes2">
<uri>
<xsl:value-of select="owk:url-rewrite('/foo')"/>
</uri>
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="xsltu:assertEqual">
<xsl:with-param name="id" select="'rewrite2'"/>
<xsl:with-param name="nodes1">
<uri>../gmpg.org/11-1.html</uri>
</xsl:with-param>
<xsl:with-param name="nodes2">
<uri>
<xsl:value-of select="owk:url-rewrite('/xfn/11')"/>
</uri>
</xsl:with-param>
</xsl:call-template>
</xsltu:test>
</xsltu:tests>
</xsl:template>
</xsl:stylesheet>