class XMLScan::XMLNamespaceDecoration

Constants

PredefinedNamespace
ReservedNamespace

Public Instance Methods

fix_namespace() click to toggle source
    # File lib/xmlscan/namespace.rb
237 def fix_namespace
238   unless @ns_undeclared.empty? then
239     @ns_undeclared.each_key { |i|
240       @visitor.ns_wellformed_error "prefix `#{i}' is not declared"
241     }
242     @ns_undeclared.clear
243   end
244   unless @dont_same.empty? then
245     @dont_same.each { |n1,n2,l|
246       if @namespace[n1] == @namespace[n2] then
247         ns_wellformed_error \
248           "doubled localpart `#{l}' in the same namespace"
249       end
250     }
251     @dont_same.clear
252   end
253   @prev_prefix.clear
254 end
ns_parse_error(msg) click to toggle source
   # File lib/xmlscan/namespace.rb
89 def ns_parse_error(msg)
90   @orig_visitor.ns_parse_error msg
91 end
ns_valid_error(msg) click to toggle source
   # File lib/xmlscan/namespace.rb
97 def ns_valid_error(msg)
98   @orig_visitor.ns_valid_error msg
99 end
ns_wellformed_error(msg) click to toggle source
   # File lib/xmlscan/namespace.rb
93 def ns_wellformed_error(msg)
94   @orig_visitor.ns_wellformed_error msg
95 end
on_attr_entityref(ref, *a) click to toggle source
    # File lib/xmlscan/namespace.rb
300 def on_attr_entityref(ref, *a)
301   if ref.include? ?: then
302     ns_parse_error "entity reference `#{ref}' includes `:'"
303   end
304   @visitor.on_attr_entityref ref, *a
305 end
on_attribute(name, *a) click to toggle source
    # File lib/xmlscan/namespace.rb
139 def on_attribute(name, *a)
140   if /:/n =~ name then
141     prefix, localpart = $`, $'
142     if localpart.include? ?: then
143       ns_parse_error "localpart `#{localpart}' includes `:'"
144     end
145     unless @namespace.key? prefix then
146       if uri = PredefinedNamespace[prefix] then
147         @namespace[prefix] = uri
148       else
149         @ns_undeclared[prefix] = true
150       end
151     end
152     if prefix == 'xmlns' then
153       @visitor = @xmlns
154       @xmlns.on_xmlns_start localpart
155     else
156       if prev = @prev_prefix[localpart] then
157         @dont_same.push [ prev, prefix, localpart ]
158       end
159       @prev_prefix[localpart] = prefix
160       @visitor.on_attribute_ns name, prefix, localpart, *a
161     end
162   elsif name == 'xmlns' then
163     @visitor = @xmlns
164     @xmlns.on_xmlns_start ''
165   else
166     @visitor.on_attribute_ns name, nil, name, *a
167   end
168 end
on_doctype(root, pubid, sysid, *a) click to toggle source
    # File lib/xmlscan/namespace.rb
276 def on_doctype(root, pubid, sysid, *a)
277   if root.count(':') > 1 then
278     ns_parse_error "qualified name `#{root}' includes `:'"
279   end
280   @visitor.on_doctype root, pubid, sysid, *a
281 end
on_entityref(ref, *a) click to toggle source
    # File lib/xmlscan/namespace.rb
292 def on_entityref(ref, *a)
293   if ref.include? ?: then
294     ns_parse_error "entity reference `#{ref}' includes `:'"
295   end
296   @visitor.on_entityref ref, *a
297 end
on_etag(name, *a) click to toggle source
    # File lib/xmlscan/namespace.rb
263 def on_etag(name, *a)
264   h = @ns_hist.pop and @namespace.update h
265   @visitor.on_etag name, *a
266 end
on_pi(target, pi, *a) click to toggle source
    # File lib/xmlscan/namespace.rb
284 def on_pi(target, pi, *a)
285   if target.include? ?: then
286     ns_parse_error "PI target `#{target}' includes `:'"
287   end
288   @visitor.on_pi target, pi, *a
289 end
on_stag(name, *a) click to toggle source
    # File lib/xmlscan/namespace.rb
114 def on_stag(name, *a)
115   @ns_hist.push nil
116   unless /:/n =~ name then
117     @visitor.on_stag_ns name, '', name, *a
118   else
119     prefix, localpart = $`, $'
120     if localpart.include? ?: then
121       ns_parse_error "localpart `#{localpart}' includes `:'"
122     end
123     if prefix == 'xmlns' then
124       ns_wellformed_error \
125         "prefix `xmlns' is not used for namespace prefix declaration"
126     end
127     unless @namespace.key? prefix then
128       if uri = PredefinedNamespace[prefix] then
129         @namespace[prefix] = uri
130       else
131         @ns_undeclared[prefix] = true
132       end
133     end
134     @visitor.on_stag_ns name, prefix, localpart, *a
135   end
136 end
on_stag_end(name, *a) click to toggle source
    # File lib/xmlscan/namespace.rb
257 def on_stag_end(name, *a)
258   fix_namespace
259   @visitor.on_stag_end_ns name, @namespace, *a
260 end
on_stag_end_empty(name, *a) click to toggle source
    # File lib/xmlscan/namespace.rb
269 def on_stag_end_empty(name, *a)
270   fix_namespace
271   @visitor.on_stag_end_empty_ns name, @namespace, *a
272   h = @ns_hist.pop and @namespace.update h
273 end
on_start_document(*a) click to toggle source
    # File lib/xmlscan/namespace.rb
102 def on_start_document(*a)
103   @namespace = {} #PredefinedNamespace.dup
104   @ns_hist = []
105   @ns_undeclared = {}     # for checking undeclared namespace prefixes.
106   @prev_prefix = {}       # for checking doubled attributes.
107   @dont_same = []         # ditto.
108   @xmlns = NamespaceDeclaration.new(self)
109   @orig_visitor = @visitor
110   @visitor.on_start_document *a
111 end
on_xmlns_end(prefix, uri, *a) click to toggle source
    # File lib/xmlscan/namespace.rb
208 def on_xmlns_end(prefix, uri, *a)
209   @visitor = @orig_visitor
210   if PredefinedNamespace.key? prefix then
211     if prefix == 'xmlns' then
212       ns_wellformed_error \
213         "prefix `xmlns' can't be bound to any namespace explicitly"
214     elsif (s = PredefinedNamespace[prefix]) != uri then
215       ns_wellformed_error \
216         "prefix `#{prefix}' can't be bound to any namespace except `#{s}'"
217     end
218   end
219   if uri.empty? then
220     if prefix.empty? then
221       uri = nil
222     else
223       ns_parse_error "`#{prefix}' is bound to empty namespace name"
224     end
225   elsif ReservedNamespace.key? uri then
226     unless (s = ReservedNamespace[uri]) == prefix then
227       ns_wellformed_error \
228         "namespace `#{uri}' is reserved for prefix `#{s}'"
229     end
230   end
231   (@ns_hist.last || @ns_hist[-1] = {})[prefix] = @namespace[prefix]
232   @namespace[prefix] = uri
233   @ns_undeclared.delete prefix
234 end