nokogiri-notepad 0.0.0
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 +7 -0
- data/Gemfile +2 -0
- data/MIT-LICENSE +22 -0
- data/README.md +97 -0
- data/Rakefile +7 -0
- data/lib/custom_errors.rb +7 -0
- data/lib/node_finder.rb +62 -0
- data/lib/parser.rb +102 -0
- data/test/tc_helper.rb +9 -0
- data/test/tc_node_finder.rb +102 -0
- data/test/tc_parser.rb +99 -0
- data/test/test_files/invalid.xml +0 -0
- data/test/test_files/xslplanes.1.xml +9 -0
- data/test/test_files/xslplanes.1.xmx +9 -0
- data/test/test_files/xslplanes.2.xml +33 -0
- data/test/test_files/xslplanes.3.xml +61 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 69b59cae51bfba530ba00d49ef8938bb05a6340a
|
|
4
|
+
data.tar.gz: c1058b552c699c44d79d72555efb8e215b000124
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: '09034313e1f6116cf247cfefdcdb745091410f823a5e2ad9d1e519eaf576d8f4b522e70b23b1f5ded1f367a195f5e8a1c00c82ae84f198f6a924c1a53ca93ecf'
|
|
7
|
+
data.tar.gz: ce990d9228442f6f26076a36411c99d46382ad4ad25aa8bf5f9a4bba9cc6bbcd5e2aecfc2b1d59cd6542e93a07aa3a45bd42e798f6bfb4f28aef96e8c1abd90c
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2009, Nick Quaranto
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
|
4
|
+
obtaining a copy of this software and associated documentation
|
|
5
|
+
files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use,
|
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the
|
|
9
|
+
Software is furnished to do so, subject to the following
|
|
10
|
+
conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# What is nokogiri-notepad?
|
|
2
|
+
|
|
3
|
+
Nokogiri-notepad is a Ruby Gem for easily extracting information from an xml file according the paths provided by Notepad++
|
|
4
|
+
|
|
5
|
+
# Installation
|
|
6
|
+
|
|
7
|
+
To download the gem do
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
gem install 'nokogiri-notepad'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
or to include it in your gemfile do
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
# Gemfile
|
|
17
|
+
|
|
18
|
+
gem 'nokogiri-notepad'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
and then do `bundle install`
|
|
22
|
+
|
|
23
|
+
# Usage
|
|
24
|
+
|
|
25
|
+
First create a parser object. This parser object takes three mandatory arguments and one optional.
|
|
26
|
+
|
|
27
|
+
The mandatory arguments are:
|
|
28
|
+
|
|
29
|
+
- file_path -> This is the absolute path to the xml file you want to process
|
|
30
|
+
- single_targets -> This is a hash containing with:
|
|
31
|
+
- keys being the names of the methods you would like available on the parser instance
|
|
32
|
+
- values being the path given to you by Notepad++ (by highlighting the value of the node and clicking `Ctrl + Shift + Alt + P`). Note they are always given as an array.
|
|
33
|
+
- multiple targets -> Sometimes a path will give you multiple nodes back. This hash is similar in construction to the single_targets except it will give you a method that will return an array of strings
|
|
34
|
+
- The Parser initalize method will only accept files with an xml extension by default. You can add an array of extensions to be accepted instead if you wish
|
|
35
|
+
|
|
36
|
+
### Example `target_hash`
|
|
37
|
+
|
|
38
|
+
```xml
|
|
39
|
+
<plane>
|
|
40
|
+
<year> 1977 </year>
|
|
41
|
+
<make> Cessna </make>
|
|
42
|
+
<model> Skyhawk </model>
|
|
43
|
+
<color> Light blue and white </color>
|
|
44
|
+
</plane>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
If we want to target "1977" and "Cessna" saved on our `parser` instance our `single_target` hash would contain
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
single_targets = { "year" => ["/plane/year"], "make" => ["/plane/make"] }
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Our parser instance would then have the following attributes
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
parser.year // "1977"
|
|
57
|
+
parser.make // "Cessna"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Sometimes we can get more complex xml files like below, where it is more difficult to target specific nodes. Let's say we want to target the "Tripacer" node. We would need to specify a node in the group that is unique. This will give us the group of nodes, from which we can access our target node. If we just give the path `planes/plane/model` this leads us to both "Centurian" and "Tripacer", which is not what we want. Instead we can do
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
single_targets = { "model" => ["/planes/plane/foo", "four", "model"] }
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
```xml
|
|
67
|
+
<planes>
|
|
68
|
+
<plane>
|
|
69
|
+
<foo> three </foo>
|
|
70
|
+
<year> 1960 </year>
|
|
71
|
+
<make> Cessna </make>
|
|
72
|
+
<model> Centurian </model>
|
|
73
|
+
<color> Yellow and white </color>
|
|
74
|
+
</plane>
|
|
75
|
+
<plane>
|
|
76
|
+
<foo> four </foo>
|
|
77
|
+
<year> 1956 </year>
|
|
78
|
+
<make> Piper </make>
|
|
79
|
+
<model> Tripacer </model>
|
|
80
|
+
<color> Blue </color>
|
|
81
|
+
</plane>
|
|
82
|
+
</planes>
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
If we wanted to capture both "Centurian" and "Tripacer" above we would define the path in our multiple_targets hash
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
multiple_targets = { "model" => ["/planes/plane/model"] }
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
This would give us the following getter method available on our instance
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
parser.model // ["Centurian", "Tripacer"]
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
module NokogiriNotepad
|
|
2
|
+
class ExtensionArgumentMustBeArray < StandardError; end
|
|
3
|
+
class ExtensionElementsMustBeStrings < StandardError; end
|
|
4
|
+
class InvalidDataTypeForTargets < StandardError; end
|
|
5
|
+
class InvalidFileName < StandardError; end
|
|
6
|
+
class InvalidExtension < StandardError; end
|
|
7
|
+
end
|
data/lib/node_finder.rb
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
module NokogiriNotepad
|
|
2
|
+
class NodeFinder
|
|
3
|
+
def initialize(doc)
|
|
4
|
+
@doc = doc
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def find_node_at(path, code=nil, tag_name=nil)
|
|
8
|
+
begin
|
|
9
|
+
nodes = @doc.remove_namespaces!.xpath(render(path))
|
|
10
|
+
rescue NoMethodError
|
|
11
|
+
return nil
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
if !(code || tag_name)
|
|
15
|
+
node = nodes.first || nil
|
|
16
|
+
else
|
|
17
|
+
matching_nodes = nodes.select do |node|
|
|
18
|
+
return nil if node.nil?
|
|
19
|
+
node.children.to_s.try(:strip) == code
|
|
20
|
+
end
|
|
21
|
+
return nil if matching_nodes.first.nil?
|
|
22
|
+
return nil if matching_nodes.first.parent.at(".//#{tag_name}").nil?
|
|
23
|
+
|
|
24
|
+
node = matching_nodes.first.parent.at(".//#{tag_name}")
|
|
25
|
+
end
|
|
26
|
+
node
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def find_grouped_nodes_at(path, code=nil, tag_name=nil)
|
|
30
|
+
begin
|
|
31
|
+
nodes = @doc.remove_namespaces!.xpath(render(path))
|
|
32
|
+
rescue NoMethodError
|
|
33
|
+
return nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
return nil if nodes.empty?
|
|
37
|
+
|
|
38
|
+
if !(code || tag_name)
|
|
39
|
+
node = nodes.first || nil
|
|
40
|
+
else
|
|
41
|
+
matching_nodes = nodes.select do |node|
|
|
42
|
+
return nil if node.nil?
|
|
43
|
+
node.children.to_s.try(:strip) == code
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
return nil if matching_nodes.first.nil?
|
|
47
|
+
return nil if matching_nodes.first.parent.at(".//#{tag_name}").nil?
|
|
48
|
+
|
|
49
|
+
nodes = matching_nodes.map do |node|
|
|
50
|
+
node.parent.at(".//#{tag_name}")
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
nodes
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def render(path)
|
|
59
|
+
path.gsub(/\//, "//")
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
data/lib/parser.rb
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
require 'nokogiri'
|
|
2
|
+
require 'nikkou'
|
|
3
|
+
require_relative 'node_finder'
|
|
4
|
+
require_relative 'custom_errors'
|
|
5
|
+
|
|
6
|
+
module NokogiriNotepad
|
|
7
|
+
class Parser
|
|
8
|
+
attr_accessor :accepted_extensions
|
|
9
|
+
attr_reader :file_path, :single_targets, :multiple_targets
|
|
10
|
+
|
|
11
|
+
def initialize(file_path, single_targets, multiple_targets, extensions = nil)
|
|
12
|
+
@file_path = file_path
|
|
13
|
+
@single_targets = single_targets
|
|
14
|
+
@multiple_targets = multiple_targets
|
|
15
|
+
@accepted_extensions = [".xml"]
|
|
16
|
+
add_extensions(extensions) if extensions
|
|
17
|
+
validate_input
|
|
18
|
+
doc = load_nokogiri_doc file_path
|
|
19
|
+
@node_finder = NodeFinder.new doc
|
|
20
|
+
initialize_data
|
|
21
|
+
doc = nil
|
|
22
|
+
@node_finder = nil
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def extensions
|
|
26
|
+
@accepted_extensions
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def add_extensions(extensions)
|
|
32
|
+
raise ExtensionArgumentMustBeArray unless extensions.kind_of?(Array)
|
|
33
|
+
extensions.each do |ext|
|
|
34
|
+
raise ExtensionElementsMustBeStrings unless ext.kind_of? String
|
|
35
|
+
@accepted_extensions << ext
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def validate_input
|
|
40
|
+
validate_file_path
|
|
41
|
+
validate_targets
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def validate_file_path
|
|
45
|
+
raise InvalidFileName, "#{file_path} is too long" unless file_path.length < 255
|
|
46
|
+
raise InvalidFileName unless File.file?(file_path)
|
|
47
|
+
raise InvalidExtension unless extension_valid?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def validate_targets
|
|
51
|
+
raise InvalidDataTypeForTargets unless single_targets.kind_of?(Hash)
|
|
52
|
+
raise InvalidDataTypeForTargets unless multiple_targets.kind_of?(Hash)
|
|
53
|
+
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def load_nokogiri_doc(file_path)
|
|
57
|
+
Nokogiri::XML(File.open(file_path))
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def initialize_data
|
|
61
|
+
initialize_single_target_data
|
|
62
|
+
initialize_multiple_target_data
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def initialize_single_target_data
|
|
66
|
+
@single_targets.each do |key, path|
|
|
67
|
+
self.instance_variable_set("@#{key}", data_at_attr(key))
|
|
68
|
+
create_method(key) { self.instance_variable_get("@#{key}") }
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def data_at_attr(target)
|
|
73
|
+
path = @single_targets[target]
|
|
74
|
+
node = @node_finder.find_node_at(*path)
|
|
75
|
+
node.nil? ? nil : node.content.strip
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def initialize_multiple_target_data
|
|
79
|
+
@multiple_targets.each do |key, path|
|
|
80
|
+
self.instance_variable_set("@#{key}", multiple_data_at_attr(key))
|
|
81
|
+
create_method(key) { self.instance_variable_get("@#{key}") }
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def multiple_data_at_attr(target)
|
|
86
|
+
path = @multiple_targets[target]
|
|
87
|
+
nodes = @node_finder.find_grouped_nodes_at(*path)
|
|
88
|
+
return nil if nodes.nil? || nodes.empty?
|
|
89
|
+
nodes.map do |node|
|
|
90
|
+
node.nil? ? nil : node.content.strip
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def create_method(name, &block)
|
|
95
|
+
self.class.send(:define_method, name, &block)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def extension_valid?
|
|
99
|
+
accepted_extensions.include? File.extname(file_path)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
data/test/tc_helper.rb
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
require_relative "tc_helper"
|
|
2
|
+
|
|
3
|
+
class BasicTest < Minitest::Test
|
|
4
|
+
def setup
|
|
5
|
+
@file_path = "#{File.dirname(__FILE__)}/test_files/xslplanes.1.xml"
|
|
6
|
+
@doc = Nokogiri::XML(File.open(@file_path))
|
|
7
|
+
@file_path2 = "#{File.dirname(__FILE__)}/test_files/xslplanes.2.xml"
|
|
8
|
+
@doc2 = Nokogiri::XML(File.open(@file_path2))
|
|
9
|
+
@file_path3 = "#{File.dirname(__FILE__)}/test_files/xslplanes.3.xml"
|
|
10
|
+
@doc3 = Nokogiri::XML(File.open(@file_path3))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_initialize_node_finder
|
|
14
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc
|
|
15
|
+
assert_equal(NokogiriNotepad::NodeFinder, node_finder.class)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_initialize_node_finder_with_invalid_doc
|
|
19
|
+
node_finder = NokogiriNotepad::NodeFinder.new nil
|
|
20
|
+
assert_equal(NokogiriNotepad::NodeFinder, node_finder.class)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_find_node_at_when_doc_is_nil
|
|
24
|
+
node_finder = NokogiriNotepad::NodeFinder.new nil
|
|
25
|
+
node = node_finder.find_node_at 'path'
|
|
26
|
+
assert_nil(node)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_find_grouped_nodes_at_when_doc_is_nil
|
|
30
|
+
node_finder = NokogiriNotepad::NodeFinder.new nil
|
|
31
|
+
node = node_finder.find_grouped_nodes_at 'path'
|
|
32
|
+
assert_nil(node)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_find_node_when_invalid_path_given
|
|
36
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc
|
|
37
|
+
node = node_finder.find_node_at 'path'
|
|
38
|
+
assert_nil(node)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_find_grouped_node_at_when_invalid_path_given
|
|
42
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc
|
|
43
|
+
node = node_finder.find_grouped_nodes_at 'path'
|
|
44
|
+
assert_nil(node)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_find_node_at_returns_expected_string
|
|
48
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc
|
|
49
|
+
node = node_finder.find_node_at "/plane/make"
|
|
50
|
+
assert_equal("Cessna", node.content.strip)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_find_node_at_returns_expected_string_when_code_and_tagname_given
|
|
54
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc2
|
|
55
|
+
node = node_finder.find_node_at "/planes/plane/foo", "four", "color"
|
|
56
|
+
assert_equal("Blue", node.content.strip)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def test_find_node_at_when_invalid_code_and_tag_name_given
|
|
60
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc2
|
|
61
|
+
node = node_finder.find_node_at '/planes/plane/foo', 'path', 'path'
|
|
62
|
+
assert_nil(node)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def test_find_node_at_when_all_args_are_invalid
|
|
66
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc2
|
|
67
|
+
node = node_finder.find_node_at 'path', 'path', 'path'
|
|
68
|
+
assert_nil(node)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def test_find_grouped_nodes_at_when_invalid_code_and_tag_name_given
|
|
72
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc2
|
|
73
|
+
node = node_finder.find_grouped_nodes_at '/planes/plane/foo', 'path', 'path'
|
|
74
|
+
assert_nil(node)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def test_find_grouped_nodes_at_when_all_args_are_invalid
|
|
78
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc2
|
|
79
|
+
node = node_finder.find_grouped_nodes_at 'path', 'path', 'path'
|
|
80
|
+
assert_nil(node)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def test_find_grouped_nodes_at_when_all_args_are_invalid
|
|
84
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc2
|
|
85
|
+
node = node_finder.find_grouped_nodes_at 'path', 'path', 'path'
|
|
86
|
+
assert_nil(node)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def test_find_grouped_nodes_at_when_valid_path
|
|
90
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc3
|
|
91
|
+
nodes = node_finder.find_grouped_nodes_at '/planes/plane/year'
|
|
92
|
+
nodes = nodes.map { |n| n.nil? ? nil : n.content.strip}
|
|
93
|
+
assert_equal(["1977", "1975", "1960", "1956", "1977_2", "1975_2", "1960_2", "1956_2"], nodes)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def test_find_grouped_nodes_at_when_valid_args
|
|
97
|
+
node_finder = NokogiriNotepad::NodeFinder.new @doc3
|
|
98
|
+
nodes = node_finder.find_grouped_nodes_at '/planes/plane/foo', 'four', 'color'
|
|
99
|
+
nodes = nodes.map { |n| n.nil? ? nil : n.content.strip}
|
|
100
|
+
assert_equal(["Blue", "Blue_2"], nodes)
|
|
101
|
+
end
|
|
102
|
+
end
|
data/test/tc_parser.rb
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
require_relative "tc_helper"
|
|
2
|
+
|
|
3
|
+
class BasicTest < Minitest::Test
|
|
4
|
+
def setup
|
|
5
|
+
@file_path = "#{File.dirname(__FILE__)}/test_files/xslplanes.1.xml"
|
|
6
|
+
@file_path2 = "#{File.dirname(__FILE__)}/test_files/xslplanes.2.xml"
|
|
7
|
+
@file_path3 = "#{File.dirname(__FILE__)}/test_files/xslplanes.3.xml"
|
|
8
|
+
@file_path4 = "#{File.dirname(__FILE__)}/test_files/xslplanes.1.xmx"
|
|
9
|
+
|
|
10
|
+
@single_targets = { "year" => ["/plane/year"], "make" => ["/plane/make"] }
|
|
11
|
+
@single_targets2 = { "color" => ["/planes/plane/foo", "four", "color"], "model" => ["/planes/plane/foo", "two", "model"] }
|
|
12
|
+
@multiple_targets = { "multiple_year" => ["/plane/year"], "multiple_make" => ["/plane/make"] }
|
|
13
|
+
@multiple_targets2 = { "multiple_color" => ["/planes/plane/foo", "four", "color"], "multiple_model" => ["/planes/plane/foo", "three", "model"] }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_initializing_with_invalid_xml_file
|
|
17
|
+
invalid_path = "#{File.dirname(__FILE__)}/test_files/invalid.xml"
|
|
18
|
+
parser = NokogiriNotepad::Parser.new(invalid_path, @single_targets, @multiple_targets)
|
|
19
|
+
assert_nil(parser.year)
|
|
20
|
+
assert_nil(parser.make)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_inititalizing_with_valid_extension_adds_extensions_to_accepted_extensions
|
|
24
|
+
extensions = [".foo", ".bar"]
|
|
25
|
+
parser = NokogiriNotepad::Parser.new(@file_path, @single_targets, @multiple_targets, extensions)
|
|
26
|
+
assert_equal(parser.extensions, [".xml", ".foo", ".bar"])
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_inititalizing_with_invalid_extension_raises_error
|
|
30
|
+
extensions = ".foo"
|
|
31
|
+
assert_raises NokogiriNotepad::ExtensionArgumentMustBeArray do
|
|
32
|
+
NokogiriNotepad::Parser.new(@file_path, @single_targets, @multiple_targets, extensions)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_initializing_with_invalid_extension_objects_raises_error
|
|
37
|
+
extensions = [[".foo"]]
|
|
38
|
+
assert_raises NokogiriNotepad::ExtensionElementsMustBeStrings do
|
|
39
|
+
NokogiriNotepad::Parser.new(@file_path, @single_targets, @multiple_targets, extensions)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_initializing_with_invalid_single_targets_raises_error
|
|
44
|
+
assert_raises NokogiriNotepad::InvalidDataTypeForTargets do
|
|
45
|
+
NokogiriNotepad::Parser.new(@file_path, "foo", @multiple_targets)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_initializing_with_invalid_multiple_targets_raises_error
|
|
50
|
+
assert_raises NokogiriNotepad::InvalidDataTypeForTargets do
|
|
51
|
+
NokogiriNotepad::Parser.new(@file_path, @single_targets, "foo")
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_parser_has_correct_attributes
|
|
56
|
+
parser = NokogiriNotepad::Parser.new(@file_path, @single_targets, @multiple_targets)
|
|
57
|
+
assert_equal(parser.year, "1977")
|
|
58
|
+
assert_equal(parser.make, "Cessna")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def test_parser_has_correct_attributes_for_single_targets
|
|
62
|
+
parser = NokogiriNotepad::Parser.new(@file_path, @single_targets, @multiple_targets)
|
|
63
|
+
assert_equal(parser.year, "1977")
|
|
64
|
+
assert_equal(parser.make, "Cessna")
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def test_parser_has_correct_attributes_for_single_targets_with_targeted_node
|
|
68
|
+
parser = NokogiriNotepad::Parser.new(@file_path2, @single_targets2, @multiple_targets)
|
|
69
|
+
assert_equal(parser.color, "Blue")
|
|
70
|
+
assert_equal(parser.model, "Apache")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def test_parser_has_correct_attributes_for_multiple_targets
|
|
74
|
+
parser = NokogiriNotepad::Parser.new(@file_path2, @single_targets, @multiple_targets)
|
|
75
|
+
assert_equal(parser.multiple_year, ["1977", "1975", "1960", "1956"])
|
|
76
|
+
assert_equal(parser.multiple_make, ["Cessna", "Piper", "Cessna", "Piper"])
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def test_parser_has_correct_attributes_for_multiple_targets_with_targeted_node
|
|
80
|
+
parser = NokogiriNotepad::Parser.new(@file_path3, @single_targets, @multiple_targets2)
|
|
81
|
+
assert_equal(parser.multiple_color, ["Blue", "Blue_2"])
|
|
82
|
+
assert_equal(parser.multiple_model, ["Centurian", "Centurian_2"])
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_parser_throws_error_if_initialized_with_invalid_extension_file
|
|
86
|
+
assert_raises NokogiriNotepad::InvalidExtension do
|
|
87
|
+
parser = NokogiriNotepad::Parser.new(@file_path4, @single_targets, @multiple_targets2)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def test_parser_has_correct_attributes_when_extension_defined
|
|
92
|
+
def test_parser_has_correct_attributes_for_single_targets
|
|
93
|
+
parser = NokogiriNotepad::Parser.new(@file_path4, @single_targets, @multiple_targets, [".xmx"])
|
|
94
|
+
assert_equal(parser.year, "1977")
|
|
95
|
+
assert_equal(parser.make, "Cessna")
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
end
|
|
File without changes
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<?xml version = "1.0" encoding = "utf-8"?>
|
|
2
|
+
<!-- xslplane.1.xml -->
|
|
3
|
+
<?xml-stylesheet type = "text/xsl" href = "xslplane.1.xsl" ?>
|
|
4
|
+
<plane>
|
|
5
|
+
<year> 1977 </year>
|
|
6
|
+
<make> Cessna </make>
|
|
7
|
+
<model> Skyhawk </model>
|
|
8
|
+
<color> Light blue and white </color>
|
|
9
|
+
</plane>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<?xml version = "1.0" encoding = "utf-8"?>
|
|
2
|
+
<!-- xslplane.1.xml -->
|
|
3
|
+
<?xml-stylesheet type = "text/xsl" href = "xslplane.1.xsl" ?>
|
|
4
|
+
<plane>
|
|
5
|
+
<year> 1977 </year>
|
|
6
|
+
<make> Cessna </make>
|
|
7
|
+
<model> Skyhawk </model>
|
|
8
|
+
<color> Light blue and white </color>
|
|
9
|
+
</plane>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<?xml version = "1.0" encoding = "utf-8"?>
|
|
2
|
+
<!-- xslplanes.2.xml -->
|
|
3
|
+
<?xml-stylesheet type = "text/xsl" href = "xslplanes.2.xsl" ?>
|
|
4
|
+
<planes>
|
|
5
|
+
<plane>
|
|
6
|
+
<foo> one </foo>
|
|
7
|
+
<year> 1977 </year>
|
|
8
|
+
<make> Cessna </make>
|
|
9
|
+
<model> Skyhawk </model>
|
|
10
|
+
<color> Light blue and white </color>
|
|
11
|
+
</plane>
|
|
12
|
+
<plane>
|
|
13
|
+
<foo> two </foo>
|
|
14
|
+
<year> 1975 </year>
|
|
15
|
+
<make> Piper </make>
|
|
16
|
+
<model> Apache </model>
|
|
17
|
+
<color> White </color>
|
|
18
|
+
</plane>
|
|
19
|
+
<plane>
|
|
20
|
+
<foo> three </foo>
|
|
21
|
+
<year> 1960 </year>
|
|
22
|
+
<make> Cessna </make>
|
|
23
|
+
<model> Centurian </model>
|
|
24
|
+
<color> Yellow and white </color>
|
|
25
|
+
</plane>
|
|
26
|
+
<plane>
|
|
27
|
+
<foo> four </foo>
|
|
28
|
+
<year> 1956 </year>
|
|
29
|
+
<make> Piper </make>
|
|
30
|
+
<model> Tripacer </model>
|
|
31
|
+
<color> Blue </color>
|
|
32
|
+
</plane>
|
|
33
|
+
</planes>
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
<?xml version = "1.0" encoding = "utf-8"?>
|
|
2
|
+
<!-- xslplanes.2.xml -->
|
|
3
|
+
<?xml-stylesheet type = "text/xsl" href = "xslplanes.2.xsl" ?>
|
|
4
|
+
<planes>
|
|
5
|
+
<plane>
|
|
6
|
+
<foo> one </foo>
|
|
7
|
+
<year> 1977 </year>
|
|
8
|
+
<make> Cessna </make>
|
|
9
|
+
<model> Skyhawk </model>
|
|
10
|
+
<color> Light blue and white </color>
|
|
11
|
+
</plane>
|
|
12
|
+
<plane>
|
|
13
|
+
<foo> two </foo>
|
|
14
|
+
<year> 1975 </year>
|
|
15
|
+
<make> Piper </make>
|
|
16
|
+
<model> Apache </model>
|
|
17
|
+
<color> White </color>
|
|
18
|
+
</plane>
|
|
19
|
+
<plane>
|
|
20
|
+
<foo> three </foo>
|
|
21
|
+
<year> 1960 </year>
|
|
22
|
+
<make> Cessna </make>
|
|
23
|
+
<model> Centurian </model>
|
|
24
|
+
<color> Yellow and white </color>
|
|
25
|
+
</plane>
|
|
26
|
+
<plane>
|
|
27
|
+
<foo> four </foo>
|
|
28
|
+
<year> 1956 </year>
|
|
29
|
+
<make> Piper </make>
|
|
30
|
+
<model> Tripacer </model>
|
|
31
|
+
<color> Blue </color>
|
|
32
|
+
</plane>
|
|
33
|
+
<plane>
|
|
34
|
+
<foo> one </foo>
|
|
35
|
+
<year> 1977_2 </year>
|
|
36
|
+
<make> Cessna_2 </make>
|
|
37
|
+
<model> Skyhawk_2 </model>
|
|
38
|
+
<color> Light blue and white_2 </color>
|
|
39
|
+
</plane>
|
|
40
|
+
<plane>
|
|
41
|
+
<foo> two </foo>
|
|
42
|
+
<year> 1975_2 </year>
|
|
43
|
+
<make> Piper_2 </make>
|
|
44
|
+
<model> Apache_2 </model>
|
|
45
|
+
<color> White_2 </color>
|
|
46
|
+
</plane>
|
|
47
|
+
<plane>
|
|
48
|
+
<foo> three </foo>
|
|
49
|
+
<year> 1960_2 </year>
|
|
50
|
+
<make> Cessna_2 </make>
|
|
51
|
+
<model> Centurian_2 </model>
|
|
52
|
+
<color> Yellow and white_2 </color>
|
|
53
|
+
</plane>
|
|
54
|
+
<plane>
|
|
55
|
+
<foo> four </foo>
|
|
56
|
+
<year> 1956_2 </year>
|
|
57
|
+
<make> Piper_2 </make>
|
|
58
|
+
<model> Tripacer_2 </model>
|
|
59
|
+
<color> Blue_2 </color>
|
|
60
|
+
</plane>
|
|
61
|
+
</planes>
|
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: nokogiri-notepad
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Adrian Carroll
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-02-11 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Gem for extracting info from xml files according to paths defined in
|
|
14
|
+
Notepad++
|
|
15
|
+
email: adrian.carroll7@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- Gemfile
|
|
21
|
+
- MIT-LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- Rakefile
|
|
24
|
+
- lib/custom_errors.rb
|
|
25
|
+
- lib/node_finder.rb
|
|
26
|
+
- lib/parser.rb
|
|
27
|
+
- test/tc_helper.rb
|
|
28
|
+
- test/tc_node_finder.rb
|
|
29
|
+
- test/tc_parser.rb
|
|
30
|
+
- test/test_files/invalid.xml
|
|
31
|
+
- test/test_files/xslplanes.1.xml
|
|
32
|
+
- test/test_files/xslplanes.1.xmx
|
|
33
|
+
- test/test_files/xslplanes.2.xml
|
|
34
|
+
- test/test_files/xslplanes.3.xml
|
|
35
|
+
homepage: http://github.com/carrola7/nokogiri-notepad
|
|
36
|
+
licenses: []
|
|
37
|
+
metadata: {}
|
|
38
|
+
post_install_message:
|
|
39
|
+
rdoc_options: []
|
|
40
|
+
require_paths:
|
|
41
|
+
- lib
|
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '0'
|
|
52
|
+
requirements: []
|
|
53
|
+
rubyforge_project:
|
|
54
|
+
rubygems_version: 2.5.2
|
|
55
|
+
signing_key:
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: XML Node extractor
|
|
58
|
+
test_files: []
|