lib/ucf/meta-inf.rb in ucf-0.6.0 vs lib/ucf/meta-inf.rb in ucf-0.7.0

- old
+ new

@@ -1,6 +1,6 @@ -# Copyright (c) 2013 The University of Manchester, UK. +# Copyright (c) 2013, 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: @@ -28,29 +28,58 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # Author: Robert Haines -require 'zip-container' +begin + require 'nokogiri' +rescue LoadError + # We don't care if there's no nokogiri but can't validate things without it. +end module UCF # This is a subclass of ManagedDirectory to represent the META-INF directory # in a basic UCF Document. class MetaInf < ZipContainer::ManagedDirectory + SCHEMA_DIR = ::File.join(::File.dirname(__FILE__), "schema") + CONTAINER_SCHEMA = ::File.join(SCHEMA_DIR, "container.rng") + MANIFEST_SCHEMA = ::File.join(SCHEMA_DIR, "OpenDocument-manifest-schema-v1.0-os.rng") + # :call-seq: # new -> MetaInf # # Create a standard META-INF ManagedDirectory. def initialize super("META-INF", false, - [ZipContainer::ManagedFile.new("container.xml"), - ZipContainer::ManagedFile.new("manifest.xml"), - ZipContainer::ManagedFile.new("metadata.xml"), - ZipContainer::ManagedFile.new("signatures.xml"), - ZipContainer::ManagedFile.new("encryption.xml"), - ZipContainer::ManagedFile.new("rights.xml")]) + [ + File.new("container.xml", CONTAINER_SCHEMA), + File.new("manifest.xml", MANIFEST_SCHEMA), + File.new("metadata.xml"), + File.new("signatures.xml"), + File.new("encryption.xml"), + File.new("rights.xml") + ] + ) + end + + class File < ZipContainer::ManagedFile + + def initialize(name, schema = nil) + super(name, false) + + @schema = nil + if defined?(::Nokogiri) + @schema = schema.nil? ? nil : Nokogiri::XML::RelaxNG(::File.open(schema)) + end + end + + protected + + def validate + @schema.nil? ? true : @schema.validate(Nokogiri::XML(contents)) == [] + end end end end