KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > sql > dynamic > elements > IterateTagHandler


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc parameterObject) {
26     IterateContext iterate = (IterateContext) ctx.getAttribute(tag);
27     if (iterate == null) {
28       
29       ctx.pushRemoveFirstPrependMarker(tag);
30       
31       Object JavaDoc collection;
32       String JavaDoc 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 JavaDoc parameterObject, StringBuffer JavaDoc 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       //iteratePropertyReplace(bodyContent, iterate);
62

63       if (iterate.isFirst()) {
64         String JavaDoc open = tag.getOpenAttr();
65         if (open != null) {
66           bodyContent.insert(0, open);
67         }
68       }
69       if (!iterate.isLast()) {
70         String JavaDoc conj = tag.getConjunctionAttr();
71         if (conj != null) {
72           bodyContent.append(conj);
73         }
74       }
75
76       if (iterate.isLast()) {
77         String JavaDoc 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 JavaDoc parameterObject, StringBuffer JavaDoc 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