SLING-8570 - Extract a generic Content Parser API from org.apache.sling.jcr.contentparser with pluggable implementations
* removed the ParseException and replaced it with IOException
* minor code cleanup
* improved code coverage
diff --git a/src/main/java/org/apache/sling/contentparser/api/ContentParser.java b/src/main/java/org/apache/sling/contentparser/api/ContentParser.java
index 4b3a29f..28df2c7 100644
--- a/src/main/java/org/apache/sling/contentparser/api/ContentParser.java
+++ b/src/main/java/org/apache/sling/contentparser/api/ContentParser.java
@@ -57,8 +57,7 @@
* @param contentHandler content handler that accepts the parsed content
* @param inputStream stream with serialized content
* @param parserOptions parser options, providing different settings for handling the serialized content
- * @throws IOException when an I/O error occurs
- * @throws ParseException when a parsing error occurs.
+ * @throws IOException when an I/O or parsing error occurs
*/
void parse(ContentHandler contentHandler, InputStream inputStream, ParserOptions parserOptions) throws IOException;
diff --git a/src/main/java/org/apache/sling/contentparser/api/ParseException.java b/src/main/java/org/apache/sling/contentparser/api/ParseException.java
deleted file mode 100644
index 4a19711..0000000
--- a/src/main/java/org/apache/sling/contentparser/api/ParseException.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ~ Licensed to the Apache Software Foundation (ASF) under one
- ~ or more contributor license agreements. See the NOTICE file
- ~ distributed with this work for additional information
- ~ regarding copyright ownership. The ASF licenses this file
- ~ to you under the Apache License, Version 2.0 (the
- ~ "License"); you may not use this file except in compliance
- ~ with the License. You may obtain a copy of the License at
- ~
- ~ http://www.apache.org/licenses/LICENSE-2.0
- ~
- ~ Unless required by applicable law or agreed to in writing,
- ~ software distributed under the License is distributed on an
- ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- ~ KIND, either express or implied. See the License for the
- ~ specific language governing permissions and limitations
- ~ under the License.
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
-package org.apache.sling.contentparser.api;
-
-import org.osgi.annotation.versioning.ProviderType;
-
-/**
- * Defines a parsing exception encountered by a {@link ContentParser}.
- */
-@ProviderType
-public final class ParseException extends RuntimeException {
- private static final long serialVersionUID = 1L;
-
- /**
- * Builds a {@code ParseException}, providing a message.
- *
- * @param message the message
- */
- public ParseException(String message) {
- super(message);
- }
-
- /**
- * Builds a {@code ParseException}, providing a message and a cause.
- *
- * @param message the message
- * @param cause the cause
- */
- public ParseException(String message, Throwable cause) {
- super(message, cause);
- }
-
-}
diff --git a/src/main/java/org/apache/sling/contentparser/api/ParserHelper.java b/src/main/java/org/apache/sling/contentparser/api/ParserHelper.java
index a0d3c11..b0a64d7 100644
--- a/src/main/java/org/apache/sling/contentparser/api/ParserHelper.java
+++ b/src/main/java/org/apache/sling/contentparser/api/ParserHelper.java
@@ -69,8 +69,8 @@
*
* @param values the multi-value property's values
* @return an object representation of the multi-value property
- * @throws ParseException when the provided {@code values} array contains null values, {@link Map} values, or the values are not of the
- * same type
+ * @throws IllegalArgumentException when the provided {@code values} array contains null values, {@link Map} values, or the values are
+ * not of the same type
*/
public static Object convertSingleTypeArray(Object[] values) {
if (values.length == 0) {
@@ -79,15 +79,15 @@
Class<?> itemType = null;
for (Object value : values) {
if (value == null) {
- throw new ParseException("Multi-value array must not contain null values.");
+ throw new IllegalArgumentException("Multi-value array must not contain null values.");
}
if (value instanceof Map) {
- throw new ParseException("Multi-value array must not contain maps/objects.");
+ throw new IllegalArgumentException("Multi-value array must not contain maps/objects.");
}
if (itemType == null) {
itemType = value.getClass();
} else if (itemType != value.getClass()) {
- throw new ParseException("Multi-value array must not contain values with different types "
+ throw new IllegalArgumentException("Multi-value array must not contain values with different types "
+ "(" + itemType.getName() + ", " + value.getClass().getName() + ").");
}
}
diff --git a/src/test/java/org/apache/sling/contentparser/api/ParserHelperTest.java b/src/test/java/org/apache/sling/contentparser/api/ParserHelperTest.java
index 9477414..f79f7e3 100644
--- a/src/test/java/org/apache/sling/contentparser/api/ParserHelperTest.java
+++ b/src/test/java/org/apache/sling/contentparser/api/ParserHelperTest.java
@@ -66,31 +66,31 @@
assertEquals(empty, ParserHelper.convertSingleTypeArray(empty));
Object[] nullValues = new Object[] {"string", null};
- ParseException nullValuesException = null;
+ IllegalArgumentException nullValuesException = null;
try {
ParserHelper.convertSingleTypeArray(nullValues);
- } catch (ParseException e) {
+ } catch (IllegalArgumentException e) {
nullValuesException = e;
}
- assertNotNull("Expected a ParseException when the Object array contains multiple types.", nullValuesException);
+ assertNotNull("Expected a IllegalArgumentException when the Object array contains multiple types.", nullValuesException);
Object[] maps = new Object[] {Collections.emptyMap()};
- ParseException mapsException = null;
+ IllegalArgumentException mapsException = null;
try {
ParserHelper.convertSingleTypeArray(maps);
- } catch (ParseException e) {
+ } catch (IllegalArgumentException e) {
mapsException = e;
}
- assertNotNull("Expected a ParseException when the Object array contains Map objects.", mapsException);
+ assertNotNull("Expected a IllegalArgumentException when the Object array contains Map objects.", mapsException);
Object[] differentTypes = new Object[] {"string", 1, 1L, 1F, Boolean.TRUE};
- ParseException differentTypesException = null;
+ IllegalArgumentException differentTypesException = null;
try {
ParserHelper.convertSingleTypeArray(differentTypes);
- } catch (ParseException e) {
+ } catch (IllegalArgumentException e) {
differentTypesException = e;
}
- assertNotNull("Expected a ParseException when the Object array contains multiple types.", differentTypesException);
+ assertNotNull("Expected a IllegalArgumentException when the Object array contains multiple types.", differentTypesException);
Object[] values = new Object[] {1, 2, 3, 4, 5};
Object result = ParserHelper.convertSingleTypeArray(values);