1 18 19 package sync4j.framework.core; 20 21 import java.io.ByteArrayOutputStream ; 22 23 import org.jibx.runtime.*; 24 import org.jibx.runtime.impl.*; 25 26 35 public final class Util { 36 38 private Util() { 39 } 40 41 43 50 public static String serializeWrapLong(Long value) { 51 return String.valueOf(value); 52 } 53 54 61 public static Long deserializeWrapLong(String value) { 62 if (value != null) { 63 return Long.valueOf(value.trim()); 64 } 65 return null; 66 } 67 68 public static Boolean deserializeBoolean(String 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 serializeBoolean(Boolean value) { 77 return value.booleanValue() ? "" : null; 78 } 79 80 87 public static String toXML(SyncML syncML) { 88 String message = null; 89 try { 90 91 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 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 (bout.toByteArray()); 99 100 } catch(Exception e) { 101 e.printStackTrace(); 102 } 103 return message; 104 } 105 106 114 public static String toXML(Object obj) { 115 String message = null; 116 try { 117 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 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 (bout.toByteArray()); 125 } catch(Exception e) { 126 e.printStackTrace(); 127 } 128 return message; 129 } 130 131 137 public static Long getItemSize(Item item) { 138 if (item.getMeta() == null) { 139 return null; 140 } 141 return item.getMeta().getSize(); 142 } 143 144 152 public static Long getCmdDataSize(AbstractCommand cmd) { 153 if (cmd.getMeta() == null) { 154 return null; 155 } 156 return cmd.getMeta().getSize(); 157 } 158 159 160 167 public static Long getRealItemSize(Item item) { 168 Data data = item.getData(); 169 if (data == null) { 170 return null; 171 } 172 String sData = data.getData(); 173 if (sData == null) { 174 return null; 175 } 176 return new Long (sData.length()); 177 } 178 } 179 | Popular Tags |