Search in sources :

Example 1 with DataUri

use of ezvcard.util.DataUri in project ez-vcard by mangstadt.

the class BinaryPropertyScribe method parse.

private T parse(String value, VCardDataType dataType, VCardParameters parameters, VCardVersion version) {
    U contentType = parseContentType(value, parameters, version);
    switch(version) {
        case V2_1:
        case V3_0:
            // parse as URL
            if (dataType == VCardDataType.URL || dataType == VCardDataType.URI) {
                return _newInstance(value, contentType);
            }
            // parse as binary
            Encoding encodingSubType = parameters.getEncoding();
            if (encodingSubType == Encoding.BASE64 || encodingSubType == Encoding.B) {
                return _newInstance(Base64.decodeBase64(value), contentType);
            }
            break;
        case V4_0:
            try {
                // parse as data URI
                DataUri uri = DataUri.parse(value);
                contentType = _mediaTypeFromMediaTypeParameter(uri.getContentType());
                return _newInstance(uri.getData(), contentType);
            } catch (IllegalArgumentException e) {
            // not a data URI
            }
            break;
    }
    return cannotUnmarshalValue(value, version, contentType);
}
Also used : Encoding(ezvcard.parameter.Encoding) DataUri(ezvcard.util.DataUri)

Example 2 with DataUri

use of ezvcard.util.DataUri in project ez-vcard by mangstadt.

the class SoundScribe method _parseHtml.

@Override
protected Sound _parseHtml(HCardElement element, ParseContext context) {
    String elementName = element.tagName();
    if (!"audio".equals(elementName) && !"source".equals(elementName)) {
        return super._parseHtml(element, context);
    }
    if ("audio".equals(elementName)) {
        // parse its child "<source>" element
        org.jsoup.nodes.Element source = element.getElement().getElementsByTag("source").first();
        if (source == null) {
            throw new CannotParseException(16);
        }
        element = new HCardElement(source);
    }
    String src = element.absUrl("src");
    if (src.length() == 0) {
        throw new CannotParseException(17);
    }
    String type = element.attr("type");
    SoundType mediaType = (type.length() == 0) ? null : _mediaTypeFromMediaTypeParameter(type);
    try {
        DataUri uri = DataUri.parse(src);
        mediaType = _mediaTypeFromMediaTypeParameter(uri.getContentType());
        return new Sound(uri.getData(), mediaType);
    } catch (IllegalArgumentException e) {
        // not a data URI
        if (mediaType == null) {
            String extension = getFileExtension(src);
            mediaType = (extension == null) ? null : _mediaTypeFromFileExtension(extension);
        }
        return new Sound(src, mediaType);
    }
}
Also used : SoundType(ezvcard.parameter.SoundType) CannotParseException(ezvcard.io.CannotParseException) Sound(ezvcard.property.Sound) HCardElement(ezvcard.io.html.HCardElement) DataUri(ezvcard.util.DataUri)

Example 3 with DataUri

use of ezvcard.util.DataUri in project ez-vcard by mangstadt.

the class ImagePropertyScribe method _parseHtml.

@Override
protected T _parseHtml(HCardElement element, ParseContext context) {
    String elementName = element.tagName();
    if (!"img".equals(elementName)) {
        return super._parseHtml(element, context);
    }
    String src = element.absUrl("src");
    if (src.length() == 0) {
        throw new CannotParseException(13);
    }
    try {
        DataUri uri = DataUri.parse(src);
        ImageType mediaType = _mediaTypeFromMediaTypeParameter(uri.getContentType());
        return _newInstance(uri.getData(), mediaType);
    } catch (IllegalArgumentException e) {
        // not a data URI
        String extension = getFileExtension(src);
        ImageType mediaType = (extension == null) ? null : _mediaTypeFromFileExtension(extension);
        return _newInstance(src, mediaType);
    }
}
Also used : CannotParseException(ezvcard.io.CannotParseException) DataUri(ezvcard.util.DataUri) ImageType(ezvcard.parameter.ImageType)

Example 4 with DataUri

use of ezvcard.util.DataUri in project ez-vcard by mangstadt.

the class BinaryPropertyScribe method write.

private String write(T property, VCardVersion version) {
    String url = property.getUrl();
    if (url != null) {
        return url;
    }
    byte[] data = property.getData();
    if (data != null) {
        switch(version) {
            case V2_1:
            case V3_0:
                return Base64.encodeBase64String(data);
            case V4_0:
                U contentType = property.getContentType();
                String mediaType = (contentType == null || contentType.getMediaType() == null) ? "application/octet-stream" : contentType.getMediaType();
                return new DataUri(mediaType, data).toString();
        }
    }
    return "";
}
Also used : DataUri(ezvcard.util.DataUri)

Example 5 with DataUri

use of ezvcard.util.DataUri in project ez-vcard by mangstadt.

the class BinaryPropertyScribe method _parseHtml.

@Override
protected T _parseHtml(HCardElement element, ParseContext context) {
    String elementName = element.tagName();
    if (!"object".equals(elementName)) {
        throw new CannotParseException(1, elementName);
    }
    String data = element.absUrl("data");
    if (data.length() == 0) {
        throw new CannotParseException(2);
    }
    try {
        DataUri uri = DataUri.parse(data);
        U mediaType = _mediaTypeFromMediaTypeParameter(uri.getContentType());
        return _newInstance(uri.getData(), mediaType);
    } catch (IllegalArgumentException e) {
        // not a data URI
        U mediaType = null;
        String type = element.attr("type");
        if (type.length() > 0) {
            mediaType = _mediaTypeFromMediaTypeParameter(type);
        } else {
            String extension = getFileExtension(data);
            mediaType = (extension == null) ? null : _mediaTypeFromFileExtension(extension);
        }
        return _newInstance(data, mediaType);
    }
}
Also used : CannotParseException(ezvcard.io.CannotParseException) DataUri(ezvcard.util.DataUri)

Aggregations

DataUri (ezvcard.util.DataUri)5 CannotParseException (ezvcard.io.CannotParseException)3 HCardElement (ezvcard.io.html.HCardElement)1 Encoding (ezvcard.parameter.Encoding)1 ImageType (ezvcard.parameter.ImageType)1 SoundType (ezvcard.parameter.SoundType)1 Sound (ezvcard.property.Sound)1