1 56 package org.objectstyle.cayenne.wocompat; 57 58 import java.io.BufferedWriter ; 59 import java.io.File ; 60 import java.io.FileNotFoundException ; 61 import java.io.FileWriter ; 62 import java.io.IOException ; 63 import java.io.InputStream ; 64 import java.io.Writer ; 65 import java.util.Iterator ; 66 import java.util.List ; 67 import java.util.Map ; 68 69 import org.objectstyle.cayenne.CayenneRuntimeException; 70 import org.objectstyle.cayenne.wocompat.parser.Parser; 71 72 81 public class PropertyListSerialization { 82 87 public static Object propertyListFromFile(File f) throws FileNotFoundException { 88 if (!f.isFile()) { 89 throw new FileNotFoundException ("No such file: " + f); 90 } 91 92 return new Parser(f).propertyList(); 93 } 94 95 100 public static Object propertyListFromStream(InputStream in) { 101 return new Parser(in).propertyList(); 102 } 103 104 107 public static void propertyListToFile(File f, Object plist) { 108 try { 109 BufferedWriter out = new BufferedWriter (new FileWriter (f)); 110 try { 111 writeObject("", out, plist); 112 } finally { 113 out.close(); 114 } 115 } catch (IOException ioex) { 116 throw new CayenneRuntimeException("Error saving plist.", ioex); 117 } 118 } 119 120 123 protected static void writeObject(String offset, Writer out, Object plist) 124 throws IOException { 125 if (plist == null) { 126 return; 127 } 128 129 if (plist instanceof List ) { 130 List list = (List ) plist; 131 132 out.write('\n'); 133 out.write(offset); 134 135 if (list.size() == 0) { 136 out.write("()"); 137 return; 138 } 139 140 out.write("(\n"); 141 142 String childOffset = offset + " "; 143 Iterator it = list.iterator(); 144 boolean appended = false; 145 while (it.hasNext()) { 146 Object obj = it.next(); 148 if (obj != null) { 149 if (appended) { 150 out.write(", \n"); 151 } 152 153 out.write(childOffset); 154 writeObject(childOffset, out, obj); 155 appended = true; 156 } 157 } 158 159 out.write('\n'); 160 out.write(offset); 161 out.write(')'); 162 } else if (plist instanceof Map ) { 163 Map map = (Map ) plist; 164 out.write('\n'); 165 out.write(offset); 166 167 if (map.size() == 0) { 168 out.write("{}"); 169 return; 170 } 171 172 out.write("{"); 173 174 String childOffset = offset + " "; 175 176 Iterator it = map.keySet().iterator(); 177 while (it.hasNext()) { 178 Object key = it.next(); 180 if (key == null) { 181 continue; 182 } 183 Object obj = map.get(key); 184 if (obj == null) { 185 continue; 186 } 187 out.write('\n'); 188 out.write(childOffset); 189 out.write(quoteString(key.toString())); 190 out.write(" = "); 191 writeObject(childOffset, out, obj); 192 out.write(';'); 193 } 194 195 out.write('\n'); 196 out.write(offset); 197 out.write('}'); 198 } else if (plist instanceof String ) { 199 out.write(quoteString(plist.toString())); 200 } else if (plist instanceof Number ) { 201 out.write(plist.toString()); 202 } else { 203 throw new CayenneRuntimeException( 204 "Unsupported class for property list serialization: " 205 + plist.getClass().getName()); 206 } 207 } 208 209 212 protected static String escapeString(String str) { 213 char[] chars = str.toCharArray(); 214 int len = chars.length; 215 StringBuffer buf = new StringBuffer (len + 3); 216 217 for (int i = 0; i < len; i++) { 218 if (chars[i] == '\"' || chars[i] == '\\') { 219 buf.append('\\'); 220 } 221 buf.append(chars[i]); 222 } 223 224 return buf.toString(); 225 } 226 227 232 protected static String quoteString(String str) { 233 boolean shouldQuote = false; 234 235 String special = " \\\"{}();,-+\'"; 238 char[] chars = str.toCharArray(); 239 int len = chars.length; 240 for (int i = 0; i < len; i++) { 241 if (special.indexOf(chars[i]) >= 0) { 242 shouldQuote = true; 243 break; 244 } 245 } 246 247 str = escapeString(str); 248 return (shouldQuote) ? '\"' + str + '\"' : str; 249 } 250 } 251 | Popular Tags |