001 package biweekly.property.marshaller; 002 003 import java.util.List; 004 005 import biweekly.ICalDataType; 006 import biweekly.io.json.JCalValue; 007 import biweekly.io.xml.XCalElement; 008 import biweekly.parameter.Encoding; 009 import biweekly.parameter.ICalParameters; 010 import biweekly.property.Attachment; 011 import biweekly.util.Base64; 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 Attachment} properties. 040 * @author Michael Angstadt 041 */ 042 public class AttachmentMarshaller extends ICalPropertyMarshaller<Attachment> { 043 public AttachmentMarshaller() { 044 super(Attachment.class, "ATTACH", ICalDataType.URI); 045 } 046 047 @Override 048 protected void _prepareParameters(Attachment property, ICalParameters copy) { 049 if (property.getUri() != null) { 050 copy.setEncoding(null); 051 } else if (property.getData() != null) { 052 copy.setEncoding(Encoding.BASE64); 053 } 054 } 055 056 @Override 057 protected ICalDataType _dataType(Attachment property) { 058 if (property.getUri() != null) { 059 return ICalDataType.URI; 060 } 061 if (property.getData() != null) { 062 return ICalDataType.BINARY; 063 } 064 return defaultDataType; 065 } 066 067 @Override 068 protected String _writeText(Attachment property) { 069 String uri = property.getUri(); 070 if (uri != null) { 071 return uri; 072 } 073 074 byte data[] = property.getData(); 075 if (data != null) { 076 return Base64.encode(data); 077 } 078 079 return ""; 080 } 081 082 @Override 083 protected Attachment _parseText(String value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) { 084 value = unescape(value); 085 086 if (dataType == ICalDataType.BINARY || parameters.getEncoding() == Encoding.BASE64) { 087 return new Attachment(null, Base64.decode(value)); 088 } 089 return new Attachment(null, value); 090 } 091 092 @Override 093 protected void _writeXml(Attachment property, XCalElement element) { 094 String uri = property.getUri(); 095 if (uri != null) { 096 element.append(ICalDataType.URI, uri); 097 return; 098 } 099 100 byte data[] = property.getData(); 101 if (data != null) { 102 element.append(ICalDataType.BINARY, Base64.encode(data)); 103 return; 104 } 105 106 element.append(defaultDataType, ""); 107 } 108 109 @Override 110 protected Attachment _parseXml(XCalElement element, ICalParameters parameters, List<String> warnings) { 111 String uri = element.first(ICalDataType.URI); 112 if (uri != null) { 113 return new Attachment(null, uri); 114 } 115 116 String base64Data = element.first(ICalDataType.BINARY); 117 if (base64Data != null) { 118 return new Attachment(null, Base64.decode(base64Data)); //formatType will be set when the parameters are assigned to the property object 119 } 120 121 throw missingXmlElements(ICalDataType.URI, ICalDataType.BINARY); 122 } 123 124 @Override 125 protected JCalValue _writeJson(Attachment property) { 126 String uri = property.getUri(); 127 if (uri != null) { 128 return JCalValue.single(uri); 129 } 130 131 byte data[] = property.getData(); 132 if (data != null) { 133 return JCalValue.single(Base64.encode(data)); 134 } 135 136 return JCalValue.single(""); 137 } 138 139 @Override 140 protected Attachment _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) { 141 String valueStr = value.asSingle(); 142 143 if (dataType == ICalDataType.BINARY) { 144 return new Attachment(null, Base64.decode(valueStr)); 145 } 146 return new Attachment(null, valueStr); 147 } 148 }