001 package ezvcard.io.scribe; 002 003 import java.util.List; 004 005 import ezvcard.VCardDataType; 006 import ezvcard.VCardVersion; 007 import ezvcard.io.CannotParseException; 008 import ezvcard.io.json.JCardValue; 009 import ezvcard.io.xml.XCardElement; 010 import ezvcard.parameter.VCardParameters; 011 import ezvcard.property.ClientPidMap; 012 013 /* 014 Copyright (c) 2013, Michael Angstadt 015 All rights reserved. 016 017 Redistribution and use in source and binary forms, with or without 018 modification, are permitted provided that the following conditions are met: 019 020 1. Redistributions of source code must retain the above copyright notice, this 021 list of conditions and the following disclaimer. 022 2. Redistributions in binary form must reproduce the above copyright notice, 023 this list of conditions and the following disclaimer in the documentation 024 and/or other materials provided with the distribution. 025 026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 027 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 028 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 029 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 030 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 031 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 032 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 033 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 034 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 035 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 036 */ 037 038 /** 039 * Marshals {@link ClientPidMap} properties. 040 * @author Michael Angstadt 041 */ 042 public class ClientPidMapScribe extends VCardPropertyScribe<ClientPidMap> { 043 public ClientPidMapScribe() { 044 super(ClientPidMap.class, "CLIENTPIDMAP"); 045 } 046 047 @Override 048 protected VCardDataType _defaultDataType(VCardVersion version) { 049 return VCardDataType.TEXT; 050 } 051 052 @Override 053 protected String _writeText(ClientPidMap property, VCardVersion version) { 054 return structured(property.getPid(), property.getUri()); 055 } 056 057 @Override 058 protected ClientPidMap _parseText(String value, VCardDataType dataType, VCardVersion version, VCardParameters parameters, List<String> warnings) { 059 SemiStructuredIterator it = semistructured(value, 2); 060 String pid = it.next(); 061 String uri = it.next(); 062 if (pid == null || uri == null) { 063 throw new CannotParseException(3); 064 } 065 066 return parse(pid, uri); 067 } 068 069 @Override 070 protected void _writeXml(ClientPidMap property, XCardElement parent) { 071 Integer pid = property.getPid(); 072 parent.append("sourceid", (pid == null) ? "" : pid.toString()); 073 074 parent.append(VCardDataType.URI, property.getUri()); 075 } 076 077 @Override 078 protected ClientPidMap _parseXml(XCardElement element, VCardParameters parameters, List<String> warnings) { 079 String sourceid = element.first("sourceid"); 080 String uri = element.first(VCardDataType.URI); 081 082 if (uri == null && sourceid == null) { 083 throw missingXmlElements(VCardDataType.URI.getName().toLowerCase(), "sourceid"); 084 } 085 if (uri == null) { 086 throw missingXmlElements(VCardDataType.URI); 087 } 088 if (sourceid == null) { 089 throw missingXmlElements("sourceid"); 090 } 091 092 return parse(sourceid, uri); 093 } 094 095 @Override 096 protected JCardValue _writeJson(ClientPidMap property) { 097 return JCardValue.structured(property.getPid(), property.getUri()); 098 } 099 100 @Override 101 protected ClientPidMap _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, List<String> warnings) { 102 StructuredIterator it = structured(value); 103 String pid = it.nextString(); 104 String uri = it.nextString(); 105 return parse(pid, uri); 106 } 107 108 private ClientPidMap parse(String pid, String uri) { 109 try { 110 return new ClientPidMap(Integer.parseInt(pid), uri); 111 } catch (NumberFormatException e) { 112 throw new CannotParseException(4); 113 } 114 } 115 }