Class: Tree

Inherits:
Object show all
Includes:
ParseTree, TextArray
Defined in:
lib/rwd/tree.rb

Direct Known Subclasses

SGML

Constant Summary collapse

@@versie =
1
@@mutex =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParseTree

#parsetree

Methods included from TextArray

#textarray

Constructor Details

#initialize(string) ⇒ Tree

Returns a new instance of Tree.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rwd/tree.rb', line 145

def initialize(string)
  string = string.join("") if string.kind_of?(Array)

  @data		= string
  @parent		= nil
  @children		= []
  @objects		= []
  @visible		= true
  @checkvisibility	= false

  buildobjects(string)
  buildparents
  buildchildren
  markclosed
  deletedummies

  @checkvisibility	= true
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



137
138
139
# File 'lib/rwd/tree.rb', line 137

def children
  @children
end

#dataObject

Returns the value of attribute data.



133
134
135
# File 'lib/rwd/tree.rb', line 133

def data
  @data
end

#parentObject

Returns the value of attribute parent.



135
136
137
# File 'lib/rwd/tree.rb', line 135

def parent
  @parent
end

#visibleObject

Returns the value of attribute visible.



139
140
141
# File 'lib/rwd/tree.rb', line 139

def visible
  @visible
end

Class Method Details

.file(file) ⇒ Object



164
165
166
# File 'lib/rwd/tree.rb', line 164

def self.file(file)
  new(File.new(file).readlines)
end

.location(url, form = Hash.new) ⇒ Object



168
169
170
171
172
# File 'lib/rwd/tree.rb', line 168

def self.location(url, form=Hash.new)
  s	= HTTPClient.get(url, {}, form)
  s	= ""	if s.nil?
  new(s)
end

.new_from_cache(data) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rwd/tree.rb', line 178

def self.new_from_cache(data)
  hash	= MD5.new("#{@@versie} #{data}")

  dir		= "#{temp}/evcache.#{user}/tree.new"
  file	= "#{dir}/#{hash}"

  tree	= nil

  File.mkpath(dir)

  if File.file?(file)
    @@mutex.synchronize do
      tree	= Marshal.restore(File.new(file, "rb"))
    end
  else
    tree	= new(data)

    if not tree.nil?
      @@mutex.synchronize do
        File.open(file, "wb") {|f| Marshal.dump(tree, f)}
      end
    end
  end

  return tree
end

.new_from_cache2(data) ⇒ Object



174
175
176
# File 'lib/rwd/tree.rb', line 174

def self.new_from_cache2(data)
  new(data)
end

Instance Method Details

#buildchildrenObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/rwd/tree.rb', line 242

def buildchildren
  @objects.each do |obj|
    obj.children = []
  end

  parse do |type, obj|
    if not obj.parent.nil?
      po = obj.parent
      po.children << obj
    else
      @children << obj
    end
  end
end

#buildobjects(string) ⇒ Object



211
212
213
# File 'lib/rwd/tree.rb', line 211

def buildobjects(string)
  raise "Has to be defined in the subclass."
end

#buildparentsObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/rwd/tree.rb', line 215

def buildparents
  level	= 1
  levels	= Hash.new
  levels[0]	= nil
  parse do |type, obj|
    case obj.upordown
    when Down
      obj.level	= level
      obj.parent	= levels[level-1]
      levels[level]	= obj
      level += 1
    when Up, Dummy
      pl = level
      1.upto(level-1) do |l|
        po = levels[l]
        pl = l if po.subtype == obj.subtype
      end
      level = pl
      obj.level	= level
      obj.parent	= levels[level-1]
    when Same
      obj.level	= level
      obj.parent	= levels[level-1]
    end
  end
end

#deletedummiesObject



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/rwd/tree.rb', line 268

def deletedummies
  ([self] + @objects).each do |obj|
    obj.children.delete_if do |obj2|
      obj2.upordown == Dummy
    end
  end

  @objects.delete_if do |obj|
    obj.upordown == Dummy
  end
end

#inspectObject



205
206
207
208
209
# File 'lib/rwd/tree.rb', line 205

def inspect
  @objects.collect do |obj|
    obj.inspect
  end.join("\n")
end

#markclosedObject



257
258
259
260
261
262
263
264
265
266
# File 'lib/rwd/tree.rb', line 257

def markclosed
  ([self] + @objects).each do |obj|
    obj.children.each_index do |i|
      co1		= obj.children[i]
      co2		= obj.children[i+1]

      co1.closed	= (not co2.nil? and co1.upordown == Down and (co2.upordown == Up or co2.upordown == Dummy) and co1.subtype == co2.subtype)
    end
  end
end

#parse(types = [], subtypes = [], once = false) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/rwd/tree.rb', line 280

def parse(types=[], subtypes=[], once=false)
  types	= [types]	if types.class == Class
  subtypes	= [subtypes]	if subtypes.class == String
  hidelevel	= nil

  catch :once do
    @objects.each do |obj|
      if (@checkvisibility and hidelevel.nil? and (not obj.visible))
        hidelevel	= obj.level
      else
        if (@checkvisibility and (not hidelevel.nil?) and obj.visible and obj.level <= hidelevel)
          hidelevel	= nil
        end
      end

      if hidelevel.nil?
        ok = false
        catch :stop do
          if types.empty?
            if subtypes.empty?
              ok = true
              throw :stop
            else
              subtypes.each do |st|
                if obj.subtype == st
                  ok = true
                  throw :stop
                end
              end
            end
          else
            if subtypes.empty?
              types.each do |t|
                if obj.kind_of?(t)
                  ok = true
                  throw :stop
                end
              end
            else
              types.each do |t|
                subtypes.each do |st|
                  if obj.kind_of?(t) and obj.subtype == st
                    ok = true
                    throw :stop
                  end
                end
              end
            end
          end
        end

        if ok
          yield(obj.class.to_s, obj)

          throw :once	if once
        end
      end
    end
  end
end

#path(pad) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/rwd/tree.rb', line 341

def path(pad)
  p1	= self

  unless pad.nil?
    pad.split(/\//).each do |deel|
      tag, voorkomen	= deel.split(/:/)

      if (not tag.nil?) and (not p1.nil?)
        voorkomen	= 1	if voorkomen.nil?
        voorkomen	= voorkomen.to_i

        teller	= 0
        p2	= nil
        p1.children.each_index do |i|
          #if p1.children[i].upordown == Down
            unless  p1.children[i].subtype.nil?
              if p1.children[i].subtype.noquotes == tag.noquotes
                teller += 1
                p2	= p1.children[i]	if teller == voorkomen
              end
            end
          #end
        end
        p1	= p2
      end
    end
  end

  p1
end