use of ezvcard.property.VCardProperty in project ez-vcard by mangstadt.
the class VCardPropertyScribe method handlePrefParam.
/**
* A utility method for switching between the "PREF" and "TYPE=PREF"
* parameters when marshalling a property (version 4.0 vCards use "PREF=1",
* while version 3.0 vCards use "TYPE=PREF"). This method is meant to be
* called from a scribe's {@link #_prepareParameters} method.
* @param property the property that is being marshalled
* @param parameters the parameters that are being marshalled (this should
* be a copy of the property's parameters so that changes can be made to
* them without affecting the original object)
* @param version the vCard version that the vCard is being marshalled to
* @param vcard the vCard that's being marshalled
*/
protected static void handlePrefParam(VCardProperty property, VCardParameters parameters, VCardVersion version, VCard vcard) {
switch(version) {
case V2_1:
case V3_0:
parameters.setPref(null);
// find the property with the lowest PREF value in the vCard
VCardProperty mostPreferred = null;
Integer lowestPref = null;
for (VCardProperty p : vcard.getProperties(property.getClass())) {
Integer pref;
try {
pref = p.getParameters().getPref();
} catch (IllegalStateException e) {
continue;
}
if (pref == null) {
continue;
}
if (lowestPref == null || pref < lowestPref) {
mostPreferred = p;
lowestPref = pref;
}
}
if (property == mostPreferred) {
parameters.put(VCardParameters.TYPE, "pref");
}
break;
case V4_0:
for (String type : property.getParameters().get(VCardParameters.TYPE)) {
if ("pref".equalsIgnoreCase(type)) {
parameters.remove(VCardParameters.TYPE, type);
parameters.setPref(1);
break;
}
}
break;
}
}
use of ezvcard.property.VCardProperty in project ez-vcard by mangstadt.
the class VCardWriter method _write.
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void _write(VCard vcard, List<VCardProperty> propertiesToAdd) throws IOException {
VCardVersion targetVersion = getTargetVersion();
TargetApplication targetApplication = getTargetApplication();
Boolean includeTrailingSemicolons = this.includeTrailingSemicolons;
if (includeTrailingSemicolons == null) {
includeTrailingSemicolons = (targetVersion == VCardVersion.V4_0);
}
WriteContext context = new WriteContext(targetVersion, targetApplication, includeTrailingSemicolons);
writer.writeBeginComponent("VCARD");
writer.writeVersion(targetVersion.getVersion());
for (VCardProperty property : propertiesToAdd) {
VCardPropertyScribe scribe = index.getPropertyScribe(property);
String value = null;
VCard nestedVCard = null;
try {
value = scribe.writeText(property, context);
} catch (SkipMeException e) {
continue;
} catch (EmbeddedVCardException e) {
nestedVCard = e.getVCard();
}
VCardParameters parameters = scribe.prepareParameters(property, targetVersion, vcard);
if (nestedVCard != null) {
writeNestedVCard(nestedVCard, property, scribe, parameters, value);
continue;
}
handleValueParameter(property, scribe, parameters);
handleLabelParameter(property, parameters);
writer.writeProperty(property.getGroup(), scribe.getPropertyName(), new VObjectParameters(parameters.getMap()), value);
fixBinaryPropertyForOutlook(property);
}
writer.writeEndComponent("VCARD");
}
use of ezvcard.property.VCardProperty in project ez-vcard by mangstadt.
the class XCardWriter method _write.
@Override
protected void _write(VCard vcard, List<VCardProperty> properties) throws IOException {
try {
if (!started) {
handler.startDocument();
if (!vcardsElementExists) {
// don't output a <vcards> element if the parent is a <vcards> element
start(VCARDS);
}
started = true;
}
// group the types by group name (null = no group name)
ListMultimap<String, VCardProperty> propertiesByGroup = new ListMultimap<String, VCardProperty>();
for (VCardProperty property : properties) {
propertiesByGroup.put(property.getGroup(), property);
}
start(VCARD);
for (Map.Entry<String, List<VCardProperty>> entry : propertiesByGroup) {
String groupName = entry.getKey();
if (groupName != null) {
AttributesImpl attr = new AttributesImpl();
attr.addAttribute(XCardQNames.NAMESPACE, "", "name", "", groupName);
start(GROUP, attr);
}
for (VCardProperty property : entry.getValue()) {
write(property, vcard);
}
if (groupName != null) {
end(GROUP);
}
}
end(VCARD);
} catch (SAXException e) {
throw new IOException(e);
}
}
Aggregations