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