File: tree.rb

package info (click to toggle)
rdtool 0.6.38-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 940 kB
  • sloc: ruby: 8,213; lisp: 387; sh: 33; makefile: 16
file content (103 lines) | stat: -rw-r--r-- 2,329 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require "rd/rdblockparser.tab"
require "rd/filter"
require "rd/document-struct"
require "rd/version"

module RD

  # document tree
  class Tree
    include Enumerable

    SYSTEM_NAME = "RDtool Framework -- Document Tree"
    SYSTEM_VERSION = "$Version: "+RD::VERSION+"$"
    VERSION = Version.new_from_version_string(SYSTEM_NAME, SYSTEM_VERSION)

    def Tree.version
      VERSION
    end

    TMP_DIR = "/tmp"

    def Tree.tmp_dir
      TMP_DIR
    end

    attr_reader :root
    attr_reader :document_struct
    attr_accessor :include_paths
    alias include_path include_paths
    alias include_path= include_paths=
    attr_reader :filters
    alias filter filters
    attr_accessor :tmp_dir

    def Tree.new_with_document_struct(document_struct, include_paths = [])
      Tree.new(document_struct, nil, include_paths)
    end

    def initialize(document_struct, src_str = nil, include_paths = [])
      @src = src_str
      @document_struct = document_struct
      @include_paths = include_paths
      @filters = Hash.new()
      @tmp_dir = TMP_DIR
      @root = nil
    end
    
    def parse
      parser = RDParser.new
      src = @src.respond_to?(:to_a) ? @src.to_a : @src.split(/^/)
      set_root(parser.parse(src, self))
    end

    def set_root(element)
      raise ArgumentError, "#{element.class} can't be root." unless
	@document_struct.is_valid?(self, element)
      @root = element
      element.parent = self
    end
    alias root= set_root

    def make_root(&block)
      child = DocumentElement.new
      set_root(child)
      child.build(&block) if block_given?
      child
    end

    def check_valid
      each_element do |i|
	raise RuntimeError,
	  "mismatched document structure, #{i.parent} <-/- #{i}." unless
	  @document_struct.is_valid?(i.parent, i)
      end
      true
    end

    def accept(visitor)
      @root.accept(visitor)
    end
    
    def each_element(&block)
      return nil unless @root
      @root.each(&block)
    end
    alias each each_element

    def tree
      self
    end
    
    def Tree.new_from_rdo(*rdos) # rdos: IOs
      tree = Tree.new("", [], nil)
      tree_content = []
      rdos.each do |i|
	subtree = Marshal.load(i)
	tree_content.concat(subtree.root.blocks)
      end
      tree.root = DocumentElement.new(tree_content)
      tree
    end
  end
end