normalizexml 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/Gemfile +6 -0
- data/LICENSE +22 -0
- data/README.md +59 -0
- data/bin/normalizexml +121 -0
- data/docs/README.CODE.txt +21 -0
- data/lib/normalizexml.rb +58 -0
- data/lib/normalizexml/config.rb +93 -0
- data/lib/normalizexml/controller.rb +83 -0
- data/lib/normalizexml/normalize_xml_task.rb +28 -0
- data/lib/normalizexml/parser.rb +273 -0
- data/lib/normalizexml/version.rb +19 -0
- data/normalizexml.gemspec +30 -0
- data/rakefile.rb +48 -0
- data/spec/data/test.xml +1 -0
- data/spec/data/test2.xml +1 -0
- data/spec/normalize_xml_task_spec.rb +64 -0
- data/spec/parser_spec.rb +145 -0
- data/spec/spec_helper.rb +42 -0
- metadata +183 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9e27d1f6df9f44b6874f8dae18328645a1f33c02
|
4
|
+
data.tar.gz: c33a09275fe1627adc3f459beaf7369b869eb34e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e70949817d4a696079f3e0bca2549726c07406a0ce9f213b26b4d116e7ff8c289ba1601c446bc35d6087271eec57a8380d44d06137d130edb1eeb8e2f61cc65d
|
7
|
+
data.tar.gz: f3e1ce52dc9009fcb2b270be855701c9ed32ca76d5debcc00eae2ac8a31e492b8cfc361960a3fd560c06589616b1ce3bd0ae852dabb37fe9a03ed5fd77b3fc0c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Jeff McAffee
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# NormalizeXml
|
2
|
+
|
3
|
+
## Summary
|
4
|
+
|
5
|
+
NormalizeXml is used to 'normalize' AMS guideline xml so different
|
6
|
+
versions can be easily compared.
|
7
|
+
|
8
|
+
As part of normalization, all `id`s are zero'd as well as `order`
|
9
|
+
attributes. XML is also pretty-printed.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your gemfile:
|
14
|
+
|
15
|
+
gem 'normalizexml'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install normalizexml
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
TODO
|
28
|
+
|
29
|
+
## Testing
|
30
|
+
|
31
|
+
NormalizeXml uses RSpec for testing.
|
32
|
+
|
33
|
+
To run all existing tests:
|
34
|
+
|
35
|
+
$ rake spec
|
36
|
+
|
37
|
+
or directly:
|
38
|
+
|
39
|
+
$ bundle exec rspec
|
40
|
+
|
41
|
+
## TODO
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it ( https://github.com/jmcaffee/normalizexml/fork )
|
46
|
+
1. Clone it (`git clone git@github.com:[my-github-username]/normalizexml.git`)
|
47
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
3. Create tests for your feature branch
|
49
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
6. Create a new Pull Request
|
52
|
+
|
53
|
+
## LICENSE
|
54
|
+
|
55
|
+
NormalizeXml is licensed under the MIT license.
|
56
|
+
|
57
|
+
See [LICENSE](https://github.com/jmcaffee/normalizexml/blob/master/LICENSE) for
|
58
|
+
details.
|
59
|
+
|
data/bin/normalizexml
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
###! C:/tools/Ruby/bin/ruby.exe
|
3
|
+
##############################################################################
|
4
|
+
# File:: normalizexml.rb
|
5
|
+
# Purpose:: Utility to ...
|
6
|
+
#
|
7
|
+
# Author:: Jeff McAffee 09/03/2010
|
8
|
+
# Copyright:: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
|
9
|
+
# Website:: http://ktechsystems.com
|
10
|
+
##############################################################################
|
11
|
+
|
12
|
+
require 'normalizexml'
|
13
|
+
require 'user-choices'
|
14
|
+
|
15
|
+
|
16
|
+
class NormalizeXmlApp < UserChoices::Command
|
17
|
+
include UserChoices
|
18
|
+
include NormalizeXml
|
19
|
+
|
20
|
+
def initialize()
|
21
|
+
super
|
22
|
+
@controller = Controller.new
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
def add_sources(builder)
|
27
|
+
builder.add_source(CommandLineSource, :usage,
|
28
|
+
"Usage: #{$0} [options] INPUT_XML {OUTPUT_FILE}",
|
29
|
+
"Normalize an XML file",
|
30
|
+
"INPUT_XML - file to normalize",
|
31
|
+
"OUTPUT_FILE - (optional) filename of output file\n")
|
32
|
+
end # def add_sources
|
33
|
+
|
34
|
+
|
35
|
+
def add_choices(builder)
|
36
|
+
# Arguments
|
37
|
+
#builder.add_choice(:cmdArg, :length=>1) { |command_line| # Use length to REQUIRE args.
|
38
|
+
builder.add_choice(:cmdArg) { |command_line|
|
39
|
+
command_line.uses_arglist
|
40
|
+
}
|
41
|
+
|
42
|
+
# Switches
|
43
|
+
builder.add_choice(:verbose, :type=>:boolean, :default=>false) { |command_line|
|
44
|
+
command_line.uses_switch("-v", "--verbose",
|
45
|
+
"Verbose output.")
|
46
|
+
}
|
47
|
+
|
48
|
+
=begin
|
49
|
+
builder.add_choice(:aswitch, :type=>:boolean, :default=>false) { |command_line|
|
50
|
+
command_line.uses_switch("-a", "--aswitch",
|
51
|
+
"Switch description.")
|
52
|
+
}
|
53
|
+
|
54
|
+
# Options
|
55
|
+
builder.add_choice(:option, :type=>:string) { |command_line|
|
56
|
+
command_line.uses_option("-o", "--option ARG",
|
57
|
+
"Option description.")
|
58
|
+
}
|
59
|
+
=end
|
60
|
+
|
61
|
+
end # def add_choices
|
62
|
+
|
63
|
+
# This method is called automatically by UserChoices.
|
64
|
+
# Use it to handle simple post processing of user choices.
|
65
|
+
def postprocess_user_choices
|
66
|
+
@user_choices[:infile] = @user_choices[:cmdArg][0]
|
67
|
+
@user_choices[:outfile] = @user_choices[:cmdArg][1]
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
# Execute the NormalizeXml application.
|
72
|
+
# This method is called automatically when 'normalizexml(.rb)' is executed from the command line.
|
73
|
+
def execute
|
74
|
+
$LOG.debug "NormalizeXmlApp::execute"
|
75
|
+
|
76
|
+
if(@user_choices[:verbose])
|
77
|
+
@controller.verbose(@user_choices[:verbose])
|
78
|
+
end
|
79
|
+
|
80
|
+
=begin
|
81
|
+
if(@user_choices[:aswitch])
|
82
|
+
@controller.doSomethingWithSwitch(@user_choices[:aswitch])
|
83
|
+
return
|
84
|
+
end
|
85
|
+
=end
|
86
|
+
if(@user_choices[:cmdArg].empty?) # If no cmd line arg...
|
87
|
+
#return unless @controller.noCmdLineArg()
|
88
|
+
@controller.noCmdLineArg()
|
89
|
+
else
|
90
|
+
#return unless @controller.doSomethingWithCmdLineArg(@user_choices[:cmdArg])
|
91
|
+
@controller.doSomethingWithCmdLineArg(@user_choices[:cmdArg])
|
92
|
+
end
|
93
|
+
|
94
|
+
@controller.infile = @user_choices[:infile] unless @user_choices[:infile].nil? || @user_choices[:infile].empty?
|
95
|
+
@controller.outfile = @user_choices[:outfile] unless @user_choices[:outfile].nil? || @user_choices[:outfile].empty?
|
96
|
+
@controller.normalize()
|
97
|
+
end # def execute
|
98
|
+
|
99
|
+
|
100
|
+
end # class NormalizeXmlApp
|
101
|
+
|
102
|
+
|
103
|
+
# Uncomment the next line if this file can be both loaded via 'require' AND called from the command line.
|
104
|
+
# This technique does NOT work if the app is running as a gem.
|
105
|
+
#if $0 == __FILE__
|
106
|
+
begin
|
107
|
+
NormalizeXmlApp.new.execute
|
108
|
+
rescue SystemExit
|
109
|
+
|
110
|
+
rescue Exception => e
|
111
|
+
puts "!!! ERROR:"
|
112
|
+
puts "\t" + e.message
|
113
|
+
puts
|
114
|
+
puts "Try -h for help."
|
115
|
+
puts
|
116
|
+
#exit if !$LOGGING
|
117
|
+
puts "Exception type: #{e.class.to_s}"
|
118
|
+
puts e.backtrace
|
119
|
+
puts
|
120
|
+
end
|
121
|
+
#end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
========================================================================
|
2
|
+
= File: README.CODE.txt
|
3
|
+
= Purpose: Instructions and hints for customizing NormalizeXml.
|
4
|
+
=
|
5
|
+
= Generated: 09/03/2010
|
6
|
+
= Copyright: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
|
7
|
+
= Website: http://ktechsystems.com
|
8
|
+
========================================================================
|
9
|
+
|
10
|
+
CUSTOMIZING YOUR CODE:
|
11
|
+
|
12
|
+
You should add all of your data functionality to the Parser
|
13
|
+
object that has been created for you in lib/parser.rb.
|
14
|
+
|
15
|
+
To modify command line arguments, you should edit bin/normalizexml.rb
|
16
|
+
and lib/normalizexmlcontroller.rb
|
17
|
+
|
18
|
+
Add or modify configuration values by editing lib/normalizexmlcfg.rb.
|
19
|
+
|
20
|
+
|
21
|
+
|
data/lib/normalizexml.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
##############################################################################
|
2
|
+
# File:: normalizexml.rb
|
3
|
+
# Purpose:: Include file for NormalizeXml library
|
4
|
+
#
|
5
|
+
# Author:: Jeff McAffee 09/03/2010
|
6
|
+
# Copyright:: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
|
7
|
+
# Website:: http://ktechsystems.com
|
8
|
+
##############################################################################
|
9
|
+
|
10
|
+
require 'find'
|
11
|
+
require 'logger'
|
12
|
+
require 'bundler/setup'
|
13
|
+
|
14
|
+
|
15
|
+
if(!$LOG)
|
16
|
+
$LOG = Logger.new(STDERR)
|
17
|
+
$LOG.level = Logger::ERROR
|
18
|
+
end
|
19
|
+
|
20
|
+
if ENV["DEBUG"] == '1'
|
21
|
+
puts "LOGGING: ON due to DEBUG=1"
|
22
|
+
$LOG.level = Logger::DEBUG
|
23
|
+
end
|
24
|
+
|
25
|
+
$LOGGING = false
|
26
|
+
# Uncomment line below to force logging:
|
27
|
+
#$LOGGING = true # TODO: Change this flag to false when releasing production build.
|
28
|
+
|
29
|
+
require "#{File.join( File.dirname(__FILE__), 'normalizexml','version')}"
|
30
|
+
require "#{File.join( File.dirname(__FILE__), 'normalizexml','config')}"
|
31
|
+
|
32
|
+
logcfg = NormalizeXml::Config.new.load
|
33
|
+
if(logcfg.key?(:logging) && (true == logcfg[:logging]) )
|
34
|
+
$LOGGING = true
|
35
|
+
end
|
36
|
+
|
37
|
+
if($LOGGING)
|
38
|
+
# Create a new log file each time:
|
39
|
+
file = File.open('normalizexml.log', File::WRONLY | File::APPEND | File::CREAT | File::TRUNC)
|
40
|
+
$LOG = Logger.new(file)
|
41
|
+
$LOG.level = Logger::DEBUG
|
42
|
+
#$LOG.level = Logger::INFO
|
43
|
+
else
|
44
|
+
if(File.exists?('normalizexml.log'))
|
45
|
+
FileUtils.rm('normalizexml.log')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
$LOG.info "**********************************************************************"
|
49
|
+
$LOG.info "Logging started for NormalizeXml library."
|
50
|
+
$LOG.info "**********************************************************************"
|
51
|
+
|
52
|
+
|
53
|
+
class_files = File.join( File.dirname(__FILE__), 'normalizexml', '*.rb')
|
54
|
+
$: << File.join( File.dirname(__FILE__), 'normalizexml') # Add directory to the include file array.
|
55
|
+
Dir.glob(class_files) do | class_file |
|
56
|
+
require class_file[/\w+\.rb$/]
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,93 @@
|
|
1
|
+
##############################################################################
|
2
|
+
# File:: normalizexmlcfg.rb
|
3
|
+
# Purpose:: NormalizeXml configuration file reader/writer class.
|
4
|
+
#
|
5
|
+
# Author:: Jeff McAffee 09/03/2010
|
6
|
+
# Copyright:: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
|
7
|
+
# Website:: http://ktechsystems.com
|
8
|
+
##############################################################################
|
9
|
+
|
10
|
+
require 'ktcommon/ktcfg'
|
11
|
+
|
12
|
+
##############################################################################
|
13
|
+
# Everything is contained in Module NormalizeXml
|
14
|
+
module NormalizeXml
|
15
|
+
|
16
|
+
class Config < KtCfg::CfgFile
|
17
|
+
|
18
|
+
attr_accessor :cfg
|
19
|
+
attr_writer :cfgFile
|
20
|
+
|
21
|
+
|
22
|
+
def initialize(rootDir=nil)
|
23
|
+
$LOG.debug "Config::initialize"
|
24
|
+
super
|
25
|
+
@cfg = {}
|
26
|
+
|
27
|
+
setDefaults()
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def setDefaults
|
32
|
+
$LOG.debug "Config::setDefaults"
|
33
|
+
|
34
|
+
# Notes about APPDATA paths:
|
35
|
+
# Local app data should be used when an app's data is too
|
36
|
+
# big to move around. Or is specific to the machine running
|
37
|
+
# the application.
|
38
|
+
#
|
39
|
+
# Roaming app data files could be pushed to a server (in a
|
40
|
+
# domain environment) and downloaded onto a different work
|
41
|
+
# station.
|
42
|
+
#
|
43
|
+
# LocalLow is used for data that must be sandboxed. Currently
|
44
|
+
# it is only used by IE for addons and storing data from
|
45
|
+
# untrusted sources (as far as I know).
|
46
|
+
#
|
47
|
+
|
48
|
+
|
49
|
+
appDataPath = ENV["APPDATA"] # APPDATA returns AppData\Roaming on Vista/W7
|
50
|
+
appDataPath ||= ENV["HOME"] # APPDATA returns AppData\Roaming on Vista/W7
|
51
|
+
#appDataPath = ENV["LOCALAPPDATA"] # LOCALAPPDATA returns AppData\Local on Vista/W7
|
52
|
+
appDataPath = File.rubypath(File.join(appDataPath, "normalizexml"))
|
53
|
+
@cfg[:appPath] = appDataPath
|
54
|
+
@cfg[:version] = NormalizeXml::VERSION
|
55
|
+
@cfg[:logging] = false
|
56
|
+
|
57
|
+
@cfgFile = "normalizexml.yml"
|
58
|
+
|
59
|
+
# Set the config file path. Default is the 'global' one in APPDATA.
|
60
|
+
if( @rootDir.nil? )
|
61
|
+
@rootDir = appDataPath
|
62
|
+
@cfgFile = "config.yml"
|
63
|
+
|
64
|
+
# Override the gobal config if there is a local (current working dir) version.
|
65
|
+
if(File.exists?(File.join(FileUtils.pwd(), "normalizexml.yml")))
|
66
|
+
@rootDir = FileUtils.pwd()
|
67
|
+
@cfgFile = "normalizexml.yml"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
# Load the YAML configuration file.
|
74
|
+
# returns:: a hash containing configuration info.
|
75
|
+
def load
|
76
|
+
$LOG.debug "Config::load"
|
77
|
+
tmpCfg = read(@cfgFile)
|
78
|
+
@cfg = tmpCfg if !tmpCfg.nil? && !tmpCfg.empty?
|
79
|
+
@cfg
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
# Save the @cfg hash to a YAML file.
|
84
|
+
def save
|
85
|
+
$LOG.debug "Config::save"
|
86
|
+
write(@cfgFile, @cfg)
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
end # class Config
|
91
|
+
|
92
|
+
|
93
|
+
end # module NormalizeXml
|
@@ -0,0 +1,83 @@
|
|
1
|
+
##############################################################################
|
2
|
+
# File:: controller.rb
|
3
|
+
# Purpose:: Main Controller object for NormalizeXml utility
|
4
|
+
#
|
5
|
+
# Author:: Jeff McAffee 09/03/2010
|
6
|
+
# Copyright:: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
|
7
|
+
# Website:: http://ktechsystems.com
|
8
|
+
##############################################################################
|
9
|
+
|
10
|
+
require 'ktcommon/ktpath'
|
11
|
+
require 'ktcommon/ktcmdline'
|
12
|
+
|
13
|
+
##############################################################################
|
14
|
+
# Everything is contained in Module NormalizeXml
|
15
|
+
module NormalizeXml
|
16
|
+
|
17
|
+
class Controller
|
18
|
+
|
19
|
+
attr_accessor :someFlag
|
20
|
+
attr_accessor :model
|
21
|
+
attr_reader :verbose
|
22
|
+
|
23
|
+
def initialize()
|
24
|
+
$LOG.debug "Controller::initialize"
|
25
|
+
@cfg = Config.new.load
|
26
|
+
@someFlag = false
|
27
|
+
@model = Parser.new
|
28
|
+
@model.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def verbose(arg)
|
33
|
+
$LOG.debug "Controller::verbose( #{arg} )"
|
34
|
+
puts "Verbose mode: #{arg.to_s}" if @verbose || arg
|
35
|
+
@model.verbose = arg
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
def verbose=(arg)
|
40
|
+
$LOG.debug "Controller::verbose=( #{arg} )"
|
41
|
+
return verbose(arg)
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def infile=(infile)
|
46
|
+
@model.infile = infile
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
def outfile=(outfile)
|
51
|
+
@model.outfile = outfile
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def doSomethingWithSwitch(arg)
|
56
|
+
$LOG.debug "Controller::doSomethingWithSwitch( #{arg} )"
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
def doSomethingWithCmdLineArg(arg)
|
61
|
+
$LOG.debug "Controller::doSomethingWithCmdLineArg( #{arg} )"
|
62
|
+
#raise ArgumentError.new("Unexpected argument: #{arg}")
|
63
|
+
return true # I want cmd line args
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def noCmdLineArg()
|
68
|
+
$LOG.debug "Controller::noCmdLineArg"
|
69
|
+
raise ArgumentError.new("Argument expected.")
|
70
|
+
return false # No arg, no worky.
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def normalize()
|
75
|
+
$LOG.debug "Controller::normalize"
|
76
|
+
@model.normalize()
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
end # class Controller
|
81
|
+
|
82
|
+
|
83
|
+
end # module NormalizeXml
|