001package ezvcard.io.scribe; 002 003import java.util.List; 004 005import ezvcard.VCard; 006import ezvcard.VCardVersion; 007import ezvcard.io.ParseContext; 008import ezvcard.io.html.HCardElement; 009import ezvcard.parameter.VCardParameters; 010import ezvcard.property.Email; 011 012/* 013 Copyright (c) 2012-2023, Michael Angstadt 014 All rights reserved. 015 016 Redistribution and use in source and binary forms, with or without 017 modification, are permitted provided that the following conditions are met: 018 019 1. Redistributions of source code must retain the above copyright notice, this 020 list of conditions and the following disclaimer. 021 2. Redistributions in binary form must reproduce the above copyright notice, 022 this list of conditions and the following disclaimer in the documentation 023 and/or other materials provided with the distribution. 024 025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 028 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 029 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 030 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 031 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 032 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 033 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 034 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 035 */ 036 037/** 038 * Marshals {@link Email} properties. 039 * @author Michael Angstadt 040 */ 041public class EmailScribe extends StringPropertyScribe<Email> { 042 public EmailScribe() { 043 super(Email.class, "EMAIL"); 044 } 045 046 @Override 047 protected void _prepareParameters(Email property, VCardParameters copy, VCardVersion version, VCard vcard) { 048 handlePrefParam(property, copy, version, vcard); 049 } 050 051 @Override 052 protected Email _parseValue(String value) { 053 return new Email(value); 054 } 055 056 @Override 057 protected Email _parseHtml(HCardElement element, ParseContext context) { 058 String href = element.attr("href"); 059 String email = extractEmailFromHrefAttribute(href); 060 if (email == null) { 061 email = element.value(); 062 } 063 064 Email property = new Email(email); 065 066 List<String> types = element.types(); 067 property.getParameters().putAll(VCardParameters.TYPE, types); 068 069 return property; 070 } 071 072 private static String extractEmailFromHrefAttribute(String value) { 073 int colon = value.indexOf(':'); 074 if (colon < 0) { 075 return null; 076 } 077 078 String scheme = value.substring(0, colon); 079 return scheme.equalsIgnoreCase("mailto") ? value.substring(colon + 1) : null; 080 } 081}