1
require "rexml/namespace"
5
# Defines an Element Attribute; IE, a attribute=value pair, as in:
6
# <element attribute="value"/>. Attributes can be in their own
7
# namespaces. General users of REXML will not interact with the
8
# Attribute class much.
13
# The element to which this attribute belongs
15
# The normalized value of this attribute. That is, the attribute with
17
attr_writer :normalized
18
PATTERN = /\s*(#{NAME_STR})\s*=\s*(["'])(.*?)\2/um
22
# Attribute.new( attribute_to_clone )
23
# Attribute.new( source )
24
# Attribute.new( "attr", "attr_value" )
25
# Attribute.new( "attr", "attr_value", parent_element )
26
def initialize( first, second=nil, parent=nil )
27
@normalized = @unnormalized = @element = nil
28
if first.kind_of? Attribute
29
self.name = first.expanded_name
31
if second.kind_of? Element
34
@element = first.element
36
elsif first.kind_of? String
37
@element = parent if parent.kind_of? Element
41
raise "illegal argument #{first.class.name} to Attribute constructor"
45
# Returns the namespace of the attribute.
47
# e = Element.new( "elns:myelement" )
48
# e.add_attribute( "nsa:a", "aval" )
49
# e.add_attribute( "b", "bval" )
50
# e.attributes.get_attribute( "a" ).prefix # -> "nsa"
51
# e.attributes.get_attribute( "b" ).prefix # -> "elns"
52
# a = Attribute.new( "x", "y" )
57
pf = @element.prefix if @element
62
# Returns the namespace URL, if defined, or nil otherwise
64
# e = Element.new("el")
65
# e.add_attributes({"xmlns:ns", "http://url"})
66
# e.namespace( "ns" ) # -> "http://url"
68
arg = prefix if arg.nil?
69
@element.namespace arg
72
# Returns true if other is an Attribute and has the same name and value,
75
other.kind_of?(Attribute) and other.name==name and other.value==@value
78
# Creates (and returns) a hash from both the name and value
80
name.hash + value.hash
83
# Returns this attribute out as XML source, expanding the name
85
# a = Attribute.new( "x", "y" )
86
# a.to_string # -> "x='y'"
87
# b = Attribute.new( "ns:x", "y" )
88
# b.to_string # -> "ns:x='y'"
90
"#@expanded_name='#{to_s().gsub(/'/, ''')}'"
93
# Returns the attribute value, with entities replaced
95
return @normalized if @normalized
99
doc = @element.document
100
doctype = doc.doctype if doc
104
@normalized = Text::normalize( @value, doctype )
107
# Returns the UNNORMALIZED value of this attribute. That is, entities
108
# have been expanded to their values
110
return @unnormalized if @unnormalized
113
doc = @element.document
114
doctype = doc.doctype if doc
117
@unnormalized = Text::unnormalize( @value, doctype )
120
# Returns a copy of this attribute
125
# Sets the element of which this object is an attribute. Normally, this
126
# is not directly called.
128
# Returns this attribute
129
def element=( element )
134
# Removes this Attribute from the tree, and returns true if successfull
136
# This method is usually not called directly.
138
@element.attributes.delete self.name unless @element.nil?
141
# Writes this attribute (EG, puts 'key="value"' to the output)
142
def write( output, indent=-1 )
157
path = @element.xpath
158
path += "/@#{self.expanded_name}"
163
#vim:ts=2 sw=2 noexpandtab: