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