PageRenderTime 63ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/tmm/nosql/mongodb/taglibs/GetJsonArray.java

https://github.com/robhinds/SpringMongoIntegrationExample
Java | 72 lines | 51 code | 13 blank | 8 comment | 3 complexity | a6d08504240c778e7a2b3076e88d026d MD5 | raw file
  1. /**
  2. *
  3. */
  4. package com.tmm.nosql.mongodb.taglibs;
  5. import javax.servlet.jsp.JspException;
  6. import javax.servlet.jsp.JspTagException;
  7. import javax.servlet.jsp.PageContext;
  8. import javax.servlet.jsp.tagext.TagSupport;
  9. import com.google.gson.JsonArray;
  10. import com.google.gson.JsonElement;
  11. import com.google.gson.JsonObject;
  12. /**
  13. * @author robert.hinds
  14. *
  15. * TagLib designed to get String from Google JSON Array
  16. */
  17. public class GetJsonArray extends TagSupport{
  18. private static final long serialVersionUID = 2617352233757753085L;
  19. private JsonObject json = null;
  20. private String key;
  21. private String var;
  22. @Override
  23. public int doStartTag() throws JspException {
  24. try {
  25. if (json!=null && key!=null){
  26. JsonElement j = json.get(key);
  27. JsonArray array;
  28. if (j instanceof JsonArray){
  29. array = (JsonArray) j;
  30. }else{
  31. array = new JsonArray();
  32. }
  33. pageContext.setAttribute(var, (array), PageContext.PAGE_SCOPE);
  34. }
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. throw new JspTagException("Error getting String From JSON: " + e.getMessage());
  38. }
  39. return SKIP_BODY;
  40. }
  41. public JsonObject getJson() {
  42. return json;
  43. }
  44. public void setJson(JsonObject json) {
  45. this.json = json;
  46. }
  47. public String getKey() {
  48. return key;
  49. }
  50. public void setKey(String key) {
  51. this.key = key;
  52. }
  53. public String getVar() {
  54. return var;
  55. }
  56. public void setVar(String var) {
  57. this.var = var;
  58. }
  59. }