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.ICalParameters;
009    import biweekly.property.RequestStatus;
010    
011    /*
012     Copyright (c) 2013, 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 RequestStatus} properties.
038     * @author Michael Angstadt
039     */
040    public class RequestStatusMarshaller extends ICalPropertyMarshaller<RequestStatus> {
041            public RequestStatusMarshaller() {
042                    super(RequestStatus.class, "REQUEST-STATUS", ICalDataType.TEXT);
043            }
044    
045            @Override
046            protected String _writeText(RequestStatus property) {
047                    return structured(property.getStatusCode(), property.getDescription(), property.getExceptionText());
048            }
049    
050            @Override
051            protected RequestStatus _parseText(String value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) {
052                    SemiStructuredIterator it = semistructured(value);
053    
054                    RequestStatus requestStatus = new RequestStatus(it.next());
055                    requestStatus.setDescription(it.next());
056                    requestStatus.setExceptionText(it.next());
057                    return requestStatus;
058            }
059    
060            @Override
061            protected void _writeXml(RequestStatus property, XCalElement element) {
062                    String code = property.getStatusCode();
063                    element.append("code", code);
064    
065                    String description = property.getDescription();
066                    element.append("description", description);
067    
068                    String data = property.getExceptionText();
069                    if (data != null) {
070                            element.append("data", data);
071                    }
072            }
073    
074            @Override
075            protected RequestStatus _parseXml(XCalElement element, ICalParameters parameters, List<String> warnings) {
076                    String code = element.first("code");
077                    if (code == null) {
078                            throw missingXmlElements("code");
079                    }
080    
081                    RequestStatus requestStatus = new RequestStatus(s(code));
082                    requestStatus.setDescription(s(element.first("description"))); //optional field
083                    requestStatus.setExceptionText(s(element.first("data"))); //optional field
084                    return requestStatus;
085            }
086    
087            @Override
088            protected JCalValue _writeJson(RequestStatus property) {
089                    return JCalValue.structured(property.getStatusCode(), property.getDescription(), property.getExceptionText());
090            }
091    
092            @Override
093            protected RequestStatus _parseJson(JCalValue value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) {
094                    StructuredIterator it = structured(value);
095    
096                    RequestStatus requestStatus = new RequestStatus(it.nextString());
097                    requestStatus.setDescription(it.nextString());
098                    requestStatus.setExceptionText(it.nextString());
099                    return requestStatus;
100            }
101    
102            private String s(String str) {
103                    return (str == null || str.length() == 0) ? null : str;
104            }
105    }