001 package ezvcard.io.scribe; 002 003 import java.util.List; 004 005 import ezvcard.VCardDataType; 006 import ezvcard.VCardVersion; 007 import ezvcard.io.json.JCardValue; 008 import ezvcard.io.xml.XCardElement; 009 import ezvcard.parameter.VCardParameters; 010 import ezvcard.property.Gender; 011 012 /* 013 Copyright (c) 2013, 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 Gender} properties. 039 * @author Michael Angstadt 040 */ 041 public class GenderScribe extends VCardPropertyScribe<Gender> { 042 public GenderScribe() { 043 super(Gender.class, "GENDER"); 044 } 045 046 @Override 047 protected VCardDataType _defaultDataType(VCardVersion version) { 048 return VCardDataType.TEXT; 049 } 050 051 @Override 052 protected String _writeText(Gender property, VCardVersion version) { 053 String gender = property.getGender(); 054 String text = property.getText(); 055 056 if (text != null) { 057 return structured(gender, text); 058 } 059 if (gender != null) { 060 return structured(new Object[] { gender }); 061 } 062 return ""; 063 } 064 065 @Override 066 protected Gender _parseText(String value, VCardDataType dataType, VCardVersion version, VCardParameters parameters, List<String> warnings) { 067 SemiStructuredIterator it = semistructured(value, 2); 068 069 String sex = it.next(); 070 if (sex != null) { 071 sex = sex.toUpperCase(); 072 } 073 String text = it.next(); 074 075 Gender property = new Gender(sex); 076 property.setText(text); 077 return property; 078 } 079 080 @Override 081 protected void _writeXml(Gender property, XCardElement parent) { 082 parent.append("sex", property.getGender()); 083 084 String text = property.getText(); 085 if (text != null) { 086 parent.append("identity", text); 087 } 088 } 089 090 @Override 091 protected Gender _parseXml(XCardElement element, VCardParameters parameters, List<String> warnings) { 092 String sex = element.first("sex"); 093 if (sex != null) { 094 Gender property = new Gender(sex); 095 property.setText(element.first("identity")); //optional field 096 return property; 097 } 098 099 throw missingXmlElements("sex"); 100 } 101 102 @Override 103 protected JCardValue _writeJson(Gender property) { 104 String gender = property.getGender(); 105 String text = property.getText(); 106 107 if (text == null) { 108 return JCardValue.single(gender); 109 } 110 return JCardValue.structured(gender, text); 111 } 112 113 @Override 114 protected Gender _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, List<String> warnings) { 115 StructuredIterator it = structured(value); 116 117 String sex = it.nextString(); 118 if (sex != null) { 119 sex = sex.toUpperCase(); 120 } 121 String text = it.nextString(); 122 123 Gender property = new Gender(sex); 124 property.setText(text); 125 return property; 126 } 127 }