001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.serializer.objects;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import javolution.xml.XMLBinding;
024: import javolution.xml.XMLFormat;
025: import javolution.xml.stream.XMLStreamException;
026:
027: import org.apache.commons.lang.StringEscapeUtils;
028: import org.apache.jetspeed.capabilities.Client;
029:
030: /**
031: * Jetspeed Serializer - Client Wrapper
032: * <p>
033: * Wrapper to process XML representation of a client
034: *
035: * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
036: * @version $Id: $
037: */
038: public class JSClient {
039: // private int refID;
040:
041: private String name;
042:
043: private int id;
044:
045: private int evalOrder;
046:
047: private String manufacturer;
048:
049: private String model;
050:
051: private String preferredMimeTypeID;
052:
053: private String userAgentPattern;
054:
055: private String version;
056:
057: private ArrayList capabilities;
058:
059: private ArrayList mimeTypes;
060:
061: private JSClientCapabilities capabilitiesString;
062:
063: private JSClientMimeTypes mimeTypesString;
064:
065: public JSClient() {
066: // refID = id;
067: }
068:
069: public JSClient(Client c) {
070: this .id = c.getClientId();
071: this .name = c.getName();
072:
073: this .userAgentPattern = c.getUserAgentPattern();
074: this .version = c.getVersion();
075: this .model = c.getModel();
076:
077: this .evalOrder = c.getEvalOrder();
078: this .manufacturer = c.getManufacturer();
079:
080: capabilities = new ArrayList();
081: mimeTypes = new ArrayList();
082: }
083:
084: public static final String XML_TAG = "Client".intern();
085:
086: /**
087: * All local attributes and list-type classes are bound here,
088: * referenced classes should return their own binding.
089: * @param binding
090: */
091:
092: public static void setupAliases(XMLBinding binding) {
093: binding.setAlias(JSClient.class, JSClient.XML_TAG);
094: }
095:
096: /***************************************************************************
097: * SERIALIZER
098: */
099: private static final XMLFormat XML = new XMLFormat(JSClient.class) {
100: public void write(Object o, OutputElement xml)
101: throws XMLStreamException {
102:
103: try {
104: JSClient g = (JSClient) o;
105: xml.setAttribute("name", g.name);
106: xml.setAttribute("evalOrder", g.evalOrder);
107: xml.setAttribute("preferredMimeTypeID",
108: g.preferredMimeTypeID);
109: xml.add(g.userAgentPattern, "userAgentPattern",
110: String.class);
111: xml.add(g.version, "version", String.class);
112: xml.add(g.model, "model", String.class);
113: xml.add(g.manufacturer, "manufacturer", String.class);
114:
115: g.capabilitiesString = new JSClientCapabilities(g
116: .putTokens(g.capabilities));
117: g.mimeTypesString = new JSClientMimeTypes(g
118: .putTokens(g.mimeTypes));
119: xml.add(g.capabilitiesString);
120: xml.add(g.mimeTypesString);
121: // xml.add(g.groupString);
122:
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: }
127:
128: public void read(InputElement xml, Object o) {
129: try {
130: JSClient g = (JSClient) o;
131: g.name = StringEscapeUtils.unescapeHtml(xml
132: .getAttribute("name", ""));
133: g.evalOrder = xml.getAttribute("evalOrder", 0);
134: g.preferredMimeTypeID = StringEscapeUtils
135: .unescapeHtml(xml.getAttribute(
136: "preferredMimeTypeID", "0"));
137:
138: g.userAgentPattern = StringEscapeUtils
139: .unescapeHtml((String) xml.get(
140: "userAgentPattern", String.class));
141: g.version = StringEscapeUtils.unescapeHtml((String) xml
142: .get("version", String.class));
143: g.model = StringEscapeUtils.unescapeHtml((String) xml
144: .get("model", String.class));
145: g.manufacturer = StringEscapeUtils
146: .unescapeHtml((String) xml.get("manufacturer",
147: String.class));
148: g.capabilitiesString = (JSClientCapabilities) xml
149: .getNext();
150: g.mimeTypesString = (JSClientMimeTypes) xml.getNext();
151: } catch (Exception e) {
152: e.printStackTrace();
153: }
154: }
155: };
156:
157: /**
158: * @return Returns the capabilities.
159: */
160: public List getCapabilities() {
161: return capabilities;
162: }
163:
164: /**
165: * @param capabilities
166: * The capabilities to set.
167: */
168: public void setCapabilities(ArrayList capabilities) {
169: this .capabilities = capabilities;
170: }
171:
172: /**
173: * @return Returns the evalOrder.
174: */
175: public int getEvalOrder() {
176: return evalOrder;
177: }
178:
179: /**
180: * @param evalOrder
181: * The evalOrder to set.
182: */
183: public void setEvalOrder(int evalOrder) {
184: this .evalOrder = evalOrder;
185: }
186:
187: /**
188: * @return Returns the id.
189: */
190: public int getId() {
191: return id;
192: }
193:
194: /**
195: * @param id
196: * The id to set.
197: */
198: public void setId(int id) {
199: this .id = id;
200: }
201:
202: /**
203: * @return Returns the manufacturer.
204: */
205: public String getManufacturer() {
206: return manufacturer;
207: }
208:
209: /**
210: * @param manufacturer
211: * The manufacturer to set.
212: */
213: public void setManufacturer(String manufacturer) {
214: this .manufacturer = manufacturer;
215: }
216:
217: /**
218: * @return Returns the mimeTypes.
219: */
220: public List getMimeTypes() {
221: return mimeTypes;
222: }
223:
224: /**
225: * @param mimeTypes
226: * The mimeTypes to set.
227: */
228: public void setMimeTypes(ArrayList mimeTypes) {
229: this .mimeTypes = mimeTypes;
230: }
231:
232: /**
233: * @return Returns the model.
234: */
235: public String getModel() {
236: return model;
237: }
238:
239: /**
240: * @param model
241: * The model to set.
242: */
243: public void setModel(String model) {
244: this .model = model;
245: }
246:
247: /**
248: * @return Returns the name.
249: */
250: public String getName() {
251: return name;
252: }
253:
254: /**
255: * @param name
256: * The name to set.
257: */
258: public void setName(String name) {
259: this .name = name;
260: }
261:
262: /**
263: * @return Returns the preferredMimeTypeID.
264: */
265: public String getPreferredMimeTypeID() {
266: return preferredMimeTypeID;
267: }
268:
269: /**
270: * @param preferredMimeTypeID
271: * The preferredMimeTypeID to set.
272: */
273: public void setPreferredMimeTypeID(String preferredMimeTypeID) {
274: this .preferredMimeTypeID = preferredMimeTypeID;
275: }
276:
277: /**
278: * @return Returns the userAgentPattern.
279: */
280: public String getUserAgentPattern() {
281: return userAgentPattern;
282: }
283:
284: /**
285: * @param userAgentPattern
286: * The userAgentPattern to set.
287: */
288: public void setUserAgentPattern(String userAgentPattern) {
289: this .userAgentPattern = userAgentPattern;
290: }
291:
292: /**
293: * @return Returns the version.
294: */
295: public String getVersion() {
296: return version;
297: }
298:
299: /**
300: * @param version
301: * The version to set.
302: */
303: public void setVersion(String version) {
304: this .version = version;
305: }
306:
307: private String append(JSCapability capability) {
308: return capability.getName();
309: }
310:
311: private String append(JSMimeType mime) {
312: return mime.getName();
313: }
314:
315: private String append(Object s) {
316: if (s instanceof JSCapability)
317: return append((JSCapability) s);
318: if (s instanceof JSMimeType)
319: return append((JSMimeType) s);
320:
321: return s.toString();
322: }
323:
324: private String putTokens(ArrayList _list) {
325: if ((_list == null) || (_list.size() == 0))
326: return "";
327: boolean _start = true;
328: Iterator _it = _list.iterator();
329: StringBuffer _sb = new StringBuffer();
330: while (_it.hasNext()) {
331: if (!_start)
332: _sb.append(',');
333: else
334: _start = false;
335:
336: _sb.append(append(_it.next()));
337: }
338: return _sb.toString();
339: }
340:
341: public JSClientCapabilities getCapabilitiesString() {
342: return capabilitiesString;
343: }
344:
345: public JSClientMimeTypes getMimeTypesString() {
346: return mimeTypesString;
347: }
348:
349: }
|