SimpleIcsWriter.java revision bc0c8c1523fc0bc42eb82dba6c2977492e441c03
1/* Copyright 2010, The Android Open Source Project
2 **
3 ** Licensed under the Apache License, Version 2.0 (the "License");
4 ** you may not use this file except in compliance with the License.
5 ** You may obtain a copy of the License at
6 **
7 **     http://www.apache.org/licenses/LICENSE-2.0
8 **
9 ** Unless required by applicable law or agreed to in writing, software
10 ** distributed under the License is distributed on an "AS IS" BASIS,
11 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 ** See the License for the specific language governing permissions and
13 ** limitations under the License.
14 */
15
16package com.android.exchange.utility;
17
18import com.android.email.Utility;
19
20import android.text.TextUtils;
21
22import java.io.ByteArrayOutputStream;
23import java.io.IOException;
24
25/**
26 * Class to generate iCalender object (*.ics) per RFC 5545.
27 */
28public class SimpleIcsWriter {
29    private static final int MAX_LINE_LENGTH = 75; // In bytes, excluding CRLF
30    private static final int CHAR_MAX_BYTES_IN_UTF8 = 4;  // Used to be 6, but RFC3629 limited it.
31    private final ByteArrayOutputStream mOut = new ByteArrayOutputStream();
32
33    public SimpleIcsWriter() {
34    }
35
36    /**
37     * Low level method to write a line, performing line-folding if necessary.
38     */
39    /* package for testing */ void writeLine(String string) {
40        int numBytes = 0;
41        for (byte b : Utility.toUtf8(string)) {
42            // Fold it when necessary.
43            // To make it simple, we assume all chars are 4 bytes.
44            // If not (and usually it's not), we end up wrapping earlier than necessary, but that's
45            // completely fine.
46            if (numBytes > (MAX_LINE_LENGTH - CHAR_MAX_BYTES_IN_UTF8)
47                    && Utility.isFirstUtf8Byte(b)) { // Only wrappable if it's before the first byte
48                mOut.write((byte) '\r');
49                mOut.write((byte) '\n');
50                mOut.write((byte) '\t');
51                numBytes = 1; // for TAB
52            }
53            mOut.write(b);
54            numBytes++;
55        }
56        mOut.write((byte) '\r');
57        mOut.write((byte) '\n');
58    }
59
60    /**
61     * Write a tag with a value.
62     */
63    public void writeTag(String name, String value) {
64        // Belt and suspenders here; don't crash on null value.  Use something innocuous
65        if (TextUtils.isEmpty(value)) {
66            value = "0";
67        }
68
69        // The following properties take a TEXT value, which need to be escaped.
70        // (These property names should be all interned, so using equals() should be faster than
71        // using a hash table.)
72
73        // TODO make constants for these literals
74        if ("CALSCALE".equals(name)
75                || "METHOD".equals(name)
76                || "PRODID".equals(name)
77                || "VERSION".equals(name)
78                || "CATEGORIES".equals(name)
79                || "CLASS".equals(name)
80                || "COMMENT".equals(name)
81                || "DESCRIPTION".equals(name)
82                || "LOCATION".equals(name)
83                || "RESOURCES".equals(name)
84                || "STATUS".equals(name)
85                || "SUMMARY".equals(name)
86                || "TRANSP".equals(name)
87                || "TZID".equals(name)
88                || "TZNAME".equals(name)
89                || "CONTACT".equals(name)
90                || "RELATED-TO".equals(name)
91                || "UID".equals(name)
92                || "ACTION".equals(name)
93                || "REQUEST-STATUS".equals(name)
94                || "X-LIC-LOCATION".equals(name)
95                ) {
96            value = escapeTextValue(value);
97        }
98        writeLine(name + ":" + value);
99    }
100
101    /**
102     * @return the entire iCalendar invitation object.
103     */
104    public byte[] getBytes() {
105        try {
106            mOut.flush();
107        } catch (IOException wonthappen) {
108        }
109        return mOut.toByteArray();
110    }
111
112    /**
113     * Quote a param-value string, according to RFC 5545, section 3.1
114     */
115    public static String quoteParamValue(String paramValue) {
116        if (paramValue == null) {
117            return null;
118        }
119        // Wrap with double quotes.
120        // The spec doesn't allow putting double-quotes in a param value, so let's use single quotes
121        // as a substitute.
122        // It's not the smartest implementation.  e.g. we don't have to wrap an empty string with
123        // double quotes.  But it works.
124        return "\"" + paramValue.replace("\"", "'") + "\"";
125    }
126
127    /**
128     * Escape a TEXT value per RFC 5545 section 3.3.11
129     */
130    /* package for testing */ static String escapeTextValue(String s) {
131        StringBuilder sb = new StringBuilder(s.length());
132        for (int i = 0; i < s.length(); i++) {
133            char ch = s.charAt(i);
134            if (ch == '\n') {
135                sb.append("\\n");
136            } else if (ch == '\r') {
137                // Remove CR
138            } else if (ch == ',' || ch == ';' || ch == '\\') {
139                sb.append('\\');
140                sb.append(ch);
141            } else {
142                sb.append(ch);
143            }
144        }
145        return sb.toString();
146    }
147}
148