From f6a732ad6a491796c2712718a061af5425acd0e4 Mon Sep 17 00:00:00 2001 From: Mike Angstadt Date: Wed, 15 Nov 2023 09:52:59 -0500 Subject: [PATCH] Move common code to shared parent class --- src/main/java/biweekly/io/StreamWriter.java | 44 +++++++++++++++++++ .../java/biweekly/io/json/JCalWriter.java | 21 ++------- .../java/biweekly/io/text/ICalWriter.java | 33 ++------------ .../java/biweekly/io/xml/XCalDocument.java | 31 +------------ src/main/java/biweekly/io/xml/XCalWriter.java | 17 +------ 5 files changed, 56 insertions(+), 90 deletions(-) diff --git a/src/main/java/biweekly/io/StreamWriter.java b/src/main/java/biweekly/io/StreamWriter.java index b8c394f0..b6f4352e 100644 --- a/src/main/java/biweekly/io/StreamWriter.java +++ b/src/main/java/biweekly/io/StreamWriter.java @@ -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 @@ -213,4 +214,47 @@ private Collection> 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 timezones = getTimezoneComponents(); + for (VTimezone timezone : timezones) { + if (!subComponents.contains(timezone)) { + subComponents.add(0, timezone); + } + } + } } diff --git a/src/main/java/biweekly/io/json/JCalWriter.java b/src/main/java/biweekly/io/json/JCalWriter.java index 4c52be4e..92e1bbdc 100644 --- a/src/main/java/biweekly/io/json/JCalWriter.java +++ b/src/main/java/biweekly/io/json/JCalWriter.java @@ -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. @@ -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) { @@ -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 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); diff --git a/src/main/java/biweekly/io/text/ICalWriter.java b/src/main/java/biweekly/io/text/ICalWriter.java index e23915dd..a1773de4 100644 --- a/src/main/java/biweekly/io/text/ICalWriter.java +++ b/src/main/java/biweekly/io/text/ICalWriter.java @@ -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; @@ -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()); @@ -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 @@ -251,8 +244,8 @@ 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) { @@ -260,6 +253,7 @@ private void writeComponent(ICalComponent component, ICalComponent parent) throw writeComponent(subComponent, component); } + boolean inVCalRoot = (component instanceof ICalendar) && (getTargetVersion() == ICalVersion.V1_0); if (inVCalRoot) { writeVCalTimezones(); } @@ -267,25 +261,6 @@ private void writeComponent(ICalComponent component, ICalComponent parent) throw 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 timezones = getTimezoneComponents(); - for (VTimezone timezone : timezones) { - if (!subComponents.contains(timezone)) { - subComponents.add(0, timezone); - } - } - } - private void writeVCalTimezones() throws IOException { Collection timezones = getTimezoneComponents(); if (timezones.isEmpty()) { diff --git a/src/main/java/biweekly/io/xml/XCalDocument.java b/src/main/java/biweekly/io/xml/XCalDocument.java index 0e9bbb5d..07ea094f 100644 --- a/src/main/java/biweekly/io/xml/XCalDocument.java +++ b/src/main/java/biweekly/io/xml/XCalDocument.java @@ -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; @@ -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; @@ -699,7 +697,7 @@ private Element buildComponentElement(ICalComponent component) { Element componentElement = buildElement(componentScribe.getComponentName().toLowerCase()); List properties = componentScribe.getProperties(component); - addVersionPropertyIfMissing(properties, component); + addVersionPropertyIfMissing(component, properties); Element propertiesWrapperElement = buildPropertiesElement(properties, component); if (propertiesWrapperElement.hasChildNodes()) { @@ -707,7 +705,7 @@ private Element buildComponentElement(ICalComponent component) { } List subComponents = componentScribe.getComponents(component); - addVTimezoneComponents(component, subComponents); + addTimezoneComponentsIfMissing(component, subComponents); Element componentsWrapperElement = buildComponentsElement(subComponents); if (componentsWrapperElement.hasChildNodes()) { @@ -717,31 +715,6 @@ private Element buildComponentElement(ICalComponent component) { return componentElement; } - private void addVersionPropertyIfMissing(List 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 subComponents) { - if (!(parent instanceof ICalendar)) { - return; - } - - Collection tzs = getTimezoneComponents(); - for (VTimezone tz : tzs) { - if (!subComponents.contains(tz)) { - subComponents.add(0, tz); - } - } - } - private Element buildPropertiesElement(List properties, ICalComponent parent) { Element propertiesWrapperElement = buildElement(PROPERTIES); diff --git a/src/main/java/biweekly/io/xml/XCalWriter.java b/src/main/java/biweekly/io/xml/XCalWriter.java index aef43c2e..0abb903f 100644 --- a/src/main/java/biweekly/io/xml/XCalWriter.java +++ b/src/main/java/biweekly/io/xml/XCalWriter.java @@ -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; @@ -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; @@ -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); @@ -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 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) {