1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.tags;
17  
18  import java.io.IOException;
19  import java.io.Writer;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.servlet.jsp.JspContext;
24  import javax.servlet.jsp.JspException;
25  import javax.servlet.jsp.tagext.JspFragment;
26  import javax.servlet.jsp.tagext.JspTag;
27  import javax.servlet.jsp.tagext.SimpleTag;
28  
29  public class MockJspFragment extends JspFragment {
30  
31  	private JspContext context;
32  	private String body = "";
33  	private List<JspTag> children = new ArrayList<JspTag>();
34  
35  	public void setJspContext(JspContext context) {
36  		this.context = context;
37  	}
38  
39  	@Override
40  	public JspContext getJspContext() {
41  		return context;
42  	}
43  
44  	@Override
45  	public void invoke(Writer out) throws JspException, IOException {
46  		out.write(body);
47  		for (JspTag child : children) {
48  			if (child instanceof SimpleTag) {
49  				SimpleTag simpleTag = (SimpleTag) child;
50  				simpleTag.setJspContext(getJspContext());
51  				simpleTag.doTag();
52  			} else {
53  				throw new UnsupportedOperationException();
54  			}
55  		}
56  	}
57  
58  	public void setBody(String body) {
59  		this.body = body;
60  	}
61  
62  	public void addChild(JspTag child) {
63  		children.add(child);
64  	}
65  }