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
21 # FIXME: The parser doesn't catch illegal characters in attributes
24 # Either: an Attribute, which this new attribute will become a
25 # clone of; or a String, which is the name of this attribute
27 # If +first+ is an Attribute, then this may be an Element, or nil.
28 # If nil, then the Element parent of this attribute is the parent
29 # of the +first+ Attribute. If the first argument is a String,
30 # then this must also be a String, and is the content of the attribute.
31 # If this is the content, it must be fully normalized (contain no
32 # illegal characters).
34 # Ignored unless +first+ is a String; otherwise, may be the Element
35 # parent of this attribute, or nil.
38 # Attribute.new( attribute_to_clone )
39 # Attribute.new( attribute_to_clone, parent_element )
40 # Attribute.new( "attr", "attr_value" )
41 # Attribute.new( "attr", "attr_value", parent_element )
42 def initialize( first, second=nil, parent=nil )
43 @normalized = @unnormalized = @element = nil
44 if first.kind_of? Attribute
45 self.name = first.expanded_name
46 @unnormalized = first.value
47 if second.kind_of? Element
50 @element = first.element
52 elsif first.kind_of? String
55 @normalized = second.to_s
57 raise "illegal argument #{first.class.name} to Attribute constructor"
61 # Returns the namespace of the attribute.
63 # e = Element.new( "elns:myelement" )
64 # e.add_attribute( "nsa:a", "aval" )
65 # e.add_attribute( "b", "bval" )
66 # e.attributes.get_attribute( "a" ).prefix # -> "nsa"
67 # e.attributes.get_attribute( "b" ).prefix # -> "elns"
68 # a = Attribute.new( "x", "y" )
73 pf = @element.prefix if @element
78 # Returns the namespace URL, if defined, or nil otherwise
80 # e = Element.new("el")
81 # e.add_attributes({"xmlns:ns", "http://url"})
82 # e.namespace( "ns" ) # -> "http://url"
84 arg = prefix if arg.nil?
85 @element.namespace arg
88 # Returns true if other is an Attribute and has the same name and value,
91 other.kind_of?(Attribute) and other.name==name and other.value==value
94 # Creates (and returns) a hash from both the name and value
96 name.hash + value.hash
99 # Returns this attribute out as XML source, expanding the name
101 # a = Attribute.new( "x", "y" )
102 # a.to_string # -> "x='y'"
103 # b = Attribute.new( "ns:x", "y" )
104 # b.to_string # -> "ns:x='y'"
106 if @element and @element.context and @element.context[:attribute_quote] == :quote
107 %Q^#@expanded_name="#{to_s().gsub(/"/, '"e;')}"^
109 "#@expanded_name='#{to_s().gsub(/'/, ''')}'"
113 # Returns the attribute value, with entities replaced
115 return @normalized if @normalized
119 doc = @element.document
120 doctype = doc.doctype if doc
123 @normalized = Text::normalize( @unnormalized, doctype )
128 # Returns the UNNORMALIZED value of this attribute. That is, entities
129 # have been expanded to their values
131 return @unnormalized if @unnormalized
134 doc = @element.document
135 doctype = doc.doctype if doc
137 @unnormalized = Text::unnormalize( @normalized, doctype )
142 # Returns a copy of this attribute
147 # Sets the element of which this object is an attribute. Normally, this
148 # is not directly called.
150 # Returns this attribute
151 def element=( element )
156 # Removes this Attribute from the tree, and returns true if successfull
158 # This method is usually not called directly.
160 @element.attributes.delete self.name unless @element.nil?
163 # Writes this attribute (EG, puts 'key="value"' to the output)
164 def write( output, indent=-1 )
179 path = @element.xpath
180 path += "/@#{self.expanded_name}"
185 #vim:ts=2 sw=2 noexpandtab: