KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.PrintWriter JavaDoc;
19 import java.io.StringWriter JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
26
27 public class SqlTagContext {
28
29   private StringWriter JavaDoc sw;
30   private PrintWriter JavaDoc out;
31
32   private HashMap JavaDoc attributes;
33
34   private LinkedList JavaDoc removeFirstPrependStack;
35   private LinkedList JavaDoc iterateContextStack;
36
37   private ArrayList JavaDoc parameterMappings = new ArrayList JavaDoc();
38
39
40   public SqlTagContext() {
41     sw = new StringWriter JavaDoc();
42     out = new PrintWriter JavaDoc(sw);
43     attributes = new HashMap JavaDoc();
44     removeFirstPrependStack = new LinkedList JavaDoc();
45     iterateContextStack = new LinkedList JavaDoc();
46   }
47
48   public PrintWriter JavaDoc getWriter() {
49     return out;
50   }
51
52   public String JavaDoc getBodyText() {
53     out.flush();
54     return sw.getBuffer().toString();
55   }
56
57   public void setAttribute(Object JavaDoc key, Object JavaDoc value) {
58     attributes.put(key, value);
59   }
60
61   public Object JavaDoc getAttribute(Object JavaDoc key) {
62     return attributes.get(key);
63   }
64
65   public void addParameterMapping(ParameterMapping mapping) {
66     parameterMappings.add(mapping);
67   }
68
69   public List JavaDoc getParameterMappings() {
70     return parameterMappings;
71   }
72
73   public boolean isEmptyRemoveFirtPrepend() {
74     return removeFirstPrependStack.size() <= 0;
75   }
76   
77   /**
78    * examine the value of the top RemoveFirstPrependMarker object on the stack
79    * @return
80    */

81   public boolean peekRemoveFirstPrependMarker(SqlTag sqlTag) {
82     
83       RemoveFirstPrependMarker removeFirstPrepend =
84         (RemoveFirstPrependMarker) removeFirstPrependStack.get(1);
85       
86       return removeFirstPrepend.isRemoveFirstPrepend();
87   }
88   
89   /**
90    * pop the first RemoveFirstPrependMarker once the recursion is on it's way out
91    * of the recursion loop and return it's internal value.
92    *
93    * @param tag
94    */

95   public void popRemoveFirstPrependMarker(SqlTag tag) {
96         
97     RemoveFirstPrependMarker removeFirstPrepend =
98       (RemoveFirstPrependMarker) removeFirstPrependStack.getFirst();
99     
100     if(tag == removeFirstPrepend.getSqlTag()) {
101       removeFirstPrependStack.removeFirst();
102     }
103   }
104   
105   /**
106    * push a new RemoveFirstPrependMarker object with the specified internal state
107    *
108    * @param tag
109    */

110   public void pushRemoveFirstPrependMarker(SqlTag tag) {
111     
112     if(tag.getHandler() instanceof DynamicTagHandler) {
113       // this was added to retain default behavior
114
if(tag.isPrependAvailable()) {
115         removeFirstPrependStack.addFirst(
116             new RemoveFirstPrependMarker(tag,true));
117       } else {
118         removeFirstPrependStack.addFirst(
119             new RemoveFirstPrependMarker(tag,false));
120       }
121     } else if("true".equals(tag.getRemoveFirstPrepend())){
122       // you must be specific about the removal otherwise it
123
// will function as ibatis has always functioned and add
124
// the prepend
125
removeFirstPrependStack.addFirst(
126           new RemoveFirstPrependMarker(tag,true));
127     } else if(!tag.isPrependAvailable() &&
128         !"true".equals(tag.getRemoveFirstPrepend()) &&
129         tag.getParent() != null) {
130       // if no prepend or removeFirstPrepend is specified
131
// we need to look to the parent tag for default values
132
if("true".equals(tag.getParent().getRemoveFirstPrepend())) {
133         removeFirstPrependStack.addFirst(
134             new RemoveFirstPrependMarker(tag,true));
135       }
136     } else {
137       removeFirstPrependStack.addFirst(
138           new RemoveFirstPrependMarker(tag,false));
139     }
140
141   }
142   
143   /**
144    * set a new internal state for top RemoveFirstPrependMarker object
145    *
146    */

147   public void disableRemoveFirstPrependMarker() {
148     ((RemoveFirstPrependMarker) removeFirstPrependStack.get(1)).setRemoveFirstPrepend(false);
149   }
150   
151   /**
152    * iterate context is stored here for nested dynamic tags in
153    * the body of the iterate tag
154    *
155    * @param iterateContext
156    */

157   public void pushIterateContext(IterateContext iterateContext) {
158     iterateContextStack.addFirst(iterateContext);
159   }
160   
161   /**
162    * iterate context is removed here from the stack when iterate tag is finished being
163    * processed
164    *
165    * @return
166    */

167   public IterateContext popIterateContext() {
168     IterateContext retVal = null;
169     if(!iterateContextStack.isEmpty()) {
170       retVal = (IterateContext)iterateContextStack.removeFirst();
171     }
172     return retVal;
173   }
174   
175   /**
176    * iterate context is removed here from the stack when iterate tag is finished being
177    * processed
178    *
179    * @return
180    */

181   public IterateContext peekIterateContext() {
182     IterateContext retVal = null;
183     if(!iterateContextStack.isEmpty()) {
184       retVal = (IterateContext)iterateContextStack.getFirst();
185     }
186     return retVal;
187   }
188   
189 }
190
191 /**
192  *
193  * This inner class i used strictly to house whether the
194  * removeFirstPrepend has been used in a particular nested
195  * situation.
196  *
197  * @author Brandon Goodin
198  */

199 class RemoveFirstPrependMarker {
200   
201   private boolean removeFirstPrepend;
202   private SqlTag tag;
203   
204   /**
205    *
206    */

207   public RemoveFirstPrependMarker(SqlTag tag, boolean removeFirstPrepend) {
208     this.removeFirstPrepend = removeFirstPrepend;
209     this.tag = tag;
210   }
211   
212   /**
213    * @return Returns the removeFirstPrepend.
214    */

215   public boolean isRemoveFirstPrepend() {
216     return removeFirstPrepend;
217   }
218   
219   /**
220    * @param removeFirstPrepend The removeFirstPrepend to set.
221    */

222   public void setRemoveFirstPrepend(boolean removeFirstPrepend) {
223     this.removeFirstPrepend = removeFirstPrepend;
224   }
225   
226   /**
227    * @return Returns the sqlTag.
228    */

229   public SqlTag getSqlTag() {
230     return tag;
231   }
232   
233 }
Popular Tags