diff --git a/src/main/java/com/dd/plist/XMLPropertyListParser.java b/src/main/java/com/dd/plist/XMLPropertyListParser.java index a40faf9..0067592 100644 --- a/src/main/java/com/dd/plist/XMLPropertyListParser.java +++ b/src/main/java/com/dd/plist/XMLPropertyListParser.java @@ -36,7 +36,6 @@ import javax.xml.parsers.ParserConfigurationException; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.Reader; @@ -110,7 +109,7 @@ public static DocumentBuilder getDocBuilder() throws ParserConfigurationExceptio * @throws java.io.IOException If any I/O error occurs while reading the file. * @throws org.xml.sax.SAXException If any parse error occurs. * @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format. - * @throws java.text.ParseException If a date string could not be parsed. + * @throws java.text.ParseException If a number or date string could not be parsed. * @see javax.xml.parsers.DocumentBuilder#parse(java.io.File) */ public static NSObject parse(File f) @@ -128,7 +127,7 @@ public static NSObject parse(File f) * @throws java.io.IOException If any I/O error occurs while reading the file. * @throws org.xml.sax.SAXException If any parse error occurs. * @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format. - * @throws java.text.ParseException If a date string could not be parsed. + * @throws java.text.ParseException If a number or date string could not be parsed. * @see javax.xml.parsers.DocumentBuilder#parse(java.io.File) */ public static NSObject parse(Path path) @@ -148,7 +147,7 @@ public static NSObject parse(Path path) * @throws java.io.IOException If any I/O error occurs while reading the file. * @throws org.xml.sax.SAXException If any parse error occurs. * @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format. - * @throws java.text.ParseException If a date string could not be parsed. + * @throws java.text.ParseException If a number or date string could not be parsed. */ public static NSObject parse(final byte[] bytes) throws ParserConfigurationException, ParseException, SAXException, PropertyListFormatException, IOException { @@ -168,7 +167,7 @@ public static NSObject parse(final byte[] bytes) * @throws java.io.IOException If any I/O error occurs while reading the file. * @throws org.xml.sax.SAXException If any parse error occurs. * @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format. - * @throws java.text.ParseException If a date string could not be parsed. + * @throws java.text.ParseException If a number or date string could not be parsed. * @see javax.xml.parsers.DocumentBuilder#parse(java.io.InputStream) */ public static NSObject parse(InputStream is) @@ -189,7 +188,7 @@ public static NSObject parse(InputStream is) * @throws java.io.IOException If any I/O error occurs while reading the file. * @throws org.xml.sax.SAXException If any parse error occurs. * @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format. - * @throws java.text.ParseException If a date string could not be parsed. + * @throws java.text.ParseException If a number or date string could not be parsed. * @see javax.xml.parsers.DocumentBuilder#parse(java.io.InputStream) */ public static NSObject parse(Reader reader) @@ -204,7 +203,7 @@ public static NSObject parse(Reader reader) * @return The root NSObject of the property list contained in the XML document. * @throws java.io.IOException If any I/O error occurs while reading the file. * @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format. - * @throws java.text.ParseException If a date string could not be parsed. + * @throws java.text.ParseException If a number or date string could not be parsed. */ public static NSObject parse(Document doc) throws PropertyListFormatException, IOException, ParseException { DocumentType docType = doc.getDoctype(); @@ -242,7 +241,7 @@ public static NSObject parse(Document doc) throws PropertyListFormatException, I * @param n The XML node. * @return The corresponding NSObject. * @throws java.io.IOException If any I/O error occurs while parsing a Base64 encoded NSData object. - * @throws java.text.ParseException If a date string could not be parsed. + * @throws java.text.ParseException If a number or date string could not be parsed. */ private static NSObject parseObject(Node n) throws ParseException, IOException { String type = n.getNodeName(); @@ -274,7 +273,11 @@ private static NSObject parseObject(Node n) throws ParseException, IOException { return new NSNumber(false); case "integer": case "real": - return new NSNumber(getNodeTextContents(n)); + try { + return new NSNumber(getNodeTextContents(n)); + } catch (IllegalArgumentException ex) { + throw new ParseException("The NSNumber object has an invalid format.", -1); + } case "string": return new NSString(getNodeTextContents(n)); case "data": @@ -334,10 +337,9 @@ private static String getNodeTextContents(Node n) { } } - return ""; - } else { - return ""; } + + return ""; } } diff --git a/src/test/java/com/dd/plist/test/IssueTest.java b/src/test/java/com/dd/plist/test/IssueTest.java index 5e8c0e7..ce31cea 100644 --- a/src/test/java/com/dd/plist/test/IssueTest.java +++ b/src/test/java/com/dd/plist/test/IssueTest.java @@ -168,11 +168,17 @@ public void testIssue75_CyclicReferencesInBinaryPropertyLists(File file) { } @Test - public void testIssue76_UnexpectedIllegalArgumentException() { + public void testIssue76_UnexpectedIllegalArgumentExceptionForInvalidNumberInAsciiPropertyList() { File plistFile = new File("test-files/github-issue76.plist"); assertThrows(ParseException.class, () -> PropertyListParser.parse(plistFile)); } + @Test + public void testIssue76_UnexpectedIllegalArgumentExceptionForInvalidNumberInXmlPropertyList() { + File plistFile = new File("test-files/github-issue76-xml.plist"); + assertThrows(ParseException.class, () -> PropertyListParser.parse(plistFile)); + } + @Test public void testIssue78_NullReferenceExceptionForInvalidNSDictionaryKey() { File plistFile = new File("test-files/github-issue78.plist"); diff --git a/test-files/github-issue76-xml.plist b/test-files/github-issue76-xml.plist new file mode 100644 index 0000000..7602bea --- /dev/null +++ b/test-files/github-issue76-xml.plist @@ -0,0 +1,8 @@ + + + + + key1 + 42xy + +