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.XMLBinding;
24  import javolution.xml.XMLFormat;
25  import javolution.xml.stream.XMLStreamException;
26  
27  import org.apache.commons.lang.StringEscapeUtils;
28  import org.apache.jetspeed.capabilities.Client;
29  /***
30   * Jetspeed Serializer - Client Wrapper
31   * <p>
32   * Wrapper to process XML representation of a client
33   * 
34   * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
35   * @version $Id: $
36   */
37  public class JSClient
38  {
39  	// private int refID;
40  
41  	private String name;
42  
43  	private int id;
44  
45  	private int evalOrder;
46  
47  	private String manufacturer;
48  
49  	private String model;
50  
51  	private String preferredMimeTypeID;
52  
53  	private String userAgentPattern;
54  
55  	private String version;
56  
57  	private ArrayList capabilities;
58  
59  	private ArrayList mimeTypes;
60  
61  	private JSClientCapabilities capabilitiesString;
62  
63  	private JSClientMimeTypes mimeTypesString;
64  
65  	public JSClient()
66  	{
67  		// refID = id;
68  	}
69  
70  	public JSClient(Client c)
71  	{
72  		this.id = c.getClientId();
73  		this.name = c.getName();
74  
75  		this.userAgentPattern = c.getUserAgentPattern();
76  		this.version = c.getVersion();
77  		this.model = c.getModel();
78  
79  		this.evalOrder = c.getEvalOrder();
80  		this.manufacturer = c.getManufacturer();
81  
82  		capabilities = new ArrayList();
83  		mimeTypes = new ArrayList();
84  	}
85  
86  	public static final String XML_TAG = "Client".intern();
87      
88  	/***
89       * All local attributes and list-type classes are bound here,
90       * referenced classes should return their own binding.
91       * @param binding
92       */
93  	
94  	public static void setupAliases(XMLBinding binding)
95  {
96          binding.setAlias(JSClient.class, JSClient.XML_TAG);
97      }
98    
99  	
100 	
101 	/****************************************************************************
102 	 * SERIALIZER
103 	 */
104 	private static final XMLFormat XML = new XMLFormat(JSClient.class)
105 	{
106 		public void write(Object o, OutputElement xml)
107 				throws XMLStreamException
108 		{
109 
110 			try
111 			{
112 				JSClient g = (JSClient) o;
113 				xml.setAttribute("name", g.name);
114 				xml.setAttribute("evalOrder", g.evalOrder);
115 				xml.setAttribute("preferredMimeTypeID", g.preferredMimeTypeID);
116 				xml.add( g.userAgentPattern, "userAgentPattern",String.class);
117 				xml.add(g.version,"version", String.class);
118 				xml.add(g.model, "model", String.class);
119 				xml.add(g.manufacturer, "manufacturer", String.class);
120 
121 				g.capabilitiesString = new JSClientCapabilities(g.putTokens(g.capabilities));
122 				g.mimeTypesString = new JSClientMimeTypes(g.putTokens(g.mimeTypes));
123 				xml.add(g.capabilitiesString);
124 				xml.add(g.mimeTypesString);
125 				// xml.add(g.groupString);
126 
127 			} catch (Exception e)
128 			{
129 				e.printStackTrace();
130 			}
131 		}
132 
133 		public void read(InputElement xml, Object o)
134 		{
135 			try
136 			{
137 				JSClient g = (JSClient) o;
138                 g.name = StringEscapeUtils.unescapeHtml(xml.getAttribute("name",""));
139                 g.evalOrder = xml.getAttribute("evalOrder",0);
140                 g.preferredMimeTypeID = StringEscapeUtils.unescapeHtml(xml.getAttribute("preferredMimeTypeID","0"));
141                 
142                 g.userAgentPattern = StringEscapeUtils.unescapeHtml((String)xml.get("userAgentPattern",String.class));
143                 g.version = StringEscapeUtils.unescapeHtml((String)xml.get("version",String.class));
144                 g.model = StringEscapeUtils.unescapeHtml((String)xml.get("model",String.class));
145                 g.manufacturer = StringEscapeUtils.unescapeHtml((String)xml.get("manufacturer",String.class));
146                 g.capabilitiesString = (JSClientCapabilities) xml.getNext();
147                 g.mimeTypesString = (JSClientMimeTypes) xml.getNext();
148 			} catch (Exception e)
149 			{
150 				e.printStackTrace();
151 			}
152 		}
153 	};
154 
155 	/***
156 	 * @return Returns the capabilities.
157 	 */
158 	public List getCapabilities()
159 	{
160 		return capabilities;
161 	}
162 
163 	/***
164 	 * @param capabilities
165 	 *            The capabilities to set.
166 	 */
167 	public void setCapabilities(ArrayList capabilities)
168 	{
169 		this.capabilities = capabilities;
170 	}
171 
172 	/***
173 	 * @return Returns the evalOrder.
174 	 */
175 	public int getEvalOrder()
176 	{
177 		return evalOrder;
178 	}
179 
180 	/***
181 	 * @param evalOrder
182 	 *            The evalOrder to set.
183 	 */
184 	public void setEvalOrder(int evalOrder)
185 	{
186 		this.evalOrder = evalOrder;
187 	}
188 
189 	/***
190 	 * @return Returns the id.
191 	 */
192 	public int getId()
193 	{
194 		return id;
195 	}
196 
197 	/***
198 	 * @param id
199 	 *            The id to set.
200 	 */
201 	public void setId(int id)
202 	{
203 		this.id = id;
204 	}
205 
206 	/***
207 	 * @return Returns the manufacturer.
208 	 */
209 	public String getManufacturer()
210 	{
211 		return manufacturer;
212 	}
213 
214 	/***
215 	 * @param manufacturer
216 	 *            The manufacturer to set.
217 	 */
218 	public void setManufacturer(String manufacturer)
219 	{
220 		this.manufacturer = manufacturer;
221 	}
222 
223 	/***
224 	 * @return Returns the mimeTypes.
225 	 */
226 	public List getMimeTypes()
227 	{
228 		return mimeTypes;
229 	}
230 
231 	/***
232 	 * @param mimeTypes
233 	 *            The mimeTypes to set.
234 	 */
235 	public void setMimeTypes(ArrayList mimeTypes)
236 	{
237 		this.mimeTypes = mimeTypes;
238 	}
239 
240 	/***
241 	 * @return Returns the model.
242 	 */
243 	public String getModel()
244 	{
245 		return model;
246 	}
247 
248 	/***
249 	 * @param model
250 	 *            The model to set.
251 	 */
252 	public void setModel(String model)
253 	{
254 		this.model = model;
255 	}
256 
257 	/***
258 	 * @return Returns the name.
259 	 */
260 	public String getName()
261 	{
262 		return name;
263 	}
264 
265 	/***
266 	 * @param name
267 	 *            The name to set.
268 	 */
269 	public void setName(String name)
270 	{
271 		this.name = name;
272 	}
273 
274 	/***
275 	 * @return Returns the preferredMimeTypeID.
276 	 */
277 	public String getPreferredMimeTypeID()
278 	{
279 		return preferredMimeTypeID;
280 	}
281 
282 	/***
283 	 * @param preferredMimeTypeID
284 	 *            The preferredMimeTypeID to set.
285 	 */
286 	public void setPreferredMimeTypeID(String preferredMimeTypeID)
287 	{
288 		this.preferredMimeTypeID = preferredMimeTypeID;
289 	}
290 
291 	/***
292 	 * @return Returns the userAgentPattern.
293 	 */
294 	public String getUserAgentPattern()
295 	{
296 		return userAgentPattern;
297 	}
298 
299 	/***
300 	 * @param userAgentPattern
301 	 *            The userAgentPattern to set.
302 	 */
303 	public void setUserAgentPattern(String userAgentPattern)
304 	{
305 		this.userAgentPattern = userAgentPattern;
306 	}
307 
308 	/***
309 	 * @return Returns the version.
310 	 */
311 	public String getVersion()
312 	{
313 		return version;
314 	}
315 
316 	/***
317 	 * @param version
318 	 *            The version to set.
319 	 */
320 	public void setVersion(String version)
321 	{
322 		this.version = version;
323 	}
324 
325 	private String append(JSCapability capability)
326 	{
327 		return capability.getName();
328 	}
329 
330 	private String append(JSMimeType mime)
331 	{
332 		return mime.getName();
333 	}
334 
335 	private String append(Object s)
336 	{
337 		if (s instanceof JSCapability)
338 			return append((JSCapability) s);
339 		if (s instanceof JSMimeType)
340 			return append((JSMimeType) s);
341 
342 		return s.toString();
343 	}
344 
345 	private String putTokens(ArrayList _list)
346 	{
347 		if ((_list == null) || (_list.size() == 0))
348 			return "";
349 		boolean _start = true;
350 		Iterator _it = _list.iterator();
351 		StringBuffer _sb = new StringBuffer();
352 		while (_it.hasNext())
353 		{
354 			if (!_start)
355 				_sb.append(',');
356 			else
357 				_start = false;
358 
359 			_sb.append(append(_it.next()));
360 		}
361 		return _sb.toString();
362 	}
363 
364 	public JSClientCapabilities getCapabilitiesString()
365 	{
366 		return capabilitiesString;
367 	}
368 
369 	public JSClientMimeTypes getMimeTypesString()
370 	{
371 		return mimeTypesString;
372 	}
373 
374 
375 	
376 }