class DocbookFiles::FileData

Represents the actual file that is included or referenced in a DocBook project. For every file there should only one FileData instance, so we use a factory method #FileData.for to create new instances.

Constants

STATUS_ERR

Error while processing the file, see error_string

STATUS_NOT_FOUND

File does not exist

STATUS_OK

File exists and no error happened

Attributes

checksum[R]

SHA1 checksum, file size in bytes, mime type, last modified timestamp

docbook[RW]

XML data: namespace, docbook flag, namespace version, start tag

error_string[RW]
exists[RW]
full_name[RW]
key[R]

Unique key (path+checksum)

mime[R]

SHA1 checksum, file size in bytes, mime type, last modified timestamp

name[RW]
namespace[RW]

XML data: namespace, docbook flag, namespace version, start tag

path[RW]
size[R]

SHA1 checksum, file size in bytes, mime type, last modified timestamp

status[RW]
tag[RW]

XML data: namespace, docbook flag, namespace version, start tag

ts[R]

SHA1 checksum, file size in bytes, mime type, last modified timestamp

version[RW]

XML data: namespace, docbook flag, namespace version, start tag

Public Class Methods

files() click to toggle source

Return all existing FileData instances

# File lib/docbook_files/file_data.rb, line 29
def self.files; @@Files.values; end
for(name,parent_dir=".") click to toggle source

Factory method for FileData instances. Checks if there is already an instance.

# File lib/docbook_files/file_data.rb, line 39
def self.for(name,parent_dir=".")
  full_name = get_full_name(name, parent_dir)
  # Initialize the main dir name for path construction
  @@MainDir = File.dirname(full_name) if @@Files.size == 0
  key = full_name
  if (@@Files[key].nil?)
    @@Files[key] = FileData.new(name, full_name, key, parent_dir)
  end
  @@Files[key]
end
new(name, full_name, key, parent_dir=".") click to toggle source
# File lib/docbook_files/file_data.rb, line 62
def initialize(name, full_name, key, parent_dir=".")
  @path = relative2main(full_name)
  @full_name = full_name
  @name = File.basename(name)
  @key = key
  @namespace = ""
  @docbook = false
  @version = ""
  @tag = ""
  @error_string = nil
  @rels = []
  if (File.exists?(@full_name))
    @status = STATUS_OK
    @ts  = File.mtime(full_name)
    @size = File.size(full_name)        
    @checksum = calc_checksum(full_name)
    @mime = get_mime_type()
  else
    @status = STATUS_NOT_FOUND
    @ts = Time.now
    @size = 0
    @checksum = ""
    @mime = ""
    @error_string = "file not found"
  end      
end
reset() click to toggle source

Reset the FileData storage – must be done before every run!

# File lib/docbook_files/file_data.rb, line 32
def self.reset
  @@Files={}
  @@MainDir = ""
end
storage() click to toggle source

Return the FileData storage – for testing only

# File lib/docbook_files/file_data.rb, line 26
def self.storage; @@Files; end

Private Class Methods

get_full_name(fname, parent_dir) click to toggle source

Produce the full path for a filename

# File lib/docbook_files/file_data.rb, line 194
def self.get_full_name(fname, parent_dir)
  dir = File.dirname(fname)
  file = File.basename(fname)
  full_name = File.expand_path(file,dir)
  unless File.exists?(full_name)
      full_name = File.expand_path(fname, parent_dir)
  end
  full_name
end

Public Instance Methods

add_includes(incs) click to toggle source

Add included FileDatas. Establishes a two way relationship between self and the included files:

self -> TYPE_INCLUDE -> target self <- TYPE_INCLUDED_BY <- target

TODO: should be 'add_includes'

# File lib/docbook_files/file_data.rb, line 118
def add_includes(incs)
  add_rels(FileRefTypes::TYPE_INCLUDE, FileRefTypes::TYPE_INCLUDED_BY, incs)
end
add_references(incs) click to toggle source

Add referenced FileDatas. Establishes a two way relationship between self and the referenced files:

self -> TYPE_REFERENCE -> target self <- TYPE_REFERENCED_BY <- target

# File lib/docbook_files/file_data.rb, line 136
def add_references(incs)
  add_rels(FileRefTypes::TYPE_REFERENCE, FileRefTypes::TYPE_REFERENCED_BY, incs)
end
add_rel(type, target) click to toggle source

Add a one-way relationship, type and target, to self

# File lib/docbook_files/file_data.rb, line 96
def add_rel(type, target)
  @rels << {:type => type, :target => target}
end
add_rels(type, invtype, targets) click to toggle source

Add a two-way relationship between self and a number of targets.

# File lib/docbook_files/file_data.rb, line 100
def add_rels(type, invtype, targets)
  targets.each {|t|
    self.add_rel(type, t)
    t.add_rel(invtype, self)
  }
end
exists?() click to toggle source

Does the really file exist?

# File lib/docbook_files/file_data.rb, line 91
def exists?
  @status != STATUS_NOT_FOUND
end
get_rel_targets(type) click to toggle source

Get all targets for a relation

# File lib/docbook_files/file_data.rb, line 107
def get_rel_targets(type)
  @rels.find_all{|rel| rel[:type] == type}.map{|r| r[:target]}
end
included_by() click to toggle source

Retrieves all FileDatas that include self

# File lib/docbook_files/file_data.rb, line 126
def included_by
  get_rel_targets(FileRefTypes::TYPE_INCLUDED_BY)
end
includes() click to toggle source

Retrieves all included FileDatas

# File lib/docbook_files/file_data.rb, line 122
def includes
  get_rel_targets(FileRefTypes::TYPE_INCLUDE)
end
referenced_by() click to toggle source

Retrieves all FileDatas that reference self

# File lib/docbook_files/file_data.rb, line 144
def referenced_by
  get_rel_targets(FileRefTypes::TYPE_REFERENCED_BY)
end
references() click to toggle source

Retrieves all referenced files

# File lib/docbook_files/file_data.rb, line 140
def references
  get_rel_targets(FileRefTypes::TYPE_REFERENCE)
end
relative2main(file_name) click to toggle source

Try to find the path of file_name that is relative to directory of the _main file_. If there is no common part return the file_name.

# File lib/docbook_files/file_data.rb, line 151
def relative2main(file_name)
  md = file_name.match("^#{@@MainDir}/")
  if md.nil?
    file_name
  else
    md.post_match
  end
end
to_hash(props) click to toggle source

Return a hash with the values for the passed symbols.

Example: to_hash([:name, :mime]) would return

{:name => "name", :mime => "application/xml"}.
# File lib/docbook_files/file_data.rb, line 165
def to_hash(props)
  me_hash = {}      
  props.each {|p|
    if ([:includes, :included_by, :references, :referenced_by].member?(p))
      me_hash[p] = self.send(p).map{|p2| p2.path}
    else
      me_hash[p] = self.send(p)
    end
  }
  me_hash
end

Private Instance Methods

calc_checksum(full_name) click to toggle source

Calculate the SHA1 checksum for the file.

# File lib/docbook_files/file_data.rb, line 184
def calc_checksum(full_name)
  if RUBY_VERSION=~ /^1.8/
    contents = open(full_name, "rb") {|io| io.read }
  else
    contents = IO.binread(full_name)
  end
  Digest::SHA1.hexdigest(contents)
end
get_mime_type() click to toggle source

Try to find the file's MIME type

# File lib/docbook_files/file_data.rb, line 205
def get_mime_type
  Wand.wave(@full_name)
end