001package biweekly.io.scribe.property;
002
003import java.util.ArrayList;
004import java.util.EnumSet;
005import java.util.List;
006import java.util.Set;
007
008import biweekly.ICalDataType;
009import biweekly.ICalVersion;
010import biweekly.io.CannotParseException;
011import biweekly.io.ParseContext;
012import biweekly.io.WriteContext;
013import biweekly.parameter.ICalParameters;
014import biweekly.property.Daylight;
015import biweekly.util.ICalDate;
016import biweekly.util.UtcOffset;
017
018/*
019 Copyright (c) 2013-2015, Michael Angstadt
020 All rights reserved.
021
022 Redistribution and use in source and binary forms, with or without
023 modification, are permitted provided that the following conditions are met: 
024
025 1. Redistributions of source code must retain the above copyright notice, this
026 list of conditions and the following disclaimer. 
027 2. Redistributions in binary form must reproduce the above copyright notice,
028 this list of conditions and the following disclaimer in the documentation
029 and/or other materials provided with the distribution. 
030
031 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
032 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
033 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
034 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
035 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
036 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
037 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
038 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
039 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
040 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
041 */
042
043/**
044 * Marshals {@link Daylight} properties.
045 * @author Michael Angstadt
046 */
047public class DaylightScribe extends ICalPropertyScribe<Daylight> {
048        public DaylightScribe() {
049                super(Daylight.class, "DAYLIGHT");
050        }
051
052        @Override
053        protected String _writeText(Daylight property, WriteContext context) {
054                if (!property.isDaylight()) {
055                        return "FALSE";
056                }
057
058                List<String> values = new ArrayList<String>();
059                values.add("TRUE");
060
061                UtcOffset offset = property.getOffset();
062                values.add((offset == null) ? "" : offset.toString());
063
064                ICalDate start = property.getStart();
065                values.add((start == null || start.getRawComponents() == null) ? "" : start.getRawComponents().toString(true, false));
066
067                ICalDate end = property.getEnd();
068                values.add((end == null || end.getRawComponents() == null) ? "" : end.getRawComponents().toString(true, false));
069
070                String standardName = property.getStandardName();
071                values.add((standardName == null) ? "" : standardName);
072
073                String daylightName = property.getDaylightName();
074                values.add((daylightName == null) ? "" : daylightName);
075
076                return structured(values.toArray());
077        }
078
079        @Override
080        protected Daylight _parseText(String value, ICalDataType dataType, ICalParameters parameters, ParseContext context) {
081                StructuredIterator it = structured(value);
082
083                String next = it.nextString();
084                boolean flag = (next == null) ? false : Boolean.parseBoolean(next);
085
086                UtcOffset offset = null;
087                next = it.nextString();
088                if (next != null) {
089                        try {
090                                offset = UtcOffset.parse(next);
091                        } catch (IllegalArgumentException e) {
092                                throw new CannotParseException(33, next);
093                        }
094                }
095
096                ICalDate start = null;
097                next = it.nextString();
098                if (next != null) {
099                        try {
100                                start = date(next).parse();
101                        } catch (IllegalArgumentException e) {
102                                throw new CannotParseException(34, next);
103                        }
104                }
105
106                ICalDate end = null;
107                next = it.nextString();
108                if (next != null) {
109                        try {
110                                end = date(next).parse();
111                        } catch (IllegalArgumentException e) {
112                                throw new CannotParseException(35, next);
113                        }
114                }
115
116                String standardName = it.nextString();
117                String daylightName = it.nextString();
118
119                return new Daylight(flag, offset, start, end, standardName, daylightName);
120        }
121
122        @Override
123        public Set<ICalVersion> getSupportedVersions() {
124                return EnumSet.of(ICalVersion.V1_0);
125        }
126}