Gadget gadget = processor.process(context);
GadgetSpec spec = gadget.getSpec();
JSONObject gadgetJson = new JSONObject();
ModulePrefs prefs = spec.getModulePrefs();
// TODO: modularize response fields based on requested items.
JSONObject views = new JSONObject();
for (View view : spec.getViews().values()) {
views.put(view.getName(), new JSONObject()
// .put("content", view.getContent())
.put("type", view.getType().toString())
.put("quirks", view.getQuirks())
.put("preferredHeight", view.getPreferredHeight())
.put("preferredWidth", view.getPreferredWidth()));
}
// Features.
Set<String> feats = prefs.getFeatures().keySet();
String[] features = feats.toArray(new String[feats.size()]);
// Links
JSONObject links = new JSONObject();
for (LinkSpec link : prefs.getLinks().values()) {
links.put(link.getRel(), link.getHref());
}
JSONObject userPrefs = new JSONObject();
// User pref specs
for (UserPref pref : spec.getUserPrefs()) {
JSONObject up = new JSONObject()
.put("displayName", pref.getDisplayName())
.put("type", pref.getDataType().toString().toLowerCase())
.put("default", pref.getDefaultValue())
.put("enumValues", pref.getEnumValues())
.put("orderedEnumValues", getOrderedEnums(pref));
userPrefs.put(pref.getName(), up);
}
// TODO: This should probably just copy all data from
// ModulePrefs.getAttributes(), but names have to be converted to
// camel case.
gadgetJson.put("iframeUrl", urlGenerator.getIframeUrl(gadget))
.put("url",context.getUrl().toString())
.put("moduleId", context.getModuleId())
.put("title", prefs.getTitle())
.put("titleUrl", prefs.getTitleUrl().toString())
.put("views", views)
.put("features", features)
.put("userPrefs", userPrefs)
.put("links", links)
// extended meta data
.put("directoryTitle", prefs.getDirectoryTitle())
.put("thumbnail", prefs.getThumbnail().toString())
.put("screenshot", prefs.getScreenshot().toString())
.put("author", prefs.getAuthor())
.put("authorEmail", prefs.getAuthorEmail())
.put("authorAffiliation", prefs.getAuthorAffiliation())
.put("authorLocation", prefs.getAuthorLocation())
.put("authorPhoto", prefs.getAuthorPhoto())
.put("authorAboutme", prefs.getAuthorAboutme())
.put("authorQuote", prefs.getAuthorQuote())
.put("authorLink", prefs.getAuthorLink())
.put("categories", prefs.getCategories())
.put("screenshot", prefs.getScreenshot().toString())
.put("height", prefs.getHeight())
.put("width", prefs.getWidth())
.put("showStats", prefs.getShowStats())
.put("showInDirectory", prefs.getShowInDirectory())
.put("singleton", prefs.getSingleton())
.put("scaling", prefs.getScaling())
.put("scrolling", prefs.getScrolling())
.put("description", prefs.getDescription());
return gadgetJson;
} catch (ProcessingException e) {
throw new RpcException(context, e);
} catch (JSONException e) {
// Shouldn't be possible