Skip to content

Commit

Permalink
Added card serializer command
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jun 9, 2024
1 parent 098b012 commit 8b442ec
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
api "org.ical4j:ical4j-connector-api:$ical4jConnectorVersion",
"org.ical4j:ical4j-integration-api:$ical4jIntegrationVersion",
"org.ical4j:ical4j-serializer:$ical4jSerializerVersion",
"org.mnode.ical4j:ical4j-vcard:$ical4jVCardVersion",
"org.mnode.ical4j:ical4j:$ical4jVersion"

implementation "org.apache.logging.log4j:log4j-core:$log4jVersion",
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ical4jExtensionsVersion = 2.0.0-rc4
ical4jTemplateVersion = 0.2.1
ical4jConnectorVersion = 2.0.0-alpha6-SNAPSHOT
ical4jIntegrationVersion = 0.3.1
ical4jSerializerVersion = 0.3.1
ical4jSerializerVersion = 0.3.2-develop-SNAPSHOT

slf4jVersion=1.7.36
log4jVersion=2.23.1
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/ical4j/command/CardCommandGroup.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.ical4j.command;

import org.ical4j.command.vcard.Serializer;
import picocli.CommandLine;

@CommandLine.Command(name = "card", description = "vCard operations",
subcommands = { })
subcommands = { Serializer.class })
public class CardCommandGroup {

}
127 changes: 127 additions & 0 deletions src/main/java/org/ical4j/command/vcard/Serializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package org.ical4j.command.vcard;

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.component.VTimeZone;
import net.fortuna.ical4j.vcard.VCard;
import org.mnode.ical4j.serializer.JCalSerializer;
import org.mnode.ical4j.serializer.JCardSerializer;
import org.mnode.ical4j.serializer.XCalSerializer;
import org.mnode.ical4j.serializer.XCardSerializer;
import org.mnode.ical4j.serializer.atom.AtomFeedCalendarSerializer;
import org.mnode.ical4j.serializer.jmap.JSCardSerializer;
import org.mnode.ical4j.serializer.jmap.JSEventSerializer;
import org.mnode.ical4j.serializer.jmap.JSGroupSerializer;
import org.mnode.ical4j.serializer.jotn.CalendarSerializer;
import org.mnode.ical4j.serializer.jotn.VCardSerializer;
import org.mnode.ical4j.serializer.jotn.component.VEventSerializer;
import org.mnode.ical4j.serializer.jotn.component.VTimeZoneSerializer;
import org.mnode.ical4j.serializer.jsonld.EventJsonLdSerializer;
import org.mnode.ical4j.serializer.jsonld.PersonJsonLdSerializer;
import picocli.CommandLine;

@CommandLine.Command(name = "serialize", description = "Serialize a specified vCard")
public class Serializer extends AbstractCardCommand<String> {

public enum FormatOption {
json, xml, jotn, atom, jmap, jsonld;
}

@CommandLine.Option(names = {"-format"}, description = "Valid values: ${COMPLETION-CANDIDATES}")
private FormatOption format;

@CommandLine.Option(names = {"-pretty"})
private boolean pretty;

@CommandLine.Option(names = {"-uid"})
private String uid;

@CommandLine.Option(names = {"-filter"}, split = ",")
private String[] filter;

@Override
public Integer call() throws Exception {
ObjectMapper mapper;
if (FormatOption.xml.equals(format)) {
mapper = serializeXml();
} else if (FormatOption.atom.equals(format)) {
mapper = serializeAtom();
} else if (FormatOption.jotn.equals(format)) {
mapper = serializeJotn(filter);
} else if (FormatOption.jmap.equals(format)) {
mapper = serializeJmap();
} else if (FormatOption.jsonld.equals(format)) {
mapper = serializeJsonld();
} else {
mapper = serializeJson();
}

Object value = getCard();
if (pretty) {
getConsumer().accept(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value));
} else {
getConsumer().accept(mapper.writeValueAsString(value));
}

return 0;
}

private ObjectMapper serializeJson() {
SimpleModule module = new SimpleModule();
module.addSerializer(VCard.class, new JCardSerializer(null));
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
return mapper;
}

private ObjectMapper serializeXml() {
SimpleModule module = new SimpleModule();
module.addSerializer(VCard.class, new XCardSerializer(null));
XmlMapper mapper = XmlMapper.builder().defaultUseWrapper(true).build();
mapper.setConfig(mapper.getSerializationConfig().withRootName(
PropertyName.construct("vcard", "urn:ietf:params:xml:ns:vcard-4.0"))
.with(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME));
mapper.registerModule(module);
return mapper;
}

private ObjectMapper serializeAtom() {
SimpleModule module = new SimpleModule();
// module.addSerializer(Calendar.class, new AtomFeedCalendarSerializer(null));
XmlMapper mapper = XmlMapper.builder().defaultUseWrapper(true).build();
mapper.setConfig(mapper.getSerializationConfig().withRootName(
PropertyName.construct("feed", "https://www.w3.org/2005/Atom"))
.with(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME));
mapper.registerModule(module);
return mapper;
}

private ObjectMapper serializeJotn(String[] filter) {
SimpleModule module = new SimpleModule();
module.addSerializer(VCard.class, new VCardSerializer(null));
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
return mapper;
}

private ObjectMapper serializeJmap() {
SimpleModule module = new SimpleModule();
module.addSerializer(VCard.class, new JSCardSerializer(null));
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
return mapper;
}

private ObjectMapper serializeJsonld() {
SimpleModule module = new SimpleModule();
module.addSerializer(VCard.class, new PersonJsonLdSerializer(null));
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
return mapper;
}
}

0 comments on commit 8b442ec

Please sign in to comment.