Class: XMLScan::XMLNamespaceDecoration

Inherits:
Decoration
  • Object
show all
Defined in:
lib/xmlscan/namespace.rb

Defined Under Namespace

Classes: NamespaceDeclaration

Constant Summary collapse

PredefinedNamespace =
{
  'xml'   => 'http://www.w3.org/XML/1998/namespace',
  'xmlns' => 'http://www.w3.org/2000/xmlns/',
}
ReservedNamespace =
PredefinedNamespace.invert

Instance Method Summary collapse

Methods inherited from Decoration

#initialize

Methods included from Visitor

#on_attr_charref, #on_attr_charref_hex, #on_attr_value, #on_attribute_end, #on_cdata, #on_chardata, #on_charref, #on_charref_hex, #on_comment, #on_end_document, #on_prolog_space, #on_xmldecl, #on_xmldecl_encoding, #on_xmldecl_end, #on_xmldecl_key, #on_xmldecl_other, #on_xmldecl_standalone, #on_xmldecl_version, #parse_error, #valid_error, #warning, #wellformed_error

Constructor Details

This class inherits a constructor from XMLScan::Decoration

Instance Method Details

#fix_namespaceObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/xmlscan/namespace.rb', line 237

def fix_namespace
  unless @ns_undeclared.empty? then
    @ns_undeclared.each_key { |i|
      @visitor.ns_wellformed_error "prefix `#{i}' is not declared"
    }
    @ns_undeclared.clear
  end
  unless @dont_same.empty? then
    @dont_same.each { |n1,n2,l|
      if @namespace[n1] == @namespace[n2] then
        ns_wellformed_error \
          "doubled localpart `#{l}' in the same namespace"
      end
    }
    @dont_same.clear
  end
  @prev_prefix.clear
end

#ns_parse_error(msg) ⇒ Object



89
90
91
# File 'lib/xmlscan/namespace.rb', line 89

def ns_parse_error(msg)
  @orig_visitor.ns_parse_error msg
end

#ns_valid_error(msg) ⇒ Object



97
98
99
# File 'lib/xmlscan/namespace.rb', line 97

def ns_valid_error(msg)
  @orig_visitor.ns_valid_error msg
end

#ns_wellformed_error(msg) ⇒ Object



93
94
95
# File 'lib/xmlscan/namespace.rb', line 93

def ns_wellformed_error(msg)
  @orig_visitor.ns_wellformed_error msg
end

#on_attr_entityref(ref, *a) ⇒ Object



300
301
302
303
304
305
# File 'lib/xmlscan/namespace.rb', line 300

def on_attr_entityref(ref, *a)
  if ref.include? ?: then
    ns_parse_error "entity reference `#{ref}' includes `:'"
  end
  @visitor.on_attr_entityref ref, *a
end

#on_attribute(name, *a) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/xmlscan/namespace.rb', line 139

def on_attribute(name, *a)
  if /:/n =~ name then
    prefix, localpart = $`, $'
    if localpart.include? ?: then
      ns_parse_error "localpart `#{localpart}' includes `:'"
    end
    unless @namespace.key? prefix then
      if uri = PredefinedNamespace[prefix] then
        @namespace[prefix] = uri
      else
        @ns_undeclared[prefix] = true
      end
    end
    if prefix == 'xmlns' then
      @visitor = @xmlns
      @xmlns.on_xmlns_start localpart
    else
      if prev = @prev_prefix[localpart] then
        @dont_same.push [ prev, prefix, localpart ]
      end
      @prev_prefix[localpart] = prefix
      @visitor.on_attribute_ns name, prefix, localpart, *a
    end
  elsif name == 'xmlns' then
    @visitor = @xmlns
    @xmlns.on_xmlns_start ''
  else
    @visitor.on_attribute_ns name, nil, name, *a
  end
end

#on_doctype(root, pubid, sysid, *a) ⇒ Object



276
277
278
279
280
281
# File 'lib/xmlscan/namespace.rb', line 276

def on_doctype(root, pubid, sysid, *a)
  if root.count(':') > 1 then
    ns_parse_error "qualified name `#{root}' includes `:'"
  end
  @visitor.on_doctype root, pubid, sysid, *a
end

#on_entityref(ref, *a) ⇒ Object



292
293
294
295
296
297
# File 'lib/xmlscan/namespace.rb', line 292

def on_entityref(ref, *a)
  if ref.include? ?: then
    ns_parse_error "entity reference `#{ref}' includes `:'"
  end
  @visitor.on_entityref ref, *a
end

#on_etag(name, *a) ⇒ Object



263
264
265
266
# File 'lib/xmlscan/namespace.rb', line 263

def on_etag(name, *a)
  h = @ns_hist.pop and @namespace.update h
  @visitor.on_etag name, *a
end

#on_pi(target, pi, *a) ⇒ Object



284
285
286
287
288
289
# File 'lib/xmlscan/namespace.rb', line 284

def on_pi(target, pi, *a)
  if target.include? ?: then
    ns_parse_error "PI target `#{target}' includes `:'"
  end
  @visitor.on_pi target, pi, *a
end

#on_stag(name, *a) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/xmlscan/namespace.rb', line 114

def on_stag(name, *a)
  @ns_hist.push nil
  unless /:/n =~ name then
    @visitor.on_stag_ns name, '', name, *a
  else
    prefix, localpart = $`, $'
    if localpart.include? ?: then
      ns_parse_error "localpart `#{localpart}' includes `:'"
    end
    if prefix == 'xmlns' then
      ns_wellformed_error \
        "prefix `xmlns' is not used for namespace prefix declaration"
    end
    unless @namespace.key? prefix then
      if uri = PredefinedNamespace[prefix] then
        @namespace[prefix] = uri
      else
        @ns_undeclared[prefix] = true
      end
    end
    @visitor.on_stag_ns name, prefix, localpart, *a
  end
end

#on_stag_end(name, *a) ⇒ Object



257
258
259
260
# File 'lib/xmlscan/namespace.rb', line 257

def on_stag_end(name, *a)
  fix_namespace
  @visitor.on_stag_end_ns name, @namespace, *a
end

#on_stag_end_empty(name, *a) ⇒ Object



269
270
271
272
273
# File 'lib/xmlscan/namespace.rb', line 269

def on_stag_end_empty(name, *a)
  fix_namespace
  @visitor.on_stag_end_empty_ns name, @namespace, *a
  h = @ns_hist.pop and @namespace.update h
end

#on_start_document(*a) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/xmlscan/namespace.rb', line 102

def on_start_document(*a)
  @namespace = {} #PredefinedNamespace.dup
  @ns_hist = []
  @ns_undeclared = {}     # for checking undeclared namespace prefixes.
  @prev_prefix = {}       # for checking doubled attributes.
  @dont_same = []         # ditto.
  @xmlns = NamespaceDeclaration.new(self)
  @orig_visitor = @visitor
  @visitor.on_start_document *a
end

#on_xmlns_end(prefix, uri, *a) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/xmlscan/namespace.rb', line 208

def on_xmlns_end(prefix, uri, *a)
  @visitor = @orig_visitor
  if PredefinedNamespace.key? prefix then
    if prefix == 'xmlns' then
      ns_wellformed_error \
        "prefix `xmlns' can't be bound to any namespace explicitly"
    elsif (s = PredefinedNamespace[prefix]) != uri then
      ns_wellformed_error \
        "prefix `#{prefix}' can't be bound to any namespace except `#{s}'"
    end
  end
  if uri.empty? then
    if prefix.empty? then
      uri = nil
    else
      ns_parse_error "`#{prefix}' is bound to empty namespace name"
    end
  elsif ReservedNamespace.key? uri then
    unless (s = ReservedNamespace[uri]) == prefix then
      ns_wellformed_error \
        "namespace `#{uri}' is reserved for prefix `#{s}'"
    end
  end
  (@ns_hist.last || @ns_hist[-1] = {})[prefix] = @namespace[prefix]
  @namespace[prefix] = uri
  @ns_undeclared.delete prefix
end