View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.serializer.objects;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import javolution.xml.XMLFormat;
24  import javolution.xml.stream.XMLStreamException;
25  
26  import org.apache.commons.lang.StringEscapeUtils;
27  import org.apache.jetspeed.capabilities.MediaType;
28  
29  public class JSMediaType
30  {
31  	// private int refID;
32  
33  	private String name;
34  
35  	private int id;
36  
37  	private String characterSet;
38  
39  	private String title;
40  
41  	private String description;
42  
43      private ArrayList capabilities;
44  
45      private ArrayList mimeTypes;
46  
47      private JSClientCapabilities capabilitiesString;
48  
49      private JSClientMimeTypes mimeTypesString;
50  
51  
52  	public JSMediaType()
53  	{
54  		// refID = id;
55  	}
56  
57  	public JSMediaType(MediaType c)
58  	{
59  		this.id = c.getMediatypeId();
60  		this.name = c.getName();
61  
62  		this.characterSet = c.getCharacterSet();
63  		this.title = c.getTitle();
64  		this.description = c.getDescription();
65          capabilities = new ArrayList();
66          mimeTypes = new ArrayList();
67          
68  	}
69  
70  	/****************************************************************************
71  	 * SERIALIZER
72  	 */
73  	private static final XMLFormat XML = new XMLFormat(JSMediaType.class)
74  	{
75  		public void write(Object o, OutputElement xml)
76  				throws XMLStreamException
77  		{
78  
79  			try
80  			{
81  				JSMediaType g = (JSMediaType) o;
82                  /*** attributes here */
83  
84  				xml.setAttribute("name", g.name);
85  
86                  /*** named fields HERE */
87  
88                  xml.add( g.characterSet, "charcterSet",String.class);
89  				xml.add(g.title,"title", String.class);
90  				xml.add(g.description, "description", String.class);
91  
92                  /*** implicitly named (through binding) fields here */
93  
94                  g.capabilitiesString = new JSClientCapabilities(g.putTokens(g.capabilities));
95                  g.mimeTypesString = new JSClientMimeTypes(g.putTokens(g.mimeTypes));
96                  xml.add(g.capabilitiesString);
97                  xml.add(g.mimeTypesString);
98  
99  				// xml.add(g.groupString);
100 
101 			} catch (Exception e)
102 			{
103 				e.printStackTrace();
104 			}
105 		}
106 
107 		public void read(InputElement xml, Object o)
108 		{
109 			try
110 			{
111 				JSMediaType g = (JSMediaType) o;
112                g.name = StringEscapeUtils.unescapeHtml(xml.getAttribute("name",""));
113 
114                 /*** named fields HERE */
115                 Object o1 = xml.get("charcterSet",String.class); //characterSet
116                 if ((o1 != null) && (o1 instanceof String))
117                 	g.characterSet = StringEscapeUtils.unescapeHtml((String)o1);
118                 g.title = StringEscapeUtils.unescapeHtml((String)xml.get("title", String.class)); //title;
119                 g.description  = StringEscapeUtils.unescapeHtml((String)xml.get("description", String.class)); //description;
120 
121                 while (xml.hasNext())
122                 {
123                     o1 = xml.getNext(); // mime
124 
125                     if (o1 instanceof JSClientCapabilities)
126                         g.capabilitiesString = (JSClientCapabilities) o1; //capabilitiesString;
127                     else
128                         if (o1 instanceof JSClientMimeTypes)
129                             g.mimeTypesString  = (JSClientMimeTypes)o1; //mimeTypesString;
130                 }
131 			} catch (Exception e)
132 			{
133 				e.printStackTrace();
134 			}
135 		}
136 	};
137 
138 
139 	/***
140 	 * @return Returns the characterSet.
141 	 */
142 	public String getCharacterSet()
143 	{
144 		return characterSet;
145 	}
146 
147 	/***
148 	 * @param characterSet The characterSet to set.
149 	 */
150 	public void setCharacterSet(String characterSet)
151 	{
152 		this.characterSet = characterSet;
153 	}
154 
155 	/***
156 	 * @return Returns the description.
157 	 */
158 	public String getDescription()
159 	{
160 		return description;
161 	}
162 
163 	/***
164 	 * @param description The description to set.
165 	 */
166 	public void setDescription(String description)
167 	{
168 		this.description = description;
169 	}
170 
171 	/***
172 	 * @return Returns the id.
173 	 */
174 	public int getId()
175 	{
176 		return id;
177 	}
178 
179 	/***
180 	 * @param id The id to set.
181 	 */
182 	public void setId(int id)
183 	{
184 		this.id = id;
185 	}
186 
187 	/***
188 	 * @return Returns the name.
189 	 */
190 	public String getName()
191 	{
192 		return name;
193 	}
194 
195 	/***
196 	 * @param name The name to set.
197 	 */
198 	public void setName(String name)
199 	{
200 		this.name = name;
201 	}
202 
203 	/***
204 	 * @return Returns the title.
205 	 */
206 	public String getTitle()
207 	{
208 		return title;
209 	}
210 
211 	/***
212 	 * @param title The title to set.
213 	 */
214 	public void setTitle(String titel)
215 	{
216 		this.title = titel;
217 	}
218     private String append(JSCapability capability)
219     {
220         return capability.getName();
221     }
222 
223     private String append(JSMimeType mime)
224     {
225         return mime.getName();
226     }
227 
228     private String append(Object s)
229     {
230         if (s instanceof JSCapability)
231             return append((JSCapability) s);
232         if (s instanceof JSMimeType)
233             return append((JSMimeType) s);
234 
235         return s.toString();
236     }
237 
238     private String putTokens(ArrayList _list)
239     {
240         if ((_list == null) || (_list.size() == 0))
241             return "";
242         boolean _start = true;
243         Iterator _it = _list.iterator();
244         StringBuffer _sb = new StringBuffer();
245         while (_it.hasNext())
246         {
247             if (!_start)
248                 _sb.append(',');
249             else
250                 _start = false;
251 
252             _sb.append(append(_it.next()));
253         }
254         return _sb.toString();
255     }
256 
257     /***
258      * @return Returns the capabilities.
259      */
260     public List getCapabilities()
261     {
262         return capabilities;
263     }
264 
265     /***
266      * @param capabilities
267      *            The capabilities to set.
268      */
269     public void setCapabilities(ArrayList capabilities)
270     {
271         this.capabilities = capabilities;
272     }
273 
274     /***
275      * @return Returns the mimeTypes.
276      */
277     public List getMimeTypes()
278     {
279         return mimeTypes;
280     }
281 
282     /***
283      * @param mimeTypes
284      *            The mimeTypes to set.
285      */
286     public void setMimeTypes(ArrayList mimeTypes)
287     {
288         this.mimeTypes = mimeTypes;
289     }
290 
291 	public JSClientCapabilities getCapabilitiesString()
292 	{
293 		return capabilitiesString;
294 	}
295 
296 	public JSClientMimeTypes getMimeTypesString()
297 	{
298 		return mimeTypesString;
299 	}
300 
301 }