Search in sources :

Example 11 with VCardProperty

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;
    }
}
Also used : VCardProperty(ezvcard.property.VCardProperty)

Example 12 with VCardProperty

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");
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) VCardParameters(ezvcard.parameter.VCardParameters) EmbeddedVCardException(ezvcard.io.EmbeddedVCardException) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) VCardProperty(ezvcard.property.VCardProperty) VCardVersion(ezvcard.VCardVersion) SkipMeException(ezvcard.io.SkipMeException) VCard(ezvcard.VCard)

Example 13 with VCardProperty

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);
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) VCardProperty(ezvcard.property.VCardProperty) NodeList(org.w3c.dom.NodeList) List(java.util.List) IOException(java.io.IOException) ListMultimap(ezvcard.util.ListMultimap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) SAXException(org.xml.sax.SAXException)

Aggregations

VCardProperty (ezvcard.property.VCardProperty)13 VCardVersion (ezvcard.VCardVersion)4 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)4 VCard (ezvcard.VCard)3 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)3 SkipMeException (ezvcard.io.SkipMeException)3 VCardParameters (ezvcard.parameter.VCardParameters)3 RawProperty (ezvcard.property.RawProperty)3 VCardDataType (ezvcard.VCardDataType)2 Label (ezvcard.property.Label)2 ProductId (ezvcard.property.ProductId)2 IdentityHashMap (java.util.IdentityHashMap)2 Test (org.junit.Test)2 VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)1 CannotParseException (ezvcard.io.CannotParseException)1 ParseContext (ezvcard.io.ParseContext)1 RawPropertyScribe (ezvcard.io.scribe.RawPropertyScribe)1