| 1 | <p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/"> |
|---|
| 2 | |
|---|
| 3 | <p:param name="archive" type="input"/> |
|---|
| 4 | <p:param name="rewritten" type="output"/> |
|---|
| 5 | <p:param name="links" type="output"/> |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | <!-- Store the document --> |
|---|
| 9 | <p:processor name="oxf:file-serializer"> |
|---|
| 10 | <p:input name="config"> |
|---|
| 11 | <config> |
|---|
| 12 | <scope>session</scope> |
|---|
| 13 | </config> |
|---|
| 14 | </p:input> |
|---|
| 15 | <p:input name="data" href="#archive#xpointer(/archive/response/document)"/> |
|---|
| 16 | <p:output name="data" id="url-written"/> |
|---|
| 17 | </p:processor> |
|---|
| 18 | |
|---|
| 19 | <!-- And read it as HTML --> |
|---|
| 20 | <p:processor name="oxf:url-generator"> |
|---|
| 21 | <p:input name="config" transform="oxf:xslt" href="#url-written"> |
|---|
| 22 | <config xsl:version="2.0"> |
|---|
| 23 | <url> |
|---|
| 24 | <xsl:value-of select="/*"/> |
|---|
| 25 | </url> |
|---|
| 26 | <mode>html</mode> |
|---|
| 27 | </config> |
|---|
| 28 | </p:input> |
|---|
| 29 | <p:output name="data" id="html" debug="html"/> |
|---|
| 30 | </p:processor> |
|---|
| 31 | |
|---|
| 32 | <!-- Get a list of links to update --> |
|---|
| 33 | <!-- TODO: support links in inline CSS --> |
|---|
| 34 | <!-- TODO: support iframes and objects --> |
|---|
| 35 | <p:processor name="oxf:unsafe-xslt"> |
|---|
| 36 | <p:input name="data" href="#html"/> |
|---|
| 37 | <p:input name="request" href="#archive#xpointer(/archive/request)"/> |
|---|
| 38 | <p:input name="config"> |
|---|
| 39 | <xsl:stylesheet version="2.0"> |
|---|
| 40 | <xsl:variable name="base" select="doc('input:request')/request/location"/> |
|---|
| 41 | <xsl:template match="/"> |
|---|
| 42 | <links> |
|---|
| 43 | <xsl:variable name="links" as="node()*"> |
|---|
| 44 | <xsl:apply-templates/> |
|---|
| 45 | </xsl:variable> |
|---|
| 46 | <xsl:for-each-group select="$links" group-by="@href"> |
|---|
| 47 | <xsl:variable name="abs-href" select="resolve-uri(@href, $base)"/> |
|---|
| 48 | <xsl:variable name="tokens" select="tokenize($abs-href, '/')"/> |
|---|
| 49 | <xsl:variable name="last-token" select="$tokens[last()]"/> |
|---|
| 50 | <xsl:variable name="tokens2" select="tokenize($last-token, '\.')"/> |
|---|
| 51 | <xsl:variable name="extension" select="$tokens2[last()]"/> |
|---|
| 52 | <link abs-href="{$abs-href}" new-href="{saxon:string-to-hexBinary(substring($abs-href, 1, string-length($abs-href) - string-length($extension) - 1), 'utf-8')}.{$extension}" |
|---|
| 53 | filename="{saxon:string-to-hexBinary($abs-href, 'utf-8')}.xml"> |
|---|
| 54 | <xsl:copy-of select="@*"/> |
|---|
| 55 | </link> |
|---|
| 56 | </xsl:for-each-group> |
|---|
| 57 | </links> |
|---|
| 58 | </xsl:template> |
|---|
| 59 | <xsl:template match="text()"/> |
|---|
| 60 | <xsl:template match="link[@rel='stylesheet']"> |
|---|
| 61 | <link> |
|---|
| 62 | <xsl:copy-of select="@*"/> |
|---|
| 63 | </link> |
|---|
| 64 | </xsl:template> |
|---|
| 65 | <xsl:template match="img"> |
|---|
| 66 | <link href="{@src}" type="image/*"/> |
|---|
| 67 | </xsl:template> |
|---|
| 68 | <xsl:template match="script[@src]"> |
|---|
| 69 | <link href="{@src}" type="{@type}"/> |
|---|
| 70 | </xsl:template> |
|---|
| 71 | </xsl:stylesheet> |
|---|
| 72 | </p:input> |
|---|
| 73 | <p:output name="data" id="links-local" debug="links"/> |
|---|
| 74 | </p:processor> |
|---|
| 75 | |
|---|
| 76 | <p:processor name="oxf:identity"> |
|---|
| 77 | <p:input name="data" href="#links-local"/> |
|---|
| 78 | <p:output name="data" ref="links"/> |
|---|
| 79 | </p:processor> |
|---|
| 80 | |
|---|
| 81 | <!-- Update the links --> |
|---|
| 82 | <p:processor name="oxf:unsafe-xslt"> |
|---|
| 83 | <p:input name="data" href="#html"/> |
|---|
| 84 | <p:input name="request" href="#archive#xpointer(/archive/request)"/> |
|---|
| 85 | <p:input name="links" href="#links-local"/> |
|---|
| 86 | <p:input name="config"> |
|---|
| 87 | <xsl:stylesheet version="2.0"> |
|---|
| 88 | <xsl:variable name="links" select="doc('input:links')/links"/> |
|---|
| 89 | <xsl:variable name="base" select="doc('input:request')/request/location"/> |
|---|
| 90 | <xsl:key name="link" match="link" use="@href"/> |
|---|
| 91 | <xsl:template match="@*|node()"> |
|---|
| 92 | <xsl:copy> |
|---|
| 93 | <xsl:apply-templates select="@*|node()"/> |
|---|
| 94 | </xsl:copy> |
|---|
| 95 | </xsl:template> |
|---|
| 96 | <xsl:template match="link[@rel='stylesheet']/@href|img/@src|script/@src"> |
|---|
| 97 | <xsl:attribute name="{name(.)}"> |
|---|
| 98 | <xsl:value-of select="$links/key('link', current())/@new-href"/> |
|---|
| 99 | </xsl:attribute> |
|---|
| 100 | </xsl:template> |
|---|
| 101 | <xsl:template match="link[@rel!='stylesheet']/@href|a/@href"> |
|---|
| 102 | <xsl:attribute name="{name(.)}"> |
|---|
| 103 | <xsl:value-of select="resolve-uri(., $base)"/> |
|---|
| 104 | </xsl:attribute> |
|---|
| 105 | </xsl:template> |
|---|
| 106 | </xsl:stylesheet> |
|---|
| 107 | </p:input> |
|---|
| 108 | <p:output name="data" ref="rewritten" debug="rewritten"/> |
|---|
| 109 | </p:processor> |
|---|
| 110 | |
|---|
| 111 | </p:config> |
|---|