Search in sources :

Example 1 with SoundType

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

Aggregations

CannotParseException (ezvcard.io.CannotParseException)1 HCardElement (ezvcard.io.html.HCardElement)1 SoundType (ezvcard.parameter.SoundType)1 Sound (ezvcard.property.Sound)1 DataUri (ezvcard.util.DataUri)1