codemodels-xml 0.1.2-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 290a3d00f8ec233e193592d14b5750ab0bca7cfe
4
+ data.tar.gz: 62300c0ac31e5640e203619701bc2ade5c06c594
5
+ SHA512:
6
+ metadata.gz: 8cf7141dbf03a91e6397b1e7290b779e31d975dc6a25e6d2852d0d5bee948e6d1307298d275f1ef7b5856ac43c390934a033f17fb159f51277a4b75fcf7f1671
7
+ data.tar.gz: 3dc0d715839e065f01c3943ee3f1139628354a9d8510063ffaa8d1d1663d67e00d6f5bf3114578116863d414e0d02c6b2707bdcd4d37a2a89e2738f698648268
@@ -0,0 +1,19 @@
1
+ Gemfile.lock
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 ftomassetti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ codemodels-xml
2
+ ==============
3
+
4
+ Plugin to add support for XML files in CodeModels.
@@ -0,0 +1,13 @@
1
+ require 'rake/testtask'
2
+ require 'rubygems/tasks'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ Gem::Tasks.new do |tasks|
9
+ tasks.console.command = 'jruby'
10
+ end
11
+
12
+ desc "Run tests"
13
+ task :default => :test
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'codemodels/xml/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.platform = 'java'
8
+ s.name = 'codemodels-xml'
9
+ s.version = CodeModels::Xml::VERSION
10
+ s.date = '2013-12-31'
11
+ s.summary = "CodeModels plugin for XML files."
12
+ s.description = "CodeModels plugin for XML files."
13
+ s.authors = ["Federico Tomassetti"]
14
+ s.email = 'https://github.com/ftomassetti/codemodels-xml'
15
+ s.homepage = 'http://federico-tomassetti.it'
16
+ s.license = "APACHE 2"
17
+
18
+ s.files = `git ls-files`.split($/)
19
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_dependency('codemodels')
24
+ s.add_dependency('nokogiri')
25
+
26
+ s.add_development_dependency "bundler", "~> 1.3"
27
+ s.add_development_dependency "rake"
28
+ s.add_development_dependency "rubygems-tasks"
29
+ s.add_development_dependency "simplecov"
30
+ end
@@ -0,0 +1,4 @@
1
+ require 'codemodels/xml/version'
2
+ require 'codemodels/xml/metamodel'
3
+ require 'codemodels/xml/parser'
4
+ require 'codemodels/xml/language'
@@ -0,0 +1,17 @@
1
+ require 'codemodels'
2
+
3
+ module CodeModels
4
+ module Xml
5
+
6
+ class XmlLanguage < Language
7
+ def initialize
8
+ super('Xml')
9
+ @extensions << 'xml'
10
+ @parser = CodeModels::Xml::Parser.new
11
+ end
12
+ end
13
+
14
+ CodeModels.register_language XmlLanguage.new
15
+
16
+ end
17
+ end
@@ -0,0 +1,35 @@
1
+ require 'codemodels'
2
+
3
+ module CodeModels
4
+ module Xml
5
+
6
+ class XmlAstNode < CodeModels::CodeModelsAstNode
7
+ end
8
+
9
+ class Namespace < XmlAstNode
10
+ end
11
+
12
+ class Attribute < XmlAstNode
13
+ has_attr 'name', String
14
+ has_attr 'value', String
15
+ end
16
+
17
+ class Text < XmlAstNode
18
+ end
19
+
20
+ class Element < XmlAstNode
21
+ has_attr 'name', String
22
+ contains_many 'children', Element, 'parent'
23
+ contains_many_uni 'texts', Text
24
+ contains_many_uni 'attributes', Attribute
25
+ end
26
+
27
+ class Document < XmlAstNode
28
+ contains_many_uni 'namespaces', Namespace
29
+ has_attr 'version', String
30
+ has_attr 'encoding', String
31
+ contains_one 'root', Element, 'document'
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,120 @@
1
+ require 'nokogiri'
2
+ require 'codemodels'
3
+
4
+ module CodeModels
5
+ module Xml
6
+
7
+ class Parser < CodeModels::Parser
8
+
9
+ def internal_parse_artifact(artifact)
10
+ raw_ast = Nokogiri.XML(artifact.code)
11
+ convert_raw_ast(raw_ast)
12
+ end
13
+
14
+ def convert_raw_ast(raw_node)
15
+ if raw_node.is_a?(Nokogiri::XML::Document)
16
+ build_document(raw_node)
17
+ elsif raw_node.is_a?(Nokogiri::XML::Element)
18
+ build_element(raw_node)
19
+ elsif raw_node.is_a?(Nokogiri::XML::Text)
20
+ build_text(raw_node)
21
+ elsif raw_node.is_a?(Nokogiri::XML::Attr)
22
+ build_attribute(raw_node)
23
+ else
24
+ raise "Unknown node type #{raw_node.class}"
25
+ end
26
+ end
27
+
28
+ def build_attribute(raw_node)
29
+ node = Attribute.new
30
+ node.name = raw_node.name
31
+ node.value = raw_node.value
32
+ node
33
+ end
34
+
35
+ def build_text(raw_node)
36
+ node = Text.new
37
+ node
38
+ end
39
+
40
+ def build_element(raw_node)
41
+ node = Element.new
42
+ node.name = raw_node.name
43
+ raw_node.attribute_nodes.each do |c|
44
+ node.addAttributes(convert_raw_ast(c))
45
+ end
46
+ raw_node.children.each do |c|
47
+ if c.is_a?(Nokogiri::XML::Text)
48
+ node.addTexts(convert_raw_ast(c))
49
+ elsif c.is_a?(Nokogiri::XML::Element)
50
+ node.addChildren(convert_raw_ast(c))
51
+ else
52
+ raise "Unexpected child type: #{c.class}"
53
+ end
54
+ end
55
+ node
56
+ end
57
+
58
+ def build_document(raw_node)
59
+ node = Document.new
60
+ node.version = raw_node.version
61
+ node.encoding = raw_node.encoding
62
+ for n in raw_node.namespaces
63
+ node.addNamespaces(convert_raw_ast(n))
64
+ end
65
+ node.root = convert_raw_ast(raw_node.root)
66
+ node
67
+ end
68
+
69
+ end
70
+
71
+ DefaultParser = Parser.new
72
+
73
+ def self.jsonize_node(node)
74
+ map = {}
75
+
76
+ map['name'] = node.name
77
+ map['blank'] = node.blank?
78
+ map['cdata'] = node.cdata?
79
+ map['comment'] = node.comment?
80
+ map['content'] = node.content
81
+ map['fragment'] = node.fragment?
82
+ map['is_text'] = node.text?
83
+ map['text'] = node.text
84
+ map['type'] = node.type
85
+
86
+ namespaces = []
87
+ node.namespace_definitions.each do |n|
88
+ namespaces << { 'href' => n.href, 'prefix' => n.prefix}
89
+ end
90
+ map['namespaces'] = namespaces
91
+
92
+ attrs = []
93
+ node.attribute_nodes.each do |a|
94
+ attr_map = {}
95
+ attr_map['value'] = a.value
96
+ attr_map['name'] = a.name
97
+ attrs << attr_map
98
+ end
99
+ map['attributes'] = attrs
100
+
101
+ children = []
102
+ node.children.each do |c|
103
+ children << jsonize_node(c)
104
+ end
105
+ map['children'] = children
106
+
107
+ map
108
+ end
109
+
110
+ def self.jsonize_doc(doc)
111
+ map = {}
112
+ map['namespaces'] = doc.namespaces
113
+ map['version'] = doc.version
114
+ map['encoding'] = doc.encoding
115
+ map['root'] = jsonize_node(doc.root)
116
+ map
117
+ end
118
+
119
+ end
120
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: UTF-8
2
+ module CodeModels
3
+ module Xml
4
+ VERSION = "0.1.2"
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ <root>
2
+ <a />
3
+ <b></b>
4
+ <c> </c>
5
+ <d>
6
+ <e />
7
+ </d>
8
+ </root>
@@ -0,0 +1,8 @@
1
+ <root a="ciao" b="mondo">
2
+ <a />
3
+ <b></b>
4
+ <c> </c>
5
+ <d>
6
+ <e />
7
+ </d>
8
+ </root>
@@ -0,0 +1,28 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/test/"
4
+ end
5
+
6
+ require 'codemodels/xml'
7
+ require 'test/unit'
8
+
9
+ include CodeModels
10
+
11
+ module TestHelper
12
+
13
+ def assert_class(expected_class,node)
14
+ assert node.class==expected_class, "Node expected to have class #{expected_class} instead it has class #{node.class}"
15
+ end
16
+
17
+ def relative_path(path)
18
+ File.join(File.dirname(__FILE__),path)
19
+ end
20
+
21
+ def read_test_data(filename)
22
+ dir = File.dirname(__FILE__)
23
+ dir = File.join(dir,'data')
24
+ path = File.join(dir,filename)
25
+ IO.read(path)
26
+ end
27
+
28
+ end
@@ -0,0 +1,44 @@
1
+ require "test_helper"
2
+
3
+ class TestParsing < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include CodeModels::Xml
7
+
8
+ def setup
9
+ @dir = File.dirname(__FILE__)+'/data'
10
+ @example1 = CodeModels.parse_file(@dir+'/example1.xml')
11
+ @example2 = CodeModels.parse_file(@dir+'/example2.xml')
12
+ end
13
+
14
+ def test_doc_is_parsed
15
+ assert_class Document, @example1
16
+ end
17
+
18
+ def test_root_is_parsed
19
+ assert_class Element, @example1.root
20
+ end
21
+
22
+ def test_root_is_correctly_parsed
23
+ assert_equal "root", @example1.root.name
24
+ assert_equal @example1, @example1.root.document
25
+ assert_equal 4, @example1.root.children.count
26
+ assert_equal 'a', @example1.root.children[0].name
27
+ assert_equal 'b', @example1.root.children[1].name
28
+ assert_equal 'c', @example1.root.children[2].name
29
+ assert_equal 'd', @example1.root.children[3].name
30
+ assert_equal 'e', @example1.root.children[3].children[0].name
31
+ end
32
+
33
+ def test_attributes_are_parsed
34
+ assert_equal 2, @example2.root.attributes.count
35
+ assert_class Attribute, @example2.root.attributes[0]
36
+ assert_class Attribute, @example2.root.attributes[1]
37
+ assert_equal 'a', @example2.root.attributes[0].name
38
+ assert_equal 'ciao', @example2.root.attributes[0].value
39
+ assert_equal 'b', @example2.root.attributes[1].name
40
+ assert_equal 'mondo', @example2.root.attributes[1].value
41
+ end
42
+
43
+
44
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codemodels-xml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: java
6
+ authors:
7
+ - Federico Tomassetti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: codemodels
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ prerelease: false
26
+ type: :runtime
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ prerelease: false
40
+ type: :runtime
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ version: '1.3'
53
+ prerelease: false
54
+ type: :development
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ prerelease: false
68
+ type: :development
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubygems-tasks
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ prerelease: false
82
+ type: :development
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ prerelease: false
96
+ type: :development
97
+ description: CodeModels plugin for XML files.
98
+ email: https://github.com/ftomassetti/codemodels-xml
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - .gitignore
104
+ - Gemfile
105
+ - LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - codemodels-xml.gemspec
109
+ - lib/codemodels/xml.rb
110
+ - lib/codemodels/xml/language.rb
111
+ - lib/codemodels/xml/metamodel.rb
112
+ - lib/codemodels/xml/parser.rb
113
+ - lib/codemodels/xml/version.rb
114
+ - test/data/example1.xml
115
+ - test/data/example2.xml
116
+ - test/test_helper.rb
117
+ - test/test_parsing.rb
118
+ homepage: http://federico-tomassetti.it
119
+ licenses:
120
+ - APACHE 2
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.2.0
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: CodeModels plugin for XML files.
142
+ test_files:
143
+ - test/data/example1.xml
144
+ - test/data/example2.xml
145
+ - test/test_helper.rb
146
+ - test/test_parsing.rb