Permalink
Browse files

next rev

  • Loading branch information...
kbrock committed Jan 16, 2009
1 parent 10d9931 commit afdc5165be203016a444bd2094d6eff602cb7c2d
View
@@ -1,128 +1,14 @@
## Template generator
module Gen
class CodeGen
- class JavaTemplate
+ class JavaTemplate < Gen:CodeTemplate
def initialize(name, offset,src=nil)
- compile(name,offset,src)
+ compile(name,offset,src, Gen:JavaHelper)
end
def run(obj,objname)
run_template(binding)
end
-
- include CodeTemplate
-
- #helper methods
- ## output imports
- def class_imports(obj,others=nil)
- lst=[]
- obj.methods.each do |m|
- add_imports(m.inputs.fields,lst)
- add_imports(m.outputs.fields,lst)
- end
- lst.push(*others) unless others.nil?
-
- lst.collect(){|l| "import #{l};"}.uniq.join("\n")
- end
-
- ## takes a list of fields or types
- def field_imports(obj,others=nil)
- lst=[]
- if obj.nil?
- obj=[]
- elsif obj.class!=Array #Could just be a collection?
- obj=[obj]
- end
- add_imports(obj,lst)
- lst.push(*others) unless others.nil?
-
- lst.collect(){|l| "import #{l};"}.uniq.join("\n")
- end
-
- def add_imports(obj,lst)
- return if obj.nil?
- obj.each do |f|
- if f.class == FieldDef and f.array?
- lst << 'java.util.List'
- lst << 'java.util.ArrayList'
- end
- if f.class == String
- lst << obj
- else #assume is a Simple, FieldsDef, or something else that can be imported
- if f.class==FieldDef
- ft=f.field_type
- #if it is a date field, then get date into imports
- if(java_name(ft)=='Date')
- lst << 'java.util.Date'
- end
- else #looping through all types in a file
- ft=f
- end
- if ft.complex?
- lst << "#{java_package(ft)}.#{java_name(ft)}"
- end
- end
- end
- end
-
- #java comment
- def javacom(str, nl="\n * ")
- "/**"+
- str.gsub(/\n/,nl) +
- " */" unless str.nil? or str.empty?
- end
-
- def java_package(t,join_char=".")
- raise "#{t.class}: #{t.name}" unless defined? t.namespace_array
- "#{t.namespace_array.reverse.join(join_char)}" #".#{t.name}"
- end
-
- def java_name(t,optional=false,array=false)
- type_name=t.name
- #simple types, need to use objects if in an array (or nullable)
- if optional or array
- str2={'boolean'=>'Boolean',
- 'datetime'=>'Date',
- 'date'=>'Date',
- 'double'=>'Double',
- 'float'=>'Float',
- 'integer'=>'Integer',
- 'int'=>'Integer',
- 'string'=>'String',
- 'time'=>'Date'}[type_name.downcase]
- else
- str2={'boolean'=>'boolean',
- 'datetime'=>'Date',
- 'date'=>'Date',
- 'double'=>'double',
- 'float'=>'float',
- 'integer'=>'int',
- 'int'=>'int',
- 'string'=>'String',
- 'time'=>'Date'}[type_name.downcase]
- end
- str2||t.cc(t.name_ext)
- end
-
- def java_var_name(t)
- java_name(t).gsub(/^(.)/) { |s| s.downcase}
- end
-
- ## display the java type
- ## basically convert the xsd types to java types
- def type_java_name(f)
- str2=java_name(f.field_type,f.array?,f.optional?)
- str2="List<#{str2}>" if f.array?
- str2
- #TODO: do str2||@name ?
- end
-
- def static_type_java_name(f)
- str2=java_name(f.field_type,f.array?,f.optional?)
- str2="ArrayList<#{str2}>" if f.array?
- str2
- #TODO: do str2||@name ?
- end
end
#public methods
@@ -1,8 +1,8 @@
module Gen
- module CodeTemplate
+ class CodeTemplate
attr_accessor :name,:src,:template
- def compile(name, offset, src)
+ def compile(name, offset, src, mixin=nil)
@name=name
if(src==nil)
@src=offset
@@ -12,6 +12,7 @@ def compile(name, offset, src)
@offset=offset
end
@template=ERB.new(@src,nil,"-")
+ extend(mixin) unless mixin.nil?
end
## obj is the object, objname is the filename / obj + ext
View
@@ -1,19 +1,11 @@
module Gen
class DotGen
- class DotTemplate
+ class DotTemplate < Gen::CodeTemplate
def initialize(name, offset,src=nil)
- compile(name,offset,src)
+ compile(name,offset,src, Gen::DotHelper)
end
- def run(code,ignore=[])
- ignore=fix_ignore(ignore)
- run_template(binding)
- end
-
- include CodeTemplate
-
- #helper
def fix_ignore(ignore)
if ignore.nil?
ignore=[]
@@ -23,72 +15,10 @@ def fix_ignore(ignore)
ignore
end
end
-
- ## helper methods
- def dot_struct(s,name=nil,label=nil, defined=nil)
- defined[s.name]=true if not defined.nil?
- rslts=[]
- rslts << "#{name||s.name} [label=\"{#{label||name||s.name}#{'|' unless @exact}"
- qualifier=""
- s.fields.each do |f| #fields in the method
- qualifier = "|<#{f.name}> " if @exact
- fname=f.name
- fname="#{fname}[]" if f.array?
- if f.hidden?
- fname="((#{fname}))"
- else
- fname="(#{fname})" if f.optional? and not f.output? and not f.array?
- end
- fname="#{fname} *" if f.output?
- rslts << "#{qualifier}#{fname}\\l"
- end #fields in the method
- rslts << "}\"]"
-
- rslts.join("")
- end
-
- def dot_links(s,name=nil,defined=[],ignore=[])
- rslts=[]
- if @exact
- qualifier="#{name||s.name}:"
- else
- qualifier=""
- end
- s.fields.each do |f| #fields
- if f.field_type.complex? and not ignore.include?(f.field_type.name) # only link to detailed types
- #if it hasn't been defined, then mark that we need more information
- defined[f.field_type.name]=false if defined[f.field_type.name].nil?
- if @exact
- src="#{qualifier}#{f.name}"
- src_compass="e"
- else
- src="#{name||s.name}"
- src_compass="s"
- end
- if f.field_type == s #link to myself
- rslts<< "#{src}:e -> #{src}:e"
- else #don't link to myself
- rslts<< "#{src}:#{src_compass} -> #{f.field_type.name}:n [len=0.2]"
- end #don't link to myself
- end #link to detailed types
- end #fields
- rslts.join("\n")
- end
-
- def dot_recurse(defined,ignore,lookup)
- rslts=[]
- while defined.has_value?(false) do #need to define something
- defined.each do |n,v| #complex types referenced
- unless v # if we still need to define this thing
- s=lookup[n]
- raise "couldn't find type #{n}" if s.nil?
- rslts << dot_struct(s,nil,nil,defined)
- raise "for some reason didn't set #{n} to true " unless defined[n]==true
- rslts << dot_links(s,nil,defined,ignore)
- end # if we still need to define this thing
- end #complex types
- end #have something to define
- rslts.join("\n")
+
+ def run(code,ignore=[])
+ ignore=fix_ignore(ignore)
+ run_template(binding)
end
end
#exact will link to this exact location. unfortunatly it ends up with extra lines. so this is disabled for now
@@ -0,0 +1,70 @@
+module Gen
+ module DotHelper
+ ## helper methods
+ def dot_struct(s,name=nil,label=nil, defined=nil)
+ defined[s.name]=true if not defined.nil?
+ rslts=[]
+ rslts << "#{name||s.name} [label=\"{#{label||name||s.name}#{'|' unless @exact}"
+ qualifier=""
+ s.fields.each do |f| #fields in the method
+ qualifier = "|<#{f.name}> " if @exact
+ fname=f.name
+ fname="#{fname}[]" if f.array?
+ if f.hidden?
+ fname="((#{fname}))"
+ else
+ fname="(#{fname})" if f.optional? and not f.output? and not f.array?
+ end
+ fname="#{fname} *" if f.output?
+ rslts << "#{qualifier}#{fname}\\l"
+ end #fields in the method
+ rslts << "}\"]"
+
+ rslts.join("")
+ end
+
+ def dot_links(s,name=nil,defined=[],ignore=[])
+ rslts=[]
+ if @exact
+ qualifier="#{name||s.name}:"
+ else
+ qualifier=""
+ end
+ s.fields.each do |f| #fields
+ if f.field_type.complex? and not ignore.include?(f.field_type.name) # only link to detailed types
+ #if it hasn't been defined, then mark that we need more information
+ defined[f.field_type.name]=false if defined[f.field_type.name].nil?
+ if @exact
+ src="#{qualifier}#{f.name}"
+ src_compass="e"
+ else
+ src="#{name||s.name}"
+ src_compass="s"
+ end
+ if f.field_type == s #link to myself
+ rslts<< "#{src}:e -> #{src}:e"
+ else #don't link to myself
+ rslts<< "#{src}:#{src_compass} -> #{f.field_type.name}:n [len=0.2]"
+ end #don't link to myself
+ end #link to detailed types
+ end #fields
+ rslts.join("\n")
+ end
+
+ def dot_recurse(defined,ignore,lookup)
+ rslts=[]
+ while defined.has_value?(false) do #need to define something
+ defined.each do |n,v| #complex types referenced
+ unless v # if we still need to define this thing
+ s=lookup[n]
+ raise "couldn't find type #{n}" if s.nil?
+ rslts << dot_struct(s,nil,nil,defined)
+ raise "for some reason didn't set #{n} to true " unless defined[n]==true
+ rslts << dot_links(s,nil,defined,ignore)
+ end # if we still need to define this thing
+ end #complex types
+ end #have something to define
+ rslts.join("\n")
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit afdc516

Please sign in to comment.