Search in sources :

Example 6 with VCardParameters

use of ezvcard.parameter.VCardParameters in project ez-vcard by mangstadt.

the class JCardRawWriterTest method group.

@Test
public void group() throws Throwable {
    StringWriter sw = new StringWriter();
    JCardRawWriter writer = new JCardRawWriter(sw, false);
    writer.writeStartVCard();
    writer.writeProperty("one", "prop", new VCardParameters(), VCardDataType.TEXT, JCardValue.single("value"));
    writer.close();
    String actual = sw.toString();
    // @formatter:off
    String expected = "[\"vcard\"," + "[" + "[\"prop\",{\"group\":\"one\"},\"text\",\"value\"]" + "]" + "]";
    // @formatter:on
    assertEquals(expected, actual);
}
Also used : VCardParameters(ezvcard.parameter.VCardParameters) StringWriter(java.io.StringWriter) Test(org.junit.Test)

Example 7 with VCardParameters

use of ezvcard.parameter.VCardParameters in project ez-vcard by mangstadt.

the class VCardPropertyScribe method prepareParameters.

/**
 * Sanitizes a property's parameters (called before the property is
 * written). Note that a copy of the parameters is returned so that the
 * property object does not get modified.
 * @param property the property
 * @param version the version of the vCard that is being generated
 * @param vcard the vCard that the property belongs to
 * @return the sanitized parameters
 */
public final VCardParameters prepareParameters(T property, VCardVersion version, VCard vcard) {
    // make a copy because the property should not get modified when it is marshalled
    VCardParameters copy = new VCardParameters(property.getParameters());
    _prepareParameters(property, copy, version, vcard);
    return copy;
}
Also used : VCardParameters(ezvcard.parameter.VCardParameters)

Example 8 with VCardParameters

use of ezvcard.parameter.VCardParameters in project ez-vcard by mangstadt.

the class VCardPropertyScribe method _parseHtml.

/**
 * <p>
 * Unmarshals the property from an hCard (HTML document).
 * </p>
 * <p>
 * This method should be overridden by child classes that wish to support
 * hCard. The default implementation of this method will retrieve the HTML
 * element's hCard value (as described in {@link HCardElement#value}), and
 * pass it into the {@link #_parseText} method.
 * </p>
 * @param element the property's HTML element
 * @param context the parse context
 * @return the unmarshalled property object
 * @throws CannotParseException if the property value could not be parsed
 * @throws SkipMeException if this property should NOT be added to the
 * {@link VCard} object
 * @throws EmbeddedVCardException if the value of this property is an
 * embedded vCard (i.e. the AGENT property)
 */
protected T _parseHtml(HCardElement element, ParseContext context) {
    String value = VObjectPropertyValues.escape(element.value());
    VCardParameters parameters = new VCardParameters();
    T property = _parseText(value, null, parameters, context);
    property.setParameters(parameters);
    return property;
}
Also used : VCardParameters(ezvcard.parameter.VCardParameters)

Example 9 with VCardParameters

use of ezvcard.parameter.VCardParameters 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)

Example 10 with VCardParameters

use of ezvcard.parameter.VCardParameters in project ez-vcard by mangstadt.

the class JCardWriter method _write.

/**
 * Writes a vCard to the stream.
 * @param vcard the vCard that is being written
 * @param properties the properties to write
 * @throws IOException if there's a problem writing to the output stream
 * @throws IllegalArgumentException if a scribe hasn't been registered for a
 * custom property class (see: {@link #registerScribe registerScribe})
 */
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void _write(VCard vcard, List<VCardProperty> properties) throws IOException {
    Object previousValue = getCurrentValue();
    writer.writeStartVCard();
    writer.writeProperty("version", VCardDataType.TEXT, JCardValue.single(targetVersion.getVersion()));
    for (VCardProperty property : properties) {
        VCardPropertyScribe scribe = index.getPropertyScribe(property);
        // marshal the value
        JCardValue value;
        try {
            value = scribe.writeJson(property);
        } catch (SkipMeException e) {
            // property has requested not to be written
            continue;
        } catch (EmbeddedVCardException e) {
            // don't write because jCard does not support embedded vCards
            continue;
        }
        String group = property.getGroup();
        String name = scribe.getPropertyName().toLowerCase();
        VCardParameters parameters = scribe.prepareParameters(property, targetVersion, vcard);
        VCardDataType dataType = scribe.dataType(property, targetVersion);
        writer.writeProperty(group, name, parameters, dataType, value);
    }
    writer.writeEndVCard();
    setCurrentValue(previousValue);
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) VCardParameters(ezvcard.parameter.VCardParameters) EmbeddedVCardException(ezvcard.io.EmbeddedVCardException) VCardProperty(ezvcard.property.VCardProperty) SkipMeException(ezvcard.io.SkipMeException) VCardDataType(ezvcard.VCardDataType)

Aggregations

VCardParameters (ezvcard.parameter.VCardParameters)32 Test (org.junit.Test)24 JCardDataStreamListener (ezvcard.io.json.JCardRawReader.JCardDataStreamListener)12 VCard (ezvcard.VCard)5 VCardVersion (ezvcard.VCardVersion)5 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)4 VCardDataType (ezvcard.VCardDataType)3 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)3 SkipMeException (ezvcard.io.SkipMeException)3 VCardProperty (ezvcard.property.VCardProperty)3 StringWriter (java.io.StringWriter)3 Pid (ezvcard.parameter.Pid)2 VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)1 ParseContext (ezvcard.io.ParseContext)1 JCardValue (ezvcard.io.json.JCardValue)1 Xml (ezvcard.property.Xml)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 QName (javax.xml.namespace.QName)1