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