KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.wocompat;
21
22 import java.io.BufferedWriter JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.Writer JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.apache.cayenne.CayenneRuntimeException;
36 import org.apache.cayenne.wocompat.parser.Parser;
37
38 /**
39  * A <b>PropertyListSerialization</b> is a utility class that reads and stores files in
40  * NeXT/Apple property list format. Unlike corresponding WebObjects class,
41  * <code>PropertyListSerialization</code> uses standard Java collections (lists and
42  * maps) to store property lists.
43  *
44  * @author Andrus Adamchik
45  */

46 public class PropertyListSerialization {
47
48     /**
49      * Reads a property list file. Returns a property list object, that is normally a
50      * java.util.List or a java.util.Map, but can also be a String or a Number.
51      */

52     public static Object JavaDoc propertyListFromFile(File JavaDoc f) throws FileNotFoundException JavaDoc {
53         return propertyListFromFile(f, null);
54     }
55
56     /**
57      * Reads a property list file. Returns a property list object, that is normally a
58      * java.util.List or a java.util.Map, but can also be a String or a Number.
59      */

60     public static Object JavaDoc propertyListFromFile(File JavaDoc f, PlistDataStructureFactory factory)
61             throws FileNotFoundException JavaDoc {
62         if (!f.isFile()) {
63             throw new FileNotFoundException JavaDoc("No such file: " + f);
64         }
65
66         return new Parser(f, factory).propertyList();
67     }
68
69     /**
70      * Reads a property list data from InputStream. Returns a property list o bject, that
71      * is normally a java.util.List or a java.util.Map, but can also be a String or a
72      * Number.
73      */

74     public static Object JavaDoc propertyListFromStream(InputStream JavaDoc in) {
75         return propertyListFromStream(in, null);
76     }
77
78     /**
79      * Reads a property list data from InputStream. Returns a property list o bject, that
80      * is normally a java.util.List or a java.util.Map, but can also be a String or a
81      * Number.
82      */

83     public static Object JavaDoc propertyListFromStream(
84             InputStream JavaDoc in,
85             PlistDataStructureFactory factory) {
86         return new Parser(in, factory).propertyList();
87     }
88
89     /**
90      * Saves property list to file.
91      */

92     public static void propertyListToFile(File JavaDoc f, Object JavaDoc plist) {
93         try {
94             BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(f));
95             try {
96                 writeObject("", out, plist);
97             }
98             finally {
99                 out.close();
100             }
101         }
102         catch (IOException JavaDoc ioex) {
103             throw new CayenneRuntimeException("Error saving plist.", ioex);
104         }
105     }
106
107     /**
108      * Saves property list to file.
109      */

110     public static void propertyListToStream(OutputStream JavaDoc os, Object JavaDoc plist) {
111         try {
112             BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(os));
113             try {
114                 writeObject("", out, plist);
115             }
116             finally {
117                 out.close();
118             }
119         }
120         catch (IOException JavaDoc ioex) {
121             throw new CayenneRuntimeException("Error saving plist.", ioex);
122         }
123     }
124
125     /**
126      * Internal method to recursively write a property list object.
127      */

128     protected static void writeObject(String JavaDoc offset, Writer JavaDoc out, Object JavaDoc plist)
129             throws IOException JavaDoc {
130         if (plist == null) {
131             return;
132         }
133
134         if (plist instanceof Collection JavaDoc) {
135             Collection JavaDoc list = (Collection JavaDoc) 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 JavaDoc childOffset = offset + " ";
148             Iterator JavaDoc it = list.iterator();
149             boolean appended = false;
150             while (it.hasNext()) {
151                 // Java collections can contain nulls, skip them
152
Object JavaDoc 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 JavaDoc) {
169             Map JavaDoc map = (Map JavaDoc) 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 JavaDoc childOffset = offset + " ";
181
182             Iterator JavaDoc it = map.entrySet().iterator();
183             while (it.hasNext()) {
184                 // Java collections can contain nulls, skip them
185
Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
186                 Object JavaDoc key = entry.getKey();
187                 if (key == null) {
188                     continue;
189                 }
190                 Object JavaDoc 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 JavaDoc) {
207             out.write(quoteString(plist.toString()));
208         }
209         else if (plist instanceof Number JavaDoc) {
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     /**
220      * Escapes all doublequotes and backslashes.
221      */

222     protected static String JavaDoc escapeString(String JavaDoc str) {
223         char[] chars = str.toCharArray();
224         int len = chars.length;
225         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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     /**
238      * Returns a quoted String, with all the escapes preprocessed. May return an unquoted
239      * String if it contains no special characters. The rule for a non-special character
240      * is the following:
241      *
242      * <pre>
243      * c &gt;= 'a' &amp;&amp; c &lt;= 'z'
244      * c &gt;= 'A' &amp;&amp; c &lt;= 'Z'
245      * c &gt;= '0' &amp;&amp; c &lt;= '9'
246      * c == '_'
247      * c == '$'
248      * c == ':'
249      * c == '.'
250      * c == '/'
251      * </pre>
252      */

253     protected static String JavaDoc quoteString(String JavaDoc str) {
254         boolean shouldQuote = false;
255
256         // scan string for special chars,
257
// if we have them, string must be quoted
258

259         String JavaDoc 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