View Javadoc

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      // Name shouldn't be set if we're within an array tag
48      if (!stack.isEmpty() && getCurrentEntity().isArray() && getName()!=null){
49        throw new JspException(Messages.getString("atg.taglib.json.error.object.4")); //$NON-NLS-1$
50      }
51          
52      try{    
53        // Create a new JSON object and push it onto the top of the stack
54        JsonEntity entity=new JsonEntity(new JSONObject());
55        
56        stack.push(entity);
57        
58        // Process the tag body - this will add properties (primitives, objects or arrays) to
59        // the JSON Object on the top of the stack
60        // Writer passed here is used to prevent any body content being written to output stream
61        if (body!=null){
62          StringWriter writer = new StringWriter();
63          body.invoke(writer);
64        }
65        
66        // Pop the object from the stack, the parent object is now on the top of the stack
67        stack.pop();     
68        
69        // Ensure that we have a name set if the object on the stack is a JSONObject
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        // Set or add property on parent object - this may be either a JSONObject or a JSONArray
76        processTagEnd(entity);
77      }
78      catch (JspException e){
79        // Just rethrow if we got a JspException - it's probably from a nested tag - no new
80        // info for us to add here.
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