Class: ActReferenceParser

Inherits:
Object
  • Object
show all
Defined in:
app/models/act_reference_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handle_internal_ids(doc) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/act_reference_parser.rb', line 80

def handle_internal_ids doc
  internal_ids = internal_ids(doc)
  internal_ids.each do |id, anchor_name|
    if (element = doc.at("//[@Id = '#{id}']") )
      begin
        element.set_attribute('anchor', anchor_name)
      rescue
        raise anchor_name.to_s + ' ' + element.inspect
      end
    end

    elements = (doc/"//[@Idref = '#{id}']")
    elements.each do |element|
      element.set_attribute('anchor-ref', anchor_name)
    end

  end
end

.inner_text(node) ⇒ Object


8
9
10
# File 'app/models/act_reference_parser.rb', line 8

def inner_text node
  node.inner_text.tr('()“','').strip
end

.internal_id_part(node) ⇒ Object


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
# File 'app/models/act_reference_parser.rb', line 12

def internal_id_part node
  part = nil

  case node.name
    when 'Amendment'
      part = "amendment"

    when 'SubSection'
      if (num = node.at('SubSection_PgfTag/PgfNumString'))
        part = "#{inner_text(num)}"
      end

    when 'Clause'
      if (num = node.at('ClauseTitle/ClauseTitle_PgfTag/PgfNumString') )
        part = "clause#{inner_text(num)}"
      end

    when 'Para'
      if (num = node.at('Paragraph_PgfTag/PgfNumString') )
        part = "#{inner_text(num)}"
      end

    when 'Schedule'
      if (num = node.at('ScheduleNumber_PgfTag/PgfNumString') )
        number = inner_text(num)[/[A-Z]+\d+/]
        number = inner_text(num)[/\d+/] unless number
        part = "schedule#{number}"
      end

    when 'Para.sch'
      if num = node['Major_Number_Only']
        part = num
      elsif (num = node.children_of_type('Paragraph.sch_PgfTag').first.at('PgfNumString') )
        part = inner_text(num)
      end

    when 'SubPara.sch'
      if num = node['Minor_Number_Only']
        part = num
      end

  end
  part
end

.internal_ids(doc) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/act_reference_parser.rb', line 57

def internal_ids doc
  ids = {}
  (doc/'//[@Id]').each do |e|

    if e['Number']
      id = "#{e.name.to_s.downcase}#{e['Number']}#{e['Letter'] ? e['Letter'] : ''}"
    elsif part = internal_id_part(e)
      id = part
    end

    parent = e.parent
    while parent
      if part = internal_id_part(parent)
        id = "#{part}-#{id}"
      end
      parent = parent.parent
    end

    ids[e['Id']] = id
  end
  ids
end

Instance Method Details

#attributes(resource, href, title) ⇒ Object


104
105
106
107
108
109
110
# File 'app/models/act_reference_parser.rb', line 104

def attributes resource, href, title
  if title
    %Q|rel="cite" resource="#{resource}" href="#{href}" title="#{title}"|
  else
    %Q|rel="cite" resource="#{resource}" href="#{href}"|
  end
end

#parse_xml(xml, options = {}) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/act_reference_parser.rb', line 112

def parse_xml xml, options={}
  doc = Hpricot.XML xml

  act_abbreviations = (doc/'//Interpretation/ActAbbreviation')
  clauses = (doc/'ClauseText')
  handle_abbreviated_act_references(act_abbreviations, clauses) unless act_abbreviations.empty?

  act_citations = (doc/'//Clause/ClauseText//Citation')
  handle_act_citation_references(act_citations) unless act_citations.empty?

  clauses = (doc/'ClauseText') + (doc/'LongTitle')
  mentions = clauses.collect {|clause| handle_raw_act_mentions(clause) }.include?(true)

  ActReferenceParser.handle_internal_ids(doc)

  no_references = (act_abbreviations.empty? && act_citations.empty? && !mentions)
  no_references ? xml : doc.to_s
end

#parse_xml_file(xml_file, options = {}) ⇒ Object


100
101
102
# File 'app/models/act_reference_parser.rb', line 100

def parse_xml_file xml_file, options={}
  parse_xml(IO.read(xml_file), options)
end