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.XMLFormat;
024: import javolution.xml.stream.XMLStreamException;
025:
026: import org.apache.commons.lang.StringEscapeUtils;
027: import org.apache.jetspeed.capabilities.MediaType;
028:
029: public class JSMediaType {
030: // private int refID;
031:
032: private String name;
033:
034: private int id;
035:
036: private String characterSet;
037:
038: private String title;
039:
040: private String description;
041:
042: private ArrayList capabilities;
043:
044: private ArrayList mimeTypes;
045:
046: private JSClientCapabilities capabilitiesString;
047:
048: private JSClientMimeTypes mimeTypesString;
049:
050: public JSMediaType() {
051: // refID = id;
052: }
053:
054: public JSMediaType(MediaType c) {
055: this .id = c.getMediatypeId();
056: this .name = c.getName();
057:
058: this .characterSet = c.getCharacterSet();
059: this .title = c.getTitle();
060: this .description = c.getDescription();
061: capabilities = new ArrayList();
062: mimeTypes = new ArrayList();
063:
064: }
065:
066: /***************************************************************************
067: * SERIALIZER
068: */
069: private static final XMLFormat XML = new XMLFormat(
070: JSMediaType.class) {
071: public void write(Object o, OutputElement xml)
072: throws XMLStreamException {
073:
074: try {
075: JSMediaType g = (JSMediaType) o;
076: /** attributes here */
077:
078: xml.setAttribute("name", g.name);
079:
080: /** named fields HERE */
081:
082: xml.add(g.characterSet, "charcterSet", String.class);
083: xml.add(g.title, "title", String.class);
084: xml.add(g.description, "description", String.class);
085:
086: /** implicitly named (through binding) fields here */
087:
088: g.capabilitiesString = new JSClientCapabilities(g
089: .putTokens(g.capabilities));
090: g.mimeTypesString = new JSClientMimeTypes(g
091: .putTokens(g.mimeTypes));
092: xml.add(g.capabilitiesString);
093: xml.add(g.mimeTypesString);
094:
095: // xml.add(g.groupString);
096:
097: } catch (Exception e) {
098: e.printStackTrace();
099: }
100: }
101:
102: public void read(InputElement xml, Object o) {
103: try {
104: JSMediaType g = (JSMediaType) o;
105: g.name = StringEscapeUtils.unescapeHtml(xml
106: .getAttribute("name", ""));
107:
108: /** named fields HERE */
109: Object o1 = xml.get("charcterSet", String.class); //characterSet
110: if ((o1 != null) && (o1 instanceof String))
111: g.characterSet = StringEscapeUtils
112: .unescapeHtml((String) o1);
113: g.title = StringEscapeUtils.unescapeHtml((String) xml
114: .get("title", String.class)); //title;
115: g.description = StringEscapeUtils
116: .unescapeHtml((String) xml.get("description",
117: String.class)); //description;
118:
119: while (xml.hasNext()) {
120: o1 = xml.getNext(); // mime
121:
122: if (o1 instanceof JSClientCapabilities)
123: g.capabilitiesString = (JSClientCapabilities) o1; //capabilitiesString;
124: else if (o1 instanceof JSClientMimeTypes)
125: g.mimeTypesString = (JSClientMimeTypes) o1; //mimeTypesString;
126: }
127: } catch (Exception e) {
128: e.printStackTrace();
129: }
130: }
131: };
132:
133: /**
134: * @return Returns the characterSet.
135: */
136: public String getCharacterSet() {
137: return characterSet;
138: }
139:
140: /**
141: * @param characterSet The characterSet to set.
142: */
143: public void setCharacterSet(String characterSet) {
144: this .characterSet = characterSet;
145: }
146:
147: /**
148: * @return Returns the description.
149: */
150: public String getDescription() {
151: return description;
152: }
153:
154: /**
155: * @param description The description to set.
156: */
157: public void setDescription(String description) {
158: this .description = description;
159: }
160:
161: /**
162: * @return Returns the id.
163: */
164: public int getId() {
165: return id;
166: }
167:
168: /**
169: * @param id The id to set.
170: */
171: public void setId(int id) {
172: this .id = id;
173: }
174:
175: /**
176: * @return Returns the name.
177: */
178: public String getName() {
179: return name;
180: }
181:
182: /**
183: * @param name The name to set.
184: */
185: public void setName(String name) {
186: this .name = name;
187: }
188:
189: /**
190: * @return Returns the title.
191: */
192: public String getTitle() {
193: return title;
194: }
195:
196: /**
197: * @param title The title to set.
198: */
199: public void setTitle(String titel) {
200: this .title = titel;
201: }
202:
203: private String append(JSCapability capability) {
204: return capability.getName();
205: }
206:
207: private String append(JSMimeType mime) {
208: return mime.getName();
209: }
210:
211: private String append(Object s) {
212: if (s instanceof JSCapability)
213: return append((JSCapability) s);
214: if (s instanceof JSMimeType)
215: return append((JSMimeType) s);
216:
217: return s.toString();
218: }
219:
220: private String putTokens(ArrayList _list) {
221: if ((_list == null) || (_list.size() == 0))
222: return "";
223: boolean _start = true;
224: Iterator _it = _list.iterator();
225: StringBuffer _sb = new StringBuffer();
226: while (_it.hasNext()) {
227: if (!_start)
228: _sb.append(',');
229: else
230: _start = false;
231:
232: _sb.append(append(_it.next()));
233: }
234: return _sb.toString();
235: }
236:
237: /**
238: * @return Returns the capabilities.
239: */
240: public List getCapabilities() {
241: return capabilities;
242: }
243:
244: /**
245: * @param capabilities
246: * The capabilities to set.
247: */
248: public void setCapabilities(ArrayList capabilities) {
249: this .capabilities = capabilities;
250: }
251:
252: /**
253: * @return Returns the mimeTypes.
254: */
255: public List getMimeTypes() {
256: return mimeTypes;
257: }
258:
259: /**
260: * @param mimeTypes
261: * The mimeTypes to set.
262: */
263: public void setMimeTypes(ArrayList mimeTypes) {
264: this .mimeTypes = mimeTypes;
265: }
266:
267: public JSClientCapabilities getCapabilitiesString() {
268: return capabilitiesString;
269: }
270:
271: public JSClientMimeTypes getMimeTypesString() {
272: return mimeTypesString;
273: }
274:
275: }
|