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
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"));
71 }
72
73
74 if (!getCurrentEntity().isArray() && getName()==null){
75 throw new JspException(Messages.getString("atg.taglib.json.error.property.3"));
76 }
77
78 try{
79
80 JsonEntity entity=getCurrentEntity();
81 Object value;
82
83 if (getValue()==null){
84
85
86
87 if (body==null){
88
89 value=new String();
90 }
91 else {
92
93 StringWriter writer = new StringWriter();
94 body.invoke(writer);
95 value=writer.toString();
96 }
97 }
98 else{
99
100
101 value=getValue();
102 }
103
104
105 value=trimAndEscapeValue(value);
106
107
108
109
110 entity.add(value,getName());
111
112 resetEscapeXmlValue();
113
114 }
115 catch (JspException e){
116
117
118 throw e;
119 }
120 catch (Exception e){
121 throw new JspException(Messages.getString("atg.taglib.json.error.property.1"),e);
122 }
123 }
124
125 }
126