llt-xml_handler 0.0.3

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b6625e462a881ed0ba364c7390ca5e8fd1acde74
4
+ data.tar.gz: a50e3fd4598e9d733a5e0fd15b4211c5b0551b03
5
+ SHA512:
6
+ metadata.gz: a6ba8f8ccb038fed2c55d29339f0c34a8896d438f2b6d11029b78b4ea052ce6ccd31b189ccce3e1ac37a3f93c7d2757a3e05aee735e932a94c70a094f1985911
7
+ data.tar.gz: e862b0c7fdf853cd07ff5e7b28cb9a31560369b377748758cc73304f5ab39339c4b57d19aa4ab935c7a064d131b841ad6af9fb84e5c81361307309792343efb4
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,8 @@
1
+ ---
2
+ language: ruby
3
+ before_script:
4
+ - export JRUBY_OPTS=--2.0
5
+ rvm:
6
+ - 2.1.0
7
+ - 2.0.0
8
+ - jruby-1.7.8
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in llt-tei_handler.gemspec
4
+ gemspec
5
+ gem 'coveralls', require: false
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 LFDM
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ # LLT::XmlHandler
2
+
3
+ [![Version](http://allthebadges.io/latin-language-toolkit/llt-xml_handler/badge_fury.png)](http://allthebadges.io/latin-language-toolkit/llt-xml_handler/badge_fury)
4
+ [![Dependencies](http://allthebadges.io/latin-language-toolkit/llt-xml_handler/gemnasium.png)](http://allthebadges.io/latin-language-toolkit/llt-xml_handler/gemnasium)
5
+ [![Build Status](http://allthebadges.io/latin-language-toolkit/llt-xml_handler/travis.png)](http://allthebadges.io/latin-language-toolkit/llt-xml_handler/travis)
6
+ [![Coverage](http://allthebadges.io/latin-language-toolkit/llt-xml_handler/coveralls.png)](http://allthebadges.io/latin-language-toolkit/llt-xml_handler/coveralls)
7
+ [![Code Climate](http://allthebadges.io/latin-language-toolkit/llt-xml_handler/code_climate.png)](http://allthebadges.io/latin-language-toolkit/llt-xml_handler/code_climate)
8
+
9
+ Leightweigth transformations of XML Documents
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'llt-xml_handler'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install llt-xml_handler
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( https://github.com/[my-github-username]/llt-xml_handler/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,7 @@
1
+ require "llt/xml_handler/version"
2
+
3
+ module LLT
4
+ module XmlHandler
5
+ require 'llt/xml_handler/pre_processor'
6
+ end
7
+ end
@@ -0,0 +1,60 @@
1
+ module LLT
2
+ module XmlHandler
3
+ class PreProcessor
4
+ require 'nokogiri'
5
+
6
+ attr_reader :document
7
+
8
+ def initialize(document, root: nil, ns: nil)
9
+ @document = Nokogiri::XML(document)
10
+ @namespace = ns
11
+ go_to_root(root) if root
12
+
13
+ @document.encoding = "UTF-8"
14
+ end
15
+
16
+ def to_xml
17
+ without_duplicate_declaration(@document.to_xml).strip
18
+ end
19
+
20
+ def remove_nodes(*nodes)
21
+ nodes.each do |node|
22
+ @document.xpath(*to_xpath(node, @namespace)).each(&:remove)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ DUPLICATE_XML_VERSION = /<\?xml version=.*\?>\s*{2}/m
29
+ def without_duplicate_declaration(doc)
30
+ if doc.match(DUPLICATE_XML_VERSION)
31
+ doc.sub(/^<\?xml version=.*\?>/, '')
32
+ else
33
+ doc
34
+ end
35
+ end
36
+
37
+ def to_xpath(elem, ns)
38
+ ns ? ["//ns:#{elem}", ns: ns] : ["//#{elem}"]
39
+ end
40
+
41
+ def go_to_root(root)
42
+ new_root = @document.xpath(*to_xpath(root, @namespace)).first
43
+ return unless new_root # or throw an error?
44
+
45
+ if RUBY_ENGINE == "jruby"
46
+ # Pretty unnecessarily reparses the document fragment, but
47
+ # nokogiri-java seems to have problems with handling namespaces
48
+ # when assigning the root node of a document by hand.
49
+ #
50
+ # The issue has been reported on the nokogiri-talk Google group
51
+ # and is described in detail there (currently awaiting approval)
52
+ @document = Nokogiri::XML(new_root.to_s)
53
+ else
54
+ @document = Nokogiri::XML::Document.new
55
+ @document.root = new_root
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ module LLT
2
+ module XmlHandler
3
+ VERSION = "0.0.3"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'llt/xml_handler/version'
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "llt-xml_handler"
7
+ spec.version = LLT::XmlHandler::VERSION
8
+ spec.authors = ["LFDM"]
9
+ spec.email = ["1986gh@gmail.com"]
10
+ spec.summary = %q{Leightweigth transformations of XML Documents}
11
+ spec.description = spec.summary
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "simplecov", "~> 0.7"
24
+ spec.add_dependency "nokogiri"
25
+ end
@@ -0,0 +1,152 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::XmlHandler::PreProcessor do
4
+ let(:pre_processor) { LLT::XmlHandler::PreProcessor }
5
+
6
+ def load_fixture(filename)
7
+ File.read(File.expand_path("../../../fixtures/#{filename}", __FILE__))
8
+ end
9
+
10
+ let(:embedded_tei_doc) do
11
+ <<-EOF
12
+ <?xml version="1.0" encoding="utf-8"?>
13
+ <reply>
14
+ <TEI xmlns="http://www.tei-c.org/ns/1.0">
15
+ <teiHeader>
16
+ <xxx/>
17
+ </teiHeader>
18
+ <body>
19
+ <head>TITLE</head>
20
+ <p>Text <ref type='note'>Note</ref> resumed</p>
21
+ </body>
22
+ </TEI>
23
+ </reply
24
+ EOF
25
+ end
26
+
27
+ let(:tei_doc) do
28
+ <<-EOF
29
+ <?xml version="1.0" encoding="utf-8"?>
30
+ <TEI xmlns="http://www.tei-c.org/ns/1.0">
31
+ <teiHeader>
32
+ <xxx/>
33
+ </teiHeader>
34
+ <body>
35
+ <head>TITLE</head>
36
+ <p>Text <ref type='note'>Note</ref> resumed</p>
37
+ </body>
38
+ </TEI>
39
+ EOF
40
+ end
41
+
42
+ let(:prefixed_tei_doc) do
43
+ <<-EOF
44
+ <?xml version="1.0" encoding="utf-8"?>
45
+ <reply xmlns:tei="http://www.tei-c.org/ns/1.0">
46
+ <tei:TEI>
47
+ <tei:teiHeader>
48
+ <xxx/>
49
+ </tei:teiHeader>
50
+ <tei:body>
51
+ <tei:head>TITLE</head>
52
+ <tei:p>Text <tei:ref type='note'>Note</tei:ref> resumed</tei:p>
53
+ </tei:body>
54
+ </tei:TEI>
55
+ </tei:reply
56
+ EOF
57
+ end
58
+
59
+ let(:doc_wo_ns) do
60
+ <<-EOF
61
+ <?xml version="1.0" encoding="utf-8"?>
62
+ <reply>
63
+ <custom_root>
64
+ <teiHeader></teiHeader>
65
+ <body></body>
66
+ </custom_root>
67
+ </reply>
68
+ EOF
69
+ end
70
+
71
+ describe "#new" do
72
+ it "takes a xml document on initialization" do
73
+ doc = <<-EOF
74
+ <?xml version="1.0" encoding="utf-8"?>
75
+ <TEI xmlns="http://www.tei-c.org/ns/1.0">
76
+ </TEI>
77
+ EOF
78
+ expect { pre_processor.new(doc) }.not_to raise_error
79
+ end
80
+
81
+ it "tries to find a root element, given as optional keyword param" do
82
+ doc = pre_processor.new(doc_wo_ns, root: 'custom_root')
83
+ doc.document.root.name.should == 'custom_root'
84
+ end
85
+
86
+ it "root element can have a namespace, given as optional keyword param" do
87
+ doc = pre_processor.new(embedded_tei_doc, root: 'TEI', ns: "http://www.tei-c.org/ns/1.0")
88
+ doc.document.root.name.should == "TEI"
89
+ end
90
+
91
+ it "really honors the namespace - it has to be present when given" do
92
+ doc = pre_processor.new(embedded_tei_doc, root: 'TEI')
93
+ doc.document.root.name.should_not == "TEI"
94
+ end
95
+
96
+ it "also works with prefixes" do
97
+ doc = pre_processor.new(prefixed_tei_doc, root: 'TEI', ns: "http://www.tei-c.org/ns/1.0")
98
+ doc.document.root.name.should == "TEI"
99
+ end
100
+ end
101
+
102
+ describe "#remove_nodes" do
103
+ def with_stripped_lines(string)
104
+ string.lines.map(&:strip).join
105
+ end
106
+
107
+ it "takes out a given node including all of its content" do
108
+ doc = pre_processor.new(doc_wo_ns)
109
+
110
+ doc.document.xpath('//teiHeader').should_not be_empty
111
+
112
+ doc.remove_nodes('teiHeader')
113
+ doc.document.xpath('//teiHeader').should be_empty
114
+ end
115
+
116
+ it "can remove several nodes at a single step" do
117
+ doc = pre_processor.new(doc_wo_ns)
118
+
119
+ doc.document.xpath('//teiHeader').should_not be_empty
120
+ doc.document.xpath('//body').should_not be_empty
121
+
122
+ doc.remove_nodes('teiHeader', 'body')
123
+ doc.document.xpath('//teiHeader').should be_empty
124
+ doc.document.xpath('//body').should be_empty
125
+ end
126
+
127
+ it "honors a namespace given on initialization" do
128
+ ns = "http://www.tei-c.org/ns/1.0"
129
+ doc1 = pre_processor.new(tei_doc, ns: ns)
130
+ doc2 = pre_processor.new(doc_wo_ns, ns: ns)
131
+
132
+ doc1.document.xpath('//ns:teiHeader', ns: ns).should_not be_empty
133
+ doc2.document.xpath('//teiHeader').should_not be_empty
134
+
135
+ doc1.remove_nodes('teiHeader')
136
+ doc2.remove_nodes('teiHeader')
137
+
138
+ doc1.document.xpath('//ns:teiHeader', ns: ns).should be_empty
139
+ doc2.document.xpath('//teiHeader').should_not be_empty
140
+ end
141
+
142
+ it "also works with prefixes" do
143
+ ns = "http://www.tei-c.org/ns/1.0"
144
+ doc = pre_processor.new(prefixed_tei_doc, ns: ns)
145
+
146
+ doc.document.xpath('//ns:teiHeader', ns: ns).should_not be_empty
147
+
148
+ doc.remove_nodes('teiHeader')
149
+ doc.document.xpath('//ns:teiHeader', ns: ns).should be_empty
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::XmlHandler do
4
+ it 'has a version number' do
5
+ expect(LLT::XmlHandler::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ Coveralls.wear!
5
+
6
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
7
+ SimpleCov::Formatter::HTMLFormatter,
8
+ Coveralls::SimpleCov::Formatter
9
+ ]
10
+
11
+ SimpleCov.start do
12
+ add_filter '/spec/'
13
+ end
14
+
15
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
16
+ require 'llt/xml_handler'
17
+
18
+ RSpec.configure do |config|
19
+ config.treat_symbols_as_metadata_keys_with_true_values = true
20
+ config.run_all_when_everything_filtered = true
21
+ config.filter_run :focus
22
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: llt-xml_handler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - LFDM
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Leightweigth transformations of XML Documents
84
+ email:
85
+ - 1986gh@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - lib/llt/xml_handler.rb
98
+ - lib/llt/xml_handler/pre_processor.rb
99
+ - lib/llt/xml_handler/version.rb
100
+ - llt-xml_handler.gemspec
101
+ - spec/lib/llt/xml_handler/pre_processor_spec.rb
102
+ - spec/lib/llt/xml_handler_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: ''
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Leightweigth transformations of XML Documents
128
+ test_files:
129
+ - spec/lib/llt/xml_handler/pre_processor_spec.rb
130
+ - spec/lib/llt/xml_handler_spec.rb
131
+ - spec/spec_helper.rb