KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > core > Util


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.core;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22
23 import org.jibx.runtime.*;
24 import org.jibx.runtime.impl.*;
25
26 /**
27  *
28  * This class contains methods for serialize and deserialize
29  *
30  * @author Stefano Fornari @ Funambol
31  *
32  * @version $Id: Util.java,v 1.7 2005/03/02 20:57:37 harrie Exp $
33  *
34  */

35 public final class Util {
36     // ------------------------------------------------------------ Constructors
37

38     private Util() {
39     }
40     
41     // ---------------------------------------------------------- Public methods
42

43     /**
44      * Serialize Long value to string.
45      *
46      * @param value Long value to be serialized
47      *
48      * @return the representation of value
49      */

50     public static String JavaDoc serializeWrapLong(Long JavaDoc value) {
51         return String.valueOf(value);
52     }
53     
54     /**
55      * Deserialize Long from string
56      *
57      * @param value string to be parsed
58      *
59      * @return the representation of value
60      */

61     public static Long JavaDoc deserializeWrapLong(String JavaDoc value) {
62         if (value != null) {
63             return Long.valueOf(value.trim());
64         }
65         return null;
66     }
67     
68     public static Boolean JavaDoc deserializeBoolean(String JavaDoc value) {
69         if (value != null &&
70         (value.equals("") || value.equalsIgnoreCase("true"))) {
71             return Boolean.TRUE;
72         }
73         return null;
74     }
75     
76     public static String JavaDoc serializeBoolean(Boolean JavaDoc value) {
77         return value.booleanValue() ? "" : null;
78     }
79     
80     /**
81      * Use marshall to create the representation XML of the object SyncML
82      *
83      * @param syncML the object SyncML
84      *
85      * @return the representation XML of the message
86      */

87     public static String JavaDoc toXML(SyncML syncML) {
88         String JavaDoc message = null;
89         try {
90             
91             ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
92             
93             IBindingFactory f = BindingDirectory.getFactory(SyncML.class);
94             IMarshallingContext c = f.createMarshallingContext();
95             c.setIndent(0);
96             c.marshalDocument(syncML, "UTF-8", null, bout);
97             
98             message = new String JavaDoc(bout.toByteArray());
99             
100         } catch(Exception JavaDoc e) {
101             e.printStackTrace();
102         }
103         return message;
104     }
105     
106     /**
107      * Use marshall to create the representation XML of the object,
108      * therefore must exist the mapping in the binding file.
109      *
110      * @param obj the object
111      *
112      * @return the representation XML of the object
113      */

114     public static String JavaDoc toXML(Object JavaDoc obj) {
115         String JavaDoc message = null;
116         try {
117             ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
118
119             IBindingFactory f = BindingDirectory.getFactory(obj.getClass());
120             IMarshallingContext c = f.createMarshallingContext();
121             c.setIndent(0);
122             c.marshalDocument(obj, "UTF-8", null, bout);
123
124             message = new String JavaDoc(bout.toByteArray());
125         } catch(Exception JavaDoc e) {
126             e.printStackTrace();
127         }
128         return message;
129     }
130     
131     /**
132      * Returns the size of the given Item, null if no size is specified
133      *
134      * @param item the item to check
135      * @return the size of the given Item, null if no size is specified
136      */

137     public static Long JavaDoc getItemSize(Item item) {
138         if (item.getMeta() == null) {
139             return null;
140         }
141         return item.getMeta().getSize();
142     }
143     
144     /**
145      * Returns the size of the contained data into command, null if no size
146      * is specified
147      *
148      * @param cmd the cmd to check
149      *
150      * @return the size of the contained data, null if no size is specified
151      */

152     public static Long JavaDoc getCmdDataSize(AbstractCommand cmd) {
153         if (cmd.getMeta() == null) {
154             return null;
155         }
156         return cmd.getMeta().getSize();
157     }
158     
159
160     /**
161      * Returns the real dimension of the data in the given Item
162      *
163      * @param item the item to check
164      * @return the real dimension of the data in the given Item,
165      * null if the item not contains data
166      */

167     public static Long JavaDoc getRealItemSize(Item item) {
168         Data data = item.getData();
169         if (data == null) {
170             return null;
171         }
172         String JavaDoc sData = data.getData();
173         if (sData == null) {
174             return null;
175         }
176         return new Long JavaDoc(sData.length());
177     }
178 }
179
Popular Tags