Search in sources :

Example 1 with VCardDataType

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

the class VCardPropertyScribe method missingXmlElements.

/**
 * Creates a {@link CannotParseException} to indicate that a scribe could
 * not find the necessary XML elements required in order to successfully
 * parse a property (xCards only).
 * @param dataTypes the expected data types (null for "unknown")
 * @return the exception object (note that the exception is NOT thrown!)
 */
protected static CannotParseException missingXmlElements(VCardDataType... dataTypes) {
    String[] elements = new String[dataTypes.length];
    for (int i = 0; i < dataTypes.length; i++) {
        VCardDataType dataType = dataTypes[i];
        elements[i] = (dataType == null) ? "unknown" : dataType.getName().toLowerCase();
    }
    return missingXmlElements(elements);
}
Also used : VCardDataType(ezvcard.VCardDataType)

Example 2 with VCardDataType

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

the class VCardPropertyScribe method _parseXml.

/**
 * <p>
 * Unmarshals a property from an XML document (xCard).
 * </p>
 * <p>
 * This method should be overridden by child classes that wish to support
 * xCard. The default implementation of this method will find the first
 * child element with the xCard namespace. The element's name will be used
 * as the property's data type and its text content (escaped for inclusion
 * in a text-based vCard, e.g. escaping comma characters) will be passed
 * into the {@link #_parseText} method. If no such child element is found,
 * then the parent element's text content will be passed into
 * {@link #_parseText} and the data type will be {@code null}.
 * </p>
 * @param element the property's XML element
 * @param parameters the parsed parameters. These parameters will be
 * assigned to the property object once this method returns. Therefore, do
 * not assign any parameters to the property object itself whilst inside of
 * this method, or else they will be overwritten.
 * @param context the parse context
 * @return the unmarshalled property object
 * @throws CannotParseException if the marshaller could not parse the
 * property's value
 * @throws SkipMeException if the property should not be added to the final
 * {@link VCard} object
 */
protected T _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
    XCardValue firstValue = element.firstValue();
    VCardDataType dataType = firstValue.getDataType();
    String value = VObjectPropertyValues.escape(firstValue.getValue());
    return _parseText(value, dataType, parameters, context);
}
Also used : XCardValue(ezvcard.io.xml.XCardElement.XCardValue) VCardDataType(ezvcard.VCardDataType)

Example 3 with VCardDataType

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

the class VCardPropertyScribe method _writeXml.

/**
 * <p>
 * Marshals a property's value to an XML element (xCard).
 * </p>
 * <p>
 * This method should be overridden by child classes that wish to support
 * xCard. The default implementation of this method will append one child
 * element to the property's XML element. The child element's name will be
 * that of the property's data type (retrieved using the {@link #dataType}
 * method), and the child element's text content will be set to the
 * property's marshalled plain-text value (retrieved using the
 * {@link #writeText} method).
 * </p>
 * @param property the property
 * @param element the property's XML element
 * @throws SkipMeException if the property should not be written to the data
 * stream
 */
protected void _writeXml(T property, XCardElement element) {
    String value = writeText(property, new WriteContext(VCardVersion.V4_0, null, false));
    VCardDataType dataType = dataType(property, VCardVersion.V4_0);
    element.append(dataType, value);
}
Also used : VCardDataType(ezvcard.VCardDataType) WriteContext(ezvcard.io.text.WriteContext)

Example 4 with VCardDataType

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

the class DateOrTimePropertyScribe method _writeXml.

@Override
protected void _writeXml(T property, XCardElement parent) {
    Date date = property.getDate();
    if (date != null) {
        boolean hasTime = property.hasTime();
        String value = date(date).time(hasTime).extended(false).utc(false).write();
        VCardDataType dataType = hasTime ? VCardDataType.DATE_TIME : VCardDataType.DATE;
        parent.append(dataType, value);
        return;
    }
    PartialDate partialDate = property.getPartialDate();
    if (partialDate != null) {
        VCardDataType dataType;
        if (partialDate.hasTimeComponent() && partialDate.hasDateComponent()) {
            dataType = VCardDataType.DATE_TIME;
        } else if (partialDate.hasTimeComponent()) {
            dataType = VCardDataType.TIME;
        } else if (partialDate.hasDateComponent()) {
            dataType = VCardDataType.DATE;
        } else {
            dataType = VCardDataType.DATE_AND_OR_TIME;
        }
        parent.append(dataType, partialDate.toISO8601(false));
        return;
    }
    String text = property.getText();
    if (text != null) {
        parent.append(VCardDataType.TEXT, text);
        return;
    }
    parent.append(VCardDataType.DATE_AND_OR_TIME, "");
}
Also used : PartialDate(ezvcard.util.PartialDate) VCardDataType(ezvcard.VCardDataType) Date(java.util.Date) PartialDate(ezvcard.util.PartialDate)

Example 5 with VCardDataType

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

the class JCardRawReader method parseProperty.

private void parseProperty() throws IOException {
    // get property name
    checkCurrent(JsonToken.VALUE_STRING);
    String propertyName = parser.getValueAsString().toLowerCase();
    // get parameters
    VCardParameters parameters = parseParameters();
    // get group
    List<String> removed = parameters.removeAll("group");
    String group = removed.isEmpty() ? null : removed.get(0);
    // get data type
    checkNext(JsonToken.VALUE_STRING);
    String dataTypeStr = parser.getText().toLowerCase();
    VCardDataType dataType = "unknown".equals(dataTypeStr) ? null : VCardDataType.get(dataTypeStr);
    // get property value(s)
    List<JsonValue> values = parseValues();
    JCardValue value = new JCardValue(values);
    listener.readProperty(group, propertyName, parameters, dataType, value);
}
Also used : VCardParameters(ezvcard.parameter.VCardParameters) 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