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);
}
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);
}
}
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);
}
}
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 "";
}
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);
}
}
Aggregations