Search in sources :

Example 11 with VCardDataType

use of ezvcard.VCardDataType in project ez-vcard by mangstadt.

the class ChainingXmlWriter method createXCardDocument.

private XCardDocument createXCardDocument() {
    XCardDocument document = new XCardDocument();
    XCardDocumentStreamWriter writer = document.writer();
    writer.setAddProdId(prodId);
    writer.setVersionStrict(versionStrict);
    for (Map.Entry<String, VCardDataType> entry : parameterDataTypes.entrySet()) {
        String parameterName = entry.getKey();
        VCardDataType dataType = entry.getValue();
        writer.registerParameterDataType(parameterName, dataType);
    }
    if (index != null) {
        writer.setScribeIndex(index);
    }
    for (VCard vcard : vcards) {
        writer.write(vcard);
    }
    return document;
}
Also used : XCardDocument(ezvcard.io.xml.XCardDocument) XCardDocumentStreamWriter(ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter) HashMap(java.util.HashMap) Map(java.util.Map) VCardDataType(ezvcard.VCardDataType) VCard(ezvcard.VCard)

Example 12 with VCardDataType

use of ezvcard.VCardDataType in project ez-vcard by mangstadt.

the class VCardWriterTest method date_time_properties_should_not_have_a_VALUE_parameter.

@Test
public void date_time_properties_should_not_have_a_VALUE_parameter() throws Throwable {
    class DateTestScribe<T extends VCardProperty> extends VCardPropertyScribe<T> {

        private final VCardDataType dataType;

        public DateTestScribe(Class<T> clazz, String name, VCardDataType dataType) {
            super(clazz, name);
            this.dataType = dataType;
        }

        @Override
        protected VCardDataType _defaultDataType(VCardVersion version) {
            return VCardDataType.DATE_AND_OR_TIME;
        }

        @Override
        protected VCardDataType _dataType(T property, VCardVersion version) {
            return dataType;
        }

        @Override
        protected String _writeText(T property, WriteContext context) {
            return "value";
        }

        @Override
        protected T _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
            return null;
        }
    }
    class DateProperty extends VCardProperty {
        // empty
    }
    class DateTimeProperty extends VCardProperty {
        // empty
    }
    class TimeProperty extends VCardProperty {
        // empty
    }
    class DateAndOrTimeProperty extends VCardProperty {
        // empty
    }
    VCard vcard = new VCard();
    vcard.addProperty(new DateProperty());
    vcard.addProperty(new DateTimeProperty());
    vcard.addProperty(new TimeProperty());
    vcard.addProperty(new DateAndOrTimeProperty());
    StringWriter sw = new StringWriter();
    VCardWriter vcw = new VCardWriter(sw, VCardVersion.V4_0);
    vcw.registerScribe(new DateTestScribe<DateProperty>(DateProperty.class, "DATE", VCardDataType.DATE));
    vcw.registerScribe(new DateTestScribe<DateTimeProperty>(DateTimeProperty.class, "DATETIME", VCardDataType.DATE_TIME));
    vcw.registerScribe(new DateTestScribe<TimeProperty>(TimeProperty.class, "TIME", VCardDataType.TIME));
    vcw.registerScribe(new DateTestScribe<DateAndOrTimeProperty>(DateAndOrTimeProperty.class, "DATEANDORTIME", VCardDataType.DATE_AND_OR_TIME));
    vcw.setAddProdId(false);
    vcw.write(vcard);
    String actual = sw.toString();
    // @formatter:off
    String expected = "BEGIN:VCARD\r\n" + "VERSION:4.0\r\n" + "DATE:value\r\n" + "DATETIME:value\r\n" + "TIME:value\r\n" + "DATEANDORTIME:value\r\n" + "END:VCARD\r\n";
    // @formatter:on
    assertEquals(actual, expected);
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) VCardVersion(ezvcard.VCardVersion) VCardParameters(ezvcard.parameter.VCardParameters) StringWriter(java.io.StringWriter) ParseContext(ezvcard.io.ParseContext) VCardProperty(ezvcard.property.VCardProperty) VCardDataType(ezvcard.VCardDataType) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 13 with VCardDataType

use of ezvcard.VCardDataType in project ez-vcard by mangstadt.

the class VCardWriter method handleValueParameter.

/**
 * <p>
 * Sets the property's VALUE parameter. This method only adds a VALUE
 * parameter if all the following conditions are met:
 * </p>
 * <ol>
 * <li>The data type is NOT "unknown"</li>
 * <li>The data type is different from the property's default data type</li>
 * <li>The data type does not fall under the "date/time special case" (see
 * {@link #isDateTimeValueParameterSpecialCase()})</li>
 * </ol>
 * @param property the property
 * @param scribe the property scribe
 * @param parameters the property parameters
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void handleValueParameter(VCardProperty property, VCardPropertyScribe scribe, VCardParameters parameters) {
    VCardDataType dataType = scribe.dataType(property, targetVersion);
    if (dataType == null) {
        return;
    }
    VCardDataType defaultDataType = scribe.defaultDataType(targetVersion);
    if (dataType == defaultDataType) {
        return;
    }
    if (isDateTimeValueParameterSpecialCase(defaultDataType, dataType)) {
        return;
    }
    parameters.setValue(dataType);
}
Also used : VCardDataType(ezvcard.VCardDataType)

Example 14 with VCardDataType

use of ezvcard.VCardDataType in project ez-vcard by mangstadt.

the class XCardElement method first.

/**
 * Gets the first value with one of the given data types.
 * @param dataTypes the data type(s) to look for (null signifies the
 * "unknown" data type)
 * @return the value or null if not found
 */
public String first(VCardDataType... dataTypes) {
    String[] names = new String[dataTypes.length];
    for (int i = 0; i < dataTypes.length; i++) {
        VCardDataType dataType = dataTypes[i];
        names[i] = toLocalName(dataType);
    }
    return first(names);
}
Also used : VCardDataType(ezvcard.VCardDataType)

Aggregations

VCardDataType (ezvcard.VCardDataType)14 VCardParameters (ezvcard.parameter.VCardParameters)3 Map (java.util.Map)3 VCard (ezvcard.VCard)2 VCardVersion (ezvcard.VCardVersion)2 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)2 XCardValue (ezvcard.io.xml.XCardElement.XCardValue)2 VCardProperty (ezvcard.property.VCardProperty)2 HashMap (java.util.HashMap)2 List (java.util.List)2 SyntaxStyle (com.github.mangstadt.vinnie.SyntaxStyle)1 AllowedCharacters (com.github.mangstadt.vinnie.validate.AllowedCharacters)1 ValidationWarning (ezvcard.ValidationWarning)1 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)1 ParseContext (ezvcard.io.ParseContext)1 SkipMeException (ezvcard.io.SkipMeException)1 WriteContext (ezvcard.io.text.WriteContext)1 XCardDocument (ezvcard.io.xml.XCardDocument)1 XCardDocumentStreamWriter (ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter)1 ClientPidMap (ezvcard.property.ClientPidMap)1