module Genio::Helper::Java

Constants

BasicTypes

Spec Types to Java Types conversion map

KeyWords

Key/Reserved Words

KeywordsSubstitute

Keyword substitute hash

ServiceImportREST

Static resource imports Resource class which have REST operation enabled depend on these core classes

ServiceImportWSDL

Static resource imports for WSDL service class; they depend on these core classes

StubImportWSDL

Static resource imports for WSDL stub classes; they depend on these core classes

Public Instance Methods

find_basic_type(key) click to toggle source

Returns the type of a member If the passed in parameter is one of Basic types return the corresponding BasicType, else the parameter is returned unmodified

# File lib/genio/helper/java.rb, line 148
def find_basic_type(key)
  only_basic_type(key) || key
end
form_cxf_args(classname, property, name) click to toggle source

Generate method formal parameters for CXF interface The argument type is appended with annotations

# File lib/genio/helper/java.rb, line 336
def form_cxf_args(classname, property, name)
  arguments = {}
  arguments["context"] = "@Context final MessageContext"
  property.parameters.each do |name, parameter|
    arguments[validate_property_name(name)] = "@#{parameter.location.capitalize}Param(\"#{name}\") String"
  end if property.parameters
  if property.request
    arguments[property.request.camelcase(:lower)] = property.request
  end
  arguments
end
form_rest_api_args(classname, property, name) click to toggle source

Generate method formal parameters for REST API calls

# File lib/genio/helper/java.rb, line 318
def form_rest_api_args(classname, property, name)
  arguments = {}
  property.path.scan(/{([^}]*)}/).each do |name, etc|
    if is_static_method(property) or validate_class_name(name) !~ /^#{classname}/i
      arguments[validate_property_name(name)] = "String"
    end
  end
  if property.request and property.request != classname
    arguments[validate_property_name(property.request)] = property.request
  end
  if property.type == 'GET' or property.type == 'HEAD' or property.type == 'DELETE'
    arguments["queryParameters"] = "Map<String, String>"
  end
  arguments
end
generate_format_hash(classname, property, resourcePath) click to toggle source

Generates a map of parameters for placeholders used to process path URI

# File lib/genio/helper/java.rb, line 350
def generate_format_hash(classname, property, resourcePath)
  map = {}
  resourcePath.scan(/\{([^}]*)\}/) { |name, etc|
    if (name.match(/^#{classname}.*/i) and !is_static_method(property))
      map[name] = 'this.getId()'
    else
      map[name] = validate_property_name(name)
    end
  }
  map
end
get_payload(classname, property) click to toggle source

Returns the expression to set for payLoad in the API call

# File lib/genio/helper/java.rb, line 363
def get_payload(classname, property)
  payLoad = '""';
  if !is_static_method(property)
    if property.request == classname
      payLoad = "this.toJSON()"
    elsif property.request
      payLoad = validate_property_name(property.request) + ".toJSON()"
    end
  end
  payLoad
end
get_property_class(property, classname = nil) click to toggle source

Returns the property type name to be used as the type name in enclosing Class

# File lib/genio/helper/java.rb, line 274
def get_property_class(property, classname = nil)
  type = find_basic_type(property.type)

  # If type is Self (as per Spec) treat is as HostType
  # classname is always in camelcase
  type = classname if type == "self"
  type = "List<#{type}>" if property.array
  type
end
get_rootname_serialization(data_type, schema) click to toggle source

Returns the name used during serialization for types that extend Serializer interface

# File lib/genio/helper/java.rb, line 390
def get_rootname_serialization(data_type, schema)
  schema.services.each do |service_name, servicedef|
    servicedef.operations.each do |operation_name, oper_definition|
      if (data_type.name == oper_definition.request)
        return (oper_definition.request_property.package + ":" + oper_definition.request_property.name)
      elsif (data_type.name == oper_definition.header)
        return (oper_definition.header_property.package + ":" + oper_definition.header_property.name)
      end
    end
  end
end
get_wsdl_operation_arguments(operation_definition) click to toggle source

Returns a hash of arguments for wsdl operations including the request type and name hash is in the format of [name] = [type]

# File lib/genio/helper/java.rb, line 405
def get_wsdl_operation_arguments(operation_definition)
  argument_hash = {}
    argument_hash[validate_property_name(operation_definition.request_property.name)] = operation_definition.request
  argument_hash
end
imports(data_type, schema, package, classname, schema_format, operation_input = false) click to toggle source

Returns the imports for the Class

# File lib/genio/helper/java.rb, line 159
def imports(data_type, schema, package, classname, schema_format, operation_input = false)
  list = []

  # custom package provided during generation
  pkg = options[:namespace]
  pkg += "." if pkg.present?

  # mandatory imports
  list += ["com.paypal.core.rest.JSONFormatter"] if (schema_format == "rest")
  data_type.properties.each do |name, property|
    type = schema.data_types[property.type] || schema.enum_types[property.type]
    if (type)
      if (pkg.present?)
        list.push(pkg + property.type)
      else
        # TODO fix this when definition namespace fixes
        defpkg = convert_ns_to_package(type.package || package)
        list.push(defpkg + "." + property.type)
      end
    end
    list.push("java.util.List") if property.array
    list.push("java.util.ArrayList") if property.array
  end

  # Add references for members of parent datatype
  # flatten classes for wsdl
  if schema.instance_of? Genio::Parser::Format::Wsdl
    x_type = schema.data_types[data_type.extends]
    while x_type
      x_type.properties.each do |name, property|
        type = schema.data_types[property.type] || schema.enum_types[property.type]
        if (type)
          if (pkg.present?)
            list.push(pkg + property.type)
          else
            # TODO fix this when definition namespace fixes
            defpkg = convert_ns_to_package(type.package || package)
            list.push(defpkg + "." + property.type)
          end
        end
        list.push("java.util.List") if property.array
        list.push("java.util.ArrayList") if property.array
      end
      x_type = schema.data_types[x_type.extends]
    end
  end

  # Add reference for request and response type
  # of operations: Applies to REST services
  service = schema.services[classname]
  if service
    service.operations.each do |name, operation|
      if operation.response
        if (pkg.present?)
          list.push(pkg + validate_class_name(operation.response))
        else
          list.push(convert_ns_to_package(schema.data_types[operation.response].try(:package) || package) + "." + validate_class_name(operation.response))
        end
      end
    if operation.request
      if (pkg.present?)
        list.push(pkg + validate_class_name(operation.request))
      else
        list.push(convert_ns_to_package(schema.data_types[operation.request].try(:package) || package) + "." + validate_class_name(operation.request))
      end
    end
    end
  end

  list += ServiceImportREST if (schema.services[classname] && (schema_format == "rest"))
  list += StubImportWSDL if (schema_format == "soap")
  list += ["com.paypal.core.message.XMLMessageSerializer"] if (operation_input && (schema_format == "soap"))
  list.uniq.sort
end
is_operation_input(data_type, schema) click to toggle source

Returns true if data_type is in services operations request or header, else returns false

# File lib/genio/helper/java.rb, line 377
def is_operation_input(data_type, schema)
  schema.services.each do |service_name, servicedef|
    servicedef.operations.each do |operation_name, oper_definition|
      if (data_type.name == oper_definition.request || data_type.name == oper_definition.header)
        return true
      end
    end
  end
  return false
end
only_basic_type(key) click to toggle source

Returns the corresponding basic data type in Java

# File lib/genio/helper/java.rb, line 154
def only_basic_type(key)
  BasicTypes[key.camelcase(:lower)]
end
service_imports(schema, service, package) click to toggle source

Generate imports for WSDL Service class

# File lib/genio/helper/java.rb, line 235
def service_imports(schema, service, package)
  list = []

  # custom package provided during generation
  pkg = options[:namespace]
  pkg += "." if pkg.present?

  # import request and response of operations
  service.operations.each do |name, definition|
    if (definition.request_property)
      if (pkg.present?)
        list.push(pkg + validate_class_name(definition.request_property.type))
      else
        list.push(convert_ns_to_package(schema.data_types[validate_class_name(definition.request_property.type)].package || package) + "." + validate_class_name(definition.request_property.type))
      end
    end
    if (definition.response_property)
      if (pkg.present?)
        list.push(pkg + validate_class_name(definition.response_property.type))
      else
        list.push(convert_ns_to_package(schema.data_types[validate_class_name(definition.response_property.type)].package || package) + "." + validate_class_name(definition.response_property.type))
      end
    end
    if (definition.fault_property)
      if (pkg.present?)
        list.push(pkg + validate_class_name(definition.fault_property.type))
      else
        list.push(convert_ns_to_package(schema.data_types[validate_class_name(definition.fault_property.type)].package || package) + "." + validate_class_name(definition.fault_property.type))
      end
    end
  end

  # mandatory imports
  list += ServiceImportWSDL
  list.uniq.sort
end
validate_class_name(name) click to toggle source

Replaces ‘-’ with ‘_’ and CamelCase(s) them [Java]

# File lib/genio/helper/java.rb, line 291
def validate_class_name(name)
only_basic_type(name) || name.gsub(/-/, "_").camelcase
end
validate_enum_name(name) click to toggle source

Replaces ‘-’ and spaces with ‘_’ used for valid enum names [Java]

# File lib/genio/helper/java.rb, line 308
def validate_enum_name(name)
  name.gsub(/[-\s]/, "_").sub(/^\d/, '_\0')
end
validate_method_name(name) click to toggle source

Prepends do to method names that are keywords

# File lib/genio/helper/java.rb, line 313
def validate_method_name(name)
  KeyWords.include?(name) ? "do#{name.gsub(/-/, "_").camelcase}" : validate_property_name(name)
end
validate_path(path) click to toggle source

Replaces any “-” present in the path URI to valid “_” used while replacing placeholders with exact values

# File lib/genio/helper/java.rb, line 286
def validate_path(path)
  path.gsub(/\{([^}]*)\}/){|match| "\{#{validate_property_name($1)}\}" }
end
validate_property_name(name) click to toggle source

Replaces ‘-’ with ‘_’ and camelCase(s) them used for valid property names [Java] replaces keywords with substitutes form KeywordsSubstitute

# File lib/genio/helper/java.rb, line 298
def validate_property_name(name)
  valid_name = name.gsub(/-/, "_").camelcase(:lower)
  if KeyWords.include? valid_name
    valid_name = KeywordsSubstitute[valid_name]
  end
  valid_name
end