PageRenderTime 1223ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/robhinds/SpringMongoIntegrationExample
Java | 91 lines | 52 code | 19 blank | 20 comment | 3 complexity | 73e9895449e9675bd51f07817be2243a MD5 | raw file
  1. /**
  2. *
  3. */
  4. package com.tmm.nosql.mongodb.taglibs;
  5. import javax.servlet.jsp.JspException;
  6. import javax.servlet.jsp.PageContext;
  7. import javax.servlet.jsp.tagext.TagSupport;
  8. import com.google.gson.JsonArray;
  9. /**
  10. * @author robert.hinds
  11. *
  12. * TagLib designed to iterate through Google JSON Array
  13. */
  14. public class IterateJson extends TagSupport{
  15. private static final long serialVersionUID = -102924264339712780L;
  16. private JsonArray items = null;
  17. private int position=0;
  18. private String var;
  19. private String counter;
  20. @Override
  21. public int doStartTag() throws JspException{
  22. return processIteration();
  23. }
  24. private int processIteration() {
  25. if (items!=null && items.size()>position){
  26. pageContext.setAttribute(var, items.get(position), PageContext.PAGE_SCOPE);
  27. if (counter!=null){
  28. pageContext.setAttribute(counter, position, PageContext.PAGE_SCOPE);
  29. }
  30. position ++;
  31. return EVAL_BODY_AGAIN;
  32. }
  33. else{
  34. position=0;
  35. return SKIP_BODY;
  36. }
  37. }
  38. @Override
  39. public int doAfterBody() throws JspException {
  40. return processIteration();
  41. }
  42. /**
  43. * @param items the items to set
  44. */
  45. public void setItems(JsonArray items) {
  46. this.items = items;
  47. }
  48. /**
  49. * @return the items
  50. */
  51. public JsonArray getItems() {
  52. return items;
  53. }
  54. /**
  55. * @param var the var to set
  56. */
  57. public void setVar(String var) {
  58. this.var = var;
  59. }
  60. /**
  61. * @return the var
  62. */
  63. public String getVar() {
  64. return var;
  65. }
  66. public String getCounter() {
  67. return counter;
  68. }
  69. public void setCounter(String counter) {
  70. this.counter = counter;
  71. }
  72. }