Module: Genio::Helper::Java

Includes:
Base, Util::NamespaceHelper
Defined in:
lib/genio/helper/java.rb

Constant Summary collapse

KeyWords =

Key/Reserved Words

[
"abstract",
"continue",
"for",
"new",
"switch",
"assert",
"default",
"goto",
"package",
"synchronized",
"boolean",
"do",
"if",
"private",
"this",
"break",
"double",
"implements",
"protected",
"throw",
"byte",
"else",
"import",
"public",
"throws",
"case",
"enum",
"instanceof",
"return",
"transient",
"catch",
"extends",
"int",
"short",
"try",
"char",
"final",
"interface",
"static",
"void",
"class",
"finally",
"long",
"strictfp**",
"volatile",
"const",
"float",
"native",
"super",
"while"
]
KeywordsSubstitute =

Keyword substitute hash

{

}
ServiceImportREST =

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

[
"com.paypal.core.rest.PayPalRESTException",
"com.paypal.core.rest.PayPalResource",
"com.paypal.core.rest.HttpMethod",
"com.paypal.core.rest.PayPalRESTException",
"com.paypal.core.rest.RESTUtil",
"com.paypal.core.rest.JSONFormatter",
"com.paypal.core.rest.APIContext",
"com.paypal.core.Constants",
"com.paypal.core.SDKVersion",
"com.paypal.sdk.info.SDKVersionImpl",
"java.io.File",
"java.io.InputStream",
"java.util.Properties",
"java.util.Map",
"java.util.HashMap"
]
ServiceImportWSDL =

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

[
"java.io.*",
"java.util.Map",
"java.util.HashMap",
"java.util.Properties",
"com.paypal.core.BaseService",
"com.paypal.exception.*",
"com.paypal.sdk.exceptions.*",
"com.paypal.core.APICallPreHandler",
"com.paypal.core.DefaultSOAPAPICallHandler",
"com.paypal.core.DefaultSOAPAPICallHandler.XmlNamespaceProvider",
"org.w3c.dom.Node",
"org.xml.sax.SAXException",
"org.xml.sax.InputSource",
"javax.xml.parsers.ParserConfigurationException",
"javax.xml.parsers.DocumentBuilderFactory",
"javax.xml.xpath.XPathConstants",
"javax.xml.xpath.XPathExpressionException",
"javax.xml.xpath.XPathFactory",
"com.paypal.core.DefaultSOAPAPICallHandler",
"com.paypal.core.BaseAPIContext"
]
StubImportWSDL =

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

[
"javax.xml.xpath.XPath",
"javax.xml.xpath.XPathConstants",
"javax.xml.xpath.XPathExpressionException",
"javax.xml.xpath.XPathFactory",
"org.w3c.dom.Node",
"org.w3c.dom.NodeList",
"com.paypal.core.SDKUtil"
]
BasicTypes =

Spec Types to Java Types conversion map

{ "int" => "Integer", "integer" => "Integer", "positiveInteger" => "Integer", "nonNegativeInteger" => "Integer", "long" => "Long", "double" => "Double", "decimal" => "Double", "float" => "Float", "boolean" => "Boolean", "string" => "String", "dateTime" => "String", "date" => "String", "number" => "Number", "object" => "Object", "token" => "String", "duration" => "String", "anyURI" => "String", "date_time" => "String", "base64Binary" => "String", "time" => "String" }

Instance Method Summary collapse

Methods included from Util::NamespaceHelper

#capitalize_package, #convert_ns_to_package, #get_package_folder, #get_slashed_package_name, #is_urn_ns, #lowercase_package, #remove_tld_in_package

Methods included from Base

#check_host_id, #comment_wrap, #contains_attribute, #header_types, #is_complex_type, #is_enum_type, #is_static_method, #request_types, #should_qualify_name

Instance Method Details

#find_basic_type(key) ⇒ Object

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



148
149
150
# 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) ⇒ Object

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



336
337
338
339
340
341
342
343
344
345
346
# 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) ⇒ Object

Generate method formal parameters for REST API calls



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# 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) ⇒ Object

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



350
351
352
353
354
355
356
357
358
359
360
# 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) ⇒ Object

Returns the expression to set for payLoad in the API call



363
364
365
366
367
368
369
370
371
372
373
# 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) ⇒ Object

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



274
275
276
277
278
279
280
281
282
# 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) ⇒ Object

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



390
391
392
393
394
395
396
397
398
399
400
# 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) ⇒ Object

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



405
406
407
408
409
# 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) ⇒ Object

Returns the imports for the Class



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# 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) ⇒ Object

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



377
378
379
380
381
382
383
384
385
386
# 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) ⇒ Object

Returns the corresponding basic data type in Java



154
155
156
# File 'lib/genio/helper/java.rb', line 154

def only_basic_type(key)
  BasicTypes[key.camelcase(:lower)]
end

#service_imports(schema, service, package) ⇒ Object

Generate imports for WSDL Service class



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# 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) ⇒ Object

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



291
292
293
# 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) ⇒ Object

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



308
309
310
# File 'lib/genio/helper/java.rb', line 308

def validate_enum_name(name)
  name.gsub(/[-\s]/, "_").sub(/^\d/, '_\0')
end

#validate_method_name(name) ⇒ Object

Prepends do to method names that are keywords



313
314
315
# 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) ⇒ Object

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



286
287
288
# File 'lib/genio/helper/java.rb', line 286

def validate_path(path)
  path.gsub(/\{([^}]*)\}/){|match| "\{#{validate_property_name($1)}\}" }
end

#validate_property_name(name) ⇒ Object

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



298
299
300
301
302
303
304
# 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