Class: XML::InternalParserNS

Inherits:
Parser
  • Object
show all
Defined in:
lib/xml/parserns.rb

Constant Summary collapse

XMLNS =
'http://www.w3.org/XML/1998/namespace'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nsObject (readonly)

Returns the value of attribute ns.



11
12
13
# File 'lib/xml/parserns.rb', line 11

def ns
  @ns
end

Class Method Details

.new(parserNS, *args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/xml/parserns.rb', line 13

def self.new(parserNS, *args)
  nssep = nil
  if args.length == 2 && !args[0].is_a?(Parser)
    nssep = args[1]
    args = args.shift
  end
  obj = super(*args)
  obj.__init__(parserNS, nssep)
  obj
end

Instance Method Details

#__init__(parserNS, nssep) ⇒ Object



24
25
26
27
28
# File 'lib/xml/parserns.rb', line 24

def __init__(parserNS, nssep)
  @ns = []
  @parserNS = parserNS
  @nssep = nssep
end

#getNamespacesObject



77
78
79
# File 'lib/xml/parserns.rb', line 77

def getNamespaces
  @ns[-1]
end

#getNSURI(prefix) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/xml/parserns.rb', line 81

def getNSURI(prefix)
  return XMLNS if prefix == 'xml'
  @ns.reverse_each do |n|
    return n[prefix] if n.include?(prefix)
  end
  nil
end

#getSpecifiedAttributesObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/xml/parserns.rb', line 104

def getSpecifiedAttributes
  ret = super
#      attrs = {}
#      ret.each do |k, v|
#        next if k =~ /^xmlns/u
#        if @nssep
#          prefix, uri, localpart = resolveAttributeQName(k)
#          k = uri.to_s + @nssep + k
#        end
#        attrs[k] = v
#      end
  attrs = []
  ret.each do |k|
    next if k =~ /^xmlns:|^xmlns$/u
    if @nssep
      prefix, uri, localpart = resolveAttributeQName(k)
      k = uri.to_s + @nssep + k
    end
    attrs.push(k)
  end
  attrs
end

#parse(*args) ⇒ Object



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
# File 'lib/xml/parserns.rb', line 31

def parse(*args)
  if block_given?
    super do |nodetype, name, args, parser|
      case nodetype
      when START_ELEM
        ns, args = getNSAttrs(args)
        @ns.push(ns)
        if @nssep
          if @parserNS.respond_to?(:startNamespaceDecl)
            ns.each do |prefix, uri|
              yield(START_NAMESPACE_DECL, prefix, uri, parser)
            end
          end

          prefix, uri, localpart = resolveElementQName(name)
          name = uri + @nssep + name if uri
          attrs = {}
          args.each do |k, v|
            prefix, uri, localpart = resolveAttributeQName(k)
            k = uri + @nssep + k if uri
            attrs[k] = v
          end
          args = attrs
        end
        yield(nodetype, name, args, parser)
      when END_ELEM
        if @nssep
          prefix, uri, localpart = resolveElementQName(name)
          name = uri + @nssep + name if uri
        end
        yield(nodetype, name, args, parser)
        ns = @ns.pop
        if @nssep and @parserNS.respond_to?(:endNamespaceDecl)
          ns.to_a.reverse.each do |prefix, uri|
            yield(END_NAMESPACE_DECL, prefix, nil, parser)
          end
        end
      else
        yield(nodetype, name, args, parser)
      end
    end
  else
    super
  end
end

#resolveAttributeQName(qname) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/xml/parserns.rb', line 96

def resolveAttributeQName(qname)
  qname =~ /^((\S+):)?(\S+)$/u
  prefix, localpart = $2, $3
  uri = nil
  uri = getNSURI(prefix) if !prefix.nil?
  [prefix, uri, localpart]
end

#resolveElementQName(qname) ⇒ Object



89
90
91
92
93
94
# File 'lib/xml/parserns.rb', line 89

def resolveElementQName(qname)
  qname =~ /^((\S+):)?(\S+)$/u
  prefix, localpart = $2, $3
  uri = getNSURI(prefix)
  [prefix, uri, localpart]
end