1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.email.mail.store.imap; 18 19 import com.android.emailcommon.Logging; 20 21 import android.util.Log; 22 23 import java.io.ByteArrayInputStream; 24 import java.io.InputStream; 25 import java.text.ParseException; 26 import java.text.SimpleDateFormat; 27 import java.util.Date; 28 import java.util.Locale; 29 30 /** 31 * Class represents an IMAP "element" that is not a list. 32 * 33 * An atom, quoted string, literal, are all represented by this. Values like OK, STATUS are too. 34 * Also, this class class may contain more arbitrary value like "BODY[HEADER.FIELDS ("DATE")]". 35 * See {@link ImapResponseParser}. 36 */ 37 public abstract class ImapString extends ImapElement { 38 private static final byte[] EMPTY_BYTES = new byte[0]; 39 40 public static final ImapString EMPTY = new ImapString() { 41 @Override public void destroy() { 42 // Don't call super.destroy(). 43 // It's a shared object. We don't want the mDestroyed to be set on this. 44 } 45 46 @Override public String getString() { 47 return ""; 48 } 49 50 @Override public InputStream getAsStream() { 51 return new ByteArrayInputStream(EMPTY_BYTES); 52 } 53 54 @Override public String toString() { 55 return ""; 56 } 57 }; 58 59 // This is used only for parsing IMAP's FETCH ENVELOPE command, in which 60 // en_US-like date format is used like "01-Jan-2009 11:20:39 -0800", so this should be 61 // handled by Locale.US 62 private final static SimpleDateFormat DATE_TIME_FORMAT = 63 new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss Z", Locale.US); 64 65 private boolean mIsInteger; 66 private int mParsedInteger; 67 private Date mParsedDate; 68 69 @Override isList()70 public final boolean isList() { 71 return false; 72 } 73 74 @Override isString()75 public final boolean isString() { 76 return true; 77 } 78 79 /** 80 * @return true if and only if the length of the string is larger than 0. 81 * 82 * Note: IMAP NIL is considered an empty string. See {@link ImapResponseParser 83 * #parseBareString}. 84 * On the other hand, a quoted/literal string with value NIL (i.e. "NIL" and {3}\r\nNIL) is 85 * treated literally. 86 */ isEmpty()87 public final boolean isEmpty() { 88 return getString().length() == 0; 89 } 90 getString()91 public abstract String getString(); 92 getAsStream()93 public abstract InputStream getAsStream(); 94 95 /** 96 * @return whether it can be parsed as a number. 97 */ isNumber()98 public final boolean isNumber() { 99 if (mIsInteger) { 100 return true; 101 } 102 try { 103 mParsedInteger = Integer.parseInt(getString()); 104 mIsInteger = true; 105 return true; 106 } catch (NumberFormatException e) { 107 return false; 108 } 109 } 110 111 /** 112 * @return value parsed as a number. 113 */ getNumberOrZero()114 public final int getNumberOrZero() { 115 if (!isNumber()) { 116 return 0; 117 } 118 return mParsedInteger; 119 } 120 121 /** 122 * @return whether it can be parsed as a date using {@link #DATE_TIME_FORMAT}. 123 */ isDate()124 public final boolean isDate() { 125 if (mParsedDate != null) { 126 return true; 127 } 128 if (isEmpty()) { 129 return false; 130 } 131 try { 132 mParsedDate = DATE_TIME_FORMAT.parse(getString()); 133 return true; 134 } catch (ParseException e) { 135 Log.w(Logging.LOG_TAG, getString() + " can't be parsed as a date."); 136 return false; 137 } 138 } 139 140 /** 141 * @return value it can be parsed as a {@link Date}, or null otherwise. 142 */ getDateOrNull()143 public final Date getDateOrNull() { 144 if (!isDate()) { 145 return null; 146 } 147 return mParsedDate; 148 } 149 150 /** 151 * @return whether the value case-insensitively equals to {@code s}. 152 */ is(String s)153 public final boolean is(String s) { 154 if (s == null) { 155 return false; 156 } 157 return getString().equalsIgnoreCase(s); 158 } 159 160 161 /** 162 * @return whether the value case-insensitively starts with {@code s}. 163 */ startsWith(String prefix)164 public final boolean startsWith(String prefix) { 165 if (prefix == null) { 166 return false; 167 } 168 final String me = this.getString(); 169 if (me.length() < prefix.length()) { 170 return false; 171 } 172 return me.substring(0, prefix.length()).equalsIgnoreCase(prefix); 173 } 174 175 // To force subclasses to implement it. 176 @Override toString()177 public abstract String toString(); 178 179 @Override equalsForTest(ImapElement that)180 public final boolean equalsForTest(ImapElement that) { 181 if (!super.equalsForTest(that)) { 182 return false; 183 } 184 ImapString thatString = (ImapString) that; 185 return getString().equals(thatString.getString()); 186 } 187 } 188