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