Skip to content

Commit

Permalink
Move common code to shared parent class
Browse files Browse the repository at this point in the history
  • Loading branch information
mangstadt committed Nov 15, 2023
1 parent 7de233f commit f6a732a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 90 deletions.
44 changes: 44 additions & 0 deletions src/main/java/biweekly/io/StreamWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import biweekly.io.scribe.property.ICalPropertyScribe;
import biweekly.property.ICalProperty;
import biweekly.property.RawProperty;
import biweekly.property.Version;

/*
Copyright (c) 2013-2023, Michael Angstadt
Expand Down Expand Up @@ -213,4 +214,47 @@ private Collection<Class<?>> findScribeless(ICalendar ical) {

return unregistered;
}

/**
* Adds a VERSION property to the given component if the component is the
* root ICalendar component and the component does not have a VERSION
* property.
* @param component the component
* @param propertyObjs the component's properties (VERSION will be prepended
* to this list)
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void addVersionPropertyIfMissing(ICalComponent component, List propertyObjs) {
if (!(component instanceof ICalendar)) {
return;
}

if (component.getProperty(Version.class) != null) {
return;
}

propertyObjs.add(0, new Version(getTargetVersion()));
}

/**
* Adds the VTIMEZONE components to the given component if the component is
* the root ICalendar component and the component does not already have the
* VTIMEZONE components.
* @param component the component
* @param subComponents the component's sub-components (the VTIMEZONEs will
* be prepended to this list)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void addTimezoneComponentsIfMissing(ICalComponent component, List subComponents) {
if (!(component instanceof ICalendar)) {
return;
}

Collection<VTimezone> timezones = getTimezoneComponents();
for (VTimezone timezone : timezones) {
if (!subComponents.contains(timezone)) {
subComponents.add(0, timezone);
}
}
}
}
21 changes: 4 additions & 17 deletions src/main/java/biweekly/io/json/JCalWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Collection;
import java.util.List;

import com.fasterxml.jackson.core.JsonGenerator;

import biweekly.ICalDataType;
import biweekly.ICalVersion;
import biweekly.ICalendar;
import biweekly.component.ICalComponent;
import biweekly.component.VTimezone;
import biweekly.io.SkipMeException;
import biweekly.io.StreamWriter;
import biweekly.io.scribe.component.ICalComponentScribe;
import biweekly.io.scribe.property.ICalPropertyScribe;
import biweekly.parameter.ICalParameters;
import biweekly.property.ICalProperty;
import biweekly.property.Version;
import biweekly.util.Utf8Writer;

import com.fasterxml.jackson.core.JsonGenerator;

/*
Copyright (c) 2013-2023, Michael Angstadt
All rights reserved.
Expand Down Expand Up @@ -177,9 +174,7 @@ private void writeComponent(ICalComponent component) throws IOException {
writer.writeStartComponent(componentScribe.getComponentName().toLowerCase());

List propertyObjs = componentScribe.getProperties(component);
if (component instanceof ICalendar && component.getProperty(Version.class) == null) {
propertyObjs.add(0, new Version(targetVersion));
}
addVersionPropertyIfMissing(component, propertyObjs);

//write properties
for (Object propertyObj : propertyObjs) {
Expand All @@ -205,15 +200,7 @@ private void writeComponent(ICalComponent component) throws IOException {

//write sub-components
List subComponents = componentScribe.getComponents(component);
if (component instanceof ICalendar) {
//add the VTIMEZONE components that were auto-generated by TimezoneOptions
Collection<VTimezone> tzs = getTimezoneComponents();
for (VTimezone tz : tzs) {
if (!subComponents.contains(tz)) {
subComponents.add(0, tz);
}
}
}
addTimezoneComponentsIfMissing(component, subComponents);
for (Object subComponentObj : subComponents) {
ICalComponent subComponent = (ICalComponent) subComponentObj;
writeComponent(subComponent);
Expand Down
33 changes: 4 additions & 29 deletions src/main/java/biweekly/io/text/ICalWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import biweekly.property.Daylight;
import biweekly.property.ICalProperty;
import biweekly.property.Timezone;
import biweekly.property.Version;
import biweekly.util.Utf8Writer;

import com.github.mangstadt.vinnie.VObjectParameters;
Expand Down Expand Up @@ -220,10 +219,6 @@ protected void _write(ICalendar ical) throws IOException {
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private void writeComponent(ICalComponent component, ICalComponent parent) throws IOException {
boolean inICalendar = component instanceof ICalendar;
boolean inVCalRoot = inICalendar && getTargetVersion() == ICalVersion.V1_0;
boolean inICalRoot = inICalendar && getTargetVersion() != ICalVersion.V1_0;

ICalComponentScribe componentScribe = index.getComponentScribe(component);
try {
componentScribe.checkForDataModelConversions(component, parent, getTargetVersion());
Expand All @@ -240,9 +235,7 @@ private void writeComponent(ICalComponent component, ICalComponent parent) throw
writer.writeBeginComponent(componentScribe.getComponentName());

List propertyObjs = componentScribe.getProperties(component);
if (inICalendar) {
addVersionIfMissing(component, propertyObjs);
}
addVersionPropertyIfMissing(component, propertyObjs);

for (Object propertyObj : propertyObjs) {
context.setParent(component); //set parent here incase a scribe resets the parent
Expand All @@ -251,41 +244,23 @@ private void writeComponent(ICalComponent component, ICalComponent parent) throw
}

List subComponents = componentScribe.getComponents(component);
if (inICalRoot) {
addTimezonesIfMissing(subComponents);
if (getTargetVersion() != ICalVersion.V1_0) {
addTimezoneComponentsIfMissing(component, subComponents);
}

for (Object subComponentObj : subComponents) {
ICalComponent subComponent = (ICalComponent) subComponentObj;
writeComponent(subComponent, component);
}

boolean inVCalRoot = (component instanceof ICalendar) && (getTargetVersion() == ICalVersion.V1_0);
if (inVCalRoot) {
writeVCalTimezones();
}

writer.writeEndComponent(componentScribe.getComponentName());
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private void addVersionIfMissing(ICalComponent component, List propertyObjs) {
if (component.getProperty(Version.class) != null) {
return;
}

propertyObjs.add(0, new Version(getTargetVersion()));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private void addTimezonesIfMissing(List subComponents) {
Collection<VTimezone> timezones = getTimezoneComponents();
for (VTimezone timezone : timezones) {
if (!subComponents.contains(timezone)) {
subComponents.add(0, timezone);
}
}
}

private void writeVCalTimezones() throws IOException {
Collection<VTimezone> timezones = getTimezoneComponents();
if (timezones.isEmpty()) {
Expand Down
31 changes: 2 additions & 29 deletions src/main/java/biweekly/io/xml/XCalDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand All @@ -42,7 +41,6 @@
import biweekly.ICalVersion;
import biweekly.ICalendar;
import biweekly.component.ICalComponent;
import biweekly.component.VTimezone;
import biweekly.io.CannotParseException;
import biweekly.io.ParseWarning;
import biweekly.io.SkipMeException;
Expand Down Expand Up @@ -699,15 +697,15 @@ private Element buildComponentElement(ICalComponent component) {
Element componentElement = buildElement(componentScribe.getComponentName().toLowerCase());

List<ICalProperty> properties = componentScribe.getProperties(component);
addVersionPropertyIfMissing(properties, component);
addVersionPropertyIfMissing(component, properties);

Element propertiesWrapperElement = buildPropertiesElement(properties, component);
if (propertiesWrapperElement.hasChildNodes()) {
componentElement.appendChild(propertiesWrapperElement);
}

List<ICalComponent> subComponents = componentScribe.getComponents(component);
addVTimezoneComponents(component, subComponents);
addTimezoneComponentsIfMissing(component, subComponents);

Element componentsWrapperElement = buildComponentsElement(subComponents);
if (componentsWrapperElement.hasChildNodes()) {
Expand All @@ -717,31 +715,6 @@ private Element buildComponentElement(ICalComponent component) {
return componentElement;
}

private void addVersionPropertyIfMissing(List<ICalProperty> propertyObjs, ICalComponent parent) {
if (!(parent instanceof ICalendar)) {
return;
}

if (parent.getProperty(Version.class) != null) {
return;
}

propertyObjs.add(0, new Version(targetVersion));
}

private void addVTimezoneComponents(ICalComponent parent, List<ICalComponent> subComponents) {
if (!(parent instanceof ICalendar)) {
return;
}

Collection<VTimezone> tzs = getTimezoneComponents();
for (VTimezone tz : tzs) {
if (!subComponents.contains(tz)) {
subComponents.add(0, tz);
}
}
}

private Element buildPropertiesElement(List<ICalProperty> properties, ICalComponent parent) {
Element propertiesWrapperElement = buildElement(PROPERTIES);

Expand Down
17 changes: 2 additions & 15 deletions src/main/java/biweekly/io/xml/XCalWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -39,13 +38,11 @@
import biweekly.ICalDataType;
import biweekly.ICalendar;
import biweekly.component.ICalComponent;
import biweekly.component.VTimezone;
import biweekly.io.SkipMeException;
import biweekly.io.scribe.component.ICalComponentScribe;
import biweekly.io.scribe.property.ICalPropertyScribe;
import biweekly.parameter.ICalParameters;
import biweekly.property.ICalProperty;
import biweekly.property.Version;
import biweekly.property.Xml;
import biweekly.util.Utf8Writer;
import biweekly.util.XmlUtils;
Expand Down Expand Up @@ -314,9 +311,7 @@ private void write(ICalComponent component) throws SAXException {
start(name);

List properties = scribe.getProperties(component);
if (component instanceof ICalendar && component.getProperty(Version.class) == null) {
properties.add(0, new Version(targetVersion));
}
addVersionPropertyIfMissing(component, properties);

if (!properties.isEmpty()) {
start(PROPERTIES);
Expand All @@ -331,15 +326,7 @@ private void write(ICalComponent component) throws SAXException {
}

List subComponents = scribe.getComponents(component);
if (component instanceof ICalendar) {
//add the VTIMEZONE components that were auto-generated by TimezoneOptions
Collection<VTimezone> tzs = getTimezoneComponents();
for (VTimezone tz : tzs) {
if (!subComponents.contains(tz)) {
subComponents.add(0, tz);
}
}
}
addTimezoneComponentsIfMissing(component, subComponents);
if (!subComponents.isEmpty()) {
start(COMPONENTS);
for (Object subComponentObj : subComponents) {
Expand Down

0 comments on commit f6a732a

Please sign in to comment.