1 /**
2 * Copyright 2007 Art Technology Group, Inc (ATG)
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 *
7 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and limitations under the License.
13 */
14
15 package atg.taglib.json;
16
17
18 import atg.taglib.json.util.JSONObject;
19
20 import java.io.StringWriter;
21 import java.util.Stack;
22
23 import javax.servlet.jsp.JspException;
24 import javax.servlet.jsp.tagext.JspFragment;
25
26
27 /**
28 * Tag that will render a JSON object. This tag is just a wrapper that should contain
29 * json:property and json:array tags.
30 *
31 * @author James Wiltshire
32 * @version $Id$
33 */
34
35 public class JsonObjectTag extends JsonBaseTag
36 {
37 /**
38 * Process the tag
39 * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
40 *
41 * @throws JspException
42 */
43 public void doTag() throws JspException{
44 JspFragment body = getJspBody();
45 Stack stack=getEntityStack();
46
47
48 if (!stack.isEmpty() && getCurrentEntity().isArray() && getName()!=null){
49 throw new JspException(Messages.getString("atg.taglib.json.error.object.4"));
50 }
51
52 try{
53
54 JsonEntity entity=new JsonEntity(new JSONObject());
55
56 stack.push(entity);
57
58
59
60
61 if (body!=null){
62 StringWriter writer = new StringWriter();
63 body.invoke(writer);
64 }
65
66
67 stack.pop();
68
69
70 if (!stack.isEmpty() && getCurrentEntity().getWrappedObject() instanceof JSONObject &&
71 (getName()==null||getName().trim().length()==0)){
72 throw new JspException(Messages.getString("atg.taglib.json.error.object.2"));
73 }
74
75
76 processTagEnd(entity);
77 }
78 catch (JspException e){
79
80
81 throw e;
82 }
83 catch (Exception e){
84 throw new JspException(Messages.getString("atg.taglib.json.error.object.0"),e);
85 }
86 }
87
88 }
89