Class REXML::Light::Node
In: lib/rexml/light/node.rb
Parent: Object

Represents a tagged XML element. Elements are characterized by having children, attributes, and names, and can themselves be children.

Methods

<<   =~   []   []=   children   each   has_name?   local_name   local_name=   name   name=   namespace   namespace=   namespace_of   namesplit   new   node_type   parent   parent=   prefix   prefix_of   root   size   text=   to_s  

Constants

NAMESPLIT = /^(?:(#{XMLTokens::NCNAME_STR}):)?(#{XMLTokens::NCNAME_STR})/u
PARENTS = [ :element, :document, :doctype ]

Public Class methods

Create a new element.

[Source]

    # File lib/rexml/light/node.rb, line 21
21:                         def initialize node=nil
22:                                 @node = node
23:                                 if node.kind_of? String
24:                                         node = [ :text, node ]
25:                                 elsif node.nil?
26:                                         node = [ :document, nil, nil ]
27:                                 elsif node[0] == :start_element
28:                                         node[0] = :element
29:                                 elsif node[0] == :start_doctype
30:                                         node[0] = :doctype
31:                                 elsif node[0] == :start_document
32:                                         node[0] = :document
33:                                 end
34:                         end

Public Instance methods

Append a child to this element, optionally under a provided namespace. The namespace argument is ignored if the element argument is an Element object. Otherwise, the element argument is a string, the namespace (if provided) is the namespace the element is created in.

[Source]

     # File lib/rexml/light/node.rb, line 122
122:                         def << element
123:                                 if node_type() == :text
124:                                         at(-1) << element
125:                                 else
126:                                         newnode = Node.new( element )
127:                                         newnode.parent = self
128:                                         self.push( newnode )
129:                                 end
130:                                 at(-1)
131:                         end

[Source]

     # File lib/rexml/light/node.rb, line 98
 98:                         def =~( path )
 99:                                 XPath.match( self, path )
100:                         end

[Source]

    # File lib/rexml/light/node.rb, line 86
86:                         def []( reference, ns=nil )
87:                                 if reference.kind_of? String
88:                                         pfx = ''
89:                                         pfx = "#{prefix(ns)}:" if ns
90:                                         at(3)["#{pfx}#{reference}"]
91:                                 elsif reference.kind_of? Range
92:                                         _old_get( Range.new(4+reference.begin, reference.end, reference.exclude_end?) )
93:                                 else
94:                                         _old_get( 4+reference )
95:                                 end
96:                         end

Doesn‘t handle namespaces yet

[Source]

     # File lib/rexml/light/node.rb, line 103
103:                         def []=( reference, ns, value=nil )
104:                                 if reference.kind_of? String
105:                                         value = ns unless value
106:                                         at( 3 )[reference] = value
107:                                 elsif reference.kind_of? Range
108:                                         _old_put( Range.new(3+reference.begin, reference.end, reference.exclude_end?), ns )
109:                                 else
110:                                         if value
111:                                                 _old_put( 4+reference, ns, value )
112:                                         else
113:                                                 _old_put( 4+reference, ns )
114:                                         end
115:                                 end
116:                         end

[Source]

     # File lib/rexml/light/node.rb, line 151
151:                         def children
152:                                 self
153:                         end

[Source]

    # File lib/rexml/light/node.rb, line 44
44:                         def each( &block )
45:                                 size.times { |x| yield( at(x+4) ) }
46:                         end

[Source]

     # File lib/rexml/light/node.rb, line 147
147:                         def has_name?( name, namespace = '' )
148:                                 at(3) == name and namespace() == namespace
149:                         end

[Source]

    # File lib/rexml/light/node.rb, line 62
62:                         def local_name
63:                                 namesplit
64:                                 @name
65:                         end

[Source]

    # File lib/rexml/light/node.rb, line 67
67:                         def local_name=( name_str )
68:                                 _old_put( 1, "#@prefix:#{name_str}" )
69:                         end

[Source]

    # File lib/rexml/light/node.rb, line 48
48:                         def name
49:                                 at(2)
50:                         end

[Source]

    # File lib/rexml/light/node.rb, line 52
52:                         def name=( name_str, ns=nil )
53:                                 pfx = ''
54:                                 pfx = "#{prefix(ns)}:" if ns
55:                                 _old_put(2, "#{pfx}#{name_str}")
56:                         end

[Source]

    # File lib/rexml/light/node.rb, line 75
75:                         def namespace( prefix=prefix() )
76:                                 namespace_of( self, prefix )
77:                         end

[Source]

    # File lib/rexml/light/node.rb, line 79
79:                         def namespace=( namespace )
80:                                 @prefix = prefix( namespace )
81:                                 pfx = ''
82:                                 pfx = "#@prefix:" if @prefix.size > 0
83:                                 _old_put(1, "#{pfx}#@name")
84:                         end

[Source]

     # File lib/rexml/light/node.rb, line 133
133:                         def node_type
134:                                 _old_get(0)
135:                         end

[Source]

     # File lib/rexml/light/node.rb, line 155
155:                         def parent
156:                                 at(1)
157:                         end

[Source]

    # File lib/rexml/light/node.rb, line 58
58:                         def parent=( node )
59:                                 _old_put(1,node)
60:                         end

[Source]

    # File lib/rexml/light/node.rb, line 71
71:                         def prefix( namespace=nil )
72:                                 prefix_of( self, namespace )
73:                         end

[Source]

     # File lib/rexml/light/node.rb, line 142
142:                         def root
143:                                 context = self
144:                                 context = context.at(1) while context.at(1)
145:                         end

[Source]

    # File lib/rexml/light/node.rb, line 36
36:                         def size
37:                                 if PARENTS.include? @node[0]
38:                                         @node[-1].size
39:                                 else
40:                                         0
41:                                 end
42:                         end

[Source]

     # File lib/rexml/light/node.rb, line 137
137:                         def text=( foo )
138:                                 replace = at(4).kind_of?(String)? 1 : 0
139:                                 self._old_put(4,replace, normalizefoo)
140:                         end

[Source]

     # File lib/rexml/light/node.rb, line 159
159:                         def to_s
160: 
161:                         end

Private Instance methods

[Source]

     # File lib/rexml/light/node.rb, line 172
172:                         def namespace_of( node, prefix=nil )
173:                                 if not prefix
174:                                         name = at(2)
175:                                         name =~ NAMESPLIT
176:                                         prefix = $1
177:                                 end
178:                                 to_find = 'xmlns'
179:                                 to_find = "xmlns:#{prefix}" if not prefix.nil?
180:                                 ns = at(3)[ to_find ]
181:                                 ns ? ns : namespace_of( @node[0], prefix )
182:                         end

[Source]

     # File lib/rexml/light/node.rb, line 165
165:                         def namesplit
166:                                 return if @name.defined?
167:                                 at(2) =~ NAMESPLIT
168:                                 @prefix = '' || $1
169:                                 @name = $2
170:                         end

[Source]

     # File lib/rexml/light/node.rb, line 184
184:                         def prefix_of( node, namespace=nil )
185:                                 if not namespace
186:                                         name = node.name
187:                                         name =~ NAMESPLIT
188:                                         $1
189:                                 else
190:                                         ns = at(3).find { |k,v| v == namespace }
191:                                         ns ? ns : prefix_of( node.parent, namespace )
192:                                 end
193:                         end

[Validate]