PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/xml/namespaces/xmlns_composite_node.e

http://github.com/tybor/Liberty
Specman e | 234 lines | 181 code | 19 blank | 34 comment | 10 complexity | c8f5376effe728f4e533c374ab9a72fd MD5 | raw file
  1-- See the Copyright notice at the end of this file.
  2--
  3class XMLNS_COMPOSITE_NODE
  4   --
  5   -- A node in an XMLNS_TREE
  6   --
  7
  8inherit
  9   XMLNS_NODE
 10
 11create {XMLNS_TREE}
 12   make
 13
 14feature {ANY}
 15   namespace: UNICODE_STRING
 16         -- The namespace of the node
 17
 18   name: UNICODE_STRING
 19         -- The name of the node
 20
 21   attribute_namespace (index: INTEGER): UNICODE_STRING
 22         -- The namespace of the i'th attribute
 23      require
 24         index.in_range(1, attributes_count)
 25      local
 26         i, d, n: INTEGER
 27      do
 28         if index <= no_namespace_attributes.count then
 29            Result := Void
 30         else
 31            from
 32               i := attributes.lower
 33               d := index - no_namespace_attributes.count
 34            until
 35               Result /= Void
 36            loop
 37               n := attributes.key(i).count
 38               if d <= n then
 39                  Result := attributes.key(i)
 40               else
 41                  d := d - n
 42                  i := i + 1
 43               end
 44            end
 45         end
 46      end
 47
 48   attribute_name (index: INTEGER): UNICODE_STRING
 49         -- The name of the i'th attribute
 50      require
 51         index.in_range(1, attributes_count)
 52      local
 53         i, d, n: INTEGER
 54      do
 55         if index <= no_namespace_attributes.count then
 56            Result := no_namespace_attributes.key(index)
 57         else
 58            from
 59               i := attributes.lower
 60               d := index - no_namespace_attributes.count
 61            until
 62               Result /= Void
 63            loop
 64               n := attributes.key(i).count
 65               if d <= n then
 66                  Result := attributes.item(i).key(d)
 67               else
 68                  d := d - n
 69                  i := i + 1
 70               end
 71            end
 72         end
 73      end
 74
 75   attribute_value (index: INTEGER): UNICODE_STRING
 76         -- The value of the i'th attribute
 77      require
 78         index.in_range(1, attributes_count)
 79      local
 80         i, d, n: INTEGER
 81      do
 82         if index <= no_namespace_attributes.count then
 83            Result := no_namespace_attributes.item(index)
 84         else
 85            from
 86               i := attributes.lower
 87               d := index - no_namespace_attributes.count
 88            until
 89               Result /= Void
 90            loop
 91               n := attributes.key(i).count
 92               if d <= n then
 93                  Result := attributes.item(i).item(d)
 94               else
 95                  d := d - n
 96                  i := i + 1
 97               end
 98            end
 99         end
100      end
101
102   attribute_at (a_attribute_namespace, a_attribute_name: UNICODE_STRING): UNICODE_STRING
103         -- The value of the attribute given by its name; Void if not set
104      local
105         att: HASHED_DICTIONARY[UNICODE_STRING, UNICODE_STRING]
106      do
107         if a_attribute_namespace = Void then
108            att := no_namespace_attributes
109         else
110            att := attributes.reference_at(a_attribute_namespace)
111         end
112         if att /= Void then
113            Result := att.reference_at(a_attribute_name)
114         end
115      end
116
117   attributes_count: INTEGER
118         -- The number of attributes
119      local
120         i: INTEGER
121      do
122         Result := no_namespace_attributes.count
123         from
124            i := attributes.lower
125         until
126            i > attributes.upper
127         loop
128            Result := Result + attributes.item(i).count
129            i := i + 1
130         end
131      end
132
133   child (index: INTEGER): XMLNS_NODE
134         -- The i'th child
135      require
136         index.in_range(1, children_count)
137      do
138         Result := children.item(index - 1)
139      end
140
141   children_count: INTEGER
142         -- The number of children
143      do
144         Result := children.count
145      end
146
147feature {ANY}
148   accept (visitor: VISITOR)
149      local
150         v: XMLNS_NODE_VISITOR
151      do
152         v ::= visitor
153         v.visit_composite_node(Current)
154      end
155
156feature {XMLNS_TREE}
157   set_attribute (a_attribute_namespace, a_attribute_name, a_attribute_value: UNICODE_STRING)
158      require
159         a_attribute_name /= Void
160         a_attribute_value /= Void
161      local
162         att: HASHED_DICTIONARY[UNICODE_STRING, UNICODE_STRING]
163      do
164         if a_attribute_namespace = Void then
165            att := no_namespace_attributes
166         else
167            att := attributes.reference_at(a_attribute_namespace)
168            if att = Void then
169               create att.make
170               attributes.add(att, a_attribute_namespace)
171            end
172         end
173         att.add(a_attribute_value, a_attribute_name)
174      ensure
175         attribute_at(a_attribute_namespace, a_attribute_name) = a_attribute_value
176      end
177
178   add_child (node: XMLNS_NODE)
179      require
180         node /= Void
181         node.parent = Void
182      do
183         children.add_last(node)
184         node.set_parent(Current)
185      ensure
186         node.parent = Current
187         child(children_count) = node
188      end
189
190feature {}
191   attributes: HASHED_DICTIONARY[HASHED_DICTIONARY[UNICODE_STRING, UNICODE_STRING], UNICODE_STRING]
192   no_namespace_attributes: HASHED_DICTIONARY[UNICODE_STRING, UNICODE_STRING]
193
194   children: FAST_ARRAY[XMLNS_NODE]
195
196   make (a_namespace: like namespace; a_name: like name; a_line: like line; a_column: like column)
197      require
198         a_name /= Void
199         a_line > 0
200         a_column > 0
201      do
202         namespace := a_namespace
203         name := a_name
204         line := a_line
205         column := a_column
206         create attributes.make
207         create no_namespace_attributes.make
208         create children.make(0)
209      end
210
211invariant
212   name /= Void
213
214end -- class XMLNS_COMPOSITE_NODE
215--
216-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
217--
218-- Permission is hereby granted, free of charge, to any person obtaining a copy
219-- of this software and associated documentation files (the "Software"), to deal
220-- in the Software without restriction, including without limitation the rights
221-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
222-- copies of the Software, and to permit persons to whom the Software is
223-- furnished to do so, subject to the following conditions:
224--
225-- The above copyright notice and this permission notice shall be included in
226-- all copies or substantial portions of the Software.
227--
228-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
229-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
230-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
231-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
232-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
233-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
234-- THE SOFTWARE.