001 package ezvcard.property; 002 003 import java.text.NumberFormat; 004 import java.util.EnumSet; 005 import java.util.List; 006 import java.util.Map; 007 import java.util.Set; 008 009 import ezvcard.VCard; 010 import ezvcard.VCardVersion; 011 import ezvcard.ValidationWarnings; 012 import ezvcard.Warning; 013 014 /* 015 Copyright (c) 2013, Michael Angstadt 016 All rights reserved. 017 018 Redistribution and use in source and binary forms, with or without 019 modification, are permitted provided that the following conditions are met: 020 021 1. Redistributions of source code must retain the above copyright notice, this 022 list of conditions and the following disclaimer. 023 2. Redistributions in binary form must reproduce the above copyright notice, 024 this list of conditions and the following disclaimer in the documentation 025 and/or other materials provided with the distribution. 026 027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 028 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 029 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 030 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 031 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 032 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 033 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 034 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 035 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 036 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 037 038 The views and conclusions contained in the software and documentation are those 039 of the authors and should not be interpreted as representing official policies, 040 either expressed or implied, of the FreeBSD Project. 041 */ 042 043 /** 044 * An embedded vCard or URL containing the information of someone who represents 045 * the person. 046 * 047 * <p> 048 * <b>URL</b> 049 * </p> 050 * 051 * <pre class="brush:java"> 052 * VCard vcard = new VCard(); 053 * Agent agent = new Agent("http://mi5.gov.uk/007"); 054 * vcard.setAgent(agent); 055 * </pre> 056 * 057 * <p> 058 * <b>vCard</b> 059 * </p> 060 * 061 * <pre class="brush:java"> 062 * VCard vcard = new VCard(); 063 * VCard agentVcard = new VCard(); 064 * agentVcard.setFormattedName("Agent 007"); 065 * Agent agent = new Agent(agentVcard); 066 * vcard.setAgent(agent); 067 * </pre> 068 * 069 * <p> 070 * <b>Property name:</b> {@code AGENT} 071 * </p> 072 * <p> 073 * <b>Supported versions:</b> {@code 2.1, 3.0} 074 * </p> 075 * 076 * @author Michael Angstadt 077 */ 078 public class Agent extends VCardProperty { 079 private String url; 080 private VCard vcard; 081 082 /** 083 * Creates an empty agent property. 084 */ 085 public Agent() { 086 //empty 087 } 088 089 /** 090 * Creates an agent property. 091 * @param url a URL pointing to the agent's information 092 */ 093 public Agent(String url) { 094 setUrl(url); 095 } 096 097 /** 098 * Creates an agent property. 099 * @param vcard a vCard containing the agent's information 100 */ 101 public Agent(VCard vcard) { 102 setVCard(vcard); 103 } 104 105 @Override 106 public Set<VCardVersion> _supportedVersions() { 107 return EnumSet.of(VCardVersion.V2_1, VCardVersion.V3_0); 108 } 109 110 /** 111 * Gets the URL to the agent's information. 112 * @return the URL or null if not set 113 */ 114 public String getUrl() { 115 return url; 116 } 117 118 /** 119 * Sets the URL to the agent's information. 120 * @param url the URL 121 */ 122 public void setUrl(String url) { 123 this.url = url; 124 vcard = null; 125 } 126 127 /** 128 * Gets an embedded vCard with the agent's information. 129 * @return the vCard or null if not set 130 */ 131 public VCard getVCard() { 132 return vcard; 133 } 134 135 /** 136 * Sets an embedded vCard with the agent's information. 137 * @param vcard the vCard 138 */ 139 public void setVCard(VCard vcard) { 140 this.vcard = vcard; 141 url = null; 142 } 143 144 @Override 145 protected void _validate(List<Warning> warnings, VCardVersion version, VCard vcard) { 146 if (url == null && this.vcard == null) { 147 warnings.add(new Warning(8)); 148 } 149 150 if (this.vcard != null) { 151 NumberFormat nf = NumberFormat.getIntegerInstance(); 152 nf.setMinimumIntegerDigits(2); 153 154 ValidationWarnings validationWarnings = this.vcard.validate(version); 155 for (Map.Entry<VCardProperty, List<Warning>> entry : validationWarnings) { 156 VCardProperty property = entry.getKey(); 157 List<Warning> propViolations = entry.getValue(); 158 159 for (Warning propViolation : propViolations) { 160 String className = (property == null) ? "" : property.getClass().getSimpleName(); 161 162 int code = propViolation.getCode(); 163 String codeStr = (code >= 0) ? "W" + nf.format(code) : ""; 164 String message = propViolation.getMessage(); 165 warnings.add(new Warning(10, className, codeStr, message)); 166 } 167 } 168 } 169 } 170 }