Skip to content

Commit

Permalink
Add the UCF::Dir class to create exploded containers.
Browse files Browse the repository at this point in the history
  • Loading branch information
hainesr committed Dec 17, 2014
1 parent d0092ba commit 0aed8de
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/ucf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

require 'ucf/version'
require 'ucf/meta-inf'
require 'ucf/dir'
require 'ucf/file'

# This is a ruby library to read and write UCF files in PK Zip format. See the
Expand Down
83 changes: 83 additions & 0 deletions lib/ucf/dir.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright (c) 2014 The University of Manchester, UK.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the names of The University of Manchester nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Author: Robert Haines

#
module UCF

# This class represents a UCF document - also known as an EPUB and very
# similar to the
# {EPUB Open Container Format (OCF)}[http://www.idpf.org/epub/30/spec/epub30-ocf.html].
# See the
# {UCF specification}[https://learn.adobe.com/wiki/display/PDFNAV/Universal+Container+Format]
# for more details.
#
# This class is a specialization of ZipContainer::Dir so you should see the
# {ZipContainer documentation}[http://mygrid.github.io/ruby-zip-container/]
# for much more information and a list of all the other methods available in
# this class. RDoc does not list inherited methods, unfortunately.
#
# There are code examples available with the source code of this library.
class Dir < ZipContainer::Dir

private_class_method :new

# :stopdoc:
DEFAULT_MIMETYPE = "application/epub+zip"

def initialize(filename)
super(filename)

# Initialize the managed entries and register the META-INF directory.
initialize_managed_entries(MetaInf.new)
end
# :startdoc:

# :call-seq:
# create(filename) -> UCF::Dir
# create(filename, mimetype) -> UCF::Dir
# create(filename) {|ucf| ...}
# create(filename, mimetype) {|ucf| ...}
#
# Create a new UCF document directory on disk and open it for editing. A
# custom mimetype for the container may be specified but if not the
# default, "application/epub+zip", will be used.
#
# Please see the
# {ZipContainer documentation}[http://mygrid.github.io/ruby-zip-container/]
# for much more information and a list of all the other methods available
# in this class. RDoc does not list inherited methods, unfortunately.
def self.create(filename, mimetype = DEFAULT_MIMETYPE, &block)
super(filename, mimetype, &block)
end

end
end
1 change: 1 addition & 0 deletions test/data/dirs/empty/mimetype
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/epub+zip
1 change: 1 addition & 0 deletions test/data/dirs/managed/greeting.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
1 change: 1 addition & 0 deletions test/data/dirs/managed/mimetype
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/epub+zip
1 change: 1 addition & 0 deletions test/data/dirs/null/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This dir should be "empty", which in this case just means no "mimetype" file.
24 changes: 24 additions & 0 deletions test/tc_managed_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ def initialize(filename)

end

class ExampleUCFDir < UCF::Dir

private_class_method :new

def initialize(filename)
super(filename)

valid = Proc.new { |contents| contents.match(/[Hh]ello/) }

register_managed_entry(ZipContainer::ManagedFile.new("greeting.txt",
:required => true, :validation_proc => valid))
end

end

class TestManagedEntries < Test::Unit::TestCase

# Check that the example UCF document does not validate as a ManagedUCF.
Expand Down Expand Up @@ -108,6 +123,15 @@ def test_pass_verification_2
end
end

# Check that the example UCF directory validates.
def test_pass_verification_dir
assert(ExampleUCFDir.verify($dir_mngd))

assert_nothing_raised(ZipContainer::MalformedContainerError) do
ExampleUCFDir.verify!($dir_mngd)
end
end

# Check that a standard UCF Container can be created and things within it
# are verified correctly.
def test_create_standard_container
Expand Down
56 changes: 56 additions & 0 deletions test/tc_read_dir.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright (c) 2014 The University of Manchester, UK.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the names of The University of Manchester nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Author: Robert Haines

require 'test/unit'
require 'ucf'

class TestReadDir < Test::Unit::TestCase

# Check that the empty directory does not verify.
def test_verify_empty_directory
assert_raise(ZipContainer::MalformedContainerError) do
UCF::Dir.verify!($dir_null)
end

refute(UCF::Dir.verify($dir_null))
end

# Check that the empty container directory does verify.
def test_verify_empty_container
assert_nothing_raised(ZipContainer::MalformedContainerError) do
UCF::Dir.verify!($dir_empty)
end

assert(UCF::Dir.verify($dir_empty))
end

end
6 changes: 5 additions & 1 deletion test/ts_ucf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
require "coveralls"
Coveralls.wear!

# Example data files.
# Example data files and directories.
$dir_null = "test/data/dirs/null"
$dir_empty = "test/data/dirs/empty"
$dir_mngd = "test/data/dirs/managed"
$file_null = "test/data/null.file"
$ucf_empty = "test/data/empty.ucf"
$zip_empty = "test/data/empty.zip"
Expand All @@ -43,6 +46,7 @@

# Run test cases.
require 'tc_create_file'
require 'tc_read_dir'
require 'tc_read_file'
require 'tc_reserved_names'
require 'tc_managed_entries'

0 comments on commit 0aed8de

Please sign in to comment.