1 16 package com.ibatis.sqlmap.engine.mapping.sql.dynamic.elements; 17 18 import com.ibatis.common.beans.Probe; 19 import com.ibatis.common.beans.ProbeFactory; 20 21 public class IterateTagHandler extends BaseTagHandler { 22 23 private static final Probe PROBE = ProbeFactory.getProbe(); 24 25 public int doStartFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject) { 26 IterateContext iterate = (IterateContext) ctx.getAttribute(tag); 27 if (iterate == null) { 28 29 ctx.pushRemoveFirstPrependMarker(tag); 30 31 Object collection; 32 String prop = tag.getPropertyAttr(); 33 if (prop != null) { 34 collection = PROBE.getObject(parameterObject, prop); 35 } else { 36 collection = parameterObject; 37 } 38 iterate = new IterateContext(collection,tag); 39 40 iterate.setProperty( null == prop ? "" : prop ); 41 42 ctx.setAttribute(tag, iterate); 43 ctx.pushIterateContext(iterate); 44 } 45 if (iterate != null && iterate.hasNext()) { 46 return INCLUDE_BODY; 47 } else { 48 return SKIP_BODY; 49 } 50 } 51 52 public int doEndFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject, StringBuffer bodyContent) { 53 IterateContext iterate = (IterateContext) ctx.getAttribute(tag); 54 55 if (iterate.hasNext() || iterate.isFinal()) { 56 57 if(iterate.isAllowNext()) { 58 iterate.next(); 59 } 60 61 63 if (iterate.isFirst()) { 64 String open = tag.getOpenAttr(); 65 if (open != null) { 66 bodyContent.insert(0, open); 67 } 68 } 69 if (!iterate.isLast()) { 70 String conj = tag.getConjunctionAttr(); 71 if (conj != null) { 72 bodyContent.append(conj); 73 } 74 } 75 76 if (iterate.isLast()) { 77 String close = tag.getCloseAttr(); 78 if (close != null) { 79 bodyContent.append(close); 80 } 81 } 82 83 iterate.setAllowNext(true); 84 if(iterate.isFinal()) { 85 return super.doEndFragment(ctx,tag,parameterObject,bodyContent); 86 } else { 87 return REPEAT_BODY; 88 } 89 90 } else { 91 return super.doEndFragment(ctx,tag,parameterObject,bodyContent); 92 } 93 } 94 95 public void doPrepend(SqlTagContext ctx, SqlTag tag, Object parameterObject, StringBuffer bodyContent) { 96 IterateContext iterate = (IterateContext) ctx.getAttribute(tag); 97 if (iterate.isFirst()) { 98 super.doPrepend(ctx, tag, parameterObject, bodyContent); 99 } 100 } 101 102 public boolean isPostParseRequired() { 103 return true; 104 } 105 106 } 107 108 | Popular Tags |