KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > wocompat > PropertyListSerialization


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.wocompat;
57
58 import java.io.BufferedWriter JavaDoc;
59 import java.io.File JavaDoc;
60 import java.io.FileNotFoundException JavaDoc;
61 import java.io.FileWriter JavaDoc;
62 import java.io.IOException JavaDoc;
63 import java.io.InputStream JavaDoc;
64 import java.io.Writer JavaDoc;
65 import java.util.Iterator JavaDoc;
66 import java.util.List JavaDoc;
67 import java.util.Map JavaDoc;
68
69 import org.objectstyle.cayenne.CayenneRuntimeException;
70 import org.objectstyle.cayenne.wocompat.parser.Parser;
71
72 /**
73  * A <b>PropertyListSerialization</b> is a utility class
74  * that reads and stores files in NeXT/Apple
75  * property list format. Unlike corresponding WebObjects
76  * class, <code>PropertyListSerialization</code> uses standard
77  * Java collections (lists and maps) to store property lists.
78  *
79  * @author Andrei Adamchik
80  */

81 public class PropertyListSerialization {
82     /**
83      * Reads a property list file. Returns a property list object, that is
84      * normally a java.util.List or a java.util.Map, but can also be a String
85      * or a Number.
86      */

87     public static Object JavaDoc propertyListFromFile(File JavaDoc f) throws FileNotFoundException JavaDoc {
88         if (!f.isFile()) {
89             throw new FileNotFoundException JavaDoc("No such file: " + f);
90         }
91
92         return new Parser(f).propertyList();
93     }
94
95     /**
96      * Reads a property list data from InputStream. Returns a property list o
97      * bject, that is normally a java.util.List or a java.util.Map,
98      * but can also be a String or a Number.
99      */

100     public static Object JavaDoc propertyListFromStream(InputStream JavaDoc in) {
101         return new Parser(in).propertyList();
102     }
103
104     /**
105      * Saves property list to file.
106      */

107     public static void propertyListToFile(File JavaDoc f, Object JavaDoc plist) {
108         try {
109             BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(f));
110             try {
111                 writeObject("", out, plist);
112             } finally {
113                 out.close();
114             }
115         } catch (IOException JavaDoc ioex) {
116             throw new CayenneRuntimeException("Error saving plist.", ioex);
117         }
118     }
119
120     /**
121      * Internal method to recursively write a property list object.
122      */

123     protected static void writeObject(String JavaDoc offset, Writer JavaDoc out, Object JavaDoc plist)
124         throws IOException JavaDoc {
125         if (plist == null) {
126             return;
127         }
128
129         if (plist instanceof List JavaDoc) {
130             List JavaDoc list = (List JavaDoc) 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 JavaDoc childOffset = offset + " ";
143             Iterator JavaDoc it = list.iterator();
144             boolean appended = false;
145             while (it.hasNext()) {
146                 // Java collections can contain nulls, skip them
147
Object JavaDoc 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 JavaDoc) {
163             Map JavaDoc map = (Map JavaDoc) 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 JavaDoc childOffset = offset + " ";
175
176             Iterator JavaDoc it = map.keySet().iterator();
177             while (it.hasNext()) {
178                 // Java collections can contain nulls, skip them
179
Object JavaDoc key = it.next();
180                 if (key == null) {
181                     continue;
182                 }
183                 Object JavaDoc 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 JavaDoc) {
199             out.write(quoteString(plist.toString()));
200         } else if (plist instanceof Number JavaDoc) {
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     /**
210      * Escapes all doublequotes and backslashes.
211      */

212     protected static String JavaDoc escapeString(String JavaDoc str) {
213         char[] chars = str.toCharArray();
214         int len = chars.length;
215         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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     /**
228      * Returns a quoted String, with all the escapes preprocessed.
229      * May return an unquoted String if it contains on special
230      * characters, such as spaces, doublequotes and braces.
231      */

232     protected static String JavaDoc quoteString(String JavaDoc str) {
233         boolean shouldQuote = false;
234         
235         // scan string for special chars,
236
// if we have them, string must be quoted
237
String JavaDoc 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