├── .gitignore ├── CONTRIBUTORS.md ├── Rakefile ├── lib ├── genio │ ├── version.rb │ ├── logging.rb │ ├── util │ │ ├── schema_helper.rb │ │ └── namespace_helper.rb │ ├── template.rb │ ├── helper │ │ ├── base.rb │ │ ├── php.rb │ │ ├── dot_net.rb │ │ └── java.rb │ └── tasks.rb └── genio.rb ├── bin └── genio ├── spec ├── spec_helper.rb ├── namespace_helper_spec.rb ├── php_spec.rb ├── java_spec.rb └── dotnet_spec.rb ├── Gemfile ├── templates ├── sdk.wsdlenum_dotnet.erb ├── sdk.rest_version_dotnet.erb ├── sdk.rest_version_java.erb ├── sdk.wsdlenum_java.erb ├── sdk.wsdlsoapserialize_php.erb ├── sdk.wsdl_php.erb ├── sdk.wsdlservice_php.erb ├── sdk.wsdl_dotnet.erb ├── sdk.wsdl_java.erb ├── sdk.rest_php.erb ├── sdk.wsdlsoapdeserialize_java.erb ├── sdk.wsdlservice_dotnet.erb ├── sdk.wsdlsoapdeserialize_dotnet.erb ├── sdk.wsdlservice_java.erb ├── sdk.wsdlsoapserialize_java.erb ├── sdk.wsdlsoapserialize_dotnet.erb ├── sdk.rest_dotnet.erb └── sdk.rest_java.erb ├── genio.gemspec ├── README.md └── LICENSE.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .ruby-version 6 | .yardoc 7 | Gemfile.lock 8 | InstalledFiles 9 | _yardoc 10 | coverage 11 | doc/ 12 | lib/bundler/man 13 | pkg 14 | rdoc 15 | spec/reports 16 | test/tmp 17 | test/version_tmp 18 | tmp 19 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | Contributors 2 | ============ 3 | 4 | These people have contributed to genio 5 | 6 | * Siddick Ebramsha 7 | * Kumaravel Jayakumar 8 | * Vidya Chandrasekaran 9 | * Latha Vairamani 10 | * Praveen Alavilli 11 | * Prasanna Annamalai 12 | * Ganesh Hegde 13 | * Tarini Kanta 14 | * Rajani Somaskandan 15 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require "bundler/gem_tasks" 18 | -------------------------------------------------------------------------------- /lib/genio/version.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | module Genio 18 | VERSION = "1.0.0" 19 | end 20 | -------------------------------------------------------------------------------- /bin/genio: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # 4 | # Copyright 2013 PayPal Inc. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # 19 | require "genio" 20 | 21 | Genio::Tasks.start(ARGV) 22 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require 'bundler/setup' 18 | Bundler.require :default 19 | 20 | RSpec.configure do |config| 21 | end 22 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | source 'https://rubygems.org' 18 | 19 | # Specify your gem's dependencies in sdk_generator.gemspec 20 | gemspec 21 | 22 | gem 'genio-parser', :git => 'https://github.com/paypal/genio-parser.git' 23 | -------------------------------------------------------------------------------- /lib/genio/logging.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require 'logger' 18 | 19 | module Genio 20 | class << self 21 | attr_accessor :logger 22 | end 23 | self.logger = Logger.new(STDERR) 24 | 25 | module Logging 26 | def logger 27 | Genio.logger 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /templates/sdk.wsdlenum_dotnet.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | using System; 20 | using System.ComponentModel; 21 | namespace <%= package %> 22 | { 23 | [Serializable] 24 | public enum <%= classname %> 25 | { 26 | 27 | <% definition["values"].each do |value| %> 28 | [Description("<%= value %>")]<%= validate_enum_name(value.upcase) %><% if value != definition["values"].last %>,<% end %> 29 | 30 | <% end %> 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /lib/genio.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require "genio/version" 18 | 19 | module Genio 20 | autoload :Template, "genio/template" 21 | autoload :Logging, "genio/logging" 22 | autoload :Tasks, "genio/tasks" 23 | 24 | module Helper 25 | autoload :Base, "genio/helper/base" 26 | autoload :PHP, "genio/helper/php" 27 | autoload :DotNet, "genio/helper/dot_net" 28 | autoload :Java, "genio/helper/java" 29 | end 30 | 31 | module Util 32 | autoload :NamespaceHelper, "genio/util/namespace_helper" 33 | autoload :SchemaHelper, "genio/util/schema_helper" 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /spec/namespace_helper_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require 'spec_helper' 18 | 19 | describe 'Genio::Util::NamespaceHelper' do 20 | before :all do 21 | @helper = Class.new do 22 | include Genio::Util::NamespaceHelper 23 | end.new 24 | end 25 | 26 | it "should validate namepsace is urn or uri" do 27 | @helper.is_urn_ns("https://api.paypal.com/payments").should be_false 28 | @helper.is_urn_ns("urn:ebay:apis:eBLBaseComponents").should be_true 29 | end 30 | 31 | it "should convert namespaces to packagename" do 32 | @helper.convert_ns_to_package("com.paypal.api").should eql "com.paypal.api" 33 | end 34 | 35 | 36 | end -------------------------------------------------------------------------------- /templates/sdk.rest_version_dotnet.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | using PayPal; 20 | 21 | namespace PayPal 22 | { 23 | public class SDKVersionImpl : SDKVersion 24 | { 25 | 26 | /// 27 | /// SDK ID used in User-Agent HTTP header 28 | /// 29 | private const string SdkId = ""; 30 | 31 | /// 32 | /// SDK Version used in User-Agent HTTP header 33 | /// 34 | private const string SdkVersion = ""; 35 | 36 | public string GetSDKId() 37 | { 38 | return SdkId; 39 | } 40 | 41 | public string GetSDKVersion() 42 | { 43 | return SdkVersion; 44 | } 45 | } 46 | 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /templates/sdk.rest_version_java.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | package com.paypal.sdk.info; 20 | 21 | import com.paypal.core.SDKVersion; 22 | 23 | /** 24 | * Implementation of SDKVersion 25 | */ 26 | public class SDKVersionImpl implements SDKVersion { 27 | 28 | /** 29 | * SDK ID used in User-Agent HTTP header 30 | */ 31 | private static final String SDK_ID = ""; 32 | 33 | /** 34 | * SDK Version used in User-Agent HTTP header 35 | */ 36 | private static final String SDK_VERSION = ""; 37 | 38 | public String getSDKId() { 39 | return SDK_ID; 40 | } 41 | 42 | public String getSDKVersion() { 43 | return SDK_VERSION; 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /templates/sdk.wsdlenum_java.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | package <%= package %>; 20 | 21 | public enum <%= classname %> { 22 | 23 | <% definition["values"].each do |value| %> 24 | 25 | <%= validate_enum_name(value.upcase) %>("<%= value %>")<% if value != definition["values"].last %>,<% else %>;<% end %> 26 | <% end %> 27 | 28 | private String value; 29 | 30 | private <%= classname %> (String value) { 31 | this.value = value; 32 | } 33 | 34 | public String getValue(){ 35 | return value; 36 | } 37 | 38 | public static <%= classname %> fromValue(String v) { 39 | for (<%= classname %> c : values()) { 40 | if (c.value.equals(v)) { 41 | return c; 42 | } 43 | } 44 | throw new IllegalArgumentException(v); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /lib/genio/util/schema_helper.rb: -------------------------------------------------------------------------------- 1 | require 'thor/error' 2 | 3 | module Genio 4 | module Util 5 | module SchemaHelper 6 | 7 | # Decide on a parser from the passed in URI and 8 | # return the parser after loading the definition 9 | # from the URI 10 | def get_parser(options) 11 | if options[:wsdl] 12 | load_wsdl(options[:wsdl]) 13 | elsif options[:wadl] 14 | load_wadl(options[:wadl]) 15 | elsif options[:json_schema] 16 | load_json_schema(options[:json_schema]) 17 | elsif options[:schema] 18 | get_parser_with_uri(options[:schema]) 19 | else 20 | raise Thor::RequiredArgumentMissingError, "No value provided for required options '--json-schema' or '--wsdl' or '--wadl'" 21 | end 22 | end 23 | 24 | def get_parser_with_uri(uri) 25 | if (uri.end_with?('wadl')) 26 | load_wadl(uri) 27 | elsif (uri.end_with?('wsdl')) 28 | load_wsdl(uri) 29 | else 30 | load_json_schema(uri) 31 | end 32 | end 33 | 34 | def load_wsdl(path) 35 | schema = Parser::Format::Wsdl.new 36 | load_schema_uri(schema, path) 37 | schema 38 | end 39 | 40 | def load_wadl(path) 41 | schema = Parser::Format::Wadl.new 42 | load_schema_uri(schema, path) 43 | schema 44 | end 45 | 46 | def load_json_schema(path) 47 | schema = Parser::Format::JsonSchema.new 48 | load_schema_uri(schema, path) 49 | schema.fix_unknown_service 50 | schema 51 | end 52 | 53 | def load_schema_uri(parser, uri) 54 | uri.split(/,/).each do |path| 55 | parser.load(path) 56 | end 57 | end 58 | 59 | # Replaces '-' with '_' and CamelCase(s) 60 | def validate_file_name(name) 61 | name.gsub(/-/, "_").camelcase 62 | end 63 | end 64 | end 65 | end 66 | -------------------------------------------------------------------------------- /lib/genio/template.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require 'tilt' 18 | require "thor" 19 | 20 | module Genio 21 | module Template 22 | include Thor::Shell 23 | include Thor::Actions 24 | 25 | def render_values 26 | @render_values ||= [{}] 27 | end 28 | 29 | def render(template, values = {}) 30 | values = { :create_file => nil }.merge(values) 31 | values = render_values.last.merge(values) 32 | values[:scope] ||= generate_scope(values) 33 | render_values.push(values) 34 | content = parse_erb(template, values) 35 | render_values.pop 36 | create_file(values[:create_file], content) if values[:create_file] 37 | content 38 | end 39 | 40 | def generate_scope(values) 41 | Class.new do 42 | include values[:helper] if values[:helper] 43 | 44 | def initialize(klass) 45 | @klass = klass 46 | end 47 | 48 | def method_missing(name, *args) 49 | @klass.send(name, *args) 50 | end 51 | end.new(self) 52 | end 53 | 54 | def parse_erb(template, values) 55 | filename = File.absolute_path(template, template_path) 56 | template = Tilt::ErubisTemplate.new(Dir["#{filename}*"].first || template) 57 | template.render(values[:scope] || self, values) 58 | end 59 | 60 | def template_path 61 | @template_path ||= File.expand_path("../../../", __FILE__) 62 | end 63 | 64 | end 65 | end 66 | -------------------------------------------------------------------------------- /genio.gemspec: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | # coding: utf-8 18 | lib = File.expand_path('../lib', __FILE__) 19 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 20 | require 'genio/version' 21 | 22 | Gem::Specification.new do |spec| 23 | spec.name = "genio" 24 | spec.version = Genio::VERSION 25 | spec.authors = ["PayPal"] 26 | spec.email = ["DL-PP-Platform-Ruby-SDK@ebay.com"] 27 | 28 | spec.summary = %q{Genio is an extensible tool that can generate code to consume APIs in multiple programming languages based on API specification formats: JSON-Schema/WSDL/WADL} 29 | spec.description = %q{Genio is an extensible tool that can generate code to consume APIs in multiple programming languages based on API specification formats: JSON-Schema/WSDL/WADL} 30 | spec.homepage = "https://github.com/paypal/genio" 31 | spec.license = "Apache License, Version 2.0" 32 | 33 | spec.files = Dir["{lib,templates,bin}/**/*"] + [ "README.md", "LICENSE.txt" ] 34 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 35 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 36 | spec.require_paths = ["lib"] 37 | 38 | spec.add_dependency "genio-parser", "~> 1.0" 39 | spec.add_dependency "activesupport" 40 | spec.add_dependency "tilt" 41 | spec.add_dependency "erubis" 42 | spec.add_dependency "thor" 43 | # spec.add_dependency "json" 44 | 45 | spec.add_development_dependency "bundler", "~> 1.3" 46 | spec.add_development_dependency "rake" 47 | spec.add_development_dependency "rspec" 48 | end 49 | -------------------------------------------------------------------------------- /templates/sdk.wsdlsoapserialize_php.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | <% if (request_types(schema).include?(classname) or header_types(schema).include?(classname)) %> 20 | public function toXMLString() 21 | { 22 | $str = '<<%= get_rootname_serialization(data_type, schema) %>>'; 23 | <% if (should_qualify_name(schema.namespaces[data_type.package], schema)) %> 24 | <% prefix = schema.namespaces[data_type.package] + ':' %> 25 | <% else %> 26 | <% prefix = '' %> 27 | <% end %> 28 | <% type = data_type %> 29 | <% while type %> 30 | <% type.properties.each do |name, property| %> 31 | if($this-><%= property.name %> != NULL) 32 | { 33 | <% if is_complex_type(property.type, schema) %> 34 | <% if property.max == 'unbounded' %> 35 | foreach($this-><%= property.name %> as $item) 36 | { 37 | $str .= '<<%= prefix %><%= property.name %>>'; 38 | $str .= $item->toXMLString(); 39 | $str .= '<%= property.name %>>'; 40 | } 41 | <% else %> 42 | $str .= '<<%= prefix %><%= property.name %>>'; 43 | $str .= $this-><%= property.name %>->toXMLString(); 44 | $str .= '<%= property.name %>>'; 45 | <% end %> 46 | <% else %> 47 | <% if property.max == 'unbounded' %> 48 | foreach($this-><%= property.name %> as $item) 49 | { 50 | $str .= '<<%= prefix %><%= property.name %>>'; 51 | $str .= $item; 52 | $str .= '<%= property.name %>>'; 53 | } 54 | <% else %> 55 | $str .= '<<%= prefix %><%= property.name %>>'; 56 | $str .= $this-><%= property.name %>; 57 | $str .= '<%= property.name %>>'; 58 | <% end %> 59 | <% end %> 60 | } 61 | <% end %> 62 | <% type = schema.data_types[type.extends] %> 63 | <% end %> 64 | $str .= '>'; 65 | return $str; 66 | } 67 | <% end %> 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Genio is an easy to use code generation tool that can generate API client libraries in multiple programming languages. Genio comes with in-built support for multiple API specification formats - WSDLs, WADLs and the [Google discovery format](https://developers.google.com/discovery) but also allows you to plug in parsers for additional specification formats. It uses a simple templating system that allows easy customization of the generated code. 2 | 3 | 4 | ## Requirements 5 | 6 | * Ruby 1.9.3 or above 7 | 8 | ## Installation 9 | 10 | ```sh 11 | gem install genio 12 | ``` 13 | 14 | ## Usage 15 | 16 | With genio installed on your local machine, run 17 | 18 | ```sh 19 | genio --schema=path/to/specification [--output-path=output/directory] 20 | ``` 21 | 22 | With the --schema argument option, Genio will attempt to guess the specification type based on the file extension of the argument. If you follow non-standard naming convention for your files, please use the --wsdl / --wadl / --json-schema arguments instead. 23 | 24 | Once you have generated source files with genio, you can use them in your project in conjunction with PayPal's core SDK libraries. You can take a look at the sample [hello world projects](https://github.com/paypal/genio-sample/tree/master/hello-world) and read language specific instructions [here](https://github.com/paypal/genio/wiki/Using-genio). 25 | 26 | ## Supported languages 27 | 28 | Genio comes with templates for the following programming languages. Support for other languages to follow soon. 29 | 30 | * java 31 | * dotnet 32 | * php 33 | 34 | ## Supported formats 35 | 36 | * WADL (`--wadl=path/to/schema.wadl`) 37 | * Google discovery format (`--json-schema=path/to/schema.json`) 38 | * WSDL (`--wsdl=path/to/schema.wsdl`) 39 | 40 | ## Upcoming features 41 | 42 | We have plans to add the following soon 43 | 44 | * Automatic API reference generation from spec. 45 | * Support for JSON schema Version 4 specification. 46 | 47 | ## Documentation 48 | 49 | * [Genio internals](https://github.com/paypal/genio/wiki/Genio-internals) 50 | 51 | ## Contributions and issues 52 | 53 | We will be happy to accept contributions by way of templates for additional languages and parsers for other API specification formats. Please submit a pull request if you would like to contribute. 54 | 55 | If you have a feature ask or an issue to report, please file an [issue](https://github.com/paypal/genio/issues/new) on github. 56 | -------------------------------------------------------------------------------- /templates/sdk.wsdl_php.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | ; 21 | <% imports(data_type, schema, package, classname, "soap").each do |import| %> 22 | use <%= import %>; 23 | <% end %> 24 | /** 25 | <%= comment_wrap(data_type.description, 60, ' * ') %> 26 | */ 27 | class <%= valid_class_name(classname) %> extends <% if data_type.fault %>PPXmlFaultMessage<% else %>PPXmlMessage<% end %> { 28 | <% const_args = {} %> 29 | <% type = data_type %> 30 | <% while type %> 31 | <% type.properties.each do |name, property| %> 32 | <% vname = to_underscore(name) %> 33 | <% vtype = get_php_type(property, schema, classname) %> 34 | <% const_args[vname] = property.type if property.required %> 35 | /** 36 | <%= comment_wrap(property.description, 60, "\t * ") %> 37 | <% if property.array %> 38 | * @array 39 | <% end %> 40 | * @access public 41 | <% if (property.value != '1' and !property.attribute) and !should_qualify_name(property.package, schema) %> 42 | * @namespace 43 | <% elsif property.package.present? %> 44 | * @namespace <%= property.package %> 45 | <% end %> 46 | <% if property.attribute %> 47 | * @attribute 48 | <% end %> 49 | <% if property.value == '1' %> 50 | * @value 51 | <% end %> 52 | * @var <%= "#{validated_package(schema.data_types[vtype].package)}\\" if schema.data_types[vtype] %><%= vtype %> 53 | <% if vname != property.name %> 54 | * @name <%= property.name %> 55 | <% end %> 56 | */ 57 | public $<%= vname %>; 58 | <% end %> 59 | <% type = schema.data_types[type.extends] %> 60 | <% end %> 61 | 62 | <% if const_args.size > 0 %> 63 | 64 | /** 65 | * Constructor with mandatory properties 66 | * 67 | <% const_args.map.each do |name, type| %> 68 | * @param <%= type %> $<%= name %> 69 | <% end %> 70 | */ 71 | public function __construct(<%= const_args.map{|name, type| "$#{name} = NULL"}.join(", ") %>) { 72 | <% const_args.each do |name, type| %> 73 | $this-><%= name %> = $<%= name %>; 74 | <% end %> 75 | } 76 | <% end %> 77 | 78 | <%= render "templates/sdk.wsdlsoapserialize_php.erb", :classname => classname, :schema => schema, :data_type => data_type %> 79 | } 80 | -------------------------------------------------------------------------------- /templates/sdk.wsdlservice_php.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | ; 21 | 22 | <% service_imports(schema, service, package).each do |import| %> 23 | use <%= import %>; 24 | <% end %> 25 | /** 26 | * AUTO GENERATED service wrapper class 27 | */ 28 | class <%= classname %> extends PPBaseService { 29 | 30 | private static $SERVICE_NAME = "<%= classname %>"; 31 | private static $SERVICE_VERSION = ""; 32 | private static $SDK_NAME = ""; 33 | private static $SDK_VERSION = ""; 34 | 35 | public function __construct() { 36 | parent::__construct('<%= classname %>', 'SOAP'); 37 | } 38 | <% service.operations.each do |name, definition| %> 39 | <% methodname = validate_method_name(name) %> 40 | 41 | /** 42 | * <%= definition.description %> 43 | * @param <%= get_namespace(schema.data_types[definition.request].package) %>\<%= definition.request %> $<%= valid_property_name(definition.request) %> 44 | * @param PayPal\Common\PPApiContext $apiContext 45 | * @return <%= get_namespace(schema.data_types[definition.response].package) %>\<%= definition.response %> 46 | <% if definition.fault %> 47 | * @throws <%= get_namespace(schema.data_types[definition.fault].package) %>\<%= definition.fault %> 48 | <% end %> 49 | */ 50 | <% argument_array = get_wsdl_operation_arguments(definition) %> 51 | public function <%= methodname %>(<% if argument_array.size > 0 %> <%= argument_array.join(', ') %><% end %>, $apiContext) { 52 | 53 | $apiContext->addHttpHeader("SOAPAction", '"<%= definition.soapAction || '""' %>"'); 54 | 55 | $handlers = array( 56 | new \PayPal\Handler\GenericSoapHandler($this->xmlNamespacePrefixProvider()), 57 | ); 58 | $resp = $this->call('<%= classname %>', '<%= methodname %>', <% if argument_array.size > 0 %> <%= argument_array.join(', ') %><% end %>, $apiContext, $handlers); 59 | 60 | $ret = new <%= definition.response %>(); 61 | <% if definition.fault %> 62 | try { 63 | $ret->init(PPUtils::xmlToArray($resp)); 64 | } catch (PPTransformerException $ex) { 65 | $fault = new <%= definition.fault%>(); 66 | $fault->init(PPUtils::xmlToArray($resp)); 67 | throw $fault; 68 | } 69 | <% else %> 70 | $ret->init(PPUtils::xmlToArray($resp)); 71 | <% end %> 72 | return $ret; 73 | } 74 | 75 | <% end %> 76 | 77 | 78 | /** 79 | * fucntion with namespaces and corresponding prefixes used in SOAP request serialization 80 | */ 81 | public function xmlNamespacePrefixProvider(){ 82 | 83 | $namespace = ""; 84 | <% schema.namespaces.each do |namespace, prefix| %> 85 | <% if prefix != '' %> 86 | $namespace .= 'xmlns:<%= prefix %>="<%= namespace %>" '; 87 | <% end %> 88 | <% end %> 89 | 90 | return $namespace; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /templates/sdk.wsdl_dotnet.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | <% operation_input = is_operation_input(data_type, schema) %> 20 | <% imports(data_type, schema, package, classname, "soap", operation_input).each do |import| %> 21 | using <%= import %>; 22 | <% end %> 23 | namespace <%= package %> 24 | { 25 | public class <%= validate_class_name(classname) %><% if data_type.fault %> : System.Exception <% end %><% if operation_input %> : XMLMessageSerializer<% end %> 26 | { 27 | 28 | /// 29 | /// Namespace of <%= classname %> 30 | /// 31 | public const string Namespace = "<%= data_type.package %>"; 32 | 33 | /// 34 | /// Default US culture info used for decimal conversion 35 | /// 36 | private static CultureInfo DefaultCulture = new CultureInfo("en-US"); 37 | 38 | <% const_args = {} %> 39 | 40 | <% type = data_type %> 41 | <% while type %> 42 | <% type.properties.each do |name, property| %> 43 | <% vname = validate_property_name(name, true) %> 44 | <% const_args[vname] = get_property_class(property, classname) if property.required %> 45 | private <%= get_property_class(property, classname) %><%= '?' if (is_nullable_type(property) and !property.array) %> <%= vname %>Property<% if property.array %> = new <%= get_property_class(property, classname) %>()<% end %>; 46 | 47 | /// 48 | /// <%= property.description %> 49 | /// 50 | public <%= get_property_class(property, classname) %><%= '?' if (is_nullable_type(property) and !property.array) %> <%= vname %> 51 | { 52 | get 53 | { 54 | return this.<%= vname %>Property; 55 | } 56 | set 57 | { 58 | this.<%= vname %>Property = value; 59 | } 60 | } 61 | 62 | <% end %> 63 | <% type = schema.data_types[type.extends] %> 64 | <% end %> 65 | /// 66 | /// Default Constructor 67 | /// 68 | public <%= classname %>() 69 | { 70 | } 71 | 72 | <% if const_args.size > 0 %> 73 | /// 74 | /// Parameterized Constructor 75 | /// 76 | public <%= classname %>(<%= const_args.map{|name, type| "#{type} #{validate_property_as_argument(name)}"}.join(", ") %>) 77 | { 78 | <% const_args.each do |name, type| %> 79 | this.<%= name %> = <%= validate_property_as_argument(name) %>; 80 | <% end %> 81 | } 82 | <% end %> 83 | 84 | <% serialization_name = get_rootname_serialization(data_type, schema) if operation_input %> 85 | <%= render "templates/sdk.wsdlsoapserialize_dotnet.erb", :classname => classname, :operation_input => operation_input, :serialization_name => serialization_name, :schema => schema, :data_type => data_type %> 86 | 87 | <%= render "templates/sdk.wsdlsoapdeserialize_dotnet.erb", :classname => classname, :data_type => data_type %> 88 | 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /lib/genio/util/namespace_helper.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | module Genio 18 | module Util 19 | module NamespaceHelper 20 | 21 | # Checks the format of the namespace as uri or urn, 22 | # and converts the namespace to package name ('.' seperated) 23 | # eg: http://github.paypal.com/payments/sale is changed to 24 | # com.paypal.github.payments.sale 25 | # eg: urn:ebay:apis:eBLBaseComponents is changed to 26 | # urn.ebay.apis.eBLBaseComponents 27 | # any numbers occuring as a part of path in the uri 28 | # based namespaces is removed 29 | def convert_ns_to_package(ns) 30 | if is_urn_ns(ns) 31 | packagename = ns.gsub(/:/, "\.") 32 | else 33 | hostname = URI.parse(ns) 34 | packagename = hostname.host.sub(/^www./, "").split(".").reverse.join(".") 35 | packagename << hostname.path.to_s.gsub(/[\d-]+/, "").sub(/\/+$/,'').gsub(/-/, '_').gsub(/\/+/, ".") 36 | end 37 | packagename 38 | end 39 | 40 | # Strips a package name off any 41 | # Top Level Domains (TLD)s; com, co, org, gov, de, us, in 42 | # eg: com.paypal.github.payments.sale is converted to 43 | # paypal.github.payments.sale 44 | def remove_tld_in_package(packagename) 45 | packagename.sub(/^(com|co|org|gov|de|us|in)\./, "") 46 | end 47 | 48 | # Lowercases parts of a package name 49 | # eg: Paypal.Github.Payments.Sale is converted to 50 | # paypal.github.payments.sale 51 | def lowercase_package(packagename) 52 | packagename.gsub(/([^\.]+[\.]?)/){ |match| $1.camelcase(:lower) } 53 | end 54 | 55 | # Capitalizes parts of a package name 56 | # eg: paypal.github.payments.sale is converted to 57 | # Paypal.Github.Payments.Sale 58 | def capitalize_package(packagename) 59 | packagename.gsub(/([^\.]+[\.]?)/){ |match| $1.camelcase } 60 | end 61 | 62 | # Returns a folder path corresponding to the 63 | # packagename; setting capitalize_folder true returns 64 | # folder names that are captialized 65 | def get_package_folder(packagename, capitalize_folder = false) 66 | if (capitalize_folder) 67 | capitalize_package(packagename).gsub(/\.|\\/, '/') 68 | else 69 | packagename.gsub(/\.|\\/, '/') 70 | end 71 | end 72 | 73 | # Returns a namespace path with '\' corresponding to the 74 | # packagename; setting capitalizefolder true returns 75 | # names that are captialized 76 | def get_slashed_package_name(packagename, capitalizefolder = false) 77 | if (capitalizefolder) 78 | capitalize_package(packagename).gsub(/\./, '\\') 79 | else 80 | packagename.gsub(/\./, '\\') 81 | end 82 | end 83 | 84 | # Checks if the uri starts with protocol 85 | # schemes and returns true, else treats 86 | # the namespace as a Uniform Resource Name 87 | def is_urn_ns(ns) 88 | !ns.start_with?('http:','https:') 89 | end 90 | 91 | end 92 | end 93 | end 94 | -------------------------------------------------------------------------------- /templates/sdk.wsdl_java.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | package <%= package %>; 20 | <% operation_input = is_operation_input(data_type, schema) %> 21 | <% imports(data_type, schema, package, classname, "soap", operation_input).each do |import| %> 22 | import <%= import %>; 23 | <% end %> 24 | 25 | public class <%= validate_class_name(classname) %><% if data_type.fault %> extends RuntimeException <% end %><% if operation_input %> implements XMLMessageSerializer<% end %> { 26 | 27 | /** 28 | * Namespace of <%= classname %> 29 | */ 30 | public static final String nameSpace = "<%= data_type.package %>"; 31 | 32 | <% const_args = {} %> 33 | <% type = data_type %> 34 | <% while type %> 35 | <% type.properties.each do |name, property| %> 36 | <% vname = validate_property_name(name) %> 37 | <% const_args[vname] = get_property_class(property, classname) if property.required %> 38 | /** 39 | * <%= property.description %> 40 | */ 41 | private <%= get_property_class(property, classname) %> <%= vname %><% if property.array %> = new ArrayList<<%= validate_class_name(find_basic_type(property.type)) %>>()<% end %>; 42 | 43 | <% end %> 44 | <% type = schema.data_types[type.extends] %> 45 | <% end %> 46 | /** 47 | * Default Constructor 48 | */ 49 | public <%= classname %>() { 50 | } 51 | 52 | <% if const_args.size > 0 %> 53 | /** 54 | * Parameterized Constructor 55 | */ 56 | public <%= classname %>(<%= const_args.map{|name, type| "#{type} #{name}"}.join(", ") %>) { 57 | <% const_args.each do |name, type| %> 58 | this.<%= name %> = <%= name %>; 59 | <% end %> 60 | } 61 | <% end %> 62 | 63 | <% data_type.properties.each do |name, property| %> 64 | <% 65 | vname = validate_property_name(name) 66 | methodname = validate_class_name(name) 67 | %> 68 | 69 | /** 70 | * Setter for <%= vname %> 71 | */ 72 | public <%= classname %> set<%= methodname %>(<%= get_property_class(property, classname) %> <%= vname %>) { 73 | this.<%= vname%> = <%= vname %>; 74 | return this; 75 | } 76 | 77 | /** 78 | * Getter for <%= vname %> 79 | */ 80 | public <%= get_property_class(property, classname) %> get<%= methodname %>() { 81 | return this.<%= vname%>; 82 | } 83 | 84 | <% end %> 85 | <% if data_type.extends %> 86 | <% extended_data_type = schema.data_types[data_type.extends] %> 87 | <% extended_data_type.properties.each do |name, property| %> 88 | <% 89 | vname = validate_property_name(name) 90 | methodname = validate_class_name(name) 91 | %> 92 | 93 | /** 94 | * Setter for <%= vname %> 95 | */ 96 | public <%= classname %> set<%= methodname %>(<%= get_property_class(property, classname) %> <%= vname %>) { 97 | this.<%= vname%> = <%= vname %>; 98 | return this; 99 | } 100 | 101 | /** 102 | * Getter for <%= vname %> 103 | */ 104 | public <%= get_property_class(property, classname) %> get<%= methodname %>() { 105 | return this.<%= vname%>; 106 | } 107 | 108 | <% end %> 109 | <% end %> 110 | <% serialization_name = get_rootname_serialization(data_type, schema) if operation_input %> 111 | <%= render "templates/sdk.wsdlsoapserialize_java.erb", :classname => classname, :operation_input => operation_input, :serialization_name => serialization_name, :schema => schema, :data_type => data_type %> 112 | 113 | <%= render "templates/sdk.wsdlsoapdeserialize_java.erb", :classname => classname, :data_type => data_type %> 114 | 115 | } 116 | -------------------------------------------------------------------------------- /lib/genio/helper/base.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require 'active_support/all' 18 | 19 | module Genio 20 | module Helper 21 | module Base 22 | 23 | # Checks for static modifier, currently all Get methods 24 | # are considered static 25 | def is_static_method(property) 26 | property.type == "GET" 27 | end 28 | 29 | # Returns true if this.getId() should be include in the 30 | # API calls - Applies to non-static methods which have a 31 | # path variable which is the host class 32 | # e.g /v1/users/{user} or /v1/users/{user-id} 33 | def check_host_id(classname, property) 34 | !is_static_method(property) && property.path.gsub(/-/, "").gsub(/_/, "") =~ /\{(#{classname}[^\}]*)\}/i 35 | end 36 | 37 | # Returns true if data_type is a input type used in 38 | # a service operation or header, false otherwise 39 | def is_operation_input(data_type, schema) 40 | schema.services.each do |service_name, servicedef| 41 | servicedef.operations.each do |operation_name, oper_definition| 42 | if (data_type.name == oper_definition.request || data_type.name == oper_definition.header) 43 | return true 44 | end 45 | end 46 | end 47 | return false 48 | end 49 | 50 | # Determine if type_name is a complex type 51 | # returns true if its complex, false otherwise 52 | def is_complex_type(type_name, schema) 53 | return true if schema.data_types[type_name] 54 | return false 55 | end 56 | 57 | # Determine if type_name is a enum type 58 | # returns true if its an enum, false otherwise 59 | def is_enum_type(type_name, schema) 60 | return true if schema.enum_types[type_name] 61 | return false 62 | end 63 | 64 | # Retruns true if the data_type has a attribute member 65 | def contains_attribute(data_type) 66 | data_type.properties.each do |name, definition| 67 | return true if definition.attribute 68 | end 69 | return false 70 | end 71 | 72 | # Determines if XML elements should be qualified 73 | # using prefix; reading elementformdefault attributes 74 | # from schemas 75 | def should_qualify_name(package, schema) 76 | return true if package.blank? 77 | namespace = schema.namespaces.find{|ns, prefix| prefix == package} 78 | namespace and schema.element_form_defaults[namespace.first] == "qualified" 79 | end 80 | 81 | # Word wraps comment 82 | def comment_wrap(text, line_width, prefix='') 83 | return prefix if ( (!text.kind_of? String) || (text.strip.length == 0) ) 84 | text.gsub(/(.{1,#{line_width}})(\s+|$)/, prefix + "\\1\n").gsub(/\n$/, "") 85 | end 86 | 87 | # Returns list of all top-level input types 88 | # defined in this service 89 | def request_types(schema) 90 | @request_types || schema.services.values.map{|service| service.operations.values.map{|opt| opt.request } }.flatten 91 | end 92 | 93 | # Returns list of all header types 94 | # defined in this service 95 | def header_types(schema) 96 | @request_types || schema.services.values.map{|service| service.operations.values.map{|opt| opt.header } }.flatten 97 | end 98 | 99 | end 100 | end 101 | end 102 | -------------------------------------------------------------------------------- /templates/sdk.rest_php.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | 21 | namespace <%= package %>; 22 | 23 | <% imports(data_type, schema, package, classname, "rest").each do |import| %> 24 | use <%= import %>; 25 | <% end %> 26 | 27 | class <%= classname %> <%= data_type.extends ? "extends #{valid_class_name(data_type.extends)} " : "extends PPModel " %><%= "implements IResource " if service %>{ 28 | <% if service %> 29 | 30 | <% end %> 31 | <% data_type.properties.each do |name, property| %> 32 | <% vname = to_underscore(name) %> 33 | <% vtype = get_php_type(property, schema, classname) %> 34 | /** 35 | * <%= property.description %> 36 | * 37 | <% if property.array %> 38 | * @array 39 | <% end %> 40 | * @param <%= "#{get_namespace(schema.data_types[vtype].package || schema.endpoint )}\\" if schema.data_types[vtype] %><%= vtype %> $<%= name %> 41 | */ 42 | public function set<%= valid_class_name(name) %>(<%if property.array %>array <%end%>$<%= vname %>) { 43 | <% if vname != name%> 44 | $this->{"<%= name %>"} = $<%= vname %>; 45 | <% else %> 46 | $this-><%= vname %> = $<%= vname %>; 47 | <% end %> 48 | return $this; 49 | } 50 | 51 | /** 52 | * <%= property.description %> 53 | * 54 | * @return <%= "#{get_namespace(schema.data_types[vtype].package || schema.endpoint )}\\" if schema.data_types[vtype] %><%= vtype %> 55 | */ 56 | public function get<%= valid_class_name(name) %>() { 57 | <% if vname != name%> 58 | return $this->{"<%= name %>"}; 59 | <% else %> 60 | return $this-><%= vname %>; 61 | <% end %> 62 | } 63 | 64 | <% end %> 65 | <% if service %> 66 | <% service.operations.each do |name, property| %> 67 | <% static = is_static_method(property) %> 68 | <% hostId_check = check_host_id(classname, property) %> 69 | <% argument_hash = form_arguments(classname, property, name) %> 70 | <% object_array = get_object_array(classname, name, property, argument_hash) %> 71 | <% payload = get_payload(classname, property) %> 72 | /* 73 | * <%= property.description %> 74 | * 75 | <% argument_hash.each do |name, type| %> 76 | * @param <%= type %> $<%= name %> 77 | <% end %> 78 | <% if( property.parameters && allowed_params(property.parameters).size > 0 )%> 79 | * allowed queryparameters <%= allowed_params(property.parameters).map{|k,v| "'#{k}'" }.join(", ") %> 80 | <% end %> 81 | * @param PayPal\Rest\ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration, credentials, and additional headers. 82 | * @return <%= "#{get_namespace(schema.data_types[property.response].package || schema.endpoint )}\\" if schema.data_types[property.response] %><%= property.response || 'string' %> 83 | */ 84 | public<%= " static" if static %> function <%= validate_function_name(name) %>(<% if argument_hash.size > 0 %><%= argument_hash.map{|name, type| type == "array" ? "array $#{name}" : "$#{name}"}.join(", ") %>, <% end %>$apiContext) { 85 | <% if hostId_check %> 86 | if ($this->getId() == null) { 87 | throw new \InvalidArgumentException("Id cannot be null"); 88 | } 89 | <% end %> 90 | <% if argument_hash.size > 0 %> 91 | <% argument_hash.each do |name, type| %> 92 | <% if name != 'queryParameters'%> 93 | 94 | if ($<%= name %> == null<%= " || strlen($#{name}) == 0" if type == "string" %>) { 95 | throw new \InvalidArgumentException("<%= name%> cannot be null or empty"); 96 | } 97 | <% end %> 98 | <% end %> 99 | <% end %> 100 | $payload = <%= payload %>; 101 | $call = new PPRestCall($apiContext); 102 | $json = $call->execute(array('PayPal\Rest\RestHandler'), <%= process_path_with_placeholders(classname, name, property.path, hostId_check, argument_hash, property.parameters) %>, "<%= property.type %>", $payload); 103 | <% if property.request == classname %> 104 | $this->fromJson($json); 105 | return $this; 106 | <% elsif property.response %> 107 | <% if property.response == "string" %> 108 | return $json; 109 | <% else %> 110 | $ret = new <%= property.response %>(); 111 | $ret->fromJson($json); 112 | return $ret; 113 | <% end %> 114 | <% else %> 115 | return $json; 116 | <% end %> 117 | } 118 | <% end %> 119 | <% end %> 120 | } 121 | -------------------------------------------------------------------------------- /templates/sdk.wsdlsoapdeserialize_java.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | /** 20 | * Checks whether the node is empty space 21 | */ 22 | private boolean isWhitespaceNode(Node n) { 23 | if (n.getNodeType() == Node.TEXT_NODE) { 24 | String val = n.getNodeValue(); 25 | return val.trim().length() == 0; 26 | } else if (n.getNodeType() == Node.ELEMENT_NODE ) { 27 | return (n.getChildNodes().getLength() == 0); 28 | } else { 29 | return false; 30 | } 31 | } 32 | 33 | /** 34 | * Constructor using a Node parameter 35 | */ 36 | public <%= classname %> (Node node) throws XPathExpressionException { 37 | XPathFactory factory = XPathFactory.newInstance(); 38 | XPath xpath = factory.newXPath(); 39 | Node childNode = null; 40 | NodeList nodeList = null; 41 | <% type = data_type %> 42 | <% while type %> 43 | <% type.properties.each do |name , definition| %> 44 | <% vname = validate_property_name(name) %> 45 | <% if definition.array %> 46 | nodeList = (NodeList) xpath.evaluate("<%= name %>", node, XPathConstants.NODESET); 47 | if (nodeList != null && nodeList.getLength() > 0) { 48 | for (int i = 0; i < nodeList.getLength(); i++) { 49 | Node subNode = nodeList.item(i); 50 | <% if is_complex_type(definition.type, schema) || is_enum_type(definition.type, schema) %> 51 | <% if definition.enum %> 52 | String value = subNode.getTextContent(); 53 | this.<%= vname %>.add(<%= validate_class_name(definition.type) %>.fromValue(value)); 54 | <% else %> 55 | this.<%= vname %>.add(new <%= validate_class_name(definition.type) %>(subNode)); 56 | <% end %> 57 | <% else %> 58 | String value = subNode.getTextContent(); 59 | <% if find_basic_type(definition.type) == 'String' %> 60 | this.<%= vname %>.add(value); 61 | <% elsif find_basic_type(definition.type) == 'Integer' %> 62 | this.<%= vname %>.add(Integer.valueOf(value)); 63 | <% elsif find_basic_type(definition.type) == 'Boolean' %> 64 | this.<%= vname %>.add(Boolean.valueOf(value)); 65 | <% elsif find_basic_type(definition.type) == 'Double' %> 66 | this.<%= vname %>.add(Double.valueOf(value)); 67 | <% elsif find_basic_type(definition.type) == 'Float' %> 68 | this.<%= vname %>.add(Float.valueOf(value)); 69 | <% end %> 70 | <% end %> 71 | } 72 | } 73 | <% else %> 74 | <% if is_complex_type(definition.type, schema) || is_enum_type(definition.type, schema) %> 75 | <% if definition.enum %> 76 | <% if definition.attribute %> 77 | childNode = (Node) xpath.evaluate("@<%= name %>", node, XPathConstants.NODE); 78 | if (childNode != null) { 79 | this.<%= vname %> = <%= validate_class_name(definition.type) %>.fromValue(childNode.getNodeValue()); 80 | } 81 | <% else %> 82 | childNode = (Node) xpath.evaluate("<%= name %>", node, XPathConstants.NODE); 83 | if (childNode != null && !isWhitespaceNode(childNode)) { 84 | this.<%= vname %> = <%= validate_class_name(definition.type) %>.fromValue(childNode.getTextContent()); 85 | } 86 | <% end %> 87 | <% else %> 88 | childNode = (Node) xpath.evaluate("<%= name %>", node, XPathConstants.NODE); 89 | if (childNode != null && !isWhitespaceNode(childNode)) { 90 | this.<%= vname %> = new <%= validate_class_name(definition.type) %>(childNode); 91 | } 92 | <% end %> 93 | <% else %> 94 | <% if definition.value == '1' %> 95 | String value = node.getTextContent(); 96 | <% if find_basic_type(definition.type) == 'String' %> 97 | this.<%= vname %> = value; 98 | <% elsif find_basic_type(definition.type) == 'Integer' %> 99 | this.<%= vname %> = Integer.valueOf(value); 100 | <% elsif find_basic_type(definition.type) == 'Boolean' %> 101 | this.<%= vname %> = Boolean.valueOf(value); 102 | <% elsif find_basic_type(definition.type) == 'Double' %> 103 | this.<%= vname %> = Double.valueOf(value); 104 | <% elsif find_basic_type(definition.type) == 'Float' %> 105 | this.<%= vname %> = Float.valueOf(value); 106 | <% end %> 107 | <% else %> 108 | childNode = (Node) xpath.evaluate("<%= name %>", node, XPathConstants.NODE); 109 | if (childNode != null && !isWhitespaceNode(childNode)) { 110 | <% if find_basic_type(definition.type) == 'String' %> 111 | this.<%= vname %> = childNode.getTextContent(); 112 | <% elsif find_basic_type(definition.type) == 'Integer' %> 113 | this.<%= vname %> = Integer.valueOf(childNode.getTextContent()); 114 | <% elsif find_basic_type(definition.type) == 'Boolean' %> 115 | this.<%= vname %> = Boolean.valueOf(childNode.getTextContent()); 116 | <% elsif find_basic_type(definition.type) == 'Double' %> 117 | this.<%= vname %> = Double.valueOf(childNode.getTextContent()); 118 | <% elsif find_basic_type(definition.type) == 'Float' %> 119 | this.<%= vname %> = Float.valueOf(childNode.getTextContent()); 120 | <% end %> 121 | } 122 | <% end %> 123 | <% end %> 124 | <% end %> 125 | <% end %> 126 | <% type = schema.data_types[type.extends] %> 127 | <% end %> 128 | } 129 | -------------------------------------------------------------------------------- /templates/sdk.wsdlservice_dotnet.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | <% service_imports(schema, service, package).each do |import| %> 20 | using <%= import %>; 21 | <% end %> 22 | namespace <%= package %> 23 | { 24 | public class <%= validate_class_name(classname) %> : BasePayPalService 25 | { 26 | 27 | static <%= validate_class_name(classname) %>() 28 | { 29 | DefaultSOAPAPICallHandler.XMLNamespaceProvider = new XmlNamespacePrefixProvider(); 30 | } 31 | 32 | /// 33 | /// Service Version 34 | /// 35 | private const string ServiceVersion = ""; 36 | 37 | /// 38 | /// Service Name 39 | /// 40 | private const string ServiceName = "<%= classname %>"; 41 | 42 | /// 43 | /// SDK Name 44 | /// 45 | private const string SDKName = ""; 46 | 47 | /// 48 | /// SDK Version 49 | /// 50 | private const string SDKVersion = ""; 51 | 52 | /// 53 | /// Default constructor for loading configuration from *.Config file 54 | /// 55 | public <%= validate_class_name(classname) %>() : base() 56 | { 57 | 58 | } 59 | 60 | /// 61 | /// constructor for passing in a dynamic configuration object 62 | /// 63 | public <%= validate_class_name(classname) %>(Dictionary config) : base(config) 64 | { 65 | 66 | } 67 | 68 | <% service.operations.each do |name, definition| %> 69 | <% methodname = validate_method_name(name) %> 70 | <% argumentHash = get_wsdl_operation_arguments(definition) %> 71 | /// 72 | /// 73 | /// 74 | <% if argumentHash.size > 0 %> 75 | <% argumentHash.each do |name, type| %> 76 | /// <%= type %> request argument 77 | <% end %> 78 | <% end %> 79 | /// APIContext argument 80 | public <%= definition.response %> <%= methodname %>(<% if argumentHash.size > 0 %><%= argumentHash.map{|name, type| "#{type.camelcase} #{name}"}.join(", ") %><% end %>, APIContext apiContext) 81 | { 82 | if (apiContext == null) 83 | { 84 | throw new ArgumentNullException("APIContext is null"); 85 | } 86 | if (apiContext.HTTPHeaders == null) 87 | { 88 | apiContext.HTTPHeaders = new Dictionary(); 89 | } 90 | <% 91 | if definition.soapAction.blank? 92 | soapAction = "\"\"" 93 | else 94 | soapAction = definition.soapAction 95 | end 96 | %> 97 | apiContext.HTTPHeaders.Add("SOAPAction", <%= soapAction.inspect %>); 98 | DefaultSOAPAPICallHandler apiCallPreHandler = new DefaultSOAPAPICallHandler(<% if argumentHash.size > 0 %><%= argumentHash.map{|name, type| "#{name}"}.join(", ") %><% end %>, apiContext, this.config, "<%= name %>"); 99 | XmlDocument xmlDocument = new XmlDocument(); 100 | string response = Call(apiCallPreHandler); 101 | xmlDocument.LoadXml(response); 102 | <%= definition.response %> <%= validate_property_as_argument(definition.response) %> = null; 103 | XmlNode xmlNode = xmlDocument.SelectSingleNode("*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='<%= definition.response_property.name %>']"); 104 | if (xmlNode != null) 105 | { 106 | <%= validate_property_as_argument(definition.response) %> = new <%= definition.response %>(xmlNode); 107 | } 108 | <% if definition.fault %> 109 | else 110 | { 111 | xmlNode = xmlDocument.SelectSingleNode("*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='Fault']/*[local-name()='detail']/*[local-name()='<%= definition.fault %>']"); 112 | if (xmlNode != null) 113 | { 114 | <%= definition.fault_property.name %> faultMessage = new <%= definition.fault_property.name %>(xmlNode); 115 | throw faultMessage; 116 | } 117 | else 118 | { 119 | throw new Exception("Unable to parse response, response = " + response); 120 | } 121 | 122 | } 123 | <% end %> 124 | return <%= validate_property_as_argument(definition.response) %>; 125 | } 126 | 127 | 128 | <% end %> 129 | 130 | private class XmlNamespacePrefixProvider : DefaultSOAPAPICallHandler.XmlNamespaceProvider 131 | { 132 | 133 | /// 134 | /// Namespace Dictionary instance 135 | /// 136 | private Dictionary namespaceDictionary; 137 | 138 | public XmlNamespacePrefixProvider() 139 | { 140 | namespaceDictionary = new Dictionary(); 141 | <% schema.namespaces.each do |namespace, prefix| %> 142 | <% if prefix != '' %> 143 | namespaceDictionary.Add("<%= prefix %>", "<%= namespace %>"); 144 | <% end %> 145 | <% end %> 146 | } 147 | 148 | public Dictionary GetNamespaceDictionary() 149 | { 150 | return namespaceDictionary; 151 | } 152 | } 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /templates/sdk.wsdlsoapdeserialize_dotnet.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | /// 20 | /// Checks whether the node is empty space 21 | /// 22 | public static bool IsWhiteSpaceNode(XmlNode n) 23 | { 24 | if (n.NodeType == XmlNodeType.Text) 25 | { 26 | string val = n.InnerText; 27 | return (val.Trim().Length == 0); 28 | } 29 | else if (n.NodeType == XmlNodeType.Element) 30 | { 31 | return (n.ChildNodes.Count == 0); 32 | } 33 | else 34 | { 35 | return false; 36 | } 37 | } 38 | 39 | /// 40 | /// Constructor using XmlNode parameter 41 | /// 42 | public <%= classname %> (XmlNode xmlNode) 43 | { 44 | XmlNode ChildNode = null; 45 | XmlNodeList ChildNodeList = null; 46 | <% type = data_type %> 47 | <% while type %> 48 | <% type.properties.each do |name , definition| %> 49 | <% vname = validate_property_name(name, true) %> 50 | <% if definition.array %> 51 | ChildNodeList = xmlNode.SelectNodes("*[local-name() = '<%= name %>']"); 52 | if (ChildNodeList != null && ChildNodeList.Count > 0) 53 | { 54 | for(int i = 0; i < ChildNodeList.Count; i++) 55 | { 56 | <% if is_complex_type(definition.type, schema) || is_enum_type(definition.type, schema) %> 57 | <% if definition.enum %> 58 | string value = ChildNodeList[i].InnerText; 59 | this.<%= vname %>.Add((<%= validate_class_name(definition.type) %>)ReflectionEnumUtil.GetValue(value, typeof(<%= validate_class_name(definition.type) %>))); 60 | <% else %> 61 | XmlNode subNode = ChildNodeList.Item(i); 62 | this.<%= vname %>.Add(new <%= validate_class_name(definition.type) %>(subNode)); 63 | 64 | <% end %> 65 | <% else %> 66 | string value = ChildNodeList[i].InnerText; 67 | <% if find_basic_type(definition.type) == 'string' %> 68 | this.<%= vname %>.Add(value); 69 | <% elsif find_basic_type(definition.type) == 'int' %> 70 | this.<%= vname %>.Add(System.Convert.ToInt32(value)); 71 | <% elsif find_basic_type(definition.type) == 'bool' %> 72 | this.<%= vname %>.Add(System.Convert.ToBoolean(value)); 73 | <% elsif find_basic_type(definition.type) == 'double' %> 74 | this.<%= vname %>.add(System.Convert.ToDouble(value)); 75 | <% elsif find_basic_type(definition.type) == 'float' %> 76 | this.<%= vname %>.add(System.Convert.ToSingle(value)); 77 | <% end %> 78 | <% end %> 79 | } 80 | } 81 | <% else %> // array 82 | <% if is_complex_type(definition.type, schema) || is_enum_type(definition.type, schema) %> 83 | <% if definition.enum %> 84 | <% if definition.attribute %> 85 | ChildNode = xmlNode.SelectSingleNode("@*[local-name() = '<%= name %>']"); 86 | if (ChildNode != null) 87 | { 88 | this.<%= vname %> = (<%= validate_class_name(definition.type) %>)ReflectionEnumUtil.GetValue(ChildNode.Value, typeof(<%= validate_class_name(definition.type) %>)); 89 | } 90 | <% else %> // attribute 91 | ChildNode = xmlNode.SelectSingleNode("*[local-name() = '<%= name %>']"); 92 | if(ChildNode != null && !IsWhiteSpaceNode(ChildNode)) 93 | { 94 | this.<%= vname %> = (<%= validate_class_name(definition.type) %>)ReflectionEnumUtil.GetValue(ChildNode.InnerText,typeof(<%= validate_class_name(definition.type) %>)); 95 | } 96 | <% end %> // attribute 97 | <% else %> // non-enum 98 | ChildNode = xmlNode.SelectSingleNode("*[local-name() = '<%= name %>']"); 99 | if(ChildNode != null && !IsWhiteSpaceNode(ChildNode)) 100 | { 101 | this.<%= vname %> = new <%= validate_class_name(definition.type) %>(ChildNode); 102 | } 103 | <% end %> // enum 104 | <% else %> // complex 105 | <% if definition.value == '1' %> 106 | <% if find_basic_type(definition.type) == 'string' %> 107 | this.<%= vname %> = xmlNode.InnerText; 108 | <% elsif find_basic_type(definition.type) == 'int' %> 109 | this.<%= vname %> = System.Convert.ToInt32(xmlNode.InnerText); 110 | <% elsif find_basic_type(definition.type) == 'bool' %> 111 | this.<%= vname %> = System.Convert.ToBoolean(xmlNode.InnerText); 112 | <% elsif find_basic_type(definition.type) == 'double' %> 113 | this.<%= vname %> = System.Convert.ToDouble(xmlNode.InnerText); 114 | <% elsif find_basic_type(definition.type) == 'float' %> 115 | this.<%= vname %> = System.Convert.ToSingle(xmlNode.InnerText); 116 | <% end %> 117 | <% else %> // value 118 | ChildNode = xmlNode.SelectSingleNode("*[local-name() = '<%= name %>']"); 119 | if(ChildNode != null && !IsWhiteSpaceNode(ChildNode)) 120 | { 121 | <% if find_basic_type(definition.type) == 'string' %> 122 | this.<%= vname %> = ChildNode.InnerText; 123 | <% elsif find_basic_type(definition.type) == 'int' %> 124 | this.<%= vname %> = System.Convert.ToInt32(ChildNode.InnerText); 125 | <% elsif find_basic_type(definition.type) == 'bool' %> 126 | this.<%= vname %> = System.Convert.ToBoolean(ChildNode.InnerText); 127 | <% elsif find_basic_type(definition.type) == 'double' %> 128 | this.<%= vname %> = System.Convert.ToDouble(ChildNode.InnerText); 129 | <% elsif find_basic_type(definition.type) == 'float' %> 130 | this.<%= vname %> = System.Convert.ToSingle(ChildNode.InnerText); 131 | <% end %> 132 | } 133 | <% end %> // value 134 | <% end %> // complex 135 | <% end %> // array 136 | <% end %> 137 | <% type = schema.data_types[type.extends] %> 138 | <% end %> 139 | } 140 | -------------------------------------------------------------------------------- /templates/sdk.wsdlservice_java.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | package <%= package %>; 20 | 21 | <% service_imports(schema, service, package).each do |import| %> 22 | import <%= import %>; 23 | <% end %> 24 | 25 | public class <%= validate_class_name(classname) %> extends BaseService { 26 | 27 | static { 28 | DefaultSOAPAPICallHandler.setXmlNamespaceProvider(new XmlNamespacePrefixProvider()); 29 | } 30 | 31 | // Service Version 32 | public static final String SERVICE_VERSION = ""; 33 | 34 | // Service Name 35 | public static final String SERVICE_NAME = "<%= classname %>"; 36 | 37 | //SDK Name 38 | private static final String SDK_NAME = ""; 39 | 40 | //SDK Version 41 | private static final String SDK_VERSION = ""; 42 | 43 | /** 44 | * Default <%= validate_class_name(classname) %> Constructor. 45 | * Initializes the SDK system with the default configuration file named 46 | * 'sdk_config.properties' found in the class-path 47 | */ 48 | public <%= validate_class_name(classname) %>() { 49 | super.initializeToDefault(); 50 | } 51 | 52 | /** 53 | * <%= validate_class_name(classname) %> that uses the supplied 54 | * {@link Properties} to initialize the SDK system. For values that the 55 | * properties should holdconsult the documentation. 56 | * The initialization context is maintained only for 57 | * this instance of the class. Service level configuration. 58 | * 59 | * @param properties 60 | * {@link Properties} object 61 | */ 62 | public <%= validate_class_name(classname) %>(Properties properties) { 63 | super(properties); 64 | } 65 | 66 | /** 67 | * <%= validate_class_name(classname) %> that uses the supplied 68 | * {@link Map} to initialize the SDK system. For values that the 69 | * configurationMap should hold consult the documentation. 70 | * The initialization context is maintained only for 71 | * this instance of the class. Service level configuration. 72 | * 73 | * @param configurationMap 74 | * {@link Map} object 75 | */ 76 | public <%= validate_class_name(classname) %>(Map configurationMap) { 77 | super(configurationMap); 78 | } 79 | 80 | <% service.operations.each do |name, definition| %> 81 | <% methodname = validate_method_name(name) %> 82 | <% argumentHash = get_wsdl_operation_arguments(definition) %> 83 | public <%= definition.response %> <%= methodname %>(<% if argumentHash.size > 0 %><%= argumentHash.map{|name, type| "#{type.camelcase} #{name}"}.join(", ") %><% end %>, BaseAPIContext baseAPIContext) throws SAXException, IOException, ParserConfigurationException, InvalidResponseDataException, HttpErrorException, ClientActionRequiredException, InvalidCredentialException, MissingCredentialException, OAuthException, SSLConfigurationException, InterruptedException { 84 | if (baseAPIContext == null) { 85 | throw new IllegalArgumentException("BaseAPIContext is null"); 86 | } 87 | if (baseAPIContext.getHTTPHeaders() == null) { 88 | baseAPIContext.setHTTPHeaders(new HashMap()); 89 | } 90 | <% 91 | if definition.soapAction.blank? 92 | soapAction = "\"\"" 93 | else 94 | soapAction = definition.soapAction 95 | end 96 | %> 97 | baseAPIContext.getHTTPHeaders().put("SOAPAction", <%= soapAction.inspect %>); 98 | APICallPreHandler apiCallPreHandler = new DefaultSOAPAPICallHandler(<% if argumentHash.size > 0 %><%= argumentHash.map{|name, type| "#{name}"}.join(", ") %><% end %>, baseAPIContext, this.configurationMap, "<%= name %>"); 99 | String response = call(apiCallPreHandler); 100 | InputSource inStream = new InputSource(); 101 | inStream.setCharacterStream(new StringReader((String) response)); 102 | try { 103 | Node node = (Node) XPathFactory.newInstance().newXPath().evaluate("Envelope/Body/<%= definition.response_property.name %>", DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inStream), XPathConstants.NODE); 104 | return new <%= definition.response %>(node); 105 | } catch (XPathExpressionException exe) { 106 | <% if definition.fault %> 107 | try { 108 | inStream = new InputSource(); 109 | inStream.setCharacterStream(new StringReader((String) response)); 110 | Node node = (Node) XPathFactory.newInstance().newXPath().evaluate("Envelope/Body/Fault/detail/<%= definition.fault %>", DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inStream), XPathConstants.NODE); 111 | <%= definition.fault_property.name %> faultException = new <%= definition.fault_property.name %>(node); 112 | throw faultException; 113 | } catch (XPathExpressionException e) { 114 | throw new RuntimeException("Unable to parse response", e); 115 | } 116 | <% else %> 117 | throw new RuntimeException("Unable to parse response", exe); 118 | <% end %> 119 | } 120 | } 121 | 122 | <% end %> 123 | 124 | /** 125 | * Implementation of DefaultSOAPAPICallHandler.XmlNamespaceProvider 126 | */ 127 | private static class XmlNamespacePrefixProvider implements XmlNamespaceProvider { 128 | 129 | private Map namespaceMap; 130 | 131 | public XmlNamespacePrefixProvider() { 132 | namespaceMap = new HashMap(); 133 | <% schema.namespaces.each do |namespace, prefix| %> 134 | <% if prefix != '' %> 135 | namespaceMap.put("<%= prefix %>", "<%= namespace %>"); 136 | <% end %> 137 | <% end %> 138 | } 139 | 140 | public Map getNamespaceMap() { 141 | return namespaceMap; 142 | } 143 | } 144 | 145 | } 146 | -------------------------------------------------------------------------------- /templates/sdk.wsdlsoapserialize_java.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | <% if operation_input %> 20 | public String toXMLString() { 21 | return toXMLString("<%= serialization_name.split(':').first %>", "<%= serialization_name.split(':').last %>"); 22 | } 23 | <% end %> 24 | 25 | /** 26 | * Serialize the object to XML String form 27 | * @param prefix 28 | * Namespace prefix to use during serialization 29 | * @param name 30 | * Name used for serialization 31 | */ 32 | public String toXMLString(String prefix, String name) { 33 | StringBuilder sb = new StringBuilder(); 34 | if (name != null) { 35 | <% if contains_attribute(data_type) %> 36 | if (prefix != null) { 37 | sb.append("<").append(prefix + ":").append(name); 38 | } else { 39 | sb.append("<").append(name); 40 | } 41 | sb.append(getAttributeAsXml()); 42 | sb.append(">"); 43 | <% else %> 44 | if (prefix != null) { 45 | sb.append("<").append(prefix + ":").append(name).append(">"); 46 | } else { 47 | sb.append("<").append(name).append(">"); 48 | } 49 | <% end %> 50 | } 51 | <% type = data_type %> 52 | <% while type %> 53 | <% type.properties.each do |name , definition| %> 54 | <% if !definition.attribute %> 55 | <% vname = validate_property_name(name) %> 56 | if (this.<%= vname %> != null) { 57 | <% if definition.array %> 58 | for (int i=0; i < this.<%= vname %>.size(); i++) { 59 | <% if is_complex_type(definition.type, schema) || is_enum_type(definition.type, schema) %> 60 | <% if definition.enum %> 61 | sb.append("<"); 62 | <% if should_qualify_name(definition.package, schema) %> 63 | sb.append("<%= definition.package %>:"); 64 | <% end %> 65 | sb.append("<%= name %>>").append(SDKUtil.escapeInvalidXmlCharsRegex(this.<%= vname %>.get(i).getValue())).append(" 67 | sb.append("<%= definition.package %>:"); 68 | <% end %> 69 | sb.append("<%= name %>>"); 70 | <% else %> 71 | <% if should_qualify_name(definition.package, schema) %> 72 | sb.append(this.<%= vname %>.get(i).toXMLString("<%= definition.package %>","<%= name %>")); 73 | <% else %> 74 | sb.append(this.<%= vname %>.get(i).toXMLString(null,"<%= name %>")); 75 | <% end %> 76 | <% end %> 77 | <% else %> 78 | <% if definition.value == '1' %> 79 | sb.append(SDKUtil.escapeInvalidXmlCharsRegex(value.get(i))); 80 | <% else %> 81 | sb.append("<"); 82 | <% if should_qualify_name(definition.package, schema) %> 83 | sb.append("<%= definition.package %>:"); 84 | <% end %> 85 | sb.append("<%= name %>>").append(SDKUtil.escapeInvalidXmlCharsRegex(this.<%= vname %>.get(i))); 86 | sb.append(" 88 | sb.append("<%= definition.package %>:"); 89 | <% end %> 90 | sb.append("<%= name %>>"); 91 | <% end %> 92 | <% end %> 93 | } 94 | <% else %> 95 | <% if is_complex_type(definition.type, schema) || is_enum_type(definition.type, schema) %> 96 | <% if definition.enum %> 97 | sb.append("<"); 98 | <% if should_qualify_name(definition.package, schema) %> 99 | sb.append("<%= definition.package %>:"); 100 | <% end %> 101 | sb.append("<%= name %>>").append(SDKUtil.escapeInvalidXmlCharsRegex(this.<%= vname %>.getValue())).append(" 103 | sb.append("<%= definition.package %>:"); 104 | <% end %> 105 | sb.append("<%= name %>>"); 106 | <% else %> 107 | <% if should_qualify_name(definition.package, schema) %> 108 | sb.append(this.<%= vname %>.toXMLString("<%= definition.package %>","<%= name %>")); 109 | <% else %> 110 | sb.append(this.<%= vname %>.toXMLString(null, "<%= name %>")); 111 | <% end %> 112 | <% end %> 113 | <% else %> 114 | <% if definition.value == '1' %> 115 | sb.append(SDKUtil.escapeInvalidXmlCharsRegex(value)); 116 | <% else %> 117 | sb.append("<"); 118 | <% if should_qualify_name(definition.package, schema) %> 119 | sb.append("<%= definition.package %>:"); 120 | <% end %> 121 | sb.append("<%= name %>>"); 122 | sb.append(SDKUtil.escapeInvalidXmlCharsRegex(this.<%= vname %>)); 123 | sb.append(" 125 | sb.append("<%= definition.package %>:"); 126 | <% end %> 127 | sb.append("<%= name %>>"); 128 | <% end %> 129 | <% end %> 130 | <% end %> 131 | } 132 | <% end %> 133 | <% end %> 134 | <% type = schema.data_types[type.extends] %> 135 | <% end %> 136 | if (name != null) { 137 | if (prefix != null) { 138 | sb.append(""); 139 | } else { 140 | sb.append(""); 141 | } 142 | } 143 | return sb.toString(); 144 | } 145 | 146 | <% if contains_attribute(data_type) %> 147 | /** 148 | * Serializes attributes if any 149 | */ 150 | private String getAttributeAsXml() { 151 | StringBuilder sb = new StringBuilder(); 152 | <% type = data_type %> 153 | <% while type %> 154 | <% type.properties.each do |name , definition| %> 155 | <% if definition.attribute %> 156 | <% vname = validate_property_name(name) %> 157 | if (this.<%= vname %> != null) { 158 | <% if definition.enum %> 159 | sb.append(" <%= name %>=\"").append(SDKUtil.escapeInvalidXmlCharsRegex(this.<%= vname %>.getValue())).append("\""); 160 | <% else %> 161 | sb.append(" <%= name %>=\"").append(SDKUtil.escapeInvalidXmlCharsRegex(this.<%= vname %>)).append("\""); 162 | <% end %> 163 | } 164 | <% end %> 165 | <% end %> 166 | <% type = schema.data_types[type.extends] %> 167 | <% end %> 168 | return sb.toString(); 169 | } 170 | <% end %> 171 | -------------------------------------------------------------------------------- /spec/php_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require 'spec_helper' 18 | 19 | describe 'PHPHelper' do 20 | 21 | before :all do 22 | @php_helper = Class.new do 23 | include Genio::Util::NamespaceHelper 24 | include Genio::Helper::PHP 25 | end.new 26 | end 27 | 28 | it "get basic types" do 29 | @php_helper.find_basic_type("int").should eq "integer" 30 | @php_helper.find_basic_type("integer").should eq "integer" 31 | @php_helper.find_basic_type("positiveInteger").should eq "integer" 32 | @php_helper.find_basic_type("nonNegativeInteger").should eq "integer" 33 | @php_helper.find_basic_type("long").should eq "long" 34 | @php_helper.find_basic_type("double").should eq "double" 35 | @php_helper.find_basic_type("decimal").should eq "double" 36 | @php_helper.find_basic_type("float").should eq "float" 37 | @php_helper.find_basic_type("boolean").should eq "boolean" 38 | @php_helper.find_basic_type("string").should eq "string" 39 | @php_helper.find_basic_type("dateTime").should eq "string" 40 | @php_helper.find_basic_type("date").should eq "string" 41 | @php_helper.find_basic_type("number").should eq "number" 42 | @php_helper.find_basic_type("object").should eq "object" 43 | @php_helper.find_basic_type("token").should eq "string" 44 | @php_helper.find_basic_type("duration").should eq "string" 45 | @php_helper.find_basic_type("anyURI").should eq "string" 46 | @php_helper.find_basic_type("date_time").should eq "string" 47 | @php_helper.find_basic_type("base64Binary").should eq "string" 48 | @php_helper.find_basic_type("time").should eq "string" 49 | @php_helper.find_basic_type("customType").should eq "CustomType" 50 | @php_helper.find_basic_type("CustomType").should eq "CustomType" 51 | end 52 | 53 | it "get property type" do 54 | property = Genio::Parser::Types::Property.new( :type => 'self' ) 55 | schema = Genio::Parser::Format::JsonSchema.new() 56 | schema.enum_types["Test"] = { } 57 | @php_helper.get_php_type(property, schema, 'HostClass').should eq "HostClass" 58 | property = Genio::Parser::Types::Property.new( :type => 'Test' ) 59 | @php_helper.get_php_type(property, schema, 'HostClass').should eq "string" 60 | property = Genio::Parser::Types::Property.new( :type => 'integer' ) 61 | @php_helper.get_php_type(property, schema, 'HostClass').should eq "integer" 62 | property = Genio::Parser::Types::Property.new( :type => 'date_time' ) 63 | @php_helper.get_php_type(property, schema, 'HostClass').should eq "string" 64 | end 65 | 66 | it "test process path with placeholders" do 67 | @php_helper.process_path_with_placeholders('Payment', 'Payment', '/v1/payments/{payment-id}', true, nil, nil).should eq '"/v1/payments/{$this->getId()}"' 68 | argument_hash = {} 69 | argument_hash['payment_id'] = 'string' 70 | query_params = {} 71 | @php_helper.process_path_with_placeholders('Payment', 'Payment', '/v1/payments/{payment-id}', false, argument_hash, nil).should eq '"/v1/payments/$payment_id"' 72 | @php_helper.process_path_with_placeholders('Payment', 'Payment', '/v1/payments/{payment-id}', false, argument_hash, query_params).should eq '"/v1/payments/$payment_id?" . http_build_query($queryParameters)' 73 | @php_helper.process_path_with_placeholders('Payment', 'Payment', '/v1/payments/{payment-id}', true, argument_hash, query_params).should eq '"/v1/payments/{$this->getId()}?" . http_build_query($queryParameters)' 74 | end 75 | 76 | it "validate class name" do 77 | @php_helper.valid_class_name('class-name').should eq "ClassName" 78 | @php_helper.valid_class_name('Class-Name').should eq "ClassName" 79 | @php_helper.valid_class_name('class-Name').should eq "ClassName" 80 | @php_helper.valid_class_name('Class-name').should eq "ClassName" 81 | @php_helper.valid_class_name('class_name').should eq "ClassName" 82 | @php_helper.valid_class_name('Class_Name').should eq "ClassName" 83 | @php_helper.valid_class_name('class_Name').should eq "ClassName" 84 | @php_helper.valid_class_name('Class_name').should eq "ClassName" 85 | @php_helper.valid_class_name('classname').should eq "Classname" 86 | end 87 | 88 | it "validate property name" do 89 | @php_helper.valid_property_name('property-name').should eq 'propertyName' 90 | @php_helper.valid_property_name('Property-name').should eq 'propertyName' 91 | @php_helper.valid_property_name('Property-Name').should eq 'propertyName' 92 | @php_helper.valid_property_name('property-Name').should eq 'propertyName' 93 | @php_helper.valid_property_name('propertyname').should eq 'propertyname' 94 | end 95 | 96 | it "get deprecated function name" do 97 | @php_helper.deprecated_function_name('function-name').should eq 'Function_name' 98 | @php_helper.deprecated_function_name('function_name').should eq 'Function_name' 99 | @php_helper.deprecated_function_name('functionname').should eq 'Functionname' 100 | end 101 | 102 | it "validate method name" do 103 | @php_helper.validate_method_name('function-name').should eq 'functionName' 104 | @php_helper.validate_method_name('function_name').should eq 'functionName' 105 | @php_helper.validate_method_name('functionname').should eq 'functionname' 106 | @php_helper.validate_method_name('abstract').should eq 'doAbstract' 107 | end 108 | 109 | it "validate package name" do 110 | @php_helper.validated_package('com.paypal.api').should eq 'Paypal\\Api' 111 | end 112 | 113 | it "test to underscore" do 114 | @php_helper.to_underscore('underscore-me').should eq 'underscore_me' 115 | @php_helper.to_underscore('Underscore-Me').should eq 'Underscore_Me' 116 | @php_helper.to_underscore('UnderscoreMe').should eq 'UnderscoreMe' 117 | end 118 | 119 | end 120 | -------------------------------------------------------------------------------- /templates/sdk.wsdlsoapserialize_dotnet.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | <% if operation_input %> 20 | public String ToXMLString() 21 | { 22 | return ToXMLString("<%= serialization_name.split(':').first %>", "<%= serialization_name.split(':').last %>"); 23 | } 24 | <% end %> 25 | 26 | /// 27 | /// Serialize the object to XML String form 28 | /// 29 | public String ToXMLString(string prefix, string name) 30 | { 31 | StringBuilder sb = new StringBuilder(); 32 | if (name != null) 33 | { 34 | <% if contains_attribute(data_type) %> 35 | if (prefix != null) 36 | { 37 | sb.Append("<").Append(prefix + ":").Append(name); 38 | } 39 | else 40 | { 41 | sb.Append("<").Append(name); 42 | } 43 | sb.Append(GetAttributeAsXml()); 44 | sb.Append(">"); 45 | <% else %> 46 | if (prefix != null) 47 | { 48 | sb.Append("<").Append(prefix + ":").Append(name).Append(">"); 49 | } 50 | else 51 | { 52 | sb.Append("<").Append(name).Append(">"); 53 | } 54 | <% end %> 55 | } 56 | <% type = data_type %> 57 | <% while type %> 58 | <% type.properties.each do |name , definition| %> 59 | <% if !definition.attribute %> 60 | <% vname = validate_property_name(name, true) %> 61 | if (this.<%= vname %> != null) 62 | { 63 | <% if definition.array %> 64 | for (int i = 0; i < this.<%= vname %>.Count; i++) 65 | { 66 | <% if is_complex_type(definition.type, schema) || is_enum_type(definition.type, schema) %> 67 | <% if definition.enum %> 68 | sb.Append("<"); 69 | <% if should_qualify_name(definition.package, schema) %> 70 | sb.Append("<%= definition.package %>:"); 71 | <% end %> 72 | sb.Append("<%= name %>>").Append(SDKUtil.EscapeInvalidXmlCharsRegex(ReflectionEnumUtil.GetDescription(this.<%= vname %>[i]))).Append(" 74 | sb.Append("<%= definition.package %>:"); 75 | <% end %> 76 | sb.Append("<%= name %>>"); 77 | <% else %> 78 | <% if should_qualify_name(definition.package, schema) %> 79 | sb.Append(this.<%= vname %>[i].ToXMLString("<%= definition.package %>","<%= name %>")); 80 | <% else %> 81 | sb.Append(this.<%= vname %>[i].ToXMLString(null,"<%= name %>")); 82 | <% end %> 83 | <% end %> 84 | <% else %> 85 | <% if definition.value == '1' %> 86 | sb.Append(SDKUtil.EscapeInvalidXmlCharsRegex(<% if find_basic_type(definition.type) == 'double' %>Convert.ToString(Value[i], DefaultCulture)<% else %>Value[i]<% end %>)); 87 | <% else %> 88 | sb.Append("<"); 89 | <% if should_qualify_name(definition.package, schema) %> 90 | sb.Append("<%= definition.package %>:"); 91 | <% end %> 92 | sb.Append("<%= name %>>").Append(SDKUtil.EscapeInvalidXmlCharsRegex(<% if find_basic_type(definition.type) == 'double' %>Convert.ToString(this.<%= vname %>[i], DefaultCulture)<% else %>this.<%= vname %>[i]<% end %>)); 93 | sb.Append(" 95 | sb.Append("<%= definition.package %>:"); 96 | <% end %> 97 | sb.Append("<%= name %>>"); 98 | <% end %> 99 | <% end %> 100 | } 101 | <% else %> 102 | <% if is_complex_type(definition.type, schema) || is_enum_type(definition.type, schema) %> 103 | <% if definition.enum %> 104 | sb.Append("<"); 105 | <% if should_qualify_name(definition.package, schema) %> 106 | sb.Append("<%= definition.package %>:"); 107 | <% end %> 108 | sb.Append("<%= name %>>").Append(SDKUtil.EscapeInvalidXmlCharsRegex(ReflectionEnumUtil.GetDescription(this.<%= vname %>))).Append(" 110 | sb.Append("<%= definition.package %>:"); 111 | <% end %> 112 | sb.Append("<%= name %>>"); 113 | <% else %> 114 | <% if should_qualify_name(definition.package, schema) %> 115 | sb.Append(this.<%= vname %>.ToXMLString("<%= definition.package %>","<%= name %>")); 116 | <% else %> 117 | sb.Append(this.<%= vname %>.ToXMLString(null, "<%= name %>")); 118 | <% end %> 119 | <% end %> 120 | <% else %> 121 | <% if definition.value == '1' %> 122 | sb.Append(SDKUtil.EscapeInvalidXmlCharsRegex(<% if find_basic_type(definition.type) == 'double' %>Convert.ToString(Value, DefaultCulture)<% else %>Value<% end %>)); 123 | <% else %> 124 | sb.Append("<"); 125 | <% if should_qualify_name(definition.package, schema) %> 126 | sb.Append("<%= definition.package %>:"); 127 | <% end %> 128 | sb.Append("<%= name %>>"); 129 | sb.Append(SDKUtil.EscapeInvalidXmlCharsRegex(<% if find_basic_type(definition.type) == 'double' %>Convert.ToString(this.<%= vname %>, DefaultCulture)<% else %>this.<%= vname %><% end %>)); 130 | sb.Append(" 132 | sb.Append("<%= definition.package %>:"); 133 | <% end %> 134 | sb.Append("<%= name %>>"); 135 | <% end %> 136 | <% end %> 137 | <% end %> 138 | } 139 | <% end %> 140 | <% end %> 141 | <% type = schema.data_types[type.extends] %> 142 | <% end %> 143 | if (name != null) 144 | { 145 | if (prefix != null) 146 | { 147 | sb.Append(""); 148 | } 149 | else 150 | { 151 | sb.Append(""); 152 | } 153 | } 154 | return sb.ToString(); 155 | } 156 | 157 | <% if contains_attribute(data_type) %> 158 | 159 | /// 160 | /// Serializes attributes if any 161 | /// 162 | private String GetAttributeAsXml() 163 | { 164 | StringBuilder sb = new StringBuilder(); 165 | <% type = data_type %> 166 | <% while type %> 167 | <% type.properties.each do |name , definition| %> 168 | <% if definition.attribute %> 169 | <% vname = validate_property_name(name, true) %> 170 | if (this.<%= vname %> != null) 171 | { 172 | <% if definition.enum %> 173 | sb.Append(" <%= name %>=\"").Append(SDKUtil.EscapeInvalidXmlCharsRegex(ReflectionEnumUtil.GetDescription(this.<%= vname %>))).Append("\""); 174 | <% else %> 175 | sb.Append(" <%= name %>=\"").Append(SDKUtil.EscapeInvalidXmlCharsRegex(this.<%= vname %>)).Append("\""); 176 | <% end %> 177 | } 178 | <% end %> 179 | <% end %> 180 | <% type = schema.data_types[type.extends] %> 181 | <% end %> 182 | return sb.ToString(); 183 | } 184 | <% end %> 185 | -------------------------------------------------------------------------------- /templates/sdk.rest_dotnet.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | <% imports(data_type, schema, package, classname, "rest").each do |import| %> 20 | using <%= import %>; 21 | <% end %> 22 | 23 | namespace <%= package %> 24 | { 25 | public class <%= validate_class_name(classname) %><%= " : #{validate_class_name(data_type.extends)}" if data_type.extends %> 26 | { 27 | <% const_args = {} %> 28 | <% data_type.properties.each do |name, property| %> 29 | <% vname = validate_property_name(name) %> 30 | <% const_args[vname] = get_property_class(property, classname) if property.required %> 31 | private <%= get_property_class(property, classname) %><%= '?' if (is_nullable_type(property) and !property.array) %> <%= vname %>Property; 32 | 33 | /// 34 | /// <%= property.description %> 35 | /// 36 | [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] 37 | public <%= get_property_class(property, classname) %><%= '?' if (is_nullable_type(property) and !property.array) %> <%= vname %> 38 | { 39 | get 40 | { 41 | return this.<%= vname %>Property; 42 | } 43 | set 44 | { 45 | this.<%= vname %>Property = value; 46 | } 47 | } 48 | 49 | <% end %> 50 | /// 51 | /// Default Constructor 52 | /// 53 | public <%= validate_class_name(classname) %>() 54 | { 55 | } 56 | 57 | <% if const_args.size > 0 %> 58 | /// 59 | /// Parameterized Constructor 60 | /// 61 | public <%= validate_class_name(classname) %>(<%= const_args.map{|name, type| "#{type} #{name}"}.join(", ") %>) 62 | { 63 | <% const_args.each do |name, type| %> 64 | this.<%= name %> = <%= name %>; 65 | <% end %> 66 | } 67 | <% end %> 68 | 69 | <% service = schema.services[classname] %> 70 | <% if service %> 71 | <% service.operations.each do |name, property| %> 72 | <% static = is_static_method(property) %> 73 | <% hostIdCheck = check_host_id(classname, property) %> 74 | <% methodName = validate_method_name(name) %> 75 | <% argumentHash = form_rest_api_args(classname, property, name) %> 76 | <% resourcePath = validate_path(property.path) %> 77 | <% pathParameters = generate_format_hash(classname, property, resourcePath) %> 78 | <% payLoad = get_payload(classname, property) %> 79 | <% if options[:gen_deprecated_methods] %> 80 | 81 | /// 82 | /// <%= property.description %> 83 | /// 84 | /// Access Token used for the API call.<% if argumentHash.size > 0 %><% argumentHash.each do |name, type| %> 85 | /// <%= type %><% end %><% end %> 86 | /// <% if property.response %><%= property.response %><% end %> 87 | public<%= " static" if static %><% if property.response %> <%= validate_class_name(property.response) %><% else %> void<% end %> <%= methodName %>(string accessToken<% if argumentHash.size > 0 %>, <%= argumentHash.map{|name, type| "#{type} #{name}"}.join(", ") %><% end %>) 88 | { 89 | APIContext apiContext = new APIContext(accessToken); 90 | <% if property.response %> 91 | return <%= methodName %>(apiContext<% if argumentHash.size > 0 %>, <%= argumentHash.map{|name, type| "#{name}"}.join(", ") %><% end %>); 92 | <% else %> 93 | <%= name.camelcase %>(apiContext<% if argumentHash.size > 0 %>, <%= argumentHash.map{|name, type| "#{name}"}.join(", ") %><% end %>); 94 | return; 95 | <% end %> 96 | } 97 | <% end %> 98 | 99 | /// 100 | /// <%= property.description %> 101 | /// 102 | /// APIContext used for the API call.<% if argumentHash.size > 0 %><% argumentHash.each do |name, type| %> 103 | /// <%= type %><% end %><% end %> 104 | /// <% if property.response %><%= property.response %><% end %> 105 | public<%= " static" if static %><% if property.response %> <%= validate_class_name(property.response) %><% else %> void<% end %> <%= methodName %>(APIContext apiContext<% if argumentHash.size > 0 %>, <%= argumentHash.map{|name, type| "#{type} #{name}"}.join(", ") %><% end %>) 106 | { 107 | if (apiContext == null) 108 | { 109 | throw new ArgumentNullException("APIContext cannot be null"); 110 | } 111 | <% if options[:mandate_oauth] %> 112 | if (string.IsNullOrEmpty(apiContext.AccessToken)) 113 | { 114 | throw new ArgumentNullException("AccessToken cannot be null or empty"); 115 | } 116 | <% end %> 117 | if (apiContext.HTTPHeaders == null) 118 | { 119 | apiContext.HTTPHeaders = new Dictionary(); 120 | } 121 | apiContext.HTTPHeaders.Add(BaseConstants.CONTENT_TYPE_HEADER, BaseConstants.CONTENT_TYPE_JSON); 122 | apiContext.SdkVersion = new SDKVersionImpl(); 123 | <% if hostIdCheck %> 124 | if (this.id == null) 125 | { 126 | throw new ArgumentNullException("Id cannot be null"); 127 | } 128 | <% end %> 129 | <% if argumentHash.size > 0 %> 130 | <% argumentHash.each do |name, type| %> 131 | <% if name != 'queryParameters' %> 132 | if (<%= name %> == null) 133 | { 134 | throw new ArgumentNullException("<%= name%> cannot be null"); 135 | } 136 | <% end %> 137 | <% end %> 138 | <% end %> 139 | Dictionary pathParameters = new Dictionary(); 140 | <% pathParameters.each do |name, value| %> 141 | pathParameters.Add("<%= name %>", <%= value %>); 142 | <% end %> 143 | string pattern = "<%= resourcePath %>"; 144 | <% if property.type != 'POST' and property.type != 'PATCH' and property.type != 'PUT' %> 145 | string resourcePath = SDKUtil.FormatURIPath(pattern, pathParameters, queryParameters); 146 | <% else %> 147 | string resourcePath = SDKUtil.FormatURIPath(pattern, pathParameters); 148 | <% end %> 149 | string payLoad = <%= payLoad %>; 150 | <% if property.response %> 151 | return PayPalResource.ConfigureAndExecute<<%= validate_class_name(property.response) %>>(apiContext, <%= "HttpMethod." + property.type %>, resourcePath, payLoad); 152 | <% else %> 153 | PayPalResource.ConfigureAndExecute(apiContext, <%= "HttpMethod." + property.type %>, resourcePath, payLoad); 154 | return; 155 | <% end %> 156 | } 157 | 158 | <% end %> 159 | <% end %> 160 | /// 161 | /// Converts the object to JSON string 162 | /// 163 | public string ConvertToJson() 164 | { 165 | return JsonFormatter.ConvertToJson(this); 166 | } 167 | 168 | public override string ToString() 169 | { 170 | return ConvertToJson(); 171 | } 172 | 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /spec/java_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require 'spec_helper' 18 | 19 | describe 'JavaHelper' do 20 | 21 | before :all do 22 | @java_helper = Class.new do 23 | include Genio::Helper::Java 24 | end.new 25 | end 26 | 27 | it "get basic types" do 28 | @java_helper.find_basic_type("int").should eq "Integer" 29 | @java_helper.find_basic_type("integer").should eq "Integer" 30 | @java_helper.find_basic_type("positiveInteger").should eq "Integer" 31 | @java_helper.find_basic_type("nonNegativeInteger").should eq "Integer" 32 | @java_helper.find_basic_type("long").should eq "Long" 33 | @java_helper.find_basic_type("double").should eq "Double" 34 | @java_helper.find_basic_type("decimal").should eq "Double" 35 | @java_helper.find_basic_type("float").should eq "Float" 36 | @java_helper.find_basic_type("boolean").should eq "Boolean" 37 | @java_helper.find_basic_type("string").should eq "String" 38 | @java_helper.find_basic_type("dateTime").should eq "String" 39 | @java_helper.find_basic_type("date").should eq "String" 40 | @java_helper.find_basic_type("number").should eq "Number" 41 | @java_helper.find_basic_type("object").should eq "Object" 42 | @java_helper.find_basic_type("token").should eq "String" 43 | @java_helper.find_basic_type("duration").should eq "String" 44 | @java_helper.find_basic_type("anyURI").should eq "String" 45 | @java_helper.find_basic_type("date_time").should eq "String" 46 | @java_helper.find_basic_type("base64Binary").should eq "String" 47 | @java_helper.find_basic_type("time").should eq "String" 48 | @java_helper.find_basic_type("Int").should eq "Integer" 49 | @java_helper.find_basic_type("Integer").should eq "Integer" 50 | @java_helper.find_basic_type("PositiveInteger").should eq "Integer" 51 | @java_helper.find_basic_type("NonNegativeInteger").should eq "Integer" 52 | @java_helper.find_basic_type("Long").should eq "Long" 53 | @java_helper.find_basic_type("Double").should eq "Double" 54 | @java_helper.find_basic_type("Decimal").should eq "Double" 55 | @java_helper.find_basic_type("Float").should eq "Float" 56 | @java_helper.find_basic_type("Boolean").should eq "Boolean" 57 | @java_helper.find_basic_type("String").should eq "String" 58 | @java_helper.find_basic_type("DateTime").should eq "String" 59 | @java_helper.find_basic_type("Date").should eq "String" 60 | @java_helper.find_basic_type("Number").should eq "Number" 61 | @java_helper.find_basic_type("Object").should eq "Object" 62 | @java_helper.find_basic_type("Token").should eq "String" 63 | @java_helper.find_basic_type("Duration").should eq "String" 64 | @java_helper.find_basic_type("AnyURI").should eq "String" 65 | @java_helper.find_basic_type("Date_time").should eq "String" 66 | @java_helper.find_basic_type("Base64Binary").should eq "String" 67 | @java_helper.find_basic_type("Time").should eq "String" 68 | end 69 | 70 | it "only basic type" do 71 | @java_helper.only_basic_type("String").should eq "String" 72 | @java_helper.only_basic_type("CustomClass").should eq nil 73 | end 74 | 75 | it "get property class" do 76 | property = Genio::Parser::Types::Property.new( :type => 'self' ) 77 | @java_helper.get_property_class(property, 'HostClass').should eq "HostClass" 78 | property.type = 'SampleClass' 79 | @java_helper.get_property_class(property, 'SampleClass').should eq "SampleClass" 80 | property.type = 'String' 81 | @java_helper.get_property_class(property, 'String').should eq "String" 82 | property.type = 'String' 83 | @java_helper.get_property_class(property).should eq "String" 84 | property.type = 'string' 85 | @java_helper.get_property_class(property).should eq "String" 86 | property.array = true 87 | @java_helper.get_property_class(property).should eq "List" 88 | end 89 | 90 | it "validate path" do 91 | path = '/v1/payments/{payment-id}' 92 | @java_helper.validate_path(path).should eq "/v1/payments/{paymentId}" 93 | path = '/v1/payments/{paymentId}' 94 | @java_helper.validate_path(path).should eq "/v1/payments/{paymentId}" 95 | path = '/v1/payments/{PaymentId}' 96 | @java_helper.validate_path(path).should eq "/v1/payments/{paymentId}" 97 | path = '/v1/payments/{Payment-Id}' 98 | @java_helper.validate_path(path).should eq "/v1/payments/{paymentId}" 99 | path = '/v1/payments/{Payment-Id-value}' 100 | @java_helper.validate_path(path).should eq "/v1/payments/{paymentIdValue}" 101 | end 102 | 103 | it "validate class name" do 104 | @java_helper.validate_class_name('string').should eq "String" 105 | @java_helper.validate_class_name('String').should eq "String" 106 | @java_helper.validate_class_name('custom_class').should eq "CustomClass" 107 | @java_helper.validate_class_name('custom-class').should eq "CustomClass" 108 | end 109 | 110 | it "validate property name" do 111 | @java_helper.validate_property_name("Line1").should eq "line1" 112 | @java_helper.validate_property_name("country-Name").should eq "countryName" 113 | @java_helper.validate_property_name("Country-Name").should eq "countryName" 114 | @java_helper.validate_property_name("country_Name").should eq "countryName" 115 | @java_helper.validate_property_name("Country_Name").should eq "countryName" 116 | @java_helper.validate_property_name("country-name").should eq "countryName" 117 | @java_helper.validate_property_name("Country-name").should eq "countryName" 118 | end 119 | 120 | it "validate method name" do 121 | @java_helper.validate_method_name("Call-Me").should eq "callMe" 122 | @java_helper.validate_method_name("Call_Me").should eq "callMe" 123 | @java_helper.validate_method_name("call-me").should eq "callMe" 124 | @java_helper.validate_method_name("call_me").should eq "callMe" 125 | @java_helper.validate_method_name("call-Me").should eq "callMe" 126 | @java_helper.validate_method_name("Call-me").should eq "callMe" 127 | @java_helper.validate_method_name("call_Me").should eq "callMe" 128 | @java_helper.validate_method_name("Call_me").should eq "callMe" 129 | @java_helper.validate_method_name("void").should eq "doVoid" 130 | end 131 | 132 | it "validate enum name" do 133 | @java_helper.validate_enum_name("Country-Name").should eq "Country_Name" 134 | @java_helper.validate_enum_name("Country Name").should eq "Country_Name" 135 | @java_helper.validate_enum_name("1Country-Name").should eq "_1Country_Name" 136 | end 137 | 138 | end 139 | -------------------------------------------------------------------------------- /spec/dotnet_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require 'spec_helper' 18 | 19 | describe 'DotNetHelper' do 20 | 21 | before :all do 22 | @dotnet_helper = Class.new do 23 | include Genio::Helper::DotNet 24 | end.new 25 | end 26 | 27 | it "get basic types" do 28 | @dotnet_helper.find_basic_type("int").should eq "int" 29 | @dotnet_helper.find_basic_type("integer").should eq "int" 30 | @dotnet_helper.find_basic_type("positiveInteger").should eq "int" 31 | @dotnet_helper.find_basic_type("nonNegativeInteger").should eq "int" 32 | @dotnet_helper.find_basic_type("long").should eq "long" 33 | @dotnet_helper.find_basic_type("double").should eq "double" 34 | @dotnet_helper.find_basic_type("decimal").should eq "double" 35 | @dotnet_helper.find_basic_type("float").should eq "float" 36 | @dotnet_helper.find_basic_type("boolean").should eq "bool" 37 | @dotnet_helper.find_basic_type("string").should eq "string" 38 | @dotnet_helper.find_basic_type("dateTime").should eq "string" 39 | @dotnet_helper.find_basic_type("date").should eq "string" 40 | @dotnet_helper.find_basic_type("number").should eq "double" 41 | @dotnet_helper.find_basic_type("object").should eq "object" 42 | @dotnet_helper.find_basic_type("token").should eq "string" 43 | @dotnet_helper.find_basic_type("duration").should eq "string" 44 | @dotnet_helper.find_basic_type("anyURI").should eq "string" 45 | @dotnet_helper.find_basic_type("date_time").should eq "string" 46 | @dotnet_helper.find_basic_type("base64Binary").should eq "string" 47 | @dotnet_helper.find_basic_type("time").should eq "string" 48 | @dotnet_helper.find_basic_type("Int").should eq "int" 49 | @dotnet_helper.find_basic_type("Integer").should eq "int" 50 | @dotnet_helper.find_basic_type("PositiveInteger").should eq "int" 51 | @dotnet_helper.find_basic_type("NonNegativeInteger").should eq "int" 52 | @dotnet_helper.find_basic_type("Long").should eq "long" 53 | @dotnet_helper.find_basic_type("Double").should eq "double" 54 | @dotnet_helper.find_basic_type("Decimal").should eq "double" 55 | @dotnet_helper.find_basic_type("Float").should eq "float" 56 | @dotnet_helper.find_basic_type("Boolean").should eq "bool" 57 | @dotnet_helper.find_basic_type("String").should eq "string" 58 | @dotnet_helper.find_basic_type("DateTime").should eq "string" 59 | @dotnet_helper.find_basic_type("Date").should eq "string" 60 | @dotnet_helper.find_basic_type("Number").should eq "double" 61 | @dotnet_helper.find_basic_type("Object").should eq "object" 62 | @dotnet_helper.find_basic_type("Token").should eq "string" 63 | @dotnet_helper.find_basic_type("Duration").should eq "string" 64 | @dotnet_helper.find_basic_type("AnyURI").should eq "string" 65 | @dotnet_helper.find_basic_type("Date_time").should eq "string" 66 | @dotnet_helper.find_basic_type("Base64Binary").should eq "string" 67 | @dotnet_helper.find_basic_type("Time").should eq "string" 68 | end 69 | 70 | it "only basic type" do 71 | @dotnet_helper.only_basic_type("String").should eq "string" 72 | @dotnet_helper.only_basic_type("CustomClass").should eq nil 73 | end 74 | 75 | it "get property class" do 76 | property = Genio::Parser::Types::Property.new( :type => 'self' ) 77 | @dotnet_helper.get_property_class(property, 'HostClass').should eq "HostClass" 78 | property.type = 'SampleClass' 79 | @dotnet_helper.get_property_class(property, 'SampleClass').should eq "SampleClass" 80 | property.type = 'String' 81 | @dotnet_helper.get_property_class(property, 'String').should eq "string" 82 | property.type = 'String' 83 | @dotnet_helper.get_property_class(property).should eq "string" 84 | property.type = 'string' 85 | @dotnet_helper.get_property_class(property).should eq "string" 86 | property.array = true 87 | @dotnet_helper.get_property_class(property).should eq "List" 88 | end 89 | 90 | it "validate path" do 91 | path = '/v1/payments/{payment-id}' 92 | @dotnet_helper.validate_path(path).should eq "/v1/payments/{payment_id}" 93 | path = '/v1/payments/{paymentId}' 94 | @dotnet_helper.validate_path(path).should eq "/v1/payments/{paymentId}" 95 | path = '/v1/payments/{PaymentId}' 96 | @dotnet_helper.validate_path(path).should eq "/v1/payments/{PaymentId}" 97 | path = '/v1/payments/{Payment-Id}' 98 | @dotnet_helper.validate_path(path).should eq "/v1/payments/{Payment_Id}" 99 | path = '/v1/payments/{Payment-Id-value}' 100 | @dotnet_helper.validate_path(path).should eq "/v1/payments/{Payment_Id_value}" 101 | end 102 | 103 | it "validate class name" do 104 | @dotnet_helper.validate_class_name('string').should eq "string" 105 | @dotnet_helper.validate_class_name('string').should eq "string" 106 | @dotnet_helper.validate_class_name('custom_class').should eq "CustomClass" 107 | @dotnet_helper.validate_class_name('custom-class').should eq "CustomClass" 108 | end 109 | 110 | it "validate property name" do 111 | @dotnet_helper.validate_property_name("Line1").should eq "Line1" 112 | @dotnet_helper.validate_property_name("country-Name").should eq "country_Name" 113 | @dotnet_helper.validate_property_name("country-Name", true).should eq "CountryName" 114 | @dotnet_helper.validate_property_name("Country-Name").should eq "Country_Name" 115 | @dotnet_helper.validate_property_name("Country-Name", true).should eq "CountryName" 116 | @dotnet_helper.validate_property_name("country_Name").should eq "country_Name" 117 | @dotnet_helper.validate_property_name("country_Name", true).should eq "CountryName" 118 | @dotnet_helper.validate_property_name("Country_Name").should eq "Country_Name" 119 | @dotnet_helper.validate_property_name("Country_Name", true).should eq "CountryName" 120 | @dotnet_helper.validate_property_name("country-name").should eq "country_name" 121 | @dotnet_helper.validate_property_name("country-name", true).should eq "CountryName" 122 | @dotnet_helper.validate_property_name("Country-name").should eq "Country_name" 123 | @dotnet_helper.validate_property_name("Country-name", true).should eq "CountryName" 124 | end 125 | 126 | it "validate method name" do 127 | @dotnet_helper.validate_method_name("Call-Me").should eq "CallMe" 128 | @dotnet_helper.validate_method_name("Call_Me").should eq "CallMe" 129 | @dotnet_helper.validate_method_name("call-me").should eq "CallMe" 130 | @dotnet_helper.validate_method_name("call_me").should eq "CallMe" 131 | @dotnet_helper.validate_method_name("call-Me").should eq "CallMe" 132 | @dotnet_helper.validate_method_name("Call-me").should eq "CallMe" 133 | @dotnet_helper.validate_method_name("call_Me").should eq "CallMe" 134 | @dotnet_helper.validate_method_name("Call_me").should eq "CallMe" 135 | @dotnet_helper.validate_method_name("void").should eq "DoVoid" 136 | end 137 | 138 | it "validate property name as argument" do 139 | @dotnet_helper.validate_property_as_argument("Line1").should eq "line1" 140 | @dotnet_helper.validate_property_as_argument("country-Name").should eq "countryName" 141 | @dotnet_helper.validate_property_as_argument("Country-Name").should eq "countryName" 142 | @dotnet_helper.validate_property_as_argument("country_Name").should eq "countryName" 143 | @dotnet_helper.validate_property_as_argument("Country_Name").should eq "countryName" 144 | @dotnet_helper.validate_property_as_argument("country-name").should eq "countryName" 145 | @dotnet_helper.validate_property_as_argument("Country-name").should eq "countryName" 146 | end 147 | 148 | it "validate enum name" do 149 | @dotnet_helper.validate_enum_name("Country-Name").should eq "Country_Name" 150 | @dotnet_helper.validate_enum_name("Country Name").should eq "Country_Name" 151 | @dotnet_helper.validate_enum_name("1Country-Name").should eq "_1Country_Name" 152 | end 153 | 154 | end 155 | -------------------------------------------------------------------------------- /templates/sdk.rest_java.erb: -------------------------------------------------------------------------------- 1 | <% 2 | # 3 | # Copyright 2013 PayPal Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # 18 | %> 19 | package <%= package %>; 20 | 21 | <% imports(data_type, schema, package, classname, "rest").each do |import| %> 22 | import <%= import %>; 23 | <% end %> 24 | 25 | public class <%= validate_class_name(classname) %> <%= "extends #{validate_class_name(data_type.extends)}" if data_type.extends %> { 26 | 27 | <% const_args = {} %> 28 | <% data_type.properties.each do |name, property| %> 29 | <% vname = validate_property_name(name) %> 30 | <% const_args[vname] = get_property_class(property, classname) if property.required %> 31 | /** 32 | * <%= property.description %> 33 | */ 34 | private <%= get_property_class(property, classname) %> <%= vname %>; 35 | 36 | <% end %> 37 | <% if schema.services[classname] and !options[:skip_stub_operations] %> 38 | /** 39 | * Returns the last request sent to the Service 40 | * 41 | * @return Last request sent to the server 42 | */ 43 | public static String getLastRequest() { 44 | return PayPalResource.getLastRequest(); 45 | } 46 | 47 | /** 48 | * Returns the last response returned by the Service 49 | * 50 | * @return Last response got from the Service 51 | */ 52 | public static String getLastResponse() { 53 | return PayPalResource.getLastResponse(); 54 | } 55 | 56 | /** 57 | * Initialize using InputStream(of a Properties file) 58 | * 59 | * @param is 60 | * InputStream 61 | * @throws PayPalRESTException 62 | */ 63 | public static void initConfig(InputStream is) throws PayPalRESTException { 64 | PayPalResource.initConfig(is); 65 | } 66 | 67 | /** 68 | * Initialize using a File(Properties file) 69 | * 70 | * @param file 71 | * File object of a properties entity 72 | * @throws PayPalRESTException 73 | */ 74 | public static void initConfig(File file) throws PayPalRESTException { 75 | PayPalResource.initConfig(file); 76 | } 77 | 78 | /** 79 | * Initialize using Properties 80 | * 81 | * @param properties 82 | * Properties object 83 | */ 84 | public static void initConfig(Properties properties) { 85 | PayPalResource.initConfig(properties); 86 | } 87 | <% end %> 88 | /** 89 | * Default Constructor 90 | */ 91 | public <%= validate_class_name(classname) %>() { 92 | } 93 | 94 | <% if const_args.size > 0 %> 95 | /** 96 | * Parameterized Constructor 97 | */ 98 | public <%= validate_class_name(classname) %>(<%= const_args.map{|name, type| "#{type} #{name}"}.join(", ") %>) { 99 | <% const_args.each do |name, type| %> 100 | this.<%= name %> = <%= name %>; 101 | <% end %> 102 | } 103 | 104 | <% end %> 105 | <% data_type.properties.each do |name, property| %> 106 | <% vname = validate_property_name(name) %> 107 | <% methodname = validate_class_name(name) %> 108 | 109 | /** 110 | * Setter for <%= vname %> 111 | */ 112 | public <%= classname %> set<%= methodname %>(<%= get_property_class(property, classname) %> <%= vname %>) { 113 | this.<%= vname%> = <%= vname %>; 114 | return this; 115 | } 116 | 117 | /** 118 | * Getter for <%= vname %> 119 | */ 120 | public <%= get_property_class(property, classname) %> get<%= methodname %>() { 121 | return this.<%= vname%>; 122 | } 123 | 124 | <% end %> 125 | <% service = schema.services[classname] %> 126 | <% if service and !options[:skip_stub_operations] %> 127 | <% service.operations.each do |name, property| %> 128 | <% static = is_static_method(property) %> 129 | <% methodName = validate_method_name(name) %> 130 | <% hostIdCheck = check_host_id(classname, property) %> 131 | <% argumentHash = form_rest_api_args(classname, property, name) %> 132 | <% resourcePath = validate_path(property.path) %> 133 | <% pathParameters = generate_format_hash(classname, property, resourcePath) %> 134 | <% payLoad = get_payload(classname, property) %> 135 | <% if options[:gen_deprecated_methods] %> 136 | 137 | /** 138 | * <%= property.description %> 139 | * @deprecated 140 | * @param accessToken 141 | * Access Token used for the API call.<% if argumentHash.size > 0 %><% argumentHash.each do |name, type| %> 142 | * @param <%= name %> 143 | * <%= type %><% end %><% end %> 144 | * @return <% if property.response %><%= property.response %><% end %> 145 | * @throws PayPalRESTException 146 | */ 147 | public<%= " static" if static %><% if property.response %> <%= validate_class_name(property.response) %><% else %> void<% end %> <%= methodName %>(String accessToken<% if argumentHash.size > 0 %>, <%= argumentHash.map{|name, type| "#{type.camelcase} #{name}"}.join(", ") %><% end %>) throws PayPalRESTException { 148 | APIContext apiContext = new APIContext(accessToken); 149 | <% if property.response %> 150 | return <%= methodName %>(apiContext<% if argumentHash.size > 0 %>, <%= argumentHash.map{|name, type| "#{name}"}.join(", ") %><% end %>); 151 | <% else %> 152 | <%= methodName %>(apiContext<% if argumentHash.size > 0 %>, <%= argumentHash.map{|name, type| "#{name}"}.join(", ") %><% end %>); 153 | return; 154 | <% end %> 155 | } 156 | <% end %> 157 | 158 | /** 159 | * <%= property.description %> 160 | * @param apiContext 161 | * {@link APIContext} used for the API call.<% if argumentHash.size > 0 %><% argumentHash.each do |name, type| %> 162 | * @param <%= name %> 163 | * <%= type %><% end %><% end %> 164 | * @return <% if property.response %><%= property.response %><% end %> 165 | * @throws PayPalRESTException 166 | */ 167 | public<%= " static" if static %><% if property.response %> <%= validate_class_name(property.response) %><% else %> void<% end %> <%= methodName %>(APIContext apiContext<% if argumentHash.size > 0 %>, <%= argumentHash.map{|name, type| "#{type.camelcase} #{name}"}.join(", ") %><% end %>) throws PayPalRESTException { 168 | if (apiContext == null) { 169 | throw new IllegalArgumentException("APIContext cannot be null"); 170 | } 171 | <% if options[:mandate_oauth] %> 172 | if (apiContext.getAccessToken() == null || apiContext.getAccessToken().trim().length() <= 0) { 173 | throw new IllegalArgumentException("AccessToken cannot be null or empty"); 174 | } 175 | <% end %> 176 | if (apiContext.getHTTPHeaders() == null) { 177 | apiContext.setHTTPHeaders(new HashMap()); 178 | } 179 | apiContext.getHTTPHeaders().put(Constants.HTTP_CONTENT_TYPE_HEADER, Constants.HTTP_CONTENT_TYPE_JSON); 180 | apiContext.setSdkVersion(new SDKVersionImpl()); 181 | <% if hostIdCheck %> 182 | if (this.getId() == null) { 183 | throw new IllegalArgumentException("Id cannot be null"); 184 | } 185 | <% end %> 186 | <% if argumentHash.size > 0 %> 187 | <% argumentHash.each do |name, type| %> 188 | <% if name != 'queryParameters' %> 189 | if (<%= name%> == null) { 190 | throw new IllegalArgumentException("<%= name%> cannot be null"); 191 | } 192 | <% end %> 193 | <% end %> 194 | <% end %> 195 | Map pathParameters = new HashMap(); 196 | <% pathParameters.each do |name, value| %> 197 | pathParameters.put("<%= name %>", <%= value%>); 198 | <% end %> 199 | String pattern = "<%= resourcePath %>"; 200 | <% if property.type != 'POST' and property.type != 'PATCH' and property.type != 'PUT' %> 201 | String resourcePath = RESTUtil.formatURIPath(pattern, pathParameters, queryParameters); 202 | <% else %> 203 | String resourcePath = RESTUtil.formatURIPath(pattern, pathParameters); 204 | <% end %> 205 | String payLoad = <%= payLoad %>; 206 | <% if property.response %> 207 | return PayPalResource.configureAndExecute(apiContext, <%= "HttpMethod." + property.type %>, resourcePath, payLoad, <%= validate_class_name(property.response) %>.class); 208 | <% else %> 209 | PayPalResource.configureAndExecute(apiContext, <%= "HttpMethod." + property.type %>, resourcePath, payLoad, null); 210 | return; 211 | <% end %> 212 | } 213 | 214 | <% end %> 215 | <% end %> 216 | <% if !options[:skip_stub_operations] %> 217 | /** 218 | * Returns a JSON string corresponding to object state 219 | * 220 | * @return JSON representation 221 | */ 222 | public String toJSON() { 223 | return JSONFormatter.toJSON(this); 224 | } 225 | 226 | @Override 227 | public String toString() { 228 | return toJSON(); 229 | } 230 | <% end %> 231 | } 232 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | 3 | Version 2.0, January 2004 4 | 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 16 | 17 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 18 | 19 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 20 | 21 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 22 | 23 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 24 | 25 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 26 | 27 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 28 | 29 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 30 | 31 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 32 | 33 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 34 | 35 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 36 | 37 | You must give any other recipients of the Work or Derivative Works a copy of this License; and 38 | You must cause any modified files to carry prominent notices stating that You changed the files; and 39 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 40 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 41 | 42 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 43 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 44 | 45 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 46 | 47 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 48 | 49 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 50 | 51 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 52 | 53 | END OF TERMS AND CONDITIONS 54 | 55 | -------------------------------------------------------------------------------- /lib/genio/tasks.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | require "genio" 18 | require "thor" 19 | require "json" 20 | require "uri" 21 | require "genio/parser" 22 | require "pp" 23 | 24 | module Genio 25 | class Tasks < Thor 26 | include Thor::Actions 27 | include Template 28 | 29 | class_option :force, :type => :boolean, :default => false, :desc => "Overwrite files that already exist" 30 | class_option :skip, :type => :boolean, :default => false, :desc => "Skip files that already exist" 31 | 32 | # Schema path 33 | class_option :json_schema, :type => :string, :desc => "JSON Schema path" 34 | class_option :wsdl, :type => :string, :desc => "WSDL path" 35 | class_option :wadl, :type => :string, :desc => "WADL path" 36 | class_option :schema, :type => :string, :desc => "Any schema path" 37 | 38 | class_option :output_path, :type => :string, :default => "output/stubs/" 39 | class_option :namespace, :type => :string, :desc => "Dot separated package name" 40 | class_option :gen_deprecated_methods, :type => :boolean, :default => false, :desc => "Generate deprecated version of methods using AccessToken parameter" 41 | 42 | 43 | desc "java", "Generate rest java stubs" 44 | method_option :mandate_oauth, :type => :boolean, :default => true, 45 | :desc => "Force APIContext object to include oauth token" 46 | def java 47 | if schema.is_a? Parser::Format::Wsdl 48 | java_with_wsdl 49 | else 50 | java_with_json_schema 51 | end 52 | end 53 | 54 | desc "php", "Generate rest php stubs" 55 | def php 56 | if schema.is_a? Parser::Format::Wsdl 57 | php_with_wsdl 58 | else 59 | php_with_json_schema 60 | end 61 | end 62 | 63 | desc "dotnet", "Generate rest dotnet stubs" 64 | method_option :mandate_oauth, :type => :boolean, :default => true, 65 | :desc => "Force APIContext object to include oauth token" 66 | def dotnet 67 | if schema.is_a? Parser::Format::Wsdl 68 | dotnet_with_wsdl 69 | else 70 | dotnet_with_json_schema 71 | end 72 | end 73 | 74 | desc "iodocs", "Generate iodocs" 75 | method_option :output_file, :type => :string, :default => "iodocs.json" 76 | def iodocs 77 | file = File.join(options[:output_path], options[:output_file]) 78 | create_file(file, JSON.pretty_generate(schema.to_iodocs)) 79 | end 80 | 81 | private 82 | 83 | def java_with_json_schema 84 | folder = options[:output_path] 85 | schema.data_types.each do|name, data_type| 86 | package = options[:namespace] || convert_ns_to_package(data_type.package || schema.endpoint); 87 | render("templates/sdk.rest_java.erb", 88 | :data_type => data_type, 89 | :classname => name, 90 | :schema => schema, 91 | :package => package, 92 | :helper => Helper::Java, 93 | :gen_deprecated_methods => options[:gen_deprecated_methods], 94 | :create_file => File.join(folder, "#{get_package_folder(package)}/#{validate_file_name(name)}.java")) 95 | end 96 | render("templates/sdk.rest_version_java.erb", 97 | :create_file => File.join(folder, "com/paypal/sdk/info/SDKVersionImpl.java")) 98 | end 99 | 100 | def java_with_wsdl 101 | folder = options[:output_path] 102 | 103 | # emit datatypes 104 | schema.data_types.each do|name, data_type| 105 | package = options[:namespace] || convert_ns_to_package(data_type.package) 106 | render("templates/sdk.wsdl_java.erb", 107 | :package => package, 108 | :classname => name, 109 | :schema => schema, 110 | :data_type => data_type, 111 | :helper => Helper::Java, 112 | :create_file => File.join(folder, "#{get_package_folder(package)}/#{validate_file_name(name)}.java")) 113 | end 114 | 115 | # emit enumtypes 116 | schema.enum_types.each do|name, definition| 117 | package = options[:namespace] || convert_ns_to_package(definition.package) 118 | render("templates/sdk.wsdlenum_java.erb", 119 | :package => package, 120 | :classname => name, 121 | :definition => definition, 122 | :helper => Helper::Java, 123 | :create_file => File.join(folder, "#{get_package_folder(package)}/#{validate_file_name(name)}.java")) 124 | end 125 | 126 | # emit service 127 | schema.services.each do|name, service| 128 | package = options[:namespace] || convert_ns_to_package(service.package) 129 | render("templates/sdk.wsdlservice_java.erb", 130 | :package => package, 131 | :classname => name, 132 | :schema => schema, 133 | :service => service, 134 | :helper => Helper::Java, 135 | :create_file => File.join(folder, "#{get_package_folder(package)}/#{validate_file_name(name)}.java")) 136 | end 137 | end 138 | 139 | def php_with_json_schema 140 | folder = options[:output_path] 141 | schema.data_types.each do|name, data_type| 142 | package = get_slashed_package_name(capitalize_package(remove_tld_in_package(convert_ns_to_package(options[:namespace] || data_type.package || schema.endpoint)))) 143 | render("templates/sdk.rest_php.erb", 144 | :data_type => data_type, 145 | :classname => name, 146 | :schema => schema, 147 | :package => package, 148 | :helper => Helper::PHP, 149 | :gen_deprecated_methods => options[:gen_deprecated_methods], 150 | :create_file => File.join(folder, "#{get_package_folder(package)}/#{name}.php")) 151 | end 152 | end 153 | 154 | def php_with_wsdl 155 | folder = options[:output_path] 156 | schema.data_types.each do|name, data_type| 157 | package = get_slashed_package_name(capitalize_package(remove_tld_in_package(convert_ns_to_package(options[:namespace] || data_type.package || schema.endpoint)))) 158 | render("templates/sdk.wsdl_php.erb", 159 | :data_type => data_type, 160 | :classname => name, 161 | :schema => schema, 162 | :package => package, 163 | :helper => Helper::PHP, 164 | :gen_deprecated_methods => options[:gen_deprecated_methods], 165 | :create_file => File.join(folder, "#{get_package_folder(package)}/#{name}.php")) 166 | end 167 | 168 | # emit service 169 | schema.services.each do|name, service| 170 | package = get_slashed_package_name(capitalize_package(remove_tld_in_package(convert_ns_to_package(options[:namespace] || service.package || schema.endpoint)))) 171 | render("templates/sdk.wsdlservice_php.erb", 172 | :package => package, 173 | :classname => name, 174 | :schema => schema, 175 | :service => service, 176 | :helper => Helper::PHP, 177 | :gen_deprecated_methods => options[:gen_deprecated_methods], 178 | :create_file => File.join(folder, "#{get_package_folder(package)}/#{name}.php")) 179 | end 180 | end 181 | 182 | def dotnet_with_json_schema 183 | folder = options[:output_path] 184 | schema.data_types.each do|name, data_type| 185 | package = capitalize_package(remove_tld_in_package(convert_ns_to_package(options[:namespace] || data_type.package || schema.endpoint))) 186 | render("templates/sdk.rest_dotnet.erb", 187 | :data_type => data_type, 188 | :classname => name, 189 | :schema => schema, 190 | :package => package, 191 | :helper => Helper::DotNet, 192 | :gen_deprecated_methods => options[:gen_deprecated_methods], 193 | :create_file => File.join(folder, "#{get_package_folder(package, true)}/#{validate_file_name(name)}.cs")) 194 | end 195 | render("templates/sdk.rest_version_dotnet.erb", 196 | :create_file => File.join(folder, "PayPal/Sdk/Info/SDKVersionImpl.cs")) 197 | end 198 | 199 | def dotnet_with_wsdl 200 | folder = options[:output_path] 201 | 202 | # emit datatypes 203 | schema.data_types.each do|name, data_type| 204 | package = capitalize_package(remove_tld_in_package(convert_ns_to_package(options[:namespace] || data_type.package))) 205 | render("templates/sdk.wsdl_dotnet.erb", 206 | :package => package, 207 | :classname => name, 208 | :schema => schema, 209 | :data_type => data_type, 210 | :helper => Helper::DotNet, 211 | :create_file => File.join(folder, "#{get_package_folder(package)}/#{validate_file_name(name)}.cs")) 212 | end 213 | 214 | # emit enumtypes 215 | schema.enum_types.each do|name, definition| 216 | package = capitalize_package(remove_tld_in_package(convert_ns_to_package(options[:namespace] || definition.package))) 217 | render("templates/sdk.wsdlenum_dotnet.erb", 218 | :package => package, 219 | :classname => name, 220 | :definition => definition, 221 | :helper => Helper::DotNet, 222 | :create_file => File.join(folder, "#{get_package_folder(package)}/#{validate_file_name(name)}.cs")) 223 | end 224 | 225 | # emit service 226 | schema.services.each do|name, service| 227 | package = capitalize_package(remove_tld_in_package(convert_ns_to_package(options[:namespace] || service.package))) 228 | render("templates/sdk.wsdlservice_dotnet.erb", 229 | :package => package, 230 | :classname => name, 231 | :schema => schema, 232 | :service => service, 233 | :helper => Helper::DotNet, 234 | :create_file => File.join(folder, "#{get_package_folder(package)}/#{validate_file_name(name)}.cs")) 235 | end 236 | end 237 | 238 | # load the schema by choosing an appropriate parser 239 | def schema 240 | @schema ||= get_parser(options) 241 | end 242 | 243 | include Util::NamespaceHelper 244 | include Util::SchemaHelper 245 | end 246 | end 247 | -------------------------------------------------------------------------------- /lib/genio/helper/php.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | module Genio 18 | module Helper 19 | module PHP 20 | 21 | include Base 22 | # Key/Reserved Words 23 | KeyWords = [ 24 | '__halt_compiler', 25 | 'abstract', 26 | 'and', 27 | 'array', 28 | 'as', 29 | 'break', 30 | 'callable', 31 | 'case', 32 | 'catch', 33 | 'class', 34 | 'clone', 35 | 'const', 36 | 'continue', 37 | 'declare', 38 | 'default', 39 | 'die', 40 | 'do', 41 | 'echo', 42 | 'else', 43 | 'elseif', 44 | 'empty', 45 | 'enddeclare', 46 | 'endfor', 47 | 'endforeach', 48 | 'endif', 49 | 'endswitch', 50 | 'endwhile', 51 | 'eval', 52 | 'exit', 53 | 'extends', 54 | 'final', 55 | 'for', 56 | 'foreach', 57 | 'function', 58 | 'global', 59 | 'goto', 60 | 'if', 61 | 'implements', 62 | 'include', 63 | 'include_once', 64 | 'instanceof', 65 | 'insteadof', 66 | 'interface', 67 | 'isset', 68 | 'list', 69 | 'namespace', 70 | 'new', 71 | 'or', 72 | 'print', 73 | 'private', 74 | 'protected', 75 | 'public', 76 | 'require', 77 | 'require_once', 78 | 'return', 79 | 'static', 80 | 'switch', 81 | 'throw', 82 | 'trait', 83 | 'try', 84 | 'unset', 85 | 'use', 86 | 'var', 87 | 'while', 88 | 'xor' 89 | ] 90 | 91 | # Class static imports/references for rest services 92 | RestServiceImport = [ 93 | "PayPal\\Rest\\IResource", 94 | "PayPal\\Transport\\PPRestCall" 95 | ] 96 | 97 | # Class static imports/references for rest services 98 | WSDLServiceImport = [ 99 | "PayPal\\Exception\\PPTransformerException", 100 | "PayPal\\Core\\PPBaseService", 101 | "PayPal\\Core\\PPUtils" 102 | ] 103 | 104 | # Class static imports/references for WSDL 105 | WSDLStubImport = [ 106 | ] 107 | # Spec Types to PHP Types conversion map 108 | BasicTypes = { "int" => "integer", "integer" => "integer", "positiveInteger" => "integer", "nonNegativeInteger" => "integer", "long" => "long", "double" => "double", "decimal" => "double", "float" => "float", "boolean" => "boolean", "string" => "string", "dateTime" => "string", "date" => "string", "date_time" => "string", "number" => "number", "object" => "object", "token" => "string", "duration" => "string", "anyURI" => "string", "base64Binary" => "string", "time" => "string" } 109 | 110 | # Returns the type of a member 111 | def find_basic_type(key) 112 | BasicTypes[key] ? BasicTypes[key] : valid_class_name(key) 113 | end 114 | 115 | def get_php_type(property, schema, classname = nil) 116 | type = find_basic_type(property.type) 117 | 118 | # for enums, return type as string 119 | type = "string" if schema.enum_types[property.type] 120 | 121 | # If type is Self (as per Spec) treat is as HostType 122 | # classname is always in camelcase 123 | type = classname if type.downcase == "self" 124 | type 125 | end 126 | 127 | def imports(data_type, schema, package, classname, schema_format) 128 | 129 | # mandatory references 130 | list = [] 131 | if (data_type.extends) 132 | defpkg = get_namespace(schema.data_types[valid_class_name(data_type.extends)].package || package) 133 | list.push(defpkg + '\\' + valid_class_name(data_type.extends)) 134 | end 135 | 136 | service = schema.services[classname] 137 | 138 | # Add use statements for function return types 139 | if service 140 | service.operations.each do |name, operation| 141 | type = schema.data_types[operation.response] 142 | if(type) 143 | defpkg = get_namespace(schema.data_types[valid_class_name(operation.response)].package || package) 144 | list.push(defpkg + '\\' + valid_class_name(operation.response)) if (operation.response && operation.response != classname) 145 | end 146 | end 147 | end 148 | 149 | if (schema_format == "rest") 150 | list.push( "PayPal\\Common\\PPModel") if !data_type.extends 151 | list += RestServiceImport if service 152 | elsif (schema_format == "soap") 153 | list += WSDLStubImport 154 | if data_type.fault 155 | list.push('PayPal\\Core\\PPXmlFaultMessage') 156 | else 157 | list.push('PayPal\\Core\\PPXmlMessage') 158 | end 159 | end 160 | list.uniq.sort 161 | end 162 | 163 | # Generate imports for WSDL Service class 164 | def service_imports(schema, service, package) 165 | list = [] 166 | 167 | # custom package provided during generation 168 | pkg = options[:namespace] 169 | pkg += "." if pkg.present? 170 | 171 | # import request and response of operations 172 | service.operations.each do |name, definition| 173 | if (definition.request_property) 174 | if (pkg.present?) 175 | list.push(pkg + valid_class_name(definition.request_property.type)) 176 | else 177 | list.push(get_slashed_package_name(get_namespace(schema.data_types[valid_class_name(definition.request_property.type)].package || package) + "." + valid_class_name(definition.request_property.type))) 178 | end 179 | end 180 | if (definition.response) 181 | if (pkg.present?) 182 | list.push(pkg + valid_class_name(definition.response)) 183 | else 184 | list.push(get_slashed_package_name(get_namespace(schema.data_types[valid_class_name(definition.response)].package || package) + "." + valid_class_name(definition.response))) 185 | end 186 | end 187 | if (definition.fault) 188 | if (pkg.present?) 189 | list.push(pkg + valid_class_name(definition.response)) 190 | else 191 | list.push(get_slashed_package_name(get_namespace(schema.data_types[valid_class_name(definition.fault)].package || package) + "." + valid_class_name(definition.fault))) 192 | end 193 | end 194 | end 195 | # mandatory imports 196 | list += WSDLServiceImport 197 | list.uniq.sort 198 | end 199 | 200 | def process_path_with_placeholders(classname, name, path, hostId_check = false, argument_hash = nil, query_params) 201 | path = '/' + path if !path.start_with?('/') 202 | returnPath = "" 203 | if hostId_check 204 | returnPath = path.sub(/\{[^}]*\}/, "{$this->getId()}") 205 | returnPath = '"' + returnPath 206 | else 207 | values = argument_hash.keys 208 | returnPath = path.gsub(/\{[^}]*\}/) do |match| 209 | "$#{values.shift}" 210 | end 211 | returnPath = '"' + returnPath 212 | end 213 | if query_params 214 | returnPath = returnPath +'?" . http_build_query($queryParameters)' 215 | else 216 | returnPath = returnPath + '"' 217 | end 218 | returnPath 219 | end 220 | 221 | def valid_class_name(name) 222 | name.gsub(/-/, "_").camelcase 223 | end 224 | 225 | def valid_property_name(name) 226 | name.gsub(/-/, "_").camelize(:lower) 227 | end 228 | 229 | def deprecated_function_name(name) 230 | name.gsub(/-/, "_").capitalize 231 | end 232 | 233 | def validate_function_name(name) 234 | returnName = name 235 | if name == 'list' 236 | returnName = 'all' 237 | end 238 | returnName 239 | end 240 | 241 | # Prepends do to method names that are keywords 242 | def validate_method_name(name) 243 | KeyWords.include?(name) ? "do#{valid_class_name(name)}" : valid_property_name(name) 244 | end 245 | 246 | def validated_package(package) 247 | get_slashed_package_name(capitalize_package(remove_tld_in_package(convert_ns_to_package(package)))) 248 | end 249 | 250 | # to underscore 251 | def to_underscore(name) 252 | name.gsub(/-/, "_") 253 | end 254 | 255 | def form_arguments(classname, property, name) 256 | arguments = {} 257 | property.path.scan(/{([^}]*)}/).each do |name, etc| 258 | if is_static_method(property) or name !~ /^#{classname}/i 259 | arguments[valid_property_name(name)] = "string" 260 | end 261 | end 262 | if property.request and property.request != classname 263 | arguments[valid_property_name(property.request)] = property.request 264 | end 265 | if property.type == 'GET' or property.type == 'HEAD' or property.type == 'DELETE' 266 | arguments["queryParameters"] = "array" 267 | elsif (property.parameters && allowed_params(property.parameters).size > 0) 268 | arguments["queryParameters"] = "array" 269 | end 270 | arguments 271 | end 272 | 273 | def get_object_array(classname, name, property, argument_hash) 274 | arr = [] 275 | if property.path 276 | arr.push("queryParameters") 277 | else 278 | argument_hash.each do |name, type| 279 | if name.include?("Id") and property.path =~ /\{([^}]*id)\}/ 280 | arr.push(name) 281 | end 282 | end 283 | if !is_static_method(property) and name != "create" 284 | arr.push("this.id") 285 | end 286 | end 287 | arr 288 | end 289 | 290 | def get_payload(classname, property) 291 | payload = '""'; 292 | if !is_static_method(property) 293 | if property.request == classname 294 | payload = "$this->toJSON()" 295 | elsif property.request 296 | payload = "$" + valid_property_name(property.request) + "->toJSON()" 297 | end 298 | end 299 | payload 300 | end 301 | 302 | def get_namespace(package) 303 | pkg = options[:namespace] 304 | return capitalize_package(remove_tld_in_package(convert_ns_to_package(pkg))) if pkg.present? 305 | return validated_package(package) 306 | end 307 | 308 | # Returns a hash of arguments for wsdl operations 309 | # including the request and header arguments 310 | # hash is in the format of [name] = [type] 311 | def get_wsdl_operation_arguments(operation_definition) 312 | argument_array = [] 313 | argument_array.push('$' + operation_definition.request.camelcase(:lower)) 314 | #argument_array.push('$' + operation_definition.header.camelcase(:lower)) 315 | argument_array 316 | end 317 | 318 | def get_rootname_serialization(data_type, schema) 319 | schema.services.each do |service_name, servicedef| 320 | servicedef.operations.each do |operation_name, oper_definition| 321 | if (data_type.name == oper_definition.request) 322 | temp = oper_definition.request_property.package + ":" + oper_definition.request_property.name 323 | return temp 324 | elsif (data_type.name == oper_definition.header) 325 | temp = oper_definition.header_property.package + ":" + oper_definition.header_property.name 326 | return temp 327 | end 328 | end 329 | end 330 | end 331 | 332 | # Returns set of alllowed query parameters 333 | # for a operation 334 | def allowed_params(parameters) 335 | parameters.select do |name, values| 336 | values.location != "path" 337 | end 338 | end 339 | 340 | end 341 | end 342 | end 343 | -------------------------------------------------------------------------------- /lib/genio/helper/dot_net.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | module Genio 18 | module Helper 19 | module DotNet 20 | 21 | include Base 22 | 23 | #Key/Reserved Words 24 | KeyWords = [ 25 | "abstract", 26 | "as", 27 | "base", 28 | "bool", 29 | "break", 30 | "byte", 31 | "case", 32 | "catch", 33 | "char", 34 | "checked", 35 | "class", 36 | "const", 37 | "continue", 38 | "decimal", 39 | "default", 40 | "delegate", 41 | "do", 42 | "double", 43 | "else", 44 | "enum", 45 | "event", 46 | "explicit", 47 | "extern", 48 | "false", 49 | "finally", 50 | "fixed", 51 | "float", 52 | "for", 53 | "foreach", 54 | "goto", 55 | "if", 56 | "implicit", 57 | "in", 58 | "int", 59 | "interface", 60 | "internal", 61 | "is", 62 | "lock", 63 | "long", 64 | "namespace", 65 | "new", 66 | "null", 67 | "object", 68 | "operator", 69 | "out", 70 | "override", 71 | "params", 72 | "private", 73 | "protected", 74 | "public", 75 | "readonly", 76 | "ref", 77 | "return", 78 | "sbyte", 79 | "sealed", 80 | "short", 81 | "sizeof", 82 | "stackalloc", 83 | "static", 84 | "string", 85 | "struct", 86 | "switch", 87 | "this", 88 | "throw", 89 | "true", 90 | "try", 91 | "typeof", 92 | "uint", 93 | "ulong", 94 | "unchecked", 95 | "unsafe", 96 | "ushort", 97 | "using", 98 | "virtual", 99 | "void", 100 | "volatile", 101 | "while" 102 | ] 103 | 104 | # Keyword substitute hash 105 | KeywordsSubstitute = { 106 | "readonly" => "readOnly" 107 | } 108 | 109 | # Static resource imports 110 | # Resource class which have REST operation enabled 111 | # depend on these core classes 112 | ServiceImportREST = [ 113 | "PayPal", 114 | "PayPal.Util", 115 | "System.Collections.Generic" 116 | ] 117 | 118 | # Static resource imports for WSDL 119 | # service class; they depend on these core classes 120 | ServiceImportWSDL = [ 121 | "PayPal", 122 | "PayPal.Util", 123 | "System.Collections", 124 | "System.Collections.Generic", 125 | "System.Xml" 126 | ] 127 | 128 | # Static resource imports for WSDL 129 | # stub classes; they depend on these core classes 130 | StubImportWSDL = [ 131 | "PayPal.Util", 132 | "System.Globalization", 133 | "System.Text", 134 | "System.Xml", 135 | ] 136 | 137 | # Spec Types to Dotnet Types conversion map 138 | BasicTypes = { "int" => "int", "integer" => "int", "positiveInteger" => "int", "nonNegativeInteger" => "int", "long" => "long", "double" => "double", "decimal" => "double", "float" => "float", "boolean" => "bool", "string" => "string", "dateTime" => "string", "date" => "string", "number" => "double", "object" => "object", "token" => "string", "duration" => "string", "anyURI" => "string", "date_time" => "string", "base64Binary" => "string", "time" => "string" } 139 | 140 | # Returns the type of a member 141 | # If the passed in parameter is one of Basic types 142 | # return the corresponding BasicType, else 143 | # the parameter is returned unmodified 144 | def find_basic_type(key) 145 | only_basic_type(key) || key 146 | end 147 | 148 | # Returns the corresponding basic 149 | # data type in Dotnet 150 | def only_basic_type(key) 151 | BasicTypes[key.camelcase(:lower)] 152 | end 153 | 154 | # Determines if a type of a property is nullable 155 | # Enum types are treated nullable 156 | def is_nullable_type(property) 157 | if ['int', 'long', 'double', 'float', 'bool'].include? find_basic_type(property.type) 158 | return true 159 | elsif schema.enum_types[property.type] 160 | return true 161 | else 162 | false 163 | end 164 | end 165 | 166 | # Returns the property type name to be used as the 167 | # type name in enclosing Class 168 | def get_property_class(property, classname = nil) 169 | type = find_basic_type(property.type) 170 | 171 | # If type is Self (as per Spec) treat is as HostType 172 | # classname is always in camelcase 173 | type = classname if type == "self" 174 | type = "List<#{type}>" if property.array 175 | type 176 | end 177 | 178 | # Returns the imports for the Class 179 | def imports(data_type, schema, package, classname, schema_format, operation_input = false) 180 | pkg = options[:namespace] 181 | pkg = capitalize_package(remove_tld_in_package(convert_ns_to_package(pkg))) if pkg.present? 182 | 183 | # mandatory references 184 | list = ["System"] 185 | 186 | if (schema_format == "rest") 187 | list.push("Newtonsoft.Json") 188 | list.push("Newtonsoft.Json.Serialization") 189 | end 190 | 191 | list.push("PayPal") 192 | data_type.properties.each do |name, property| 193 | type = schema.data_types[property.type] || schema.enum_types[property.type] 194 | if (type) 195 | if (pkg.present?) 196 | list.push(pkg) 197 | else 198 | # TODO fix this when definition namespace fixes 199 | defpkg = capitalize_package(remove_tld_in_package(convert_ns_to_package(type.package || package))) 200 | list.push(defpkg) 201 | end 202 | end 203 | list.push("System.Collections") if property.array 204 | list.push("System.Collections.Generic") if property.array 205 | end 206 | 207 | # Add references for members of parent datatype 208 | # flatten classes for wsdl 209 | if schema.instance_of? Genio::Parser::Format::Wsdl 210 | x_type = schema.data_types[data_type.extends] 211 | while x_type 212 | x_type.properties.each do |name, property| 213 | type = schema.data_types[property.type] || schema.enum_types[property.type] 214 | if (type) 215 | if (pkg.present?) 216 | list.push(pkg) 217 | else 218 | # TODO fix this when definition namespace fixes 219 | defpkg = capitalize_package(remove_tld_in_package(convert_ns_to_package(type.package || package))) 220 | list.push(defpkg) 221 | end 222 | end 223 | list.push("System.Collections") if property.array 224 | list.push("System.Collections.Generic") if property.array 225 | end 226 | x_type = schema.data_types[x_type.extends] 227 | end 228 | end 229 | 230 | # Add reference for request and response type 231 | # of operations: Applies to REST services 232 | service = schema.services[classname] 233 | if service 234 | service.operations.each do |name, operation| 235 | if operation.response 236 | if (pkg.present?) 237 | list.push(pkg) 238 | else 239 | list.push(capitalize_package(remove_tld_in_package(convert_ns_to_package(schema.data_types[operation.response].try(:package) || package)))) 240 | end 241 | end 242 | if operation.request 243 | if (pkg.present?) 244 | list.push(pkg) 245 | else 246 | list.push(capitalize_package(remove_tld_in_package(convert_ns_to_package(schema.data_types[operation.request].try(:package) || package)))) 247 | end 248 | end 249 | end 250 | end 251 | 252 | list += ServiceImportREST if schema.services[classname] 253 | list += StubImportWSDL if (schema_format == "soap") 254 | #TODO fix for XMLSerialization 255 | list += [] if (operation_input && (schema_format == "soap")) 256 | list.uniq.sort 257 | end 258 | 259 | # Generate imports for WSDL Service class 260 | def service_imports(schema, service, package) 261 | pkg = options[:namespace] 262 | pkg = capitalize_package(remove_tld_in_package(convert_ns_to_package(pkg))) if pkg.present? 263 | 264 | # mandatory references 265 | list = ["System"] 266 | 267 | # import request and response of operations 268 | service.operations.each do |name, definition| 269 | if (definition.request_property) 270 | if (pkg.present?) 271 | list.push(pkg) 272 | else 273 | list.push(capitalize_package(remove_tld_in_package(convert_ns_to_package(schema.data_types[validate_class_name(definition.request_property.type)].package || package)))) 274 | end 275 | end 276 | if (definition.response_property) 277 | if (pkg.present?) 278 | list.push(pkg) 279 | else 280 | list.push(capitalize_package(remove_tld_in_package(convert_ns_to_package(schema.data_types[validate_class_name(definition.response_property.type)].package || package)))) 281 | end 282 | end 283 | if (definition.fault_property) 284 | if (pkg.present?) 285 | list.push(pkg) 286 | else 287 | list.push(capitalize_package(remove_tld_in_package(convert_ns_to_package(schema.data_types[validate_class_name(definition.fault_property.type)].package || package)))) 288 | end 289 | end 290 | end 291 | 292 | # mandatory imports 293 | list += ServiceImportWSDL 294 | list.uniq.sort 295 | end 296 | 297 | # Replaces any "-" present in the path URI to valid "_" 298 | # used while replacing placeholders with exact values 299 | def validate_path(path) 300 | path.gsub(/\{([^}]*)\}/){|match| "\{#{validate_property_name($1)}\}" } 301 | end 302 | 303 | # Replaces '-' with '_' and CamelCase(s) them [DotNet] 304 | def validate_class_name(name) 305 | only_basic_type(name) || name.gsub(/-/, "_").camelcase 306 | end 307 | 308 | # Replaces '-' with '_', 309 | # used for valid property names [DotNet] 310 | # replaces keywords with substitutes form KeywordsSubstitute 311 | def validate_property_name(name, camel = false) 312 | if camel 313 | valid_name = name.gsub(/-/, "_").camelcase 314 | else 315 | valid_name = name.gsub(/-/, "_") 316 | end 317 | if KeyWords.include? valid_name 318 | valid_name = KeywordsSubstitute[valid_name] 319 | end 320 | valid_name 321 | end 322 | 323 | # Replaces '-' and spaces with '_' 324 | # used for valid enum names [DotNet] 325 | def validate_enum_name(name) 326 | name.gsub(/[-\s]/, "_").sub(/^\d/, '_\0') 327 | end 328 | 329 | # Replaces '-' with '_' and camelCase(s) them 330 | # used for valid property names as method/constructor 331 | # arguments [DotNet] 332 | def validate_property_as_argument(name) 333 | name.gsub(/-/, "_").camelcase(:lower) 334 | end 335 | 336 | # Prepends do to method names that are keywords 337 | def validate_method_name(name) 338 | KeyWords.include?(name.camelcase(:lower)) ? "Do#{name.gsub(/-/, '_').camelcase}" : name.gsub(/-/, '_').camelcase 339 | end 340 | 341 | # Generate method formal parameters for REST API calls 342 | def form_rest_api_args(classname, property, name) 343 | arguments = {} 344 | property.path.scan(/{([^}]*)}/).each do |name, etc| 345 | if is_static_method(property) or validate_class_name(name) !~ /^#{classname}/i 346 | arguments[validate_property_as_argument(name)] = "string" 347 | end 348 | end 349 | if property.request and property.request != classname 350 | arguments[validate_property_as_argument(property.request)] = property.request 351 | end 352 | if property.type == 'GET' or property.type == 'HEAD' or property.type == 'DELETE' 353 | arguments["queryParameters"] = "Dictionary" 354 | end 355 | arguments 356 | end 357 | 358 | # Generates a map of parameters for placeholders used 359 | # to process path URI 360 | def generate_format_hash(classname, property, resourcePath) 361 | map = {} 362 | resourcePath.scan(/\{([^}]*)\}/) { |name, etc| 363 | if (name.gsub(/-/, "").gsub(/_/, "").match(/^#{classname}.*/i) and !is_static_method(property)) 364 | map[name] = 'this.id' 365 | else 366 | map[name] = validate_property_as_argument(name) 367 | end 368 | } 369 | map 370 | end 371 | 372 | # Returns the expression to set for payLoad in the API call 373 | def get_payload(classname, property) 374 | payLoad = '""'; 375 | if !is_static_method(property) 376 | if property.request == classname 377 | payLoad = "this.ConvertToJson()" 378 | elsif property.request 379 | payLoad = validate_property_as_argument(property.request) + ".ConvertToJson()" 380 | end 381 | end 382 | payLoad 383 | end 384 | 385 | # Returns the name used during serialization for types 386 | # that extend Serializer interface 387 | def get_rootname_serialization(data_type, schema) 388 | schema.services.each do |service_name, servicedef| 389 | servicedef.operations.each do |operation_name, oper_definition| 390 | if (data_type.name == oper_definition.request) 391 | return (oper_definition.request_property.package + ":" + oper_definition.request_property.name) 392 | elsif (data_type.name == oper_definition.header) 393 | return (oper_definition.header_property.package + ":" + oper_definition.header_property.name) 394 | end 395 | end 396 | end 397 | end 398 | 399 | # Returns a hash of arguments for wsdl operations 400 | # including the request type and name 401 | # hash is in the format of [name] = [type] 402 | def get_wsdl_operation_arguments(operation_definition) 403 | argument_hash = {} 404 | argument_hash[validate_property_as_argument(operation_definition.request_property.name)] = operation_definition.request 405 | argument_hash 406 | end 407 | 408 | end 409 | end 410 | end 411 | -------------------------------------------------------------------------------- /lib/genio/helper/java.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 PayPal Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | 18 | module Genio 19 | module Helper 20 | module Java 21 | 22 | include Base 23 | 24 | # Key/Reserved Words 25 | KeyWords = [ 26 | "abstract", 27 | "continue", 28 | "for", 29 | "new", 30 | "switch", 31 | "assert", 32 | "default", 33 | "goto", 34 | "package", 35 | "synchronized", 36 | "boolean", 37 | "do", 38 | "if", 39 | "private", 40 | "this", 41 | "break", 42 | "double", 43 | "implements", 44 | "protected", 45 | "throw", 46 | "byte", 47 | "else", 48 | "import", 49 | "public", 50 | "throws", 51 | "case", 52 | "enum", 53 | "instanceof", 54 | "return", 55 | "transient", 56 | "catch", 57 | "extends", 58 | "int", 59 | "short", 60 | "try", 61 | "char", 62 | "final", 63 | "interface", 64 | "static", 65 | "void", 66 | "class", 67 | "finally", 68 | "long", 69 | "strictfp**", 70 | "volatile", 71 | "const", 72 | "float", 73 | "native", 74 | "super", 75 | "while" 76 | ] 77 | 78 | # Keyword substitute hash 79 | KeywordsSubstitute = { 80 | 81 | } 82 | 83 | # Static resource imports 84 | # Resource class which have REST operation enabled 85 | # depend on these core classes 86 | ServiceImportREST = [ 87 | "com.paypal.core.rest.PayPalRESTException", 88 | "com.paypal.core.rest.PayPalResource", 89 | "com.paypal.core.rest.HttpMethod", 90 | "com.paypal.core.rest.PayPalRESTException", 91 | "com.paypal.core.rest.RESTUtil", 92 | "com.paypal.core.rest.JSONFormatter", 93 | "com.paypal.core.rest.APIContext", 94 | "com.paypal.core.Constants", 95 | "com.paypal.core.SDKVersion", 96 | "com.paypal.sdk.info.SDKVersionImpl", 97 | "java.io.File", 98 | "java.io.InputStream", 99 | "java.util.Properties", 100 | "java.util.Map", 101 | "java.util.HashMap" 102 | ] 103 | 104 | # Static resource imports for WSDL 105 | # service class; they depend on these core classes 106 | ServiceImportWSDL = [ 107 | "java.io.*", 108 | "java.util.Map", 109 | "java.util.HashMap", 110 | "java.util.Properties", 111 | "com.paypal.core.BaseService", 112 | "com.paypal.exception.*", 113 | "com.paypal.sdk.exceptions.*", 114 | "com.paypal.core.APICallPreHandler", 115 | "com.paypal.core.DefaultSOAPAPICallHandler", 116 | "com.paypal.core.DefaultSOAPAPICallHandler.XmlNamespaceProvider", 117 | "org.w3c.dom.Node", 118 | "org.xml.sax.SAXException", 119 | "org.xml.sax.InputSource", 120 | "javax.xml.parsers.ParserConfigurationException", 121 | "javax.xml.parsers.DocumentBuilderFactory", 122 | "javax.xml.xpath.XPathConstants", 123 | "javax.xml.xpath.XPathExpressionException", 124 | "javax.xml.xpath.XPathFactory", 125 | "com.paypal.core.DefaultSOAPAPICallHandler", 126 | "com.paypal.core.BaseAPIContext" 127 | ] 128 | 129 | # Static resource imports for WSDL 130 | # stub classes; they depend on these core classes 131 | StubImportWSDL = [ 132 | "javax.xml.xpath.XPath", 133 | "javax.xml.xpath.XPathConstants", 134 | "javax.xml.xpath.XPathExpressionException", 135 | "javax.xml.xpath.XPathFactory", 136 | "org.w3c.dom.Node", 137 | "org.w3c.dom.NodeList", 138 | "com.paypal.core.SDKUtil" 139 | ] 140 | 141 | # Spec Types to Java Types conversion map 142 | BasicTypes = { "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" } 143 | 144 | # Returns the type of a member 145 | # If the passed in parameter is one of Basic types 146 | # return the corresponding BasicType, else 147 | # the parameter is returned unmodified 148 | def find_basic_type(key) 149 | only_basic_type(key) || key 150 | end 151 | 152 | # Returns the corresponding basic 153 | # data type in Java 154 | def only_basic_type(key) 155 | BasicTypes[key.camelcase(:lower)] 156 | end 157 | 158 | # Returns the imports for the Class 159 | def imports(data_type, schema, package, classname, schema_format, operation_input = false) 160 | list = [] 161 | 162 | # custom package provided during generation 163 | pkg = options[:namespace] 164 | pkg += "." if pkg.present? 165 | 166 | # mandatory imports 167 | list += ["com.paypal.core.rest.JSONFormatter"] if (schema_format == "rest") 168 | data_type.properties.each do |name, property| 169 | type = schema.data_types[property.type] || schema.enum_types[property.type] 170 | if (type) 171 | if (pkg.present?) 172 | list.push(pkg + property.type) 173 | else 174 | # TODO fix this when definition namespace fixes 175 | defpkg = convert_ns_to_package(type.package || package) 176 | list.push(defpkg + "." + property.type) 177 | end 178 | end 179 | list.push("java.util.List") if property.array 180 | list.push("java.util.ArrayList") if property.array 181 | end 182 | 183 | # Add references for members of parent datatype 184 | # flatten classes for wsdl 185 | if schema.instance_of? Genio::Parser::Format::Wsdl 186 | x_type = schema.data_types[data_type.extends] 187 | while x_type 188 | x_type.properties.each do |name, property| 189 | type = schema.data_types[property.type] || schema.enum_types[property.type] 190 | if (type) 191 | if (pkg.present?) 192 | list.push(pkg + property.type) 193 | else 194 | # TODO fix this when definition namespace fixes 195 | defpkg = convert_ns_to_package(type.package || package) 196 | list.push(defpkg + "." + property.type) 197 | end 198 | end 199 | list.push("java.util.List") if property.array 200 | list.push("java.util.ArrayList") if property.array 201 | end 202 | x_type = schema.data_types[x_type.extends] 203 | end 204 | end 205 | 206 | # Add reference for request and response type 207 | # of operations: Applies to REST services 208 | service = schema.services[classname] 209 | if service 210 | service.operations.each do |name, operation| 211 | if operation.response 212 | if (pkg.present?) 213 | list.push(pkg + validate_class_name(operation.response)) 214 | else 215 | list.push(convert_ns_to_package(schema.data_types[operation.response].try(:package) || package) + "." + validate_class_name(operation.response)) 216 | end 217 | end 218 | if operation.request 219 | if (pkg.present?) 220 | list.push(pkg + validate_class_name(operation.request)) 221 | else 222 | list.push(convert_ns_to_package(schema.data_types[operation.request].try(:package) || package) + "." + validate_class_name(operation.request)) 223 | end 224 | end 225 | end 226 | end 227 | 228 | list += ServiceImportREST if (schema.services[classname] && (schema_format == "rest")) 229 | list += StubImportWSDL if (schema_format == "soap") 230 | list += ["com.paypal.core.message.XMLMessageSerializer"] if (operation_input && (schema_format == "soap")) 231 | list.uniq.sort 232 | end 233 | 234 | # Generate imports for WSDL Service class 235 | def service_imports(schema, service, package) 236 | list = [] 237 | 238 | # custom package provided during generation 239 | pkg = options[:namespace] 240 | pkg += "." if pkg.present? 241 | 242 | # import request and response of operations 243 | service.operations.each do |name, definition| 244 | if (definition.request_property) 245 | if (pkg.present?) 246 | list.push(pkg + validate_class_name(definition.request_property.type)) 247 | else 248 | 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)) 249 | end 250 | end 251 | if (definition.response_property) 252 | if (pkg.present?) 253 | list.push(pkg + validate_class_name(definition.response_property.type)) 254 | else 255 | 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)) 256 | end 257 | end 258 | if (definition.fault_property) 259 | if (pkg.present?) 260 | list.push(pkg + validate_class_name(definition.fault_property.type)) 261 | else 262 | 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)) 263 | end 264 | end 265 | end 266 | 267 | # mandatory imports 268 | list += ServiceImportWSDL 269 | list.uniq.sort 270 | end 271 | 272 | # Returns the property type name to be used as the 273 | # type name in enclosing Class 274 | def get_property_class(property, classname = nil) 275 | type = find_basic_type(property.type) 276 | 277 | # If type is Self (as per Spec) treat is as HostType 278 | # classname is always in camelcase 279 | type = classname if type == "self" 280 | type = "List<#{type}>" if property.array 281 | type 282 | end 283 | 284 | # Replaces any "-" present in the path URI to valid "_" 285 | # used while replacing placeholders with exact values 286 | def validate_path(path) 287 | path.gsub(/\{([^}]*)\}/){|match| "\{#{validate_property_name($1)}\}" } 288 | end 289 | 290 | # Replaces '-' with '_' and CamelCase(s) them [Java] 291 | def validate_class_name(name) 292 | only_basic_type(name) || name.gsub(/-/, "_").camelcase 293 | end 294 | 295 | # Replaces '-' with '_' and camelCase(s) them 296 | # used for valid property names [Java] 297 | # replaces keywords with substitutes form KeywordsSubstitute 298 | def validate_property_name(name) 299 | valid_name = name.gsub(/-/, "_").camelcase(:lower) 300 | if KeyWords.include? valid_name 301 | valid_name = KeywordsSubstitute[valid_name] 302 | end 303 | valid_name 304 | end 305 | 306 | # Replaces '-' and spaces with '_' 307 | # used for valid enum names [Java] 308 | def validate_enum_name(name) 309 | name.gsub(/[-\s]/, "_").sub(/^\d/, '_\0') 310 | end 311 | 312 | # Prepends do to method names that are keywords 313 | def validate_method_name(name) 314 | KeyWords.include?(name) ? "do#{name.gsub(/-/, "_").camelcase}" : validate_property_name(name) 315 | end 316 | 317 | # Generate method formal parameters for REST API calls 318 | def form_rest_api_args(classname, property, name) 319 | arguments = {} 320 | property.path.scan(/{([^}]*)}/).each do |name, etc| 321 | if is_static_method(property) or validate_class_name(name) !~ /^#{classname}/i 322 | arguments[validate_property_name(name)] = "String" 323 | end 324 | end 325 | if property.request and property.request != classname 326 | arguments[validate_property_name(property.request)] = property.request 327 | end 328 | if property.type == 'GET' or property.type == 'HEAD' or property.type == 'DELETE' 329 | arguments["queryParameters"] = "Map" 330 | end 331 | arguments 332 | end 333 | 334 | # Generate method formal parameters for CXF interface 335 | # The argument type is appended with annotations 336 | def form_cxf_args(classname, property, name) 337 | arguments = {} 338 | arguments["context"] = "@Context final MessageContext" 339 | property.parameters.each do |name, parameter| 340 | arguments[validate_property_name(name)] = "@#{parameter.location.capitalize}Param(\"#{name}\") String" 341 | end if property.parameters 342 | if property.request 343 | arguments[property.request.camelcase(:lower)] = property.request 344 | end 345 | arguments 346 | end 347 | 348 | # Generates a map of parameters for placeholders used 349 | # to process path URI 350 | def generate_format_hash(classname, property, resourcePath) 351 | map = {} 352 | resourcePath.scan(/\{([^}]*)\}/) { |name, etc| 353 | if (name.match(/^#{classname}.*/i) and !is_static_method(property)) 354 | map[name] = 'this.getId()' 355 | else 356 | map[name] = validate_property_name(name) 357 | end 358 | } 359 | map 360 | end 361 | 362 | # Returns the expression to set for payLoad in the API call 363 | def get_payload(classname, property) 364 | payLoad = '""'; 365 | if !is_static_method(property) 366 | if property.request == classname 367 | payLoad = "this.toJSON()" 368 | elsif property.request 369 | payLoad = validate_property_name(property.request) + ".toJSON()" 370 | end 371 | end 372 | payLoad 373 | end 374 | 375 | # Returns true if data_type is in services operations 376 | # request or header, else returns false 377 | def is_operation_input(data_type, schema) 378 | schema.services.each do |service_name, servicedef| 379 | servicedef.operations.each do |operation_name, oper_definition| 380 | if (data_type.name == oper_definition.request || data_type.name == oper_definition.header) 381 | return true 382 | end 383 | end 384 | end 385 | return false 386 | end 387 | 388 | # Returns the name used during serialization for types 389 | # that extend Serializer interface 390 | def get_rootname_serialization(data_type, schema) 391 | schema.services.each do |service_name, servicedef| 392 | servicedef.operations.each do |operation_name, oper_definition| 393 | if (data_type.name == oper_definition.request) 394 | return (oper_definition.request_property.package + ":" + oper_definition.request_property.name) 395 | elsif (data_type.name == oper_definition.header) 396 | return (oper_definition.header_property.package + ":" + oper_definition.header_property.name) 397 | end 398 | end 399 | end 400 | end 401 | 402 | # Returns a hash of arguments for wsdl operations 403 | # including the request type and name 404 | # hash is in the format of [name] = [type] 405 | def get_wsdl_operation_arguments(operation_definition) 406 | argument_hash = {} 407 | argument_hash[validate_property_name(operation_definition.request_property.name)] = operation_definition.request 408 | argument_hash 409 | end 410 | 411 | private 412 | include Genio::Util::NamespaceHelper 413 | end 414 | end 415 | end 416 | --------------------------------------------------------------------------------