<p> function JavaCodeGenerator(code_generator){
code_generator.reset();
this.code_generator = code_generator;
this.code_generator.json_options['null_value'] = 'None';
this.code_generator.json_options['true_value'] = 'True';
this.code_generator.json_options['false_value'] = 'False';
this.code_generator.addPreGenerator('java', 'generateImports');
this.code_generator.addPreGenerator('java', 'generateClient');
this.code_generator.addServiceGenerator('java', 'mqlread');
// this.code_generator.addServiceGenerator('java', 'search');
// this.code_generator.addServiceGenerator('java', 'topic');
this.code_generator.addPostGenerator('java', 'generateClientEnd');
this.indent = [];
}
CodeGenerator.generatorsByLanguage['java'] = JavaCodeGenerator;
JavaCodeGenerator.prototype.generateImports = function(params, options) {
var output = "";
var libs = [];
if (options['library'] != null) {
libs = [
'org.json.simple.JSONArray',
'org.json.simple.JSONObject',
'org.json.simple.parser.JSONParser',
'com.freebase.FreebaseClient'
];
} else {
libs = [
'org.apache.http.HttpResponse',
'org.apache.http.client.HttpClient',
'org.apache.http.client.methods.HttpGet',
'org.apache.http.impl.client.DefaultHttpClient',
'org.apache.http.util.EntityUtils',
'org.json.simple.JSONArray',
'org.json.simple.JSONObject',
'org.json.simple.parser.JSONParser',
'java.net.URLEncoder',
];
}
for (i in libs) {
output += "import " + libs[i] + ";\n";
}
output += "\n";
return output;
}
JavaCodeGenerator.prototype.generateClient = function(params, options) {
var service = options['service'];
var output = "";
output += "public class NewFreebaseMqlReadTest {\n";
this.increaseIndent();
output += this.getIndent() + "public static void main(String[] args) {\n";
this.increaseIndent();
output += this.getIndent() + "try {\n";
this.increaseIndent();
output += this.getIndent() + this.const("developer_key") + " = open('DEVELOPER_KEY').read()\n"
if (options['library'] != null) {
output += this.getIndent() + "FreebaseClient " + this.var("freebase_client") + " = new FreebaseClient(" + this.const("developer_key");
if (options['version'] != CodeGenerator.defaultApiVersion) {
output += ", version='" + options['version'] + "')\n";
} else {
output += ");\n";
}
} else {
var service_url = "https://" + options['domain'] + "/" + options['api'] + "/" + options['version'] + "/" + service;
output += this.getIndent() + "String " + this.var("service_url") + " = '" + service_url + "'\n";
}
return output;
}
JavaCodeGenerator.prototype.generateClientEnd = function(params, options) {
var output = "";
this.decreaseIndent();
output += this.getIndent() + "} catch (Exception ex) {\n";
this.increaseIndent();
output += this.getIndent() + "ex.printStackTrace();\n";
this.decreaseIndent();
output += this.getIndent() + "}\n";
this.decreaseIndent();
output += this.getIndent() + "}\n";
this.decreaseIndent();
output += this.getIndent() + "}\n";
this.decreaseIndent();
return output;
}
JavaCodeGenerator.prototype.formatVariableName = function(name_parts, is_constant) {
if (!is_constant) {
name_parts[0] = name_parts[0].toLowerCase();
}
var var_name = name_parts.join(is_constant ? '_' : '');
return is_constant ? var_name.toUpperCase() : var_name;
}
JavaCodeGenerator.prototype.var = function(key) {
return this.formatVariableName(CodeGenerator.variableNames[key], false);
}
JavaCodeGenerator.prototype.const = function(key) {
return this.formatVariableName(CodeGenerator.variableNames[key], true);
}
JavaCodeGenerator.prototype.getIndent = function() {
return this.indent.join('');
}
JavaCodeGenerator.prototype.increaseIndent = function() {
return this.indent.push(CodeGenerator.tab);
}
JavaCodeGenerator.prototype.decreaseIndent = function() {
return this.indent.pop();
}
JavaCodeGenerator.prototype.generateMqlread = function(params, options) {
var query = JSON.parse(params['query']);
var multiresult_query = ((query instanceof Array) && query.length == 1) || !params['cursor'];
var output = "";
output += this.getIndent() + "String " + this.var("query") + " = " + this.formatMqlQuery(query) + ";\n";
if (options['library'] != null) {
if (params['cursor'] == '') {
var filtered_params = this.code_generator.filterParams(params, ["query","cursor"]);
output += this.var("results") + " = " + this.var("freebase_client") + ".mqlreaditer(" + this.var("query");
if (filtered_params.size() > 0) {
output += "," + this.formatParams(filtered_params, options) + ")\n\n";
} else {
output += ")\n";
}
if (multiresult_query) {
output += "\nfor " + this.var("topic") + " in " + this.var("results") + ":\n" + CodeGenerator.tab;
} else {
output += this.getIndent() + "JSONObject " + this.var("topic") + " = " + this.var("response") + "['result']\n\n";
}
} else {
var filtered_params = this.code_generator.filterParams(params, ["query"]);
output += this.getIndent() + "JSONObject " + this.var("response") + " = " + this.var("freebase_client") + ".mqlread(" + this.var("query");
if (filtered_params.size() > 0) {
output += "," + this.formatParams(filtered_params, options) + ")\n\n";
} else {
output += ");\n";
}
if (multiresult_query) {
output += "\n" + this.getIndent() + "for " + this.var("topic") + " in " + this.var("response") + "['result']:\n" + CodeGenerator.tab;
} else {
output += this.getIndent() + "JSONObject " + this.var("topic") + " = " + this.var("response") + "['result']\n\n";
}
}
} else {
var filtered_params = this.code_generator.filterParams(params, ["query"]);
if (filtered_params.size() > 0) {
output += this.formatParams(filtered_params, options);
}
output += this.getIndent() + "String " + this.var("url") + " = " + this.var("service_url") + " + '?query=' + " + "json.dumps(" + this.var("query") + ")";
if (filtered_params.size() > 0) {
output += " + " + "urllib.urlencode(" + this.var("params") + ")" + ";\n";
} else {
output += ";\n";
}
output += this.var("response") + " = json.loads(urllib.urlopen(" + this.var("url") + ").read())\n\n";
if (multiresult_query) {
output += this.getIndent() + "for (Object " + this.var("topic") + " : " + this.var("results") + ") {\n";
this.increaseIndent();
} else {
output += this.getIndent() + "JSONObject " + this.var("topic") + " = " + this.var("response") + "['result']\n\n";
}
if (multiresult_query) {
this.increaseIndent();
output += this.getIndent() + "}\n";
}
}
var path = this.code_generator.getQuerySamplePath(query);
output += "print " + this.var("topic") + this.formatPath(path) + "\n";
return output;
}
JavaCodeGenerator.prototype.formatParams = function(params, options) {
var output = "";
if (options['library'] != null) {
var json_options = {
key_delimiter: '',
object_key_seperator: ' = ',
object_start: '',
object_end: ''
};
output += this.code_generator.formatJSON(params.params, params.size() > 1, json_options);
} else {
output += this.var("params") + " = ";
output += this.code_generator.formatJSON(params.params, params.size() > 1);
output += "\n";
}
return output;
}
JavaCodeGenerator.prototype.formatMqlQuery = function(mql_query, params, options) {
var query = this.code_generator.formatJSON(mql_query, false);
if (query.length > 80) {
query = this.code_generator.formatJSON(mql_query, true);
}
return query;
}
JavaCodeGenerator.prototype.formatPath = function(path, params, options) {
var output = "";
for (var i=0; i</p>