1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.github.maven_nar.cpptasks.apple;
21
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.xml.transform.TransformerConfigurationException;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.sax.SAXTransformerFactory;
32 import javax.xml.transform.sax.TransformerHandler;
33 import javax.xml.transform.stream.StreamResult;
34
35 import org.xml.sax.ContentHandler;
36 import org.xml.sax.SAXException;
37 import org.xml.sax.helpers.AttributesImpl;
38
39
40
41
42
43
44 public final class PropertyListSerialization {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public static void serialize(final Map propertyList, final List comments, final File file)
60 throws IOException, SAXException, TransformerConfigurationException {
61 final SAXTransformerFactory sf = (SAXTransformerFactory) TransformerFactory.newInstance();
62 final TransformerHandler handler = sf.newTransformerHandler();
63
64 final FileOutputStream os = new FileOutputStream(file);
65 final StreamResult result = new StreamResult(os);
66 handler.setResult(result);
67
68 handler.startDocument();
69 for (final Object comment1 : comments) {
70 final char[] comment = String.valueOf(comment1).toCharArray();
71 handler.comment(comment, 0, comment.length);
72 }
73 final AttributesImpl attributes = new AttributesImpl();
74 handler.startElement(null, "plist", "plist", attributes);
75 serializeMap(propertyList, handler);
76 handler.endElement(null, "plist", "plist");
77
78 handler.endDocument();
79 }
80
81
82
83
84
85
86
87
88
89
90
91 private static void serializeBoolean(final Boolean val, final ContentHandler handler) throws SAXException {
92 String tag = "false";
93 if (val.booleanValue()) {
94 tag = "true";
95 }
96 final AttributesImpl attributes = new AttributesImpl();
97 handler.startElement(null, tag, tag, attributes);
98 handler.endElement(null, tag, tag);
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113 private static void serializeElement(final String tag, final String content, final ContentHandler handler)
114 throws SAXException {
115 final AttributesImpl attributes = new AttributesImpl();
116 handler.startElement(null, tag, tag, attributes);
117 handler.characters(content.toCharArray(), 0, content.length());
118 handler.endElement(null, tag, tag);
119 }
120
121
122
123
124
125
126
127
128
129
130
131 private static void serializeInteger(final Number integer, final ContentHandler handler) throws SAXException {
132 serializeElement("integer", String.valueOf(integer.longValue()), handler);
133 }
134
135
136
137
138
139
140
141
142
143
144
145 private static void serializeList(final List list, final ContentHandler handler) throws SAXException {
146 final AttributesImpl attributes = new AttributesImpl();
147 handler.startElement(null, "array", "array", attributes);
148 for (final Object aList : list) {
149 serializeObject(aList, handler);
150 }
151 handler.endElement(null, "array", "array");
152 }
153
154
155
156
157
158
159
160
161
162
163
164 private static void serializeMap(final Map map, final ContentHandler handler) throws SAXException {
165 final AttributesImpl attributes = new AttributesImpl();
166 handler.startElement(null, "dict", "dict", attributes);
167
168 if (map.size() > 0) {
169
170
171
172
173 final Object[] keys = map.keySet().toArray();
174 Arrays.sort(keys);
175 for (final Object key2 : keys) {
176 final String key = String.valueOf(key2);
177 handler.startElement(null, "key", "key", attributes);
178 handler.characters(key.toCharArray(), 0, key.length());
179 handler.endElement(null, "key", "key");
180 serializeObject(map.get(key2), handler);
181 }
182 }
183 handler.endElement(null, "dict", "dict");
184 }
185
186
187
188
189
190
191
192
193
194
195
196 private static void serializeObject(final Object obj, final ContentHandler handler) throws SAXException {
197 if (obj instanceof Map) {
198 serializeMap((Map) obj, handler);
199 } else if (obj instanceof List) {
200 serializeList((List) obj, handler);
201 } else if (obj instanceof Number) {
202 if (obj instanceof Double || obj instanceof Float) {
203 serializeReal((Number) obj, handler);
204 } else {
205 serializeInteger((Number) obj, handler);
206 }
207 } else if (obj instanceof Boolean) {
208 serializeBoolean((Boolean) obj, handler);
209 } else {
210 serializeString(String.valueOf(obj), handler);
211 }
212 }
213
214
215
216
217
218
219
220
221
222
223
224 private static void serializeReal(final Number real, final ContentHandler handler) throws SAXException {
225 serializeElement("real", String.valueOf(real.doubleValue()), handler);
226 }
227
228
229
230
231
232
233
234
235
236
237
238 private static void serializeString(final String val, final ContentHandler handler) throws SAXException {
239 serializeElement("string", val, handler);
240 }
241
242
243
244
245 private PropertyListSerialization() {
246
247 }
248 }