Class: HTree::Context

Inherits:
Object
  • Object
show all
Includes:
HTree
Defined in:
lib/htree/output.rb,
lib/htree/context.rb,
lib/htree/modules.rb,
lib/htree/equality.rb

Constant Summary collapse

DefaultNamespaces =

:stopdoc:

{'xml'=>'http://www.w3.org/XML/1998/namespace'}

Constants included from HTree

DefaultContext, ElementContent, ElementExclusions, ElementInclusions, EmptyBindingObject, HTMLContext, NamedCharacters, NamedCharactersPattern, OmittedAttrName

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTree

#==, build_node, #check_equality, compile_template, #exact_equal?, #exact_equal_object, expand_template, fix_element, fix_structure_list, frozen_string, #hash, parse, parse_as, parse_pairs, parse_xml, scan, #usual_equal_object, with_frozen_string_hash

Constructor Details

#initialize(namespaces = nil) ⇒ Context

The optional argument ‘namespaces’ should be a hash or nil. HTree::DefaultNamespaces is used if nil is specified.

If it is a hash, its key should be nil or a string. nil means default namespace. The string means some prefix which must not be empty.

The hash value should be a string. The empty string “” means unbound namespace.



18
19
20
21
22
23
24
25
26
# File 'lib/htree/context.rb', line 18

def initialize(namespaces=nil)
  namespaces ||= DefaultNamespaces
  namespaces.each_pair {|k, v|
    check_namespace_prefix(k)
    check_namespace_uri(v)
  }
  namespaces = namespaces.dup.freeze unless namespaces.frozen?
  @namespaces = namespaces
end

Instance Attribute Details

#namespacesObject (readonly)

Returns the value of attribute namespaces.



27
28
29
# File 'lib/htree/context.rb', line 27

def namespaces
  @namespaces
end

Instance Method Details

#make_exact_equal_objectObject Also known as: make_usual_equal_object



110
111
112
113
114
# File 'lib/htree/equality.rb', line 110

def make_exact_equal_object
  @namespaces.keys.sort {|prefix1, prefix2|
    Util.cmp_with_nil(prefix1, prefix2)
  }.map {|prefix| [prefix, @namespaces[prefix]] }
end

#namespace_uri(prefix) ⇒ Object

return a namespace URI corresponding to prefix. It returns nil if prefix is not defined.



31
32
33
# File 'lib/htree/context.rb', line 31

def namespace_uri(prefix)
  @namespaces[prefix]
end

#output_namespaces(out, outer_context) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/htree/output.rb', line 127

def output_namespaces(out, outer_context)
  unknown_namespaces = {}
  @namespaces.each {|prefix, uri|
    outer_uri = outer_context.namespace_uri(prefix)
    if outer_uri == nil
      unknown_namespaces[prefix] = uri
    elsif outer_uri != uri
      if prefix
        out.output_string " xmlns:#{prefix}="
      else
        out.output_string " xmlns="
      end
      Text.new(uri).output_attvalue(out, outer_context)
    end
  }
  unless unknown_namespaces.empty?
    out.output_xmlns(unknown_namespaces)
  end
  outer_context.subst_namespaces(@namespaces)
end

#subst_namespaces(declared_namespaces) ⇒ Object

generate a new Context object which namespaces are substituted by a hash declared_namespaces.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/htree/context.rb', line 37

def subst_namespaces(declared_namespaces)
  namespaces = @namespaces.dup
  declared_namespaces.each {|k, v|
    check_namespace_prefix(k)
    check_namespace_uri(v)
    namespaces[k] = v
  }
  if namespaces == @namespaces
    self
  else
    Context.new(namespaces)
  end
end