001package biweekly.io.scribe.property;
002
003import java.util.EnumSet;
004import java.util.Set;
005
006import biweekly.ICalDataType;
007import biweekly.ICalVersion;
008import biweekly.io.ParseContext;
009import biweekly.io.WriteContext;
010import biweekly.io.json.JCalValue;
011import biweekly.io.xml.XCalElement;
012import biweekly.parameter.ICalParameters;
013import biweekly.property.RequestStatus;
014
015/*
016 Copyright (c) 2013-2015, Michael Angstadt
017 All rights reserved.
018
019 Redistribution and use in source and binary forms, with or without
020 modification, are permitted provided that the following conditions are met: 
021
022 1. Redistributions of source code must retain the above copyright notice, this
023 list of conditions and the following disclaimer. 
024 2. Redistributions in binary form must reproduce the above copyright notice,
025 this list of conditions and the following disclaimer in the documentation
026 and/or other materials provided with the distribution. 
027
028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
029 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
030 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
032 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
034 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
037 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038 */
039
040/**
041 * Marshals {@link RequestStatus} properties.
042 * @author Michael Angstadt
043 */
044public class RequestStatusScribe extends ICalPropertyScribe<RequestStatus> {
045        public RequestStatusScribe() {
046                super(RequestStatus.class, "REQUEST-STATUS", ICalDataType.TEXT);
047        }
048
049        @Override
050        protected String _writeText(RequestStatus property, WriteContext context) {
051                return structured(property.getStatusCode(), property.getDescription(), property.getExceptionText());
052        }
053
054        @Override
055        protected RequestStatus _parseText(String value, ICalDataType dataType, ICalParameters parameters, ParseContext context) {
056                SemiStructuredIterator it = semistructured(value, true);
057
058                RequestStatus requestStatus = new RequestStatus(it.next());
059                requestStatus.setDescription(it.next());
060                requestStatus.setExceptionText(it.next());
061                return requestStatus;
062        }
063
064        @Override
065        protected void _writeXml(RequestStatus property, XCalElement element, WriteContext context) {
066                String code = property.getStatusCode();
067                element.append("code", code);
068
069                String description = property.getDescription();
070                element.append("description", description);
071
072                String data = property.getExceptionText();
073                if (data != null) {
074                        element.append("data", data);
075                }
076        }
077
078        @Override
079        protected RequestStatus _parseXml(XCalElement element, ICalParameters parameters, ParseContext context) {
080                String code = element.first("code");
081                if (code == null) {
082                        throw missingXmlElements("code");
083                }
084
085                RequestStatus requestStatus = new RequestStatus(s(code));
086                requestStatus.setDescription(s(element.first("description"))); //optional field
087                requestStatus.setExceptionText(s(element.first("data"))); //optional field
088                return requestStatus;
089        }
090
091        @Override
092        protected JCalValue _writeJson(RequestStatus property, WriteContext context) {
093                return JCalValue.structured(property.getStatusCode(), property.getDescription(), property.getExceptionText());
094        }
095
096        @Override
097        protected RequestStatus _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, ParseContext context) {
098                StructuredIterator it = structured(value);
099
100                RequestStatus requestStatus = new RequestStatus(it.nextString());
101                requestStatus.setDescription(it.nextString());
102                requestStatus.setExceptionText(it.nextString());
103                return requestStatus;
104        }
105
106        private String s(String str) {
107                return (str == null || str.length() == 0) ? null : str;
108        }
109
110        @Override
111        public Set<ICalVersion> getSupportedVersions() {
112                return EnumSet.of(ICalVersion.V2_0_DEPRECATED, ICalVersion.V2_0);
113        }
114}