jaspion-kilza 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +180 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/kilza +145 -0
- data/bin/setup +7 -0
- data/jaspion-kilza.gemspec +45 -0
- data/lib/jaspion.rb +6 -0
- data/lib/jaspion/kilza.rb +44 -0
- data/lib/jaspion/kilza/class.rb +88 -0
- data/lib/jaspion/kilza/language.rb +141 -0
- data/lib/jaspion/kilza/language/java.rb +75 -0
- data/lib/jaspion/kilza/language/java/java.erb +131 -0
- data/lib/jaspion/kilza/language/objc.rb +74 -0
- data/lib/jaspion/kilza/language/objc/h.erb +29 -0
- data/lib/jaspion/kilza/language/objc/m.erb +162 -0
- data/lib/jaspion/kilza/property.rb +72 -0
- data/lib/jaspion/kilza/source.rb +12 -0
- data/lib/jaspion/kilza/version.rb +6 -0
- metadata +197 -0
data/lib/jaspion.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'jaspion/kilza/version'
|
2
|
+
|
3
|
+
require 'jaspion/kilza/source'
|
4
|
+
require 'jaspion/kilza/class'
|
5
|
+
require 'jaspion/kilza/property'
|
6
|
+
require 'jaspion/kilza/language'
|
7
|
+
require 'jaspion/kilza/language/objc'
|
8
|
+
require 'jaspion/kilza/language/java'
|
9
|
+
|
10
|
+
# Ruby class
|
11
|
+
class String
|
12
|
+
# Test if the string can be a number
|
13
|
+
#
|
14
|
+
# @param str [String] string to be tested
|
15
|
+
#
|
16
|
+
# @return [Boolean] true in case of success
|
17
|
+
def number?
|
18
|
+
true if Float(self) rescue false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Tranforms a JSON string into Objects
|
23
|
+
module Jaspion
|
24
|
+
module Kilza
|
25
|
+
# Removes everything except numbers and letters.
|
26
|
+
#
|
27
|
+
# @param str [String] string to be cleaned
|
28
|
+
#
|
29
|
+
# @return [String] cleaned string
|
30
|
+
def self.clean(str)
|
31
|
+
str = '_' + str if str[0].number?
|
32
|
+
str.gsub(/[^a-zA-Z0-9]/, '_')
|
33
|
+
end
|
34
|
+
|
35
|
+
# Cleans the string and make it lowercase.
|
36
|
+
#
|
37
|
+
# @param str [String] string to be cleaned
|
38
|
+
#
|
39
|
+
# @return [String] cleaned string
|
40
|
+
def self.normalize(str)
|
41
|
+
Jaspion::Kilza.clean(str).downcase
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module Jaspion
|
2
|
+
module Kilza
|
3
|
+
# Represents one single object class
|
4
|
+
module Class
|
5
|
+
# Class name
|
6
|
+
attr_accessor :name
|
7
|
+
|
8
|
+
# Array with all class dependecies
|
9
|
+
# Specific for each language
|
10
|
+
attr_accessor :imports
|
11
|
+
|
12
|
+
# Array with all class properties
|
13
|
+
attr_accessor :properties
|
14
|
+
|
15
|
+
# Initializes a Class object
|
16
|
+
#
|
17
|
+
# @param name [String] Class Name
|
18
|
+
def initialize(name)
|
19
|
+
@name = Kilza.normalize(name).capitalize
|
20
|
+
@properties = []
|
21
|
+
@imports = []
|
22
|
+
end
|
23
|
+
|
24
|
+
# Adds a new property
|
25
|
+
#
|
26
|
+
# @param property [Kilza::Property] Property to include
|
27
|
+
def push(property)
|
28
|
+
index = @properties.index(property)
|
29
|
+
if !index.nil?
|
30
|
+
current = @properties[index]
|
31
|
+
@properties[index] = update(current, property)
|
32
|
+
else
|
33
|
+
@properties.push(property)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def sources
|
38
|
+
fail 'It should be implemented'
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns the #Source object of this Class.
|
42
|
+
#
|
43
|
+
# @param lang [String] Language name (java, objc, ...)
|
44
|
+
# @param file_name [String] Source file name
|
45
|
+
#
|
46
|
+
# @return [Kilza::Source] Source object of this Class
|
47
|
+
def code(lang, file_name)
|
48
|
+
cur_path = File.expand_path(__FILE__)
|
49
|
+
erb_path = File.join(File.dirname(cur_path), 'language', lang)
|
50
|
+
|
51
|
+
path = File.join(erb_path, file_name + '.erb')
|
52
|
+
eruby = Erubis::Eruby.new(File.read(path))
|
53
|
+
|
54
|
+
s = Kilza::Source.new
|
55
|
+
s.source = eruby.result(binding)
|
56
|
+
s.file_name = @name.capitalize + '.' + file_name
|
57
|
+
s
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_s
|
61
|
+
properties = []
|
62
|
+
@properties.each { |p| properties.push(p.to_s) }
|
63
|
+
{
|
64
|
+
name: @name,
|
65
|
+
imports: @imports,
|
66
|
+
properties: properties
|
67
|
+
}.to_s
|
68
|
+
end
|
69
|
+
|
70
|
+
protected
|
71
|
+
|
72
|
+
# Compares two properties and fill the src property with relevant
|
73
|
+
# dst property values. If src.type is nilclass and dst.type not, replaces
|
74
|
+
# it with dst.type
|
75
|
+
#
|
76
|
+
# @param property [Kilza::Property] Property to include
|
77
|
+
#
|
78
|
+
# @return [Kilza::Property] src property with new values
|
79
|
+
def update(src, dst)
|
80
|
+
src.type = dst.type if src.null? && !dst.null?
|
81
|
+
src.original_type = dst.original_type if src.null? && !dst.null?
|
82
|
+
src.array = dst.array unless src.array?
|
83
|
+
src.key = dst.key unless src.key
|
84
|
+
src
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'erubis'
|
3
|
+
|
4
|
+
module Jaspion
|
5
|
+
module Kilza
|
6
|
+
# Represents an program language
|
7
|
+
module Language
|
8
|
+
# Array with all Class classes
|
9
|
+
attr_accessor :classes
|
10
|
+
|
11
|
+
# Name used to represent the first generated class
|
12
|
+
attr_accessor :base_name
|
13
|
+
|
14
|
+
# JSON that will be used to generate objects
|
15
|
+
attr_accessor :json_string
|
16
|
+
|
17
|
+
# String that will be used to prefix reserved words
|
18
|
+
attr_accessor :reserved_delimiter
|
19
|
+
|
20
|
+
# Words that will receive an undescore before property name
|
21
|
+
attr_accessor :reserved_words
|
22
|
+
|
23
|
+
# Array with all properties that will be used to compare other objects
|
24
|
+
attr_accessor :equal_keys
|
25
|
+
|
26
|
+
# Hash table with all language types mapped to target language
|
27
|
+
attr_accessor :types
|
28
|
+
|
29
|
+
def initialize(json_string)
|
30
|
+
@json_string = json_string
|
31
|
+
@classes = []
|
32
|
+
@types = {}
|
33
|
+
@reserved_words = []
|
34
|
+
@reserved_delimiter = '_'
|
35
|
+
@equal_keys = []
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns all available classes
|
39
|
+
#
|
40
|
+
# @param base_name [String] First class name
|
41
|
+
#
|
42
|
+
# @return [Array] All available classes
|
43
|
+
def classes(base_name)
|
44
|
+
hash = JSON.parse(json_string)
|
45
|
+
hash = { base_name + 'Object' => hash } if hash.is_a?(Array)
|
46
|
+
parse_hash(base_name, hash)
|
47
|
+
|
48
|
+
@classes
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
# Creates a new Class object and checks for valid name
|
54
|
+
#
|
55
|
+
# @param name [String] name of the class to be created
|
56
|
+
#
|
57
|
+
# @return [Kilza::Class] new class
|
58
|
+
def clazz(name)
|
59
|
+
name = @reserved_delimiter + name unless @reserved_words.index(name).nil?
|
60
|
+
Class.new(name)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Creates a new Property object and checks for valid name
|
64
|
+
#
|
65
|
+
# @param name [String] name of the property to be created
|
66
|
+
# @param type [String] type of the property based on class name
|
67
|
+
# @param array [Boolean] indicates if this property represents an array
|
68
|
+
# @param key [Boolean] indicates if this property can be used to compare
|
69
|
+
# objects
|
70
|
+
#
|
71
|
+
# @return [Kilza::Property] new property
|
72
|
+
def property(name, type, array, key)
|
73
|
+
name = @reserved_delimiter + name unless @reserved_words.index(name).nil?
|
74
|
+
Property.new(name, type, array, key)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Searches for a Kilza::Class inside @classes
|
78
|
+
# and creates a new one if it could not be found
|
79
|
+
#
|
80
|
+
# @param name [String] class name to find
|
81
|
+
#
|
82
|
+
# @return [Kilza::Class] class with the specified name
|
83
|
+
def find(name)
|
84
|
+
name = Kilza.normalize(name).capitalize
|
85
|
+
@classes.each { |cl| return cl if cl.name == name }
|
86
|
+
@classes.push(clazz(name))
|
87
|
+
@classes.last
|
88
|
+
end
|
89
|
+
|
90
|
+
# Parses an element value an verify if it should create a new Classs
|
91
|
+
# inside @classes
|
92
|
+
#
|
93
|
+
# @param class_name [String] Name of the class the element is inside
|
94
|
+
# @param name [String] The element name
|
95
|
+
# @param value [Any] The element value
|
96
|
+
# @param array [Boolean] Indicates the element is inside an Array
|
97
|
+
def parse_el(class_name, name, value, array = false)
|
98
|
+
type = value.class.name.split('::').last.downcase
|
99
|
+
|
100
|
+
return parse_array(class_name, name, value) if type == 'array'
|
101
|
+
|
102
|
+
cur_class = find(class_name)
|
103
|
+
key = @equal_keys.index(name).nil? ? false : true
|
104
|
+
cur_class.push(property(name, type, array, key))
|
105
|
+
|
106
|
+
# if value is nil, consider it as an Object
|
107
|
+
find(name) if type == 'nilclass'
|
108
|
+
|
109
|
+
parse_hash(name, value) if type == 'hash'
|
110
|
+
end
|
111
|
+
|
112
|
+
# Parses an hash calling parse_el for each element
|
113
|
+
#
|
114
|
+
# @param class_name [String] Name of the class the hash is inside
|
115
|
+
# @param hash [Hash] The hash value
|
116
|
+
def parse_hash(class_name, hash)
|
117
|
+
hash.each do |property_name, value|
|
118
|
+
parse_el(class_name, property_name, value)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Parses an element that represents an array calling parse_el
|
123
|
+
# for each element.
|
124
|
+
# If the array is empty, it creates a new class to represent
|
125
|
+
# each array's element.
|
126
|
+
#
|
127
|
+
# @param class_name [String] Name of the class the array is inside
|
128
|
+
# @param name [String] The element name
|
129
|
+
# @param value [Any] The element value
|
130
|
+
def parse_array(class_name, name, value)
|
131
|
+
if value.length == 0
|
132
|
+
parse_el(class_name, name, nil, true)
|
133
|
+
else
|
134
|
+
value.each do |val|
|
135
|
+
parse_el(class_name, name, val, true)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module Jaspion
|
4
|
+
module Kilza
|
5
|
+
class Java
|
6
|
+
class Class
|
7
|
+
include Jaspion::Kilza::Class
|
8
|
+
|
9
|
+
# Represents the Java class package
|
10
|
+
attr_accessor :package
|
11
|
+
|
12
|
+
def initialize(name, package = nil)
|
13
|
+
super(name)
|
14
|
+
@package = package
|
15
|
+
end
|
16
|
+
|
17
|
+
def sources
|
18
|
+
[code('java', 'java')]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module Jaspion
|
26
|
+
module Kilza
|
27
|
+
# Objective-C Language parser
|
28
|
+
class Java
|
29
|
+
include Jaspion::Kilza::Language
|
30
|
+
|
31
|
+
def initialize(json_string)
|
32
|
+
super(json_string)
|
33
|
+
|
34
|
+
@reserved_words = %w(
|
35
|
+
abstract continue for new switch assert default goto,
|
36
|
+
package synchronized boolean do if private this break double implements,
|
37
|
+
protected throw byte else import public throws case enum instanceof,
|
38
|
+
null return transient catch extends int short try char final interface static,
|
39
|
+
void class finally long strictfp volatile const float native super while
|
40
|
+
)
|
41
|
+
|
42
|
+
@types = {
|
43
|
+
'nilclass' => 'Object',
|
44
|
+
'string' => 'String',
|
45
|
+
'fixnum' => 'Long',
|
46
|
+
'float' => 'Double',
|
47
|
+
'falseclass' => 'Boolean',
|
48
|
+
'trueclass' => 'Boolean',
|
49
|
+
'hash' => 'Object'
|
50
|
+
}
|
51
|
+
|
52
|
+
@equal_keys = %w(id identifier uid)
|
53
|
+
end
|
54
|
+
|
55
|
+
def clazz(name)
|
56
|
+
Jaspion::Kilza::Java::Class.new(name)
|
57
|
+
end
|
58
|
+
|
59
|
+
def classes(class_name)
|
60
|
+
super(class_name)
|
61
|
+
|
62
|
+
@classes.each do |cl|
|
63
|
+
cl.properties.each do |pr|
|
64
|
+
pr.type = pr.name.capitalize if pr.object? || (pr.array? && pr.null?)
|
65
|
+
|
66
|
+
cl.imports.push('import java.util.ArrayList;') if pr.array? &&
|
67
|
+
cl.imports.index('import java.util.ArrayList;').nil?
|
68
|
+
|
69
|
+
pr.type = @types[pr.type] unless @types[pr.type].nil?
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
package <%= @package %>;
|
2
|
+
|
3
|
+
import org.json.*;
|
4
|
+
import java.io.Serializable;
|
5
|
+
<% for @import in @imports %>
|
6
|
+
<%= @import %>
|
7
|
+
<% end %>
|
8
|
+
|
9
|
+
import com.google.gson.Gson;
|
10
|
+
import com.google.gson.GsonBuilder;
|
11
|
+
import com.google.gson.annotations.SerializedName;
|
12
|
+
import com.google.gson.annotations.Expose;
|
13
|
+
|
14
|
+
public class <%= @name %> implements Serializable
|
15
|
+
{
|
16
|
+
<% for @property in @properties %>
|
17
|
+
private static final String FIELD_<%= @property.name.upcase %> = "<%= @property.original_name %>";
|
18
|
+
<% end %>
|
19
|
+
|
20
|
+
<% for @property in @properties %>
|
21
|
+
@Expose
|
22
|
+
@SerializedName(FIELD_<%= @property.name.upcase %>)
|
23
|
+
<% if @property.array? %>
|
24
|
+
private ArrayList<<%= @property.type %>> <%= @property.name %>;
|
25
|
+
<% else %>
|
26
|
+
private <%= @property.type %> <%= @property.name %>;
|
27
|
+
<% end %>
|
28
|
+
<% end %>
|
29
|
+
|
30
|
+
public <%= @name %>() {
|
31
|
+
|
32
|
+
}
|
33
|
+
|
34
|
+
public <%= @name %>(JSONObject jsonObject) {
|
35
|
+
parseObject(jsonObject);
|
36
|
+
}
|
37
|
+
|
38
|
+
public <%= @name %>(String jsonString) {
|
39
|
+
try {
|
40
|
+
parseString(jsonString);
|
41
|
+
} catch (JSONException e) {
|
42
|
+
e.printStackTrace();
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
protected void parseString(String jsonString) throws JSONException {
|
47
|
+
JSONObject jsonObject = new JSONObject(jsonString);
|
48
|
+
parseObject(jsonObject);
|
49
|
+
}
|
50
|
+
|
51
|
+
protected void parseObject(JSONObject object)
|
52
|
+
{
|
53
|
+
<% for @property in @properties %>
|
54
|
+
<% if @property.array? %>
|
55
|
+
if (object.optJSONArray(FIELD_<%= @property.name.upcase %>) != null)
|
56
|
+
{
|
57
|
+
this.<%= @property.name %> = new ArrayList<>();
|
58
|
+
JSONArray <%= @property.name %>JsonArray = object.optJSONArray(FIELD_<%= @property.name.upcase %>);
|
59
|
+
for (int i = 0; i < <%= @property.name %>JsonArray.length(); i++) {
|
60
|
+
<% if @property.object? || @property.null? %>
|
61
|
+
JSONObject <%= @property.name %> = <%= @property.name %>JsonArray.optJSONObject(i);
|
62
|
+
<% else %>
|
63
|
+
<%= @property.type %> <%= @property.name %> = <%= @property.name %>JsonArray.optJSON<%= @property.type %>(i);
|
64
|
+
<% end %>
|
65
|
+
this.<%= @property.name %>.add(new <%= @property.type %>(<%= @property.name %>));
|
66
|
+
}
|
67
|
+
}
|
68
|
+
<% else %>
|
69
|
+
<% if @property.object? %>
|
70
|
+
this.<%= @property.name %> = new <%= @property.type %>(object.optJSONObject(FIELD_<%= @property.name.upcase %>));
|
71
|
+
<% else %>
|
72
|
+
<% if @property.null? %>
|
73
|
+
this.<%= @property.name %> = object.opt(FIELD_<%= @property.name.upcase %>);
|
74
|
+
<% else %>
|
75
|
+
this.<%= @property.name %> = object.opt<%= @property.type %>(FIELD_<%= @property.name.upcase %>);
|
76
|
+
<% end %>
|
77
|
+
<% end %>
|
78
|
+
<% end %>
|
79
|
+
<% end %>
|
80
|
+
}
|
81
|
+
|
82
|
+
<% for @property in @properties %>
|
83
|
+
public void set<%= @property.name.capitalize %>(<%= @property.array? ? "ArrayList<" + @property.type + ">" : @property.type %> value) {
|
84
|
+
this.<%= @property.name %> = value;
|
85
|
+
}
|
86
|
+
|
87
|
+
<% if @property.array? %>
|
88
|
+
public ArrayList<<%= @property.type %>> get<%= @property.name.capitalize %>() {
|
89
|
+
<% elsif @property.boolean? %>
|
90
|
+
public <%= @property.type %> is<%= @property.name.capitalize %>() {
|
91
|
+
<% else %>
|
92
|
+
public <%= @property.type %> get<%= @property.name.capitalize %>() {
|
93
|
+
<% end %>
|
94
|
+
return this.<%= @property.name %>;
|
95
|
+
}
|
96
|
+
|
97
|
+
<% end %>
|
98
|
+
<%
|
99
|
+
@eq = []
|
100
|
+
@hs = []
|
101
|
+
for @property in @properties
|
102
|
+
if @property.boolean?
|
103
|
+
@eq.push("((#{@name}) obj).is#{@property.name.capitalize}().equals(#{@property.name})") if @property.key?
|
104
|
+
else
|
105
|
+
@eq.push("((#{@name}) obj).get#{@property.name.capitalize}().equals(#{@property.name})") if @property.key?
|
106
|
+
end
|
107
|
+
@hs.push("#{@property.name}.hashCode()") if @property.key?
|
108
|
+
end
|
109
|
+
%>
|
110
|
+
<% if @eq.length > 0 %>
|
111
|
+
@Override
|
112
|
+
public boolean equals(Object obj) {
|
113
|
+
if (obj instanceof <%= @name %>) {
|
114
|
+
return <%= @eq.join(" &&\n ") %> ;
|
115
|
+
}
|
116
|
+
return false;
|
117
|
+
}
|
118
|
+
<% end %>
|
119
|
+
<% if @hs.length > 0 %>
|
120
|
+
@Override
|
121
|
+
public int hashCode(){
|
122
|
+
return (<%= @hs.join(" +\n ") %>);
|
123
|
+
}
|
124
|
+
<% end %>
|
125
|
+
|
126
|
+
@Override
|
127
|
+
public String toString() {
|
128
|
+
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
|
129
|
+
return gson.toJson(this);
|
130
|
+
}
|
131
|
+
}
|