1 19 20 package org.apache.cayenne.wocompat; 21 22 import java.io.BufferedWriter ; 23 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileWriter ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.io.OutputStreamWriter ; 30 import java.io.Writer ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import org.apache.cayenne.CayenneRuntimeException; 36 import org.apache.cayenne.wocompat.parser.Parser; 37 38 46 public class PropertyListSerialization { 47 48 52 public static Object propertyListFromFile(File f) throws FileNotFoundException { 53 return propertyListFromFile(f, null); 54 } 55 56 60 public static Object propertyListFromFile(File f, PlistDataStructureFactory factory) 61 throws FileNotFoundException { 62 if (!f.isFile()) { 63 throw new FileNotFoundException ("No such file: " + f); 64 } 65 66 return new Parser(f, factory).propertyList(); 67 } 68 69 74 public static Object propertyListFromStream(InputStream in) { 75 return propertyListFromStream(in, null); 76 } 77 78 83 public static Object propertyListFromStream( 84 InputStream in, 85 PlistDataStructureFactory factory) { 86 return new Parser(in, factory).propertyList(); 87 } 88 89 92 public static void propertyListToFile(File f, Object plist) { 93 try { 94 BufferedWriter out = new BufferedWriter (new FileWriter (f)); 95 try { 96 writeObject("", out, plist); 97 } 98 finally { 99 out.close(); 100 } 101 } 102 catch (IOException ioex) { 103 throw new CayenneRuntimeException("Error saving plist.", ioex); 104 } 105 } 106 107 110 public static void propertyListToStream(OutputStream os, Object plist) { 111 try { 112 BufferedWriter out = new BufferedWriter (new OutputStreamWriter (os)); 113 try { 114 writeObject("", out, plist); 115 } 116 finally { 117 out.close(); 118 } 119 } 120 catch (IOException ioex) { 121 throw new CayenneRuntimeException("Error saving plist.", ioex); 122 } 123 } 124 125 128 protected static void writeObject(String offset, Writer out, Object plist) 129 throws IOException { 130 if (plist == null) { 131 return; 132 } 133 134 if (plist instanceof Collection ) { 135 Collection list = (Collection ) plist; 136 137 out.write('\n'); 138 out.write(offset); 139 140 if (list.size() == 0) { 141 out.write("()"); 142 return; 143 } 144 145 out.write("(\n"); 146 147 String childOffset = offset + " "; 148 Iterator it = list.iterator(); 149 boolean appended = false; 150 while (it.hasNext()) { 151 Object obj = it.next(); 153 if (obj != null) { 154 if (appended) { 155 out.write(", \n"); 156 } 157 158 out.write(childOffset); 159 writeObject(childOffset, out, obj); 160 appended = true; 161 } 162 } 163 164 out.write('\n'); 165 out.write(offset); 166 out.write(')'); 167 } 168 else if (plist instanceof Map ) { 169 Map map = (Map ) plist; 170 out.write('\n'); 171 out.write(offset); 172 173 if (map.size() == 0) { 174 out.write("{}"); 175 return; 176 } 177 178 out.write("{"); 179 180 String childOffset = offset + " "; 181 182 Iterator it = map.entrySet().iterator(); 183 while (it.hasNext()) { 184 Map.Entry entry = (Map.Entry ) it.next(); 186 Object key = entry.getKey(); 187 if (key == null) { 188 continue; 189 } 190 Object obj = entry.getValue(); 191 if (obj == null) { 192 continue; 193 } 194 out.write('\n'); 195 out.write(childOffset); 196 out.write(quoteString(key.toString())); 197 out.write(" = "); 198 writeObject(childOffset, out, obj); 199 out.write(';'); 200 } 201 202 out.write('\n'); 203 out.write(offset); 204 out.write('}'); 205 } 206 else if (plist instanceof String ) { 207 out.write(quoteString(plist.toString())); 208 } 209 else if (plist instanceof Number ) { 210 out.write(plist.toString()); 211 } 212 else { 213 throw new CayenneRuntimeException( 214 "Unsupported class for property list serialization: " 215 + plist.getClass().getName()); 216 } 217 } 218 219 222 protected static String escapeString(String str) { 223 char[] chars = str.toCharArray(); 224 int len = chars.length; 225 StringBuffer buf = new StringBuffer (len + 3); 226 227 for (int i = 0; i < len; i++) { 228 if (chars[i] == '\"' || chars[i] == '\\') { 229 buf.append('\\'); 230 } 231 buf.append(chars[i]); 232 } 233 234 return buf.toString(); 235 } 236 237 253 protected static String quoteString(String str) { 254 boolean shouldQuote = false; 255 256 259 String noQuoteExtras = "_$:./"; 260 char[] chars = str.toCharArray(); 261 int len = chars.length; 262 if (len == 0) { 263 shouldQuote = true; 264 } 265 for (int i = 0; !shouldQuote && i < len; i++) { 266 char c = chars[i]; 267 268 if ((c >= 'a' && c <= 'z') 269 || (c >= 'A' && c <= 'Z') 270 || (c >= '0' && c <= '9') 271 || noQuoteExtras.indexOf(c) >= 0) { 272 continue; 273 } 274 275 shouldQuote = true; 276 } 277 278 str = escapeString(str); 279 return (shouldQuote) ? '\"' + str + '\"' : str; 280 } 281 282 283 } 284 | Popular Tags |