View Javadoc

1   // Copyright May 16, 2006 The Apache Software Foundation
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     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
13  // limitations under the License.
14  
15  package net.sf.tacos.util;
16  
17  import org.apache.tapestry.IMarkupWriter;
18  import org.apache.tapestry.NestedMarkupWriter;
19  import org.apache.tapestry.json.JSONObject;
20  import org.apache.tapestry.markup.Attribute;
21  
22  /**
23   * A {@link IMarkupWriter} that only gathers attributes and
24   * attribute values into a JSON structure.
25   */
26  public class JSONMarkupWriter implements IMarkupWriter
27  {
28      private JSONObject json;
29      
30      public JSONMarkupWriter()
31      {
32          json = new JSONObject();
33      }
34      
35      public JSONObject getAttributes()
36      {
37          return json;
38      }
39  
40      public void attribute(String name, int value)
41      {
42          json.put(name, value);
43      }
44  
45      public void attribute(String name, boolean value)
46      {
47          json.put(name, value);
48      }
49  
50      public void attribute(String name, String value)
51      {
52          // look for hints
53          if (name.endsWith("AsInt"))
54              attribute(name.substring(0, name.length() - 5), Integer.parseInt(value));
55          else if (name.endsWith("AsBoolean"))
56              attribute(name.substring(0, name.length() - 9), Boolean.valueOf(value).booleanValue());
57          
58          // look for known names
59          else if (name.endsWith("Duration"))
60              attribute(name, Integer.parseInt(value));
61          else
62              json.put(name, value);
63      }
64  
65      public void attributeRaw(String name, String value)
66      {
67          json.put(name, value);
68      }
69      
70      public void appendAttribute(String name, int value)
71      {        
72      }
73  
74      public void appendAttribute(String name, boolean value)
75      {        
76      }
77  
78      public void appendAttribute(String name, String value)
79      {        
80      }
81  
82      public void appendAttributeRaw(String name, String value)
83      {        
84      }
85      
86      public boolean hasAttribute(String name)
87      {
88          return false;
89      }
90      
91      public Attribute getAttribute(String name)
92      {
93          return null;
94      }
95      
96      public Attribute removeAttribute(String name)
97      {
98          return null;
99      }
100     
101     public void clearAttributes()
102     {        
103     }
104 
105     public void begin(String name)
106     {
107     }
108 
109     public void beginEmpty(String name)
110     {
111     }
112 
113     public boolean checkError()
114     {
115         return false;
116     }
117 
118     public void close()
119     {
120     }
121 
122     public void closeTag()
123     {
124     }
125 
126     public void comment(String value)
127     {
128     }
129 
130     public void end()
131     {
132     }
133 
134     public void end(String name)
135     {
136     }
137 
138     public void flush()
139     {
140     }
141 
142     public NestedMarkupWriter getNestedWriter()
143     {
144         return null;
145     }
146 
147     public void print(char[] data, int offset, int length)
148     {
149     }
150 
151     public void print(char[] data, int offset, int length, boolean raw)
152     {
153     }
154 
155     public void print(char value)
156     {
157     }
158 
159     public void print(int value)
160     {
161     }
162 
163     public void print(String value)
164     {
165     }
166 
167     public void print(String value, boolean raw)
168     {
169     }
170 
171     public void println()
172     {
173     }
174 
175     public void printRaw(char[] buffer, int offset, int length)
176     {
177     }
178 
179     public void printRaw(String value)
180     {
181     }
182 
183     public String getContentType()
184     {
185         return "";
186     }
187     
188 }