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 java.io.StringWriter;
19  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.tagext.JspFragment;
22  
23  /**
24   * Tag to render a JSON property. This is a simple string/value pair. The value may
25   * be set either as a value= attribute of the tag, or it may be contained within the 
26   * tag's body.
27   * <br/>
28   * If the value is set in the body, then it is always assumed to be a String. However,
29   * if it is set with a value= attribute, then it may be a Boolean, Integer, Long, 
30   *
31   * @author James Wiltshire
32   * @version $Id$
33   */
34  
35  public class JsonPropertyTag extends JsonBaseTag
36  {
37    protected Object mValue;
38  
39    /**
40     * Gets the Value
41     * @return the Value
42     */
43    public Object getValue()
44    {
45      return mValue;
46    }
47  
48    /**
49     * Sets the Value
50     *
51     * @param pValue The Value to set
52     */
53    public void setValue(Object pValue)
54    {
55      mValue = pValue;
56    }
57    
58    /**
59     * Process the tag
60     * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
61     *
62     * @throws JspException
63     */
64    public void doTag() throws JspException{
65      JspFragment body = getJspBody();
66      
67      // json:property tags can't be nested in themselves
68      if (getParent()!=null && 
69          getParent().getClass().getName().equals("atg.taglib.json.JsonPropertyTag")){
70        throw new JspException(Messages.getString("atg.taglib.json.error.property.2")); //$NON-NLS-1$
71      }
72      
73      // property tag must have a name attribute unless within array tag
74      if (!getCurrentEntity().isArray() && getName()==null){
75        throw new JspException(Messages.getString("atg.taglib.json.error.property.3")); //$NON-NLS-1$
76      }
77      
78      try{            
79        // Get the current JSON object from the stack
80        JsonEntity entity=getCurrentEntity();
81        Object value;
82        
83        if (getValue()==null){
84          // No value attribute has been set, so invoke the tag body. 
85          // The tag body should contain textual data.
86          
87          if (body==null){
88            // null value attribute and no tag body so set value to empty string
89            value=new String();
90          }
91          else {
92            // Invoke the tag body, capturing the output to a writer
93            StringWriter writer = new StringWriter();
94            body.invoke(writer);
95            value=writer.toString();
96          }
97        }
98        else{
99          // Value attribute has been set, so ignore body and use whatever is set 
100         // for the value
101         value=getValue();
102       }
103       
104       // Trim and xml-escape the value
105       value=trimAndEscapeValue(value);
106       
107       // Put a new property into the JSON object. This performs some validation on the type passed and
108       // will throw a JSONException if invalid. Any unrecognized object type will just have toString() 
109       // called to obtain a String value.
110       entity.add(value,getName());
111       
112       resetEscapeXmlValue();
113       
114     }
115     catch (JspException e){
116       // Just rethrow if we got a JspException - it's probably from a nested tag - no new
117       // info for us to add here.
118       throw e;
119     }
120     catch (Exception e){
121       throw new JspException(Messages.getString("atg.taglib.json.error.property.1"),e); //$NON-NLS-1$
122     }
123   }
124 
125 }
126