duxml 0.8.6 → 0.8.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/duxml/doc.rb +6 -0
- data/lib/duxml/doc/element.rb +23 -1
- data/lib/duxml/saxer.rb +10 -6
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2112e7514be18ba981441e3bf2ce709214022103
|
|
4
|
+
data.tar.gz: c937e76e2c260d688f7607078f4c924224ef1c16
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 961905236877bba259f7331b993417b381f97ed0307bddb2206a16409c3ad630d1c1d814b50008d562062cc9471ac06ff920f63e3575df06e6baaa5819965f2d
|
|
7
|
+
data.tar.gz: d0c73ee66fc501bacd47cae5ddbee3117824154962cd41695d504b59c5b76181dcf26b389ce003089bfbee79fe87aed2c1458bb1b44b52d03c2c594c35becf8a
|
data/lib/duxml/doc.rb
CHANGED
data/lib/duxml/doc/element.rb
CHANGED
|
@@ -18,6 +18,15 @@ module Duxml
|
|
|
18
18
|
class Element < ::Ox::Element
|
|
19
19
|
include ElementGuts
|
|
20
20
|
|
|
21
|
+
# line number
|
|
22
|
+
@line
|
|
23
|
+
|
|
24
|
+
# column
|
|
25
|
+
@column
|
|
26
|
+
|
|
27
|
+
# document to which this Element belongs
|
|
28
|
+
@doc
|
|
29
|
+
|
|
21
30
|
# operates in two modes:
|
|
22
31
|
# - from Ruby
|
|
23
32
|
# - from file
|
|
@@ -27,6 +36,7 @@ module Duxml
|
|
|
27
36
|
# @param name [String] name of element, in both Ruby and file modes
|
|
28
37
|
# @param _line_or_content [Fixnum, Array, Hash] line number of element file mode; if Array, new child nodes; if Hash, attributes; can be nil
|
|
29
38
|
# @param _col_or_children [Fixnum, Array] column position in file mode; if Array, new child nodes; can be nil
|
|
39
|
+
# @return [Element] new XML Element
|
|
30
40
|
def initialize(name, _line_or_content=nil, _col_or_children=nil)
|
|
31
41
|
super name
|
|
32
42
|
@line = _line_or_content if _line_or_content.respond_to?(:%)
|
|
@@ -37,7 +47,7 @@ module Duxml
|
|
|
37
47
|
@nodes = NodeSet.new(self) if @nodes.empty?
|
|
38
48
|
end
|
|
39
49
|
|
|
40
|
-
attr_reader :line, :column
|
|
50
|
+
attr_reader :line, :column, :doc
|
|
41
51
|
|
|
42
52
|
attr_accessor :nodes
|
|
43
53
|
end
|
|
@@ -48,6 +58,17 @@ module Duxml
|
|
|
48
58
|
line < 0 || column < 0
|
|
49
59
|
end
|
|
50
60
|
|
|
61
|
+
# @param _doc [Doc] document to which this element belongs - recursively applies to all descendants of this node
|
|
62
|
+
# @return [Element] self
|
|
63
|
+
def set_doc!(_doc)
|
|
64
|
+
@doc = _doc
|
|
65
|
+
traverse do |node|
|
|
66
|
+
next if node === self or node.is_a?(String)
|
|
67
|
+
node.set_doc!(_doc)
|
|
68
|
+
end
|
|
69
|
+
self
|
|
70
|
+
end
|
|
71
|
+
|
|
51
72
|
# @see Ox::Element#<<
|
|
52
73
|
# this override reports changes to history; NewText for Strings, Add for elements
|
|
53
74
|
#
|
|
@@ -82,6 +103,7 @@ module Duxml
|
|
|
82
103
|
if obj.count_observers < 1 && @observer_peers
|
|
83
104
|
obj.add_observer(@observer_peers.first.first)
|
|
84
105
|
end
|
|
106
|
+
obj.set_doc! @doc
|
|
85
107
|
end
|
|
86
108
|
report(type, obj, index)
|
|
87
109
|
self
|
data/lib/duxml/saxer.rb
CHANGED
|
@@ -7,18 +7,22 @@ module Duxml
|
|
|
7
7
|
@io
|
|
8
8
|
|
|
9
9
|
attr_accessor :io
|
|
10
|
-
# @param
|
|
11
|
-
# @return [Doc] finished document with each Element's line and column info added
|
|
12
|
-
def sax(
|
|
13
|
-
|
|
10
|
+
# @param path_or_xml [String] path of file to parse or XML as String
|
|
11
|
+
# @return [Doc, Element] finished document with each Element's line and column info added
|
|
12
|
+
def sax(path_or_xml, obs=nil)
|
|
13
|
+
doc_or_no = File.exists?(path_or_xml)
|
|
14
|
+
io = doc_or_no ? File.open(path_or_xml): path_or_xml
|
|
14
15
|
saxer = DocuLiner.new(Duxml::Doc.new, obs)
|
|
15
16
|
Ox.sax_parse(saxer, io, {convert_special: true, symbolize: false})
|
|
16
17
|
doc = saxer.cursor
|
|
18
|
+
return doc.root unless doc_or_no
|
|
17
19
|
doc.add_observer obs if obs and doc.count_observers < 1
|
|
18
|
-
doc.path =
|
|
20
|
+
doc.path = path_or_xml
|
|
19
21
|
doc
|
|
20
22
|
end
|
|
21
23
|
|
|
24
|
+
alias_method :parse, :sax
|
|
25
|
+
|
|
22
26
|
class DocuLiner < ::Ox::Sax
|
|
23
27
|
# @param doc [Ox::Document] document that is being constructed as XML is parsed
|
|
24
28
|
# @param _observer [Object] object that will observe this document's content
|
|
@@ -39,7 +43,7 @@ module Duxml
|
|
|
39
43
|
|
|
40
44
|
def start_element(name)
|
|
41
45
|
cursor.nodes.insert(-1, Duxml::Element.new(name, line, column))
|
|
42
|
-
cursor_stack << cursor.nodes.last
|
|
46
|
+
cursor_stack << cursor.nodes.last.set_doc!(cursor_stack.first)
|
|
43
47
|
end
|
|
44
48
|
|
|
45
49
|
def attr(name, val)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: duxml
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Peter Kong
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-
|
|
11
|
+
date: 2016-09-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: ox
|