Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save peterdietz/1584202 to your computer and use it in GitHub Desktop.
Save peterdietz/1584202 to your computer and use it in GitHub Desktop.
(DSpace XMLUI) Document Preview with Google Docs Viewer
Index: dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/themes/Reference/Reference.xsl
===================================================================
--- dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/themes/Reference/Reference.xsl (revision 0)
+++ dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/themes/Reference/Reference.xsl (revision 0)
@@ -0,0 +1,201 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ The contents of this file are subject to the license and copyright
+ detailed in the LICENSE and NOTICE files at the root of the source
+ tree and available online at
+
+ http://www.dspace.org/license/
+
+-->
+<!--
+ TODO: Describe this XSL file
+ Author: Alexey Maslov
+
+-->
+
+<xsl:stylesheet xmlns:i18n="http://apache.org/cocoon/i18n/2.1"
+ xmlns:dri="http://di.tamu.edu/DRI/1.0/"
+ xmlns:mets="http://www.loc.gov/METS/"
+ xmlns:xlink="http://www.w3.org/TR/xlink/"
+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
+ xmlns:dim="http://www.dspace.org/xmlns/dspace/dim"
+ xmlns:xhtml="http://www.w3.org/1999/xhtml"
+ xmlns:mods="http://www.loc.gov/mods/v3"
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
+ xmlns="http://www.w3.org/1999/xhtml"
+ exclude-result-prefixes="i18n dri mets xlink xsl dim xhtml mods dc">
+
+ <xsl:import href="../dri2xhtml.xsl"/>
+ <xsl:output indent="yes"/>
+
+
+ <!-- Document Preview with Google Docs viewer -->
+ <!-- Generate the bitstream information from the file section -->
+ <xsl:template match="mets:fileGrp[@USE='CONTENT']">
+ <xsl:param name="context"/>
+ <xsl:param name="primaryBitstream" select="-1"/>
+
+ <h2><i18n:text>xmlui.dri2xhtml.METS-1.0.item-files-head</i18n:text></h2>
+ <table class="ds-table file-list">
+ <tr class="ds-table-header-row">
+ <th><i18n:text>xmlui.dri2xhtml.METS-1.0.item-files-file</i18n:text></th>
+ <th><i18n:text>xmlui.dri2xhtml.METS-1.0.item-files-size</i18n:text></th>
+ <th><i18n:text>xmlui.dri2xhtml.METS-1.0.item-files-format</i18n:text></th>
+ <th><i18n:text>xmlui.dri2xhtml.METS-1.0.item-files-view</i18n:text></th>
+ <!-- Display header for 'Description' only if at least one bitstream contains a description -->
+ <xsl:if test="mets:file/mets:FLocat/@xlink:label != ''">
+ <th><i18n:text>xmlui.dri2xhtml.METS-1.0.item-files-description</i18n:text></th>
+ </xsl:if>
+ </tr>
+ <xsl:choose>
+ <!-- If one exists and it's of text/html MIME type, only display the primary bitstream -->
+ <xsl:when test="mets:file[@ID=$primaryBitstream]/@MIMETYPE='text/html'">
+ <xsl:apply-templates select="mets:file[@ID=$primaryBitstream]">
+ <xsl:with-param name="context" select="$context"/>
+ </xsl:apply-templates>
+ </xsl:when>
+ <!-- Otherwise, iterate over and display all of them -->
+ <xsl:otherwise>
+ <xsl:apply-templates select="mets:file">
+ <xsl:sort data-type="number" select="boolean(./@ID=$primaryBitstream)" order="descending" />
+ <xsl:sort select="mets:FLocat[@LOCTYPE='URL']/@xlink:title"/>
+ <xsl:with-param name="context" select="$context"/>
+ </xsl:apply-templates>
+ </xsl:otherwise>
+ </xsl:choose>
+ <!-- Add the document previewer window -->
+ <tr>
+ <td colspan='5'>
+ <a name="preview"></a>
+ <div id="preview-embed"/>
+ </td>
+ </tr>
+ </table>
+ </xsl:template>
+
+
+ <!-- Build a single row in the bitsreams table of the item view page -->
+ <xsl:template match="mets:file">
+ <xsl:param name="context" select="."/>
+ <tr>
+ <xsl:attribute name="class">
+ <xsl:text>ds-table-row </xsl:text>
+ <xsl:if test="(position() mod 2 = 0)">even </xsl:if>
+ <xsl:if test="(position() mod 2 = 1)">odd </xsl:if>
+ </xsl:attribute>
+ <td>
+ <a>
+ <xsl:attribute name="href">
+ <xsl:value-of select="mets:FLocat[@LOCTYPE='URL']/@xlink:href"/>
+ </xsl:attribute>
+ <xsl:attribute name="title">
+ <xsl:value-of select="mets:FLocat[@LOCTYPE='URL']/@xlink:title"/>
+ </xsl:attribute>
+ <xsl:choose>
+ <xsl:when test="string-length(mets:FLocat[@LOCTYPE='URL']/@xlink:title) > 50">
+ <xsl:variable name="title_length" select="string-length(mets:FLocat[@LOCTYPE='URL']/@xlink:title)"/>
+ <xsl:value-of select="substring(mets:FLocat[@LOCTYPE='URL']/@xlink:title,1,15)"/>
+ <xsl:text> ... </xsl:text>
+ <xsl:value-of select="substring(mets:FLocat[@LOCTYPE='URL']/@xlink:title,$title_length - 25,$title_length)"/>
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="mets:FLocat[@LOCTYPE='URL']/@xlink:title"/>
+ </xsl:otherwise>
+ </xsl:choose>
+ </a>
+ </td>
+ <!-- File size always comes in bytes and thus needs conversion -->
+ <td>
+ <xsl:choose>
+ <xsl:when test="@SIZE &lt; 1000">
+ <xsl:value-of select="@SIZE"/>
+ <i18n:text>xmlui.dri2xhtml.METS-1.0.size-bytes</i18n:text>
+ </xsl:when>
+ <xsl:when test="@SIZE &lt; 1000000">
+ <xsl:value-of select="substring(string(@SIZE div 1000),1,5)"/>
+ <i18n:text>xmlui.dri2xhtml.METS-1.0.size-kilobytes</i18n:text>
+ </xsl:when>
+ <xsl:when test="@SIZE &lt; 1000000000">
+ <xsl:value-of select="substring(string(@SIZE div 1000000),1,5)"/>
+ <i18n:text>xmlui.dri2xhtml.METS-1.0.size-megabytes</i18n:text>
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="substring(string(@SIZE div 1000000000),1,5)"/>
+ <i18n:text>xmlui.dri2xhtml.METS-1.0.size-gigabytes</i18n:text>
+ </xsl:otherwise>
+ </xsl:choose>
+ </td>
+ <!-- Lookup File Type description in local messages.xml based on MIME Type.
+ In the original DSpace, this would get resolved to an application via
+ the Bitstream Registry, but we are constrained by the capabilities of METS
+ and can't really pass that info through. -->
+ <td>
+ <xsl:call-template name="getFileTypeDesc">
+ <xsl:with-param name="mimetype">
+ <xsl:value-of select="substring-before(@MIMETYPE,'/')"/>
+ <xsl:text>/</xsl:text>
+ <xsl:value-of select="substring-after(@MIMETYPE,'/')"/>
+ </xsl:with-param>
+ </xsl:call-template>
+ </td>
+ <td>
+ <xsl:choose>
+ <xsl:when test="$context/mets:fileSec/mets:fileGrp[@USE='THUMBNAIL']/
+ mets:file[@GROUPID=current()/@GROUPID]">
+ <a class="image-link">
+ <xsl:attribute name="href">
+ <xsl:value-of select="mets:FLocat[@LOCTYPE='URL']/@xlink:href"/>
+ </xsl:attribute>
+ <img alt="Thumbnail">
+ <xsl:attribute name="src">
+ <xsl:value-of select="$context/mets:fileSec/mets:fileGrp[@USE='THUMBNAIL']/
+ mets:file[@GROUPID=current()/@GROUPID]/mets:FLocat[@LOCTYPE='URL']/@xlink:href"/>
+ </xsl:attribute>
+ </img>
+ </a>
+ </xsl:when>
+ <xsl:otherwise>
+ <a>
+ <xsl:attribute name="href">
+ <xsl:value-of select="mets:FLocat[@LOCTYPE='URL']/@xlink:href"/>
+ </xsl:attribute>
+ <i18n:text>xmlui.dri2xhtml.METS-1.0.item-files-viewOpen</i18n:text>
+ </a>
+ <xsl:choose>
+ <xsl:when test="@MIMETYPE='application/pdf'">
+ <xsl:text> or </xsl:text>
+ <a>
+ <xsl:attribute name="href">
+ <xsl:text>#preview</xsl:text>
+ </xsl:attribute>
+ <xsl:attribute name="onclick">
+ <xsl:text>embeddedPreview("</xsl:text>
+ <xsl:text>http://docs.google.com/viewer?url=</xsl:text>
+ <!--Google can't reach a local development machines, so you may want to code in your production server, where Google downloads the file from-->
+ <!--<xsl:text>http://example.edu/</xsl:text>-->
+ <!-- Getting bitstreams by ID -- XMLUI: /xmlui/bitstream/id JSPUI: /jspui/retrieve -->
+ <xsl:text>/xmlui/bitstream/id/</xsl:text>
+ <xsl:value-of select="substring(@ID,6)"/>
+ <xsl:text>&amp;embedded=true</xsl:text>
+ <xsl:text>");</xsl:text>
+ </xsl:attribute>
+ Preview
+ </a>
+ </xsl:when>
+ </xsl:choose>
+ </xsl:otherwise>
+ </xsl:choose>
+ </td>
+ <!-- Display the contents of 'Description' as long as at least one bitstream contains a description -->
+ <xsl:if test="$context/mets:fileSec/mets:fileGrp/mets:file/mets:FLocat/@xlink:label != ''">
+ <td>
+ <xsl:value-of select="mets:FLocat[@LOCTYPE='URL']/@xlink:label"/>
+ </td>
+ </xsl:if>
+
+ </tr>
+ </xsl:template>
+
+
+</xsl:stylesheet>
\ No newline at end of file
Index: dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/themes/Reference/lib/googleviewer.js
===================================================================
--- dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/themes/Reference/lib/googleviewer.js (revision 0)
+++ dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/themes/Reference/lib/googleviewer.js (revision 0)
@@ -0,0 +1,15 @@
+
+/**
+ * Creates an iframe with the specified source
+ * @REQUIRES jQuery
+ * @REQUIRES that there exists an element with ID preview-embed
+ */
+function embeddedPreview(source) {
+ if($("#embed").length > 0) {
+ //set the source to the location asked for
+ $("#embed").attr("src", source);
+ } else {
+ //Create the embed iframe
+ $("#preview-embed").append("<iframe id='embed' src='"+source+"' width='100%' height='342px' style='border:none;'/>"); //requires jQuery
+ }
+}
\ No newline at end of file
Index: dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/themes/Reference/sitemap.xmap
===================================================================
--- dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/themes/Reference/sitemap.xmap (revision 6916)
+++ dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/themes/Reference/sitemap.xmap (working copy)
@@ -116,6 +116,7 @@
<map:transform type="IncludePageMeta">
<map:parameter name="stylesheet.screen#1" value="lib/style.css"/>
<map:parameter name="stylesheet.screen#2" value="lib/style-ie.css"/>
+ <map:parameter name="javascript" value="lib/googleviewer.js"/>
<map:parameter name="theme.path" value="{global:theme-path}"/>
<map:parameter name="theme.name" value="{global:theme-name}"/>
@@ -124,6 +125,7 @@
<map:otherwise>
<map:transform type="IncludePageMeta">
<map:parameter name="stylesheet.screen" value="lib/style.css"/>
+ <map:parameter name="javascript" value="lib/googleviewer.js"/>
<map:parameter name="theme.path" value="{global:theme-path}"/>
<map:parameter name="theme.name" value="{global:theme-name}"/>
@@ -137,7 +139,7 @@
</map:match>
<!-- Step 3: Transform to XHTML -->
- <map:transform src="../dri2xhtml.xsl"/>
+ <map:transform src="Reference.xsl"/>
<!-- Step 4: Localize the page -->
<map:act type="locale">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment