841f71bde97031b1c3a619c2b1178d3b4ec14eee
[booh] / lib / booh / rexml / namespace.rb
1 require 'booh/rexml/xmltokens'
2
3 module REXML
4         # Adds named attributes to an object.
5         module Namespace
6                 # The name of the object, valid if set
7                 attr_reader :name, :expanded_name
8                 # The expanded name of the object, valid if name is set
9                 attr_accessor :prefix
10                 include XMLTokens
11                 NAMESPLIT = /^(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u
12
13                 # Sets the name and the expanded name
14                 def name=( name )
15                         @expanded_name = name
16                         name =~ NAMESPLIT
17                         if $1
18                                 @prefix = $1
19                         else
20                                 @prefix = ""
21                                 @namespace = ""
22                         end
23                         @name = $2
24                 end
25
26                 # Compares names optionally WITH namespaces
27                 def has_name?( other, ns=nil )
28                         if ns
29                                 return (namespace() == ns and name() == other)
30                         elsif other.include? ":"
31                                 return fully_expanded_name == other
32                         else
33                                 return name == other
34                         end
35                 end
36
37                 alias :local_name :name
38
39                 # Fully expand the name, even if the prefix wasn't specified in the
40                 # source file.
41                 def fully_expanded_name
42                         ns = prefix
43                         return "#{ns}:#@name" if ns.size > 0 
44                         return @name
45                 end
46         end
47 end