Class: HTree::Name

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

Overview

Name represents a element name and attribute name. It consists of a namespace prefix, a namespace URI and a local name.

Direct Known Subclasses

TemplateCompiler::TemplateAttrName

Constant Summary collapse

NameCache =
{}

Constants included from HTree

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

Instance Attribute Summary collapse

Class Method 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(namespace_prefix, namespace_uri, local_name) ⇒ Name

Returns a new instance of Name.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/htree/name.rb', line 60

def initialize(namespace_prefix, namespace_uri, local_name)
  @namespace_prefix = namespace_prefix
  @namespace_uri = namespace_uri
  @local_name = local_name
  if @namespace_prefix && /\A#{Pat::Nmtoken}\z/o !~ @namespace_prefix
    raise HTree::Error, "invalid namespace prefix: #{@namespace_prefix.inspect}"
  end
  if @local_name && /\A#{Pat::Nmtoken}\z/o !~ @local_name
    raise HTree::Error, "invalid local name: #{@local_name.inspect}"
  end
  if @namespace_prefix == 'xmlns'
    unless @namespace_uri == nil
      raise HTree::Error, "Name object for xmlns:* must not have namespace URI: #{@namespace_uri.inspect}"
    end
  else
    unless String === @namespace_uri 
      raise HTree::Error, "invalid namespace URI: #{@namespace_uri.inspect}"
    end
  end
end

Instance Attribute Details

#local_nameObject (readonly)

Returns the value of attribute local_name.



80
81
82
# File 'lib/htree/name.rb', line 80

def local_name
  @local_name
end

#namespace_prefixObject (readonly)

Returns the value of attribute namespace_prefix.



80
81
82
# File 'lib/htree/name.rb', line 80

def namespace_prefix
  @namespace_prefix
end

#namespace_uriObject (readonly)

Returns the value of attribute namespace_uri.



80
81
82
# File 'lib/htree/name.rb', line 80

def namespace_uri
  @namespace_uri
end

Class Method Details

.new(namespace_prefix, namespace_uri, local_name) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/htree/name.rb', line 52

def Name.new(namespace_prefix, namespace_uri, local_name)
  key = [namespace_prefix, namespace_uri, local_name, self]
  NameCache.fetch(key) {
    0.upto(2) {|i| key[i] = key[i].dup.freeze if key[i] }
    NameCache[key] = super(key[0], key[1], key[2])
  }
end

.parse_attribute_name(name, context) ⇒ Object



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

def Name.parse_attribute_name(name, context)
  if name == 'xmlns'
    Name.new('xmlns', nil, nil)
  elsif /\Axmlns:/ =~ name
    Name.new('xmlns', nil, $')
  elsif /\{(.*)\}/ =~ name
    case $`
    when ''; Name.new(nil, $1, $')
    else Name.new($`, $1, $')
    end
  elsif /:/ =~ name && !context.namespace_uri($`).empty?
    Name.new($`, context.namespace_uri($`), $')
  else
    Name.new(nil, '', name)
  end
end

.parse_element_name(name, context) ⇒ Object

element name prefix uri localname un, n with xmlns=u nil ‘u’ ‘n’ pun, p:n with xmlns:p=u ‘p’ ‘u’ ‘n’ n with xmlns=” nil ” ‘n’

attribute name xmlns= ‘xmlns’ nil nil xmlns:n= ‘xmlns’ nil ‘n’ pun=, p:n= with xmlns:p=u ‘p’ ‘u’ ‘n’ n= nil ” ‘n’



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/htree/name.rb', line 20

def Name.parse_element_name(name, context)
  if /\{(.*)\}/ =~ name
    # "{u}n" means "use default namespace",
    # "p{u}n" means "use the specified prefix p"
    $` == '' ? Name.new(nil, $1, $') : Name.new($`, $1, $')
  elsif /:/ =~ name && !context.namespace_uri($`).empty?
    Name.new($`, context.namespace_uri($`), $')
  elsif !context.namespace_uri(nil).empty?
    Name.new(nil, context.namespace_uri(nil), name)
  else
    Name.new(nil, '', name)
  end
end

Instance Method Details

#inspectObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/htree/inspect.rb', line 56

def inspect
  if xmlns?
    @local_name ? "xmlns:#{@local_name}" : "xmlns"
  elsif !@namespace_uri || @namespace_uri.empty?
    @local_name
  elsif @namespace_prefix
    "#{@namespace_prefix}{#{@namespace_uri}}#{@local_name}"
  elsif @namespace_prefix == false
    "-{#{@namespace_uri}}#{@local_name}"
  else
    "{#{@namespace_uri}}#{@local_name}"
  end
end

#make_exact_equal_objectObject



81
82
83
# File 'lib/htree/equality.rb', line 81

def make_exact_equal_object
  [@namespace_prefix, @namespace_uri, @local_name]
end

#make_usual_equal_objectObject



85
86
87
# File 'lib/htree/equality.rb', line 85

def make_usual_equal_object
  xmlns? ? @local_name : [@namespace_uri, @local_name]
end

#output(out, context) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/htree/output.rb', line 41

def output(out, context)
  # xxx: validate namespace prefix
  if xmlns?
    if @local_name
      out.output_string "xmlns:#{@local_name}"
    else
      out.output_string "xmlns"
    end
  else
    out.output_string qualified_name
  end
end

#output_attribute(text, out, context) ⇒ Object



54
55
56
57
58
# File 'lib/htree/output.rb', line 54

def output_attribute(text, out, context)
  output(out, context)
  out.output_string '='
  text.output_attvalue(out, context)
end

#qualified_nameObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/htree/name.rb', line 94

def qualified_name
  if @namespace_uri && !@namespace_uri.empty?
    if @namespace_prefix
      "#{@namespace_prefix}:#{@local_name}"
    else
      @local_name.dup
    end
  elsif @local_name
    @local_name.dup
  else
    "xmlns"
  end
end

#to_sObject



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/htree/name.rb', line 108

def to_s
  if @namespace_uri && !@namespace_uri.empty?
    if @namespace_prefix
      "#{@namespace_prefix}{#{@namespace_uri}}#{@local_name}"
    else
      "{#{@namespace_uri}}#{@local_name}"
    end
  elsif @local_name
    @local_name.dup
  else
    "xmlns"
  end
end

#universal_nameObject



86
87
88
89
90
91
92
# File 'lib/htree/name.rb', line 86

def universal_name
  if @namespace_uri && !@namespace_uri.empty?
    "{#{@namespace_uri}}#{@local_name}"
  else
    @local_name.dup
  end
end

#xmlns?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/htree/name.rb', line 82

def xmlns?
  @namespace_prefix == 'xmlns' && @namespace_uri == nil
end