001package biweekly.io.scribe.property;
002
003import java.util.Arrays;
004import java.util.List;
005
006import biweekly.ICalDataType;
007import biweekly.ICalVersion;
008import biweekly.property.AudioAlarm;
009import biweekly.util.org.apache.commons.codec.binary.Base64;
010
011/*
012 Copyright (c) 2013-2015, Michael Angstadt
013 All rights reserved.
014
015 Redistribution and use in source and binary forms, with or without
016 modification, are permitted provided that the following conditions are met: 
017
018 1. Redistributions of source code must retain the above copyright notice, this
019 list of conditions and the following disclaimer. 
020 2. Redistributions in binary form must reproduce the above copyright notice,
021 this list of conditions and the following disclaimer in the documentation
022 and/or other materials provided with the distribution. 
023
024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
025 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
028 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
031 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034 */
035
036/**
037 * Marshals {@link AudioAlarm} properties.
038 * @author Michael Angstadt
039 */
040public class AudioAlarmScribe extends VCalAlarmPropertyScribe<AudioAlarm> {
041        public AudioAlarmScribe() {
042                super(AudioAlarm.class, "AALARM");
043        }
044
045        @Override
046        protected ICalDataType _dataType(AudioAlarm property, ICalVersion version) {
047                if (property.getUri() != null) {
048                        return ICalDataType.URL;
049                }
050                if (property.getData() != null) {
051                        return ICalDataType.BINARY;
052                }
053                if (property.getContentId() != null) {
054                        return ICalDataType.CONTENT_ID;
055                }
056                return null;
057        }
058
059        @Override
060        protected List<String> writeData(AudioAlarm property) {
061                String uri = property.getUri();
062                if (uri != null) {
063                        return Arrays.asList(uri);
064                }
065
066                byte data[] = property.getData();
067                if (data != null) {
068                        String base64Str = Base64.encodeBase64String(data);
069                        return Arrays.asList(base64Str);
070                }
071
072                String contentId = property.getContentId();
073                if (contentId != null) {
074                        return Arrays.asList(contentId);
075                }
076
077                return Arrays.asList();
078        }
079
080        @Override
081        protected AudioAlarm create(ICalDataType dataType, SemiStructuredIterator it) {
082                AudioAlarm aalarm = new AudioAlarm();
083                String next = it.next();
084                if (next == null) {
085                        return aalarm;
086                }
087
088                if (dataType == ICalDataType.BINARY) {
089                        byte[] data = Base64.decodeBase64(next);
090                        aalarm.setData(data);
091                } else if (dataType == ICalDataType.URL) {
092                        aalarm.setUri(next);
093                } else if (dataType == ICalDataType.CONTENT_ID) {
094                        aalarm.setContentId(next);
095                } else {
096                        aalarm.setUri(next);
097                }
098
099                return aalarm;
100        }
101}