Class: JavaHelp::JarConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/javahelp.rb

Overview

This class converts Javahelp jar files to TOC objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jar_file) ⇒ JarConverter

Returns a new instance of JarConverter.



11
12
13
14
15
16
# File 'lib/javahelp.rb', line 11

def initialize(jar_file)
  raise "Javehelp file must be supplied" unless jar_file
  @jar_file = jar_file
  init_maps
  init_urls
end

Instance Attribute Details

#jar_fileObject (readonly)

Returns the value of attribute jar_file.



9
10
11
# File 'lib/javahelp.rb', line 9

def jar_file
  @jar_file
end

#mapsObject (readonly)

Returns the value of attribute maps.



9
10
11
# File 'lib/javahelp.rb', line 9

def maps
  @maps
end

#urlsObject (readonly)

Returns the value of attribute urls.



9
10
11
# File 'lib/javahelp.rb', line 9

def urls
  @urls
end

Instance Method Details

#convert_tocsObject

Converts the Javahelp table of contents files to TOC objects and returns them in an array.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/javahelp.rb', line 20

def convert_tocs
  tocs = []

  Zip::ZipFile.open(@jar_file) do |zf|
    zf.dir.foreach('.') do |entry|
      if File.extname(entry).downcase.eql?('.hs')
        contents = zf.file.read(entry)
        doc = REXML::Document.new(contents)           
        doc.elements.each('helpset/view') do |e|
          view_type = e.elements['type'].texts.join
          if view_type.eql?('javax.help.TOCView')
            toc_name = e.elements['data'].texts.join
            tocs << TOC.new(toc_name)
          end
        end
      end
    end        
    tocs.each do |toc|
      contents = zf.file.read(toc.name)
      doc = REXML::Document.new(contents)
      root = doc.root
      io = StringIO.new
      xml = Builder::XmlMarkup.new(:indent => 2, :target => io)
      xml.instruct!
      xml.toc(:label => 'toc') do
        root.elements.each do |e|                        
          assemble_tags(e, xml)
        end
      end
      toc.xml = io.string
    end
  end
  return tocs
end

#copyContentsTo(target) ⇒ Object

Copy the contents of the Javahelp jar into the target ZipFile. The block is called for each entry in the Javahelp file. If the block returns false, the entry is not copied.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/javahelp.rb', line 58

def copyContentsTo(target)
  Zip::ZipFile.open(@jar_file) do |zf|
    zf.each do |entry|
      if (yield(entry))            
        target.get_output_stream(entry) do |out| 
          contents = zf.read(entry.name)
          out.write(contents)
        end
      end
    end
  end
end