View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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 org.apache.commons.jelly.tags.sql;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.commons.jelly.JellyTagException;
23  import org.apache.commons.jelly.MissingAttributeException;
24  import org.apache.commons.jelly.TagSupport;
25  import org.apache.commons.jelly.XMLOutput;
26  
27  /***
28   * This Tag creates a result set object based on its body content via child row tags.
29   * This tag is useful for unit testing with Mock Tags to simulate the results returned by databases.
30   *
31   */
32  public class ResultSetTag extends TagSupport {
33  
34      private List rows;
35      private String var;
36  
37      /***
38       * Adds the given row to the list of rows
39       */
40      public void addRow(Map row) {
41          rows.add(row);
42      }
43  
44      // Tag interface
45      //-------------------------------------------------------------------------
46      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
47          if (var == null) {
48              throw new MissingAttributeException( "var" );
49          }
50          rows = new ArrayList();
51          invokeBody(output);
52  
53          // now lets create a new Result implementation
54          ResultImpl results = new ResultImpl( rows );
55          context.setVariable(var, results);
56          rows = null;
57      }
58  
59      // Properties
60      //-------------------------------------------------------------------------
61      /***
62       * Sets the variable to export the result set to.
63       */
64      public void setVar(String var) {
65          this.var = var;
66      }
67  
68  }