File: document.rb

package info (click to toggle)
ruby-xmlparser 0.7.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 980 kB
  • sloc: ruby: 9,342; ansic: 2,017; makefile: 11
file content (317 lines) | stat: -rw-r--r-- 6,418 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
## -*- Ruby -*-
## XML::DOM
## 1998-2001 by yoshidam
##

require 'xml/dom2/node'
require 'xml/dom2/nodelist'
require 'xml/dom2/domexception'

require 'xml/dom2/documentfragment'
require 'xml/dom2/attr'
require 'xml/dom2/element'
require 'xml/dom2/text'
require 'xml/dom2/comment'
require 'xml/dom2/cdatasection'
require 'xml/dom2/entityreference'
require 'xml/dom2/processinginstruction'
require 'xml/dom2/domimplementation'


module XML
  module DOM

=begin
== Class XML::DOM::Document

=== superclass
Node

=end
    class Document<Node
=begin
=== Class Methods
    --- Document.new(*children)

creates a new Document.
=end

      ## new([child1, child2, ...]) or
      ## new(child1, child2, ...)
      ##     child?: String or Node
      def initialize(*children)
        super(*children)
      end

=begin
=== Methods

    --- Document#nodeType

[DOM]
returns the nodeType.
=end
      ## [DOM]
      def nodeType
        DOCUMENT_NODE
      end

=begin
    --- Document#nodeName

[DOM]
returns the nodeName.
=end
      ## [DOM]
      def nodeName
        "#document"
      end

=begin
    --- Document#documentElement

[DOM]
returns root element of the Docuemnt.
=end
      ## [DOM]
      def documentElement
        @children.each do |child|
          if child.nodeType == ELEMENT_NODE
            return child
          end
        end if @children
        nil
      end

=begin
    --- Document#doctype

[DOM]
returns DTD associated with this document.
=end
      ## [DOM]
      def doctype
        @children.each do |child|
          if child.nodeType == DOCUMENT_TYPE_NODE
            return child
          end
        end if @children
        nil
      end

=begin
    --- Document#getElementsByTagName(tagname)

[DOM]
returns a NodeList of all the Elements with a given tag name.
=end
      ## [DOM] (but this is not "live")
      def getElementsByTagName(tagname)
        ret = NodeList.new
        @children.each do |node|
          if node.nodeType == ELEMENT_NODE
            if tagname == '*' || node.nodeName == tagname
              ret << node
            end
            ret << node.getElementsByTagName(tagname)
          end
        end if @children
        ret
      end

=begin
    --- Document#createElement(tagName)

[DOM]
creates a Element.
=end
      ## [DOM]
      def createElement(tagName)
        ret = Element.new(tagName)
        ret.ownerDocument = self
        ret
      end

=begin
    --- Document#createTextNode(data)

[DOM]
creates a TextNode.
=end
      ## [DOM]
      def createTextNode(data)
        ret = Text.new(data)
        ret.ownerDocument = self
        ret
      end

=begin
    --- Document#createCDATASection(data)

[DOM]
creates a CDATASection.
=end
      ## [DOM]
      def createCDATASection(data)
        ret = CDATASection.new(data)
        ret.ownerDocument = self
        ret
      end

=begin
    --- Document#createComment(data)

[DOM]
create a Comment.
=end
      ## [DOM]
      def createComment(data)
        ret = Comment.new(data)
        ret.ownerDocument = self
        ret
      end

=begin
    --- Document#createProcessingInstruction(target, data)

[DOM]
create a ProcessingInstruction.
=end
      ## [DOM]
      def createProcessingInstruction(target, data)
        ret = ProcessingInstruction.new(target, data)
        ret.ownerDocument = self
        ret
      end

=begin
    --- Document#createAttribute(name)

[DOM]
create a Attribute.
=end
      ## [DOM]
      def createAttribute(name)
        ret = Attr.new(name)
        ret.ownerDocument = self
        ret
      end

=begin
    --- Document#createEntityReference(name)

[DOM]
create a EntityReference.
=end
      ## [DOM]
      def createEntityReference(name)
        ret = EntityReference.new(name)
        ret.ownerDocument = self
        ret
      end

=begin
    --- Document#createDocumentFragment()

[DOM]
create a DocumentFragment.
=end
      ## [DOM]
      def createDocumentFragment
        ret = DocumentFragment.new
        ret.ownerDocument = self
        ret
      end

      ## [DOM]
      def implementation
        return @implemantation if @implemantation
        ## singleton
        @implemantation = DOMImplementation.instance
      end

      def implementation=(impl)
        @implemantation = impl
      end

      ## [DOM2]
      def importNode(impnode, deep)
        ## [NOT IMPLEMENTED]
        raise "not implemented"
      end

=begin
    --- Document#createElementNS(nsuri, qname)

[DOM2]
creates a Element with namespace.
=end
      ## [DOM2]
      def createElementNS(nsuri, qname)
        ret = Element.new([nsuri, qname])
        ret.ownerDocument = self
        ret
      end

      XMLNSNS = 'http://www.w3.org/2000/xmlns/'
      ## [DOM2]
      def createAttributeNS(nsuri, qname)
        nsuri = XMLNSNS if qname == 'xmlns' or qname =~ /^xmlns:/u
        ret = Attr.new([nsuri, qname])
        ret.ownerDocument = self
        ret
      end

      ## [DOM2]
      def getElementsByTagNameNS(nsuri, localname)
        ret = NodeList.new
        @children.each do |node|
          if node.nodeType == ELEMENT_NODE
            if (localname == '*' || node.localname == localname) and
                (nsuri == '*' || node.namespaceURI == nsuri)
              ret << node
            end
            ret << node.getElementsByTagNameNS(nsuri, localname)
          end
        end if @children
        ret
      end

      ## [DOM2]
      def getElementById(elementId)
        ## [NOT IMPLEMENTED]
        raise "not implemented"
      end

      def _checkNode(node)
        unless node.nodeType == ELEMENT_NODE ||
            node.nodeType == PROCESSING_INSTRUCTION_NODE ||
            node.nodeType == COMMENT_NODE ||
            node.nodeType == DOCUMENT_TYPE_NODE
          raise DOMException.new(DOMException::HIERARCHY_REQUEST_ERR)
        end

        if node.nodeType == ELEMENT_NODE
          @children.each do |n|
            if n.nodeType == ELEMENT_NODE
              raise DOMException.new(DOMException::HIERARCHY_REQUEST_ERR)
            end
          end
        end

        if node.nodeType == DOCUMENT_TYPE_NODE
          @children.each do |n|
            if n.nodeType == DOCUMENT_TYPE_NODE
              raise DOMException.new(DOMException::HIERARCHY_REQUEST_ERR)
            end
          end
        end
      end

      def _getNamespaces(parentNamespaces = {}, all = false)
        { nil => nil }
      end

    end
  end
end