Skip to content

Commit cea7d4e

Browse files
committed
Fix handling of null values during serialization (See updated comments on issue 41)
1 parent 39f7ab9 commit cea7d4e

File tree

6 files changed

+81
-41
lines changed

6 files changed

+81
-41
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<groupId>com.googlecode.plist</groupId>
99
<artifactId>dd-plist</artifactId>
1010
<packaging>jar</packaging>
11-
<version>1.13</version>
11+
<version>1.15</version>
1212
<name>dd-plist</name>
1313
<url>http://plist.googlecode.com</url>
1414
<description>

src/main/java/com/dd/plist/BinaryPropertyListParser.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,6 @@ public static NSObject parse(InputStream is) throws IOException, PropertyListFor
163163
* @throws PropertyListFormatException When the property list's format could not be parsed.
164164
*/
165165
public static NSObject parse(File f) throws IOException, PropertyListFormatException {
166-
if (f.length() > Runtime.getRuntime().freeMemory()) {
167-
throw new OutOfMemoryError("To little heap space available! Wanted to read " + f.length() + " bytes, but only " + Runtime.getRuntime().freeMemory() + " are available.");
168-
//not yet even implemented in Core Foundation as of revision 855.17
169-
}
170166
return parse(new FileInputStream(f));
171167
}
172168

src/main/java/com/dd/plist/NSArray.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ public void remove(int i) {
8888
* @param value The object.
8989
*/
9090
public void setValue(int key, Object value) {
91-
if(value == null)
92-
throw new NullPointerException("Cannot add null values to an NSArray!");
9391
array[key] = NSObject.wrap(value);
9492
}
9593

@@ -123,6 +121,11 @@ public int count() {
123121
public boolean containsObject(Object obj) {
124122
NSObject nso = NSObject.wrap(obj);
125123
for (NSObject elem : array) {
124+
if(elem == null) {
125+
if(obj == null)
126+
return true;
127+
continue;
128+
}
126129
if (elem.equals(nso)) {
127130
return true;
128131
}
@@ -197,6 +200,8 @@ public NSObject[] objectsAtIndexes(int... indexes) {
197200

198201
@Override
199202
public boolean equals(Object obj) {
203+
if(obj == null)
204+
return false;
200205
if(obj.getClass().equals(NSArray.class)) {
201206
return Arrays.equals(((NSArray) obj).getArray(), this.array);
202207
} else {

src/main/java/com/dd/plist/NSDictionary.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -148,56 +148,26 @@ public void putAll(Map<? extends String, ? extends NSObject> values) {
148148
* or null, if no value was associated to it.
149149
*/
150150
public NSObject put(String key, NSObject obj) {
151+
if(key == null)
152+
return null;
151153
if(obj == null)
152154
return dict.get(key);
153155
return dict.put(key, obj);
154156
}
155157

156158
/**
157159
* Puts a new key-value pair into this dictionary.
158-
* If the value is null, no operation will be performed on the dictionary.
160+
* If key or value are null, no operation will be performed on the dictionary.
159161
*
160162
* @param key The key.
161163
* @param obj The value. Supported object types are numbers, byte-arrays, dates, strings and arrays or sets of those.
162164
* @return The value previously associated to the given key,
163165
* or null, if no value was associated to it.
164166
*/
165167
public NSObject put(String key, Object obj) {
166-
if(obj == null)
167-
return dict.get(key);
168168
return put(key, NSObject.wrap(obj));
169169
}
170170

171-
/**
172-
* Puts a new key-value pair into this dictionary.
173-
*
174-
* @param key The key.
175-
* @param obj The value.
176-
*/
177-
public NSObject put(String key, long obj) {
178-
return put(key, new NSNumber(obj));
179-
}
180-
181-
/**
182-
* Puts a new key-value pair into this dictionary.
183-
*
184-
* @param key The key.
185-
* @param obj The value.
186-
*/
187-
public NSObject put(String key, double obj) {
188-
return put(key, new NSNumber(obj));
189-
}
190-
191-
/**
192-
* Puts a new key-value pair into this dictionary.
193-
*
194-
* @param key The key.
195-
* @param obj The value.
196-
*/
197-
public NSObject put(String key, boolean obj) {
198-
return put(key, new NSNumber(obj));
199-
}
200-
201171
/**
202172
* Removes a key-value pair from this dictionary.
203173
*

src/main/java/com/dd/plist/NSObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public static NSSet wrap(Set<Object> value) {
233233
*/
234234
public static NSObject wrap(Object o) {
235235
if(o == null)
236-
throw new NullPointerException("A null object cannot be wrapped as a NSObject");
236+
return null;
237237

238238
if(o instanceof NSObject)
239239
return (NSObject)o;

src/test/java/com/dd/plist/test/IssueTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import com.dd.plist.*;
44
import junit.framework.TestCase;
5+
import org.xml.sax.SAXException;
56

7+
import javax.xml.parsers.ParserConfigurationException;
68
import java.io.File;
9+
import java.io.IOException;
10+
import java.text.ParseException;
11+
import java.util.HashMap;
12+
import java.util.Map;
713

814
public class IssueTest extends TestCase {
915
public static void testIssue4() throws Exception {
@@ -71,6 +77,69 @@ public static void testIssue38() throws Exception {
7177
assertTrue(fileRef.equals(new NSString("65541A9B16D13B8C00A968D5")));
7278
}
7379

80+
/**
81+
* Test storing null values
82+
*/
83+
public static void testIssue41() {
84+
//Dictionary
85+
Map<String, Object> nullMap = new HashMap<String, Object>();
86+
nullMap.put("key", null);
87+
assertFalse(nullMap.isEmpty());
88+
NSDictionary nullDict = NSObject.wrap(nullMap);
89+
assertTrue(nullDict.isEmpty());
90+
91+
nullDict.put(null, "test");
92+
assertTrue(nullDict.isEmpty());
93+
94+
nullDict.put("test", null);
95+
assertTrue(nullDict.isEmpty());
96+
97+
try {
98+
assertTrue(((NSDictionary)PropertyListParser.parse(nullDict.toXMLPropertyList().getBytes())).isEmpty());
99+
} catch (Exception e) {
100+
throw new AssertionError("No exception should have occurred while parsing an empty dictionary", e);
101+
}
102+
103+
//Array
104+
String[] strArr = new String[3];
105+
strArr[0] = "";
106+
strArr[1] = null;
107+
strArr[2] = null;
108+
NSArray nsArr = NSObject.wrap(strArr);
109+
assertTrue(nsArr.containsObject(null));
110+
assertEquals(nsArr.objectAtIndex(1), null);
111+
assertEquals(nsArr.objectAtIndex(2), null);
112+
113+
try {
114+
nsArr.toXMLPropertyList();
115+
throw new AssertionError("Storing a NSArray containing a null value as a XML property list should throw an exception");
116+
} catch(NullPointerException ex) {
117+
//expected exception
118+
}
119+
120+
try {
121+
nsArr.toASCIIPropertyList();
122+
throw new AssertionError("Storing a NSArray containing a null value as a ASCII property list should throw an exception");
123+
} catch(NullPointerException ex) {
124+
//expected exception
125+
}
126+
127+
try {
128+
nsArr.toGnuStepASCIIPropertyList();
129+
throw new AssertionError("Storing a NSArray containing a null value as a GnuStep ASCII property list should throw an exception");
130+
} catch(NullPointerException ex) {
131+
//expected exception
132+
}
133+
134+
try {
135+
byte[] bin = BinaryPropertyListWriter.writeToArray(nsArr);
136+
throw new AssertionError("Storing a NSArray containing a null value as a binary property list should throw an exception");
137+
} catch(IOException ex) {
138+
//expect IOException because binary v1.0 format (which could theoretically store null values) is not supported
139+
//But v1.0 format is not even supported by OS X 10.10, so there is no plan as of yet to implement it
140+
}
141+
}
142+
74143
public static void testIssue49() throws Exception {
75144
NSDictionary dict = (NSDictionary)PropertyListParser.parse(new File("test-files/issue49.plist"));
76145
assertEquals(0, dict.count());

0 commit comments

Comments
 (0)