ImapString.java revision 7e5ba0e1eaee76ab6e6c7ea9362348f660796596
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 17package com.android.email.mail.store.imap; 18 19import com.android.email.Email; 20 21import android.util.Log; 22 23import java.io.ByteArrayInputStream; 24import java.io.InputStream; 25import java.text.ParseException; 26import java.text.SimpleDateFormat; 27import java.util.Date; 28import 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 */ 37public 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 String getString() { 42 return ""; 43 } 44 45 @Override public InputStream getAsStream() { 46 return new ByteArrayInputStream(EMPTY_BYTES); 47 } 48 49 @Override public String toString() { 50 return ""; 51 } 52 }; 53 54 // This is used only for parsing IMAP's FETCH ENVELOPE command, in which 55 // en_US-like date format is used like "01-Jan-2009 11:20:39 -0800", so this should be 56 // handled by Locale.US 57 private final static SimpleDateFormat DATE_TIME_FORMAT = 58 new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss Z", Locale.US); 59 60 private boolean mIsInteger; 61 private int mParsedInteger; 62 private Date mParsedDate; 63 64 @Override 65 public final boolean isList() { 66 return false; 67 } 68 69 @Override 70 public final boolean isString() { 71 return true; 72 } 73 74 /** 75 * @return true if and only if the length of the string is larger than 0. 76 * 77 * Note: IMAP NIL is considered an empty string. See {@link ImapResponseParser 78 * #parseBareString}. 79 * On the other hand, a quoted/literal string with value NIL (i.e. "NIL" and {3}\r\nNIL) is 80 * treated literally. 81 */ 82 public final boolean isEmpty() { 83 return getString().length() == 0; 84 } 85 86 public abstract String getString(); 87 88 public abstract InputStream getAsStream(); 89 90 /** 91 * @return whether it can be parsed as a number. 92 */ 93 public final boolean isNumber() { 94 if (mIsInteger) { 95 return true; 96 } 97 try { 98 mParsedInteger = Integer.parseInt(getString()); 99 mIsInteger = true; 100 return true; 101 } catch (NumberFormatException e) { 102 return false; 103 } 104 } 105 106 /** 107 * @return value parsed as a number. 108 */ 109 public final int getNumberOrZero() { 110 if (!isNumber()) { 111 return 0; 112 } 113 return mParsedInteger; 114 } 115 116 /** 117 * @return whether it can be parsed as a date using {@link #DATE_TIME_FORMAT}. 118 */ 119 public final boolean isDate() { 120 if (mParsedDate != null) { 121 return true; 122 } 123 if (isEmpty()) { 124 return false; 125 } 126 try { 127 mParsedDate = DATE_TIME_FORMAT.parse(getString()); 128 return true; 129 } catch (ParseException e) { 130 Log.w(Email.LOG_TAG, getString() + " can't be parsed as a date."); 131 return false; 132 } 133 } 134 135 /** 136 * @return value it can be parsed as a {@link Date}, or null otherwise. 137 */ 138 public final Date getDateOrNull() { 139 if (!isDate()) { 140 return null; 141 } 142 return mParsedDate; 143 } 144 145 /** 146 * @return whether the value case-insensitively equals to {@code s}. 147 */ 148 public final boolean is(String s) { 149 if (s == null) { 150 return false; 151 } 152 return getString().equalsIgnoreCase(s); 153 } 154 155 156 /** 157 * @return whether the value case-insensitively starts with {@code s}. 158 */ 159 public final boolean startsWith(String prefix) { 160 if (prefix == null) { 161 return false; 162 } 163 final String me = this.getString(); 164 if (me.length() < prefix.length()) { 165 return false; 166 } 167 return me.substring(0, prefix.length()).equalsIgnoreCase(prefix); 168 } 169 170 // To force subclasses to implement it. 171 @Override 172 public abstract String toString(); 173 174 @Override 175 public final boolean equalsForTest(ImapElement that) { 176 if (!super.equalsForTest(that)) { 177 return false; 178 } 179 ImapString thatString = (ImapString) that; 180 return getString().equals(thatString.getString()); 181 } 182} 183