001 package ezvcard.io.scribe; 002 003 import java.util.List; 004 005 import ezvcard.io.CannotParseException; 006 import ezvcard.io.html.HCardElement; 007 import ezvcard.parameter.ImageType; 008 import ezvcard.property.ImageProperty; 009 import ezvcard.util.DataUri; 010 011 /* 012 Copyright (c) 2013, Michael Angstadt 013 All rights reserved. 014 015 Redistribution and use in source and binary forms, with or without 016 modification, are permitted provided that the following conditions are met: 017 018 1. Redistributions of source code must retain the above copyright notice, this 019 list of conditions and the following disclaimer. 020 2. Redistributions in binary form must reproduce the above copyright notice, 021 this list of conditions and the following disclaimer in the documentation 022 and/or other materials provided with the distribution. 023 024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 025 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 028 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 031 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 034 */ 035 036 /** 037 * Marshals properties that contain images. 038 * @param <T> the property class 039 * @author Michael Angstadt 040 */ 041 public abstract class ImagePropertyScribe<T extends ImageProperty> extends BinaryPropertyScribe<T, ImageType> { 042 public ImagePropertyScribe(Class<T> clazz, String propertyName) { 043 super(clazz, propertyName); 044 } 045 046 @Override 047 protected ImageType _buildTypeObj(String type) { 048 return ImageType.get(type, null, null); 049 } 050 051 @Override 052 protected ImageType _buildMediaTypeObj(String mediaType) { 053 return ImageType.get(null, mediaType, null); 054 } 055 056 @Override 057 protected T _parseHtml(HCardElement element, List<String> warnings) { 058 String elementName = element.tagName(); 059 if (!"img".equals(elementName)) { 060 return super._parseHtml(element, warnings); 061 } 062 063 String src = element.absUrl("src"); 064 if (src.length() == 0) { 065 throw new CannotParseException(13); 066 } 067 068 try { 069 DataUri uri = new DataUri(src); 070 ImageType mediaType = _buildMediaTypeObj(uri.getContentType()); 071 return _newInstance(uri.getData(), mediaType); 072 } catch (IllegalArgumentException e) { 073 //not a data URI 074 //TODO create buildTypeObjFromExtension() method 075 return _newInstance(src, null); 076 } 077 } 078 }