001 package ezvcard.io.scribe; 002 003 import java.util.List; 004 005 import ezvcard.VCardDataType; 006 import ezvcard.VCardVersion; 007 import ezvcard.io.html.HCardElement; 008 import ezvcard.io.json.JCardValue; 009 import ezvcard.io.xml.XCardElement; 010 import ezvcard.parameter.VCardParameters; 011 import ezvcard.property.StructuredName; 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 StructuredName} properties. 040 * @author Michael Angstadt 041 */ 042 public class StructuredNameScribe extends VCardPropertyScribe<StructuredName> { 043 public StructuredNameScribe() { 044 super(StructuredName.class, "N"); 045 } 046 047 @Override 048 protected VCardDataType _defaultDataType(VCardVersion version) { 049 return VCardDataType.TEXT; 050 } 051 052 @Override 053 protected String _writeText(StructuredName property, VCardVersion version) { 054 return structured(property.getFamily(), property.getGiven(), property.getAdditional(), property.getPrefixes(), property.getSuffixes()); 055 } 056 057 @Override 058 protected StructuredName _parseText(String value, VCardDataType dataType, VCardVersion version, VCardParameters parameters, List<String> warnings) { 059 StructuredName property = new StructuredName(); 060 StructuredIterator it = structured(value); 061 062 property.setFamily(it.nextString()); 063 property.setGiven(it.nextString()); 064 property.getAdditional().addAll(it.nextComponent()); 065 property.getPrefixes().addAll(it.nextComponent()); 066 property.getSuffixes().addAll(it.nextComponent()); 067 068 return property; 069 } 070 071 @Override 072 protected void _writeXml(StructuredName property, XCardElement parent) { 073 parent.append("surname", property.getFamily()); //the XML element still needs to be printed if value == null 074 parent.append("given", property.getGiven()); 075 parent.append("additional", property.getAdditional()); 076 parent.append("prefix", property.getPrefixes()); 077 parent.append("suffix", property.getSuffixes()); 078 } 079 080 @Override 081 protected StructuredName _parseXml(XCardElement element, VCardParameters parameters, List<String> warnings) { 082 StructuredName property = new StructuredName(); 083 084 property.setFamily(s(element.first("surname"))); 085 property.setGiven(s(element.first("given"))); 086 property.getAdditional().addAll(element.all("additional")); 087 property.getPrefixes().addAll(element.all("prefix")); 088 property.getSuffixes().addAll(element.all("suffix")); 089 090 return property; 091 } 092 093 private String s(String value) { 094 return (value == null || value.length() == 0) ? null : value; 095 } 096 097 @Override 098 protected StructuredName _parseHtml(HCardElement element, List<String> warnings) { 099 StructuredName property = new StructuredName(); 100 101 property.setFamily(s(element.firstValue("family-name"))); 102 property.setGiven(s(element.firstValue("given-name"))); 103 property.getAdditional().addAll(element.allValues("additional-name")); 104 property.getPrefixes().addAll(element.allValues("honorific-prefix")); 105 property.getSuffixes().addAll(element.allValues("honorific-suffix")); 106 107 return property; 108 } 109 110 @Override 111 protected JCardValue _writeJson(StructuredName property) { 112 return JCardValue.structured(property.getFamily(), property.getGiven(), property.getAdditional(), property.getPrefixes(), property.getSuffixes()); 113 } 114 115 @Override 116 protected StructuredName _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, List<String> warnings) { 117 StructuredName property = new StructuredName(); 118 StructuredIterator it = structured(value); 119 120 property.setFamily(it.nextString()); 121 property.setGiven(it.nextString()); 122 property.getAdditional().addAll(it.nextComponent()); 123 property.getPrefixes().addAll(it.nextComponent()); 124 property.getSuffixes().addAll(it.nextComponent()); 125 126 return property; 127 } 128 }