Class: Mappum::WorkdirLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/mappum/xml_transform.rb,
lib/mappum/java_transform.rb

Overview

Class supporting loading working directory of the layout:

schema/ - Directory containing xsd files

map/ - directory containing Mappum maps

Instance Method Summary collapse

Constructor Details

#initialize(schema_path = "schema", map_dir = "map", basedir = nil) ⇒ WorkdirLoader

Returns a new instance of WorkdirLoader.



232
233
234
235
236
237
238
239
240
# File 'lib/mappum/xml_transform.rb', line 232

def initialize(schema_path = "schema", map_dir="map", basedir=nil)
  schema_path = "schema" if schema_path.nil?
  @schema_path = schema_path
  @basedir = basedir
  @basedir ||= mktmpdir
  map_dir = "map" if map_dir.nil?
  @map_dir = map_dir
  @mapper_scripts = []
end

Instance Method Details

#cleanupObject

Remove tmpdir



345
346
347
# File 'lib/mappum/xml_transform.rb', line 345

def cleanup
  FileUtils.rm_rf(@basedir) if @cleanup
end

#defined_element_trees(schema_definition = nil) ⇒ Object

Returns simple tree structure representing all elements defined in schemas



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/mappum/xml_transform.rb', line 305

def defined_element_trees(schema_definition = nil)
  returning = []
  if schema_definition == nil then
    XSD::Mapping::Mapper.mappers.each do |mapper|
       mapper.registry.class_schema_definition.each do |k,v|
         returning << defined_element_trees(v)
       end
    end
    return returning
  end
  name = schema_definition.varname if schema_definition.respond_to?(:varname)
  name ||= schema_definition.class_for
  is_array = false
  is_array = schema_definition.as_array? if schema_definition.respond_to?(:as_array?)

  subelems = nil
  mapped_class = schema_definition.mapped_class if schema_definition.respond_to?(:mapped_class)

  if schema_definition.respond_to?(:elements) and not schema_definition.elements.nil?
    subelems ||= []
    schema_definition.elements.each do |element|
      subelems << defined_element_trees(element)
    end
    mapped_class = nil
  end
  if schema_definition.respond_to?(:attributes) and not schema_definition.attributes.nil?
        
    subelems ||= []
    schema_definition.attributes.each do |qname, type|
      subelems << TreeElement.new("xmlattr_#{qname.name}", nil,false,type)
    end
  end
  
  subelems.sort! unless subelems.nil?
  
  return TreeElement.new(name, subelems,is_array,mapped_class)
end

#generate_and_requireObject



241
242
243
244
# File 'lib/mappum/xml_transform.rb', line 241

def generate_and_require
  generate_classes
  require_all
end

#generate_classes(schema_path = nil, module_path = nil) ⇒ Object

Generate classes from xsd files in shema_path (defaults to schema). Files containt in subdirectories are generated to modules where module name is optaind from folder name. Generated classes are saved to basedir (defaults to tmp)



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/mappum/xml_transform.rb', line 253

def generate_classes(schema_path=nil, module_path=nil)
  class_dir = @basedir
  modname = module_path
  unless module_path.nil?
    class_dir = File.join(class_dir, module_path) 
    modname = modname.gsub(File::SEPARATOR, "::")
    modname = modname.gsub(/^[a-z]|\s+[a-z]|\:\:+[a-z]/) { |a| a.upcase }
  end
  
  Dir.mkdir(class_dir) unless File.exist? class_dir
  

  
  schema_path ||= @schema_path 
  
  Dir.foreach(schema_path) do |file_name|
    full_name = File.join(schema_path,file_name)
    #when file is a directory
    #generate classes in module (recursive)
    if File.directory?(full_name) and not file_name[0..0] == "." 
      #make directory for future class files
      module_pth = file_name
      module_pth = File.join(module_path, module_pth) unless module_path.nil?
       
      generate_classes(full_name, module_pth)
    # for XSD files generate classes using XSD2Ruby
    elsif /.*.xsd/ =~ file_name
      run_xsd2ruby(full_name, file_name, module_path, modname)
      @mapper_scripts <<  class_dir + File::SEPARATOR + file_name[0..-5] + "_mapper.rb"
    end
  end
end

#require_allObject



245
246
247
248
# File 'lib/mappum/xml_transform.rb', line 245

def require_all
  require_schemas
  require_maps
end

#require_mapsObject



291
292
293
294
295
296
297
298
299
300
# File 'lib/mappum/xml_transform.rb', line 291

def require_maps
  $:.unshift @map_dir
  Dir.foreach(@map_dir) do |file_name|
    if /.*.rb/ =~ file_name
      Mappum.source = File.join(@map_dir, file_name)
      require File.join(@map_dir, file_name)
      Mappum.source = nil
    end
  end
end

#require_schemasObject



285
286
287
288
289
290
# File 'lib/mappum/xml_transform.rb', line 285

def require_schemas
  $:.unshift @basedir
  @mapper_scripts.each do |script|
    require script
  end
end