Skip to content

Commit

Permalink
Throw runtime exception for invalid input to model creation (IllegalA…
Browse files Browse the repository at this point in the history
…rgumentException)
  • Loading branch information
benfortuna committed May 2, 2022
1 parent 69cfa43 commit 3017ee3
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 67 deletions.
9 changes: 4 additions & 5 deletions src/main/java/net/fortuna/ical4j/data/ContentHandler.java
Expand Up @@ -80,20 +80,19 @@ public interface ContentHandler {
* @throws URISyntaxException where the property value is not a valid URI for applicable properties
* @throws IOException where data cannot be read for applicable properties
*/
void propertyValue(String value) throws URISyntaxException,
IOException;
void propertyValue(String value);

/**
* Triggers the end of handling a property.
* @param name a property name
*/
void endProperty(String name) throws URISyntaxException, IOException;
void endProperty(String name);

/**
* Triggers the handling of a parameter.
* @param name a parameter name
* @param value a parameter value
* @throws URISyntaxException where the parameter value is not a valid URI for applicable parameters
* @throws IllegalArgumentException where the parameter value is not a valid URI for applicable parameters
*/
void parameter(String name, String value) throws URISyntaxException;
void parameter(String name, String value);
}
Expand Up @@ -5,8 +5,6 @@
import net.fortuna.ical4j.model.component.VTimeZone;
import net.fortuna.ical4j.util.Constants;

import java.io.IOException;
import java.net.URISyntaxException;
import java.time.zone.ZoneRulesProvider;
import java.util.ArrayList;
import java.util.LinkedList;
Expand Down Expand Up @@ -143,7 +141,7 @@ public void propertyValue(String value) {
}

@Override
public void endProperty(String name) throws URISyntaxException, IOException {
public void endProperty(String name) {
if (!context.getIgnoredPropertyNames().contains(name.toUpperCase())) {
assertProperty(propertyBuilder);
Property property = propertyBuilder.build();
Expand All @@ -159,7 +157,7 @@ public void endProperty(String name) throws URISyntaxException, IOException {
}

@Override
public void parameter(String name, String value) throws URISyntaxException {
public void parameter(String name, String value) {
if (propertyBuilder != null) {
Parameter parameter = new ParameterBuilder(context.getParameterFactorySupplier().get())
.name(name).value(value).build();
Expand Down
32 changes: 6 additions & 26 deletions src/main/java/net/fortuna/ical4j/data/HCalendarParser.java
Expand Up @@ -49,7 +49,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -284,12 +283,8 @@ private void buildCalendar(Document d, ContentHandler handler) throws ParserExce
// no PRODID, as the using application should set that itself

handler.startProperty(Property.VERSION);
try {
handler.propertyValue(Version.VERSION_2_0.getValue());
handler.endProperty(Property.VERSION);
} catch (IOException | URISyntaxException e) {
LOG.warn("Caught exception", e);
}
handler.propertyValue(Version.VERSION_2_0.getValue());
handler.endProperty(Property.VERSION);

Element method = findElement(XPATH_METHOD, d);
if (method != null) {
Expand Down Expand Up @@ -417,11 +412,7 @@ private void buildProperty(Element element, String propName, ContentHandler hand
value = date.toString();

if (!(date instanceof DateTime))
try {
handler.parameter(Parameter.VALUE, Value.DATE.getValue());
} catch (URISyntaxException e) {
LOG.warn("Caught exception", e);
}
handler.parameter(Parameter.VALUE, Value.DATE.getValue());
} catch (ParseException e) {
throw new ParserException("Malformed date value for element '" + className + "'", -1, e);
}
Expand All @@ -430,24 +421,13 @@ private void buildProperty(Element element, String propName, ContentHandler hand
if (isTextProperty(propName)) {
String lang = element.getAttributeNS(XMLConstants.XML_NS_URI, "lang");
if (!StringUtils.isBlank(lang))
try {
handler.parameter(Parameter.LANGUAGE, lang);
} catch (URISyntaxException e) {
LOG.warn("Caught exception", e);
}
handler.parameter(Parameter.LANGUAGE, lang);
}

// XXX: other parameters?

try {
handler.propertyValue(value);

handler.endProperty(propName);
} catch (URISyntaxException e) {
throw new ParserException("Malformed URI value for element '" + className + "'", -1, e);
} catch (IOException e) {
throw new CalendarException(e);
}
handler.propertyValue(value);
handler.endProperty(propName);
}

// "The basic format of hCalendar is to use iCalendar object/property
Expand Down
28 changes: 5 additions & 23 deletions src/main/java/net/fortuna/ical4j/filter/AbstractFilter.java
Expand Up @@ -44,8 +44,6 @@
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.PropertyBuilder;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -126,11 +124,7 @@ protected Property property(FilterTarget operand) {
spec.value("");
}
operand.getAttributes().forEach(a -> spec.parameter(parameter(a)));
try {
return spec.build();
} catch (IOException | URISyntaxException e) {
throw new IllegalArgumentException(e);
}
return spec.build();
}

/**
Expand All @@ -147,11 +141,7 @@ protected Property property(FilterTarget operand, String value) {
spec.value("");
}
operand.getAttributes().forEach(a -> spec.parameter(parameter(a)));
try {
return spec.build();
} catch (IOException | URISyntaxException e) {
throw new IllegalArgumentException(e);
}
return spec.build();
}

/**
Expand Down Expand Up @@ -190,12 +180,8 @@ protected List<Comparable<Parameter>> parameters(BinaryExpression expression) {
}

protected Parameter parameter(FilterTarget.Attribute a) {
try {
return new ParameterBuilder(new DefaultParameterFactorySupplier().get())
.name(a.getName()).value(a.getValue()).build();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
return new ParameterBuilder(new DefaultParameterFactorySupplier().get())
.name(a.getName()).value(a.getValue()).build();
}

/**
Expand All @@ -205,10 +191,6 @@ protected Parameter parameter(FilterTarget.Attribute a) {
* @return a parameter instance
*/
protected Parameter parameter(String name, String value) {
try {
return new ParameterBuilder(new DefaultParameterFactorySupplier().get()).name(name).value(value).build();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
return new ParameterBuilder(new DefaultParameterFactorySupplier().get()).name(name).value(value).build();
}
}
3 changes: 1 addition & 2 deletions src/main/java/net/fortuna/ical4j/model/ParameterBuilder.java
Expand Up @@ -3,7 +3,6 @@
import net.fortuna.ical4j.model.parameter.XParameter;
import org.apache.commons.codec.DecoderException;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -53,7 +52,7 @@ public ParameterBuilder value(String value) {
/**
* @return a new parameter instance
*/
public Parameter build() throws URISyntaxException {
public Parameter build() {
Parameter parameter = null;
String decodedValue;
try {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/fortuna/ical4j/model/Property.java
Expand Up @@ -611,7 +611,7 @@ public int hashCode() {
*
* @return the copy of the property
*/
public final Property copy() throws URISyntaxException {
public final Property copy() {
return newFactory().createProperty(parameters, getValue());
}

Expand Down
5 changes: 1 addition & 4 deletions src/main/java/net/fortuna/ical4j/model/PropertyBuilder.java
Expand Up @@ -5,9 +5,6 @@
import net.fortuna.ical4j.model.property.XProperty;
import org.apache.commons.codec.DecoderException;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -71,7 +68,7 @@ public PropertyBuilder timeZoneRegistry(TimeZoneRegistry timeZoneRegistry) {
return this;
}

public Property build() throws IOException, URISyntaxException {
public Property build() {
Property property = null;
String decodedValue;
try {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/fortuna/ical4j/model/property/Attendee.java
Expand Up @@ -68,9 +68,9 @@ public Attendee() {

/**
* @param aValue a value string for this component
* @throws URISyntaxException where the specified value string is not a valid uri
* @throws IllegalArgumentException where the specified value string is not a valid uri
*/
public Attendee(final String aValue) throws URISyntaxException {
public Attendee(final String aValue) {
super(ATTENDEE);
setValue(aValue);
}
Expand Down

0 comments on commit 3017ee3

Please sign in to comment.