xml_convert 0.0.1
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.
- data/.gitignore +17 -0
- data/.yardopts +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +19 -0
- data/lib/xml_convert/version.rb +3 -0
- data/lib/xml_convert.rb +87 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/xml_convert_spec.rb +40 -0
- data/xml_convert.gemspec +26 -0
- metadata +130 -0
data/.gitignore
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jeremy Redburn
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# XmlConvert
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'xml_convert'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install xml_convert
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
Bundler::GemHelper.install_tasks
|
10
|
+
|
11
|
+
require 'yard'
|
12
|
+
YARD::Rake::YardocTask.new do |t|
|
13
|
+
t.files = ['lib/**/*.rb', 'README.md']
|
14
|
+
end
|
15
|
+
|
16
|
+
require "rspec/core/rake_task"
|
17
|
+
RSpec::Core::RakeTask.new(:spec)
|
18
|
+
|
19
|
+
task :default => :spec
|
data/lib/xml_convert.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require "xml_convert/version"
|
2
|
+
|
3
|
+
module XmlConvert
|
4
|
+
|
5
|
+
# Converts the name to a valid XML name.
|
6
|
+
#
|
7
|
+
# @example XmlConvert.encode_name('Order Details') #=> 'Order_x0020_Details'
|
8
|
+
#
|
9
|
+
# @param [String] name the name to be encoded.
|
10
|
+
# @return [String, nil] the encoded name or nil if the name cannot be
|
11
|
+
# converted to a string.
|
12
|
+
def self.encode_name(name)
|
13
|
+
return name if name.nil? || name.length == 0
|
14
|
+
|
15
|
+
encoded_name = ""
|
16
|
+
name.chars.each_with_index do |c, i|
|
17
|
+
if is_invalid?(c, encoded_name == "")
|
18
|
+
encoded_name << "_x#{'%04x' % c.ord}_"
|
19
|
+
elsif c == '_' && i+6 < name.length && name[i+1] == 'x' && name[i+6] == '_'
|
20
|
+
encoded_name << '_x005f_'
|
21
|
+
else
|
22
|
+
encoded_name << c
|
23
|
+
end
|
24
|
+
end
|
25
|
+
encoded_name
|
26
|
+
end
|
27
|
+
|
28
|
+
# Converts the name to a valid XML local name.
|
29
|
+
#
|
30
|
+
# @example (see .encode_name)
|
31
|
+
# @example XmlConvert.encode_local_name('a:b') #=> 'a_x003a_b'
|
32
|
+
#
|
33
|
+
# @param (see .encode_name)
|
34
|
+
# @return (see .encode_name)
|
35
|
+
def self.encode_local_name(name)
|
36
|
+
encode_name(name).gsub(':', '_x003a_')
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def self.is_invalid?(char, is_first_letter)
|
42
|
+
!self.is_valid?(char, is_first_letter)
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.is_valid?(char, is_first_letter)
|
46
|
+
is_first_letter ? is_name_start_char?(char) : is_name_char?(char)
|
47
|
+
end
|
48
|
+
|
49
|
+
# NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] |
|
50
|
+
# [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] |
|
51
|
+
# [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
|
52
|
+
# [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] |
|
53
|
+
# [#x10000-#xEFFFF]
|
54
|
+
def self.is_name_start_char?(char)
|
55
|
+
ord = char.ord
|
56
|
+
|
57
|
+
(ord == ':'.ord) ||
|
58
|
+
(ord >= 'A'.ord && ord <= 'Z'.ord) ||
|
59
|
+
(ord == '_'.ord) ||
|
60
|
+
(ord >= 'a'.ord && ord <= 'z'.ord) ||
|
61
|
+
(ord >= 'C0'.hex && ord <= 'D6'.hex) ||
|
62
|
+
(ord >= 'D8'.hex && ord <= 'F6'.hex) ||
|
63
|
+
(ord >= 'F8'.hex && ord <= '2FF'.hex) ||
|
64
|
+
(ord >= '370'.hex && ord <= '37D'.hex) ||
|
65
|
+
(ord >= '37F'.hex && ord <= '1FFF'.hex) ||
|
66
|
+
(ord >= '200C'.hex && ord <= '200D'.hex) ||
|
67
|
+
(ord >= '2070'.hex && ord <= '218F'.hex) ||
|
68
|
+
(ord >= '2C00'.hex && ord <= '2FEF'.hex) ||
|
69
|
+
(ord >= '3001'.hex && ord <= 'D7FF'.hex) ||
|
70
|
+
(ord >= 'F900'.hex && ord <= 'FDCF'.hex) ||
|
71
|
+
(ord >= 'FDFO'.hex && ord <= 'FFFD'.hex) ||
|
72
|
+
(ord >= '10000'.hex && ord <= 'EFFFF'.hex)
|
73
|
+
end
|
74
|
+
|
75
|
+
# NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] |
|
76
|
+
# [#x203F-#x2040]
|
77
|
+
def self.is_name_char?(char)
|
78
|
+
ord = char.ord
|
79
|
+
|
80
|
+
is_name_start_char?(char) ||
|
81
|
+
(ord == '-'.ord) || (ord == '.'.ord) ||
|
82
|
+
(ord >= '0'.ord && ord <= '9'.ord) ||
|
83
|
+
(ord == 'B7'.hex) ||
|
84
|
+
(ord >= '300'.hex && ord <= '36F'.hex) ||
|
85
|
+
(ord >= '203F'.hex && ord <= '2040'.hex)
|
86
|
+
end
|
87
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rspec'
|
6
|
+
require 'rspec/autorun'
|
7
|
+
|
8
|
+
require 'xml_convert'
|
9
|
+
|
10
|
+
Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "XmlConvert" do
|
4
|
+
|
5
|
+
describe ".encode_name" do
|
6
|
+
it "returns nil when argument is nil" do
|
7
|
+
XmlConvert.encode_name(nil).should eq nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns the empty string when argument is an empty string" do
|
11
|
+
XmlConvert.encode_name("").should eq ""
|
12
|
+
end
|
13
|
+
|
14
|
+
it "does not escape underscores when no escaping is required" do
|
15
|
+
XmlConvert.encode_name("Order_Details").should eq "Order_Details"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "escapes spaces" do
|
19
|
+
XmlConvert.encode_name("Order Details").should eq "Order_x0020_Details"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "escapes characters that could be an escape sequence" do
|
23
|
+
XmlConvert.encode_name("Order_x0020_").should eq "Order_x005f_x0020_"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "escapes periods as first letter" do
|
27
|
+
XmlConvert.encode_name(".Order").should eq "_x002e_Order"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "does not escape colons" do
|
31
|
+
XmlConvert.encode_name("a:b").should eq "a:b"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe ".encode_local_name" do
|
36
|
+
it "escapes colons" do
|
37
|
+
XmlConvert.encode_local_name("a:b").should eq "a_x003a_b"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/xml_convert.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'xml_convert/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "xml_convert"
|
8
|
+
gem.version = XmlConvert::VERSION
|
9
|
+
gem.authors = ["Jeremy Redburn"]
|
10
|
+
gem.email = ["jredburn@salsify.com"]
|
11
|
+
gem.description = %q{xml_convert provides utility methods for encoding and
|
12
|
+
decoding XML names.}
|
13
|
+
gem.summary = %q{xml_convert provides utility methods for encoding and
|
14
|
+
decoding XML names.}
|
15
|
+
gem.homepage = "https://github.com/socialceramics/xml_convert"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
|
22
|
+
gem.add_development_dependency("rake")
|
23
|
+
gem.add_development_dependency("yard")
|
24
|
+
gem.add_development_dependency("redcarpet")
|
25
|
+
gem.add_development_dependency("rspec")
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xml_convert
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy Redburn
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: yard
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: redcarpet
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: ! "xml_convert provides utility methods for encoding and\n decoding
|
79
|
+
XML names."
|
80
|
+
email:
|
81
|
+
- jredburn@salsify.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- .yardopts
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- lib/xml_convert.rb
|
93
|
+
- lib/xml_convert/version.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
- spec/xml_convert_spec.rb
|
96
|
+
- xml_convert.gemspec
|
97
|
+
homepage: https://github.com/socialceramics/xml_convert
|
98
|
+
licenses: []
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: 957471371544989854
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
hash: 957471371544989854
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.24
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: xml_convert provides utility methods for encoding and decoding XML names.
|
127
|
+
test_files:
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
- spec/xml_convert_spec.rb
|
130
|
+
has_rdoc:
|