?? maildateformat.java
字號:
/** * Helper class to deal with parsing the characters */class MailDateParser { int index = 0; char[] orig = null; public MailDateParser(char[] orig) { this.orig = orig; } /** * skips chars until it finds a number (0-9) * * if it does not find a number, it will throw * an ArrayIndexOutOfBoundsException */ public void skipUntilNumber() throws ParseException { try { while (true) { switch ( orig[index] ) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': return; default: index++; break; } } } catch (ArrayIndexOutOfBoundsException e) { throw new ParseException("No Number Found", index); } } /** * skips any number of tabs, spaces, CR, and LF - folding whitespace */ public void skipWhiteSpace() { int len = orig.length; while (index < len) { switch (orig[index]) { case ' ': // space case '\t': // tab case '\r': // CR case '\n': // LF index++; break; default: return; } } } /** * used to look at the next character without "parsing" that * character. */ public int peekChar() throws ParseException { if (index < orig.length) return orig[index]; else throw new ParseException("No more characters", index); } /** * skips the given character. if the current char does not * match a ParseException will be thrown */ public void skipChar(char c) throws ParseException { if (index < orig.length) { if (orig[index] == c) { index++; } else { throw new ParseException("Wrong char", index); } } else { throw new ParseException("No more characters", index); } } /** * will only skip the current char if it matches the given * char */ public boolean skipIfChar(char c) throws ParseException { if (index < orig.length) { if (orig[index] == c) { index++; return true; } else { return false; } } else { throw new ParseException("No more characters", index); } } /** * current char must point to a number. the number will be * parsed and the resulting number will be returned. if a * number is not found, a ParseException will be thrown */ public int parseNumber() throws ParseException { int length = orig.length; boolean gotNum = false; int result = 0; while (index < length) { switch( orig[index] ) { case '0': result *= 10; gotNum = true; break; case '1': result = result * 10 + 1; gotNum = true; break; case '2': result = result * 10 + 2; gotNum = true; break; case '3': result = result * 10 + 3; gotNum = true; break; case '4': result = result * 10 + 4; gotNum = true; break; case '5': result = result * 10 + 5; gotNum = true; break; case '6': result = result * 10 + 6; gotNum = true; break; case '7': result = result * 10 + 7; gotNum = true; break; case '8': result = result * 10 + 8; gotNum = true; break; case '9': result = result * 10 + 9; gotNum = true; break; default: if (gotNum) return result; else throw new ParseException("No Number found", index); } index++; } // check the result if (gotNum) return result; // else, throw a parse error throw new ParseException("No Number found", index); } /** * will look for one of "Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dev" * and return the numerical version of the month. (0-11). a ParseException * error is thrown if a month cannot be found. */ public int parseMonth() throws ParseException { char curr; try { switch(orig[index++]) { case 'J': case 'j': // "Jan" (0) / "Jun" (5) / "Jul" (6) // check next char switch(orig[index++]) { case 'A': case 'a': curr = orig[index++]; if (curr == 'N' || curr == 'n') { return 0; } break; case 'U': case 'u': curr = orig[index++]; if (curr == 'N' || curr == 'n') { return 5; } else if (curr == 'L' || curr == 'l') { return 6; } break; } break; case 'F': case 'f': // "Feb" curr = orig[index++]; if (curr == 'E' || curr == 'e') { curr = orig[index++]; if (curr == 'B' || curr == 'b') { return 1; } } break; case 'M': case 'm': // "Mar" (2) / "May" (4) curr = orig[index++]; if (curr == 'A' || curr == 'a') { curr = orig[index++]; if (curr == 'R' || curr == 'r') { return 2; } else if (curr == 'Y' || curr == 'y') { return 4; } } break; case 'A': case 'a': // "Apr" (3) / "Aug" (7) curr = orig[index++]; if (curr == 'P' || curr == 'p') { curr = orig[index++]; if (curr == 'R' || curr == 'r') { return 3; } } else if (curr == 'U' || curr == 'u') { curr = orig[index++]; if (curr == 'G' || curr == 'g') { return 7; } } break; case 'S': case 's': // "Sep" (8) curr = orig[index++]; if (curr == 'E' || curr == 'e') { curr = orig[index++]; if (curr == 'P' || curr == 'p') { return 8; } } break; case 'O': case 'o': // "Oct" curr = orig[index++]; if (curr == 'C' || curr == 'c') { curr = orig[index++]; if (curr == 'T' || curr == 't') { return 9; } } break; case 'N': case 'n': // "Nov" curr = orig[index++]; if (curr == 'O' || curr == 'o') { curr = orig[index++]; if (curr == 'V' || curr == 'v') { return 10; } } break; case 'D': case 'd': // "Dec" curr = orig[index++]; if (curr == 'E' || curr == 'e') { curr = orig[index++]; if (curr == 'C' || curr == 'c') { return 11; } } break; } } catch (ArrayIndexOutOfBoundsException e) { } throw new ParseException("Bad Month", index); } /** * will parse the timezone - either Numerical version (e.g. +0800, -0500) * or the alpha version (e.g. PDT, PST). the result will be returned in * minutes needed to be added to the date to bring it to GMT. */ public int parseTimeZone() throws ParseException { if (index >= orig.length) throw new ParseException("No more characters", index); char test = orig[index]; if ( test == '+' || test == '-' ) { return parseNumericTimeZone(); } else { return parseAlphaTimeZone(); } } /** * will parse the Numerical time zone version (e.g. +0800, -0500) * the result will be returned in minutes needed to be added * to the date to bring it to GMT. */ public int parseNumericTimeZone() throws ParseException { // we switch the sign if it is a '+' // since the time in the string we are // parsing is off from GMT by that amount. // and we want to get the time back into // GMT, so we substract it. boolean switchSign = false; char first = orig[index++]; if (first == '+') { switchSign = true; } else if (first != '-') { throw new ParseException("Bad Numeric TimeZone", index); } int tz = parseNumber(); int offset = (tz / 100) * 60 + (tz % 100); if (switchSign) { return -offset; } else { return offset; } } /** * will parse the alpha time zone version (e.g. PDT, PST). * the result will be returned in minutes needed to be added * to the date to bring it to GMT. */ public int parseAlphaTimeZone() throws ParseException { int result = 0; boolean foundCommon = false; char curr; try { switch(orig[index++]) { case 'U': case 'u': // "UT" / Universal Time curr = orig[index++]; if (curr == 'T' || curr == 't') { result = 0; break; } throw new ParseException("Bad Alpha TimeZone", index); case 'G': case 'g': // "GMT" ; Universal Time curr = orig[index++]; if (curr == 'M' || curr == 'm') { curr = orig[index++]; if (curr == 'T' || curr == 't') { result = 0; break; } } throw new ParseException("Bad Alpha TimeZone", index); case 'E': case 'e': // "EST" / "EDT" ; Eastern: - 5/ - 4 result = 300; foundCommon = true; break; case 'C': case 'c': // "CST" / "CDT" ; Central: - 6/ - 5 result = 360; foundCommon = true; break; case 'M': case 'm': // "MST" / "MDT" ; Mountain: - 7/ - 6 result = 420; foundCommon = true; break; case 'P': case 'p': // "PST" / "PDT" ; Pacific: - 8/ - 7 result = 480; foundCommon = true; break; default: throw new ParseException("Bad Alpha TimeZone", index); } } catch (ArrayIndexOutOfBoundsException e) { throw new ParseException("Bad Alpha TimeZone", index); } if (foundCommon) { curr = orig[index++]; if (curr == 'S' || curr == 's') { curr = orig[index++]; if (curr != 'T' && curr != 't') { throw new ParseException("Bad Alpha TimeZone", index); } } else if (curr == 'D' || curr == 'd') { curr = orig[index++]; if (curr == 'T' || curr != 't') { // for daylight time result -= 60; } else { throw new ParseException("Bad Alpha TimeZone", index); } } } return result; } int getIndex() { return index; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -