001    // Copyright May 16, 2006 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package net.sf.tacos.util;
016    
017    import org.apache.tapestry.IMarkupWriter;
018    import org.apache.tapestry.NestedMarkupWriter;
019    import org.apache.tapestry.json.JSONObject;
020    import org.apache.tapestry.markup.Attribute;
021    
022    /**
023     * A {@link IMarkupWriter} that only gathers attributes and
024     * attribute values into a JSON structure.
025     */
026    public class JSONMarkupWriter implements IMarkupWriter
027    {
028        private JSONObject json;
029        
030        public JSONMarkupWriter()
031        {
032            json = new JSONObject();
033        }
034        
035        public JSONObject getAttributes()
036        {
037            return json;
038        }
039    
040        public void attribute(String name, int value)
041        {
042            json.put(name, value);
043        }
044    
045        public void attribute(String name, boolean value)
046        {
047            json.put(name, value);
048        }
049    
050        public void attribute(String name, String value)
051        {
052            // look for hints
053            if (name.endsWith("AsInt"))
054                attribute(name.substring(0, name.length() - 5), Integer.parseInt(value));
055            else if (name.endsWith("AsBoolean"))
056                attribute(name.substring(0, name.length() - 9), Boolean.valueOf(value).booleanValue());
057            
058            // look for known names
059            else if (name.endsWith("Duration"))
060                attribute(name, Integer.parseInt(value));
061            else
062                json.put(name, value);
063        }
064    
065        public void attributeRaw(String name, String value)
066        {
067            json.put(name, value);
068        }
069        
070        public void appendAttribute(String name, int value)
071        {        
072        }
073    
074        public void appendAttribute(String name, boolean value)
075        {        
076        }
077    
078        public void appendAttribute(String name, String value)
079        {        
080        }
081    
082        public void appendAttributeRaw(String name, String value)
083        {        
084        }
085        
086        public boolean hasAttribute(String name)
087        {
088            return false;
089        }
090        
091        public Attribute getAttribute(String name)
092        {
093            return null;
094        }
095        
096        public Attribute removeAttribute(String name)
097        {
098            return null;
099        }
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    }