Search in sources :

Example 46 with FieldPosition

use of java.text.FieldPosition in project tomcat by apache.

the class Rfc6265CookieProcessor method generateHeader.

@Override
public String generateHeader(javax.servlet.http.Cookie cookie) {
    // Can't use StringBuilder due to DateFormat
    StringBuffer header = new StringBuffer();
    // TODO: Name validation takes place in Cookie and cannot be configured
    //       per Context. Moving it to here would allow per Context config
    //       but delay validation until the header is generated. However,
    //       the spec requires an IllegalArgumentException on Cookie
    //       generation.
    header.append(cookie.getName());
    header.append('=');
    String value = cookie.getValue();
    if (value != null && value.length() > 0) {
        validateCookieValue(value);
        header.append(value);
    }
    // RFC 6265 prefers Max-Age to Expires but... (see below)
    int maxAge = cookie.getMaxAge();
    if (maxAge > -1) {
        // Negative Max-Age is equivalent to no Max-Age
        header.append("; Max-Age=");
        header.append(maxAge);
        // Microsoft IE and Microsoft Edge don't understand Max-Age so send
        // expires as well. Without this, persistent cookies fail with those
        // browsers. See http://tomcat.markmail.org/thread/g6sipbofsjossacn
        // Wdy, DD-Mon-YY HH:MM:SS GMT ( Expires Netscape format )
        header.append("; Expires=");
        // To expire immediately we need to set the time in past
        if (maxAge == 0) {
            header.append(ANCIENT_DATE);
        } else {
            COOKIE_DATE_FORMAT.get().format(new Date(System.currentTimeMillis() + maxAge * 1000L), header, new FieldPosition(0));
        }
    }
    String domain = cookie.getDomain();
    if (domain != null && domain.length() > 0) {
        validateDomain(domain);
        header.append("; Domain=");
        header.append(domain);
    }
    String path = cookie.getPath();
    if (path != null && path.length() > 0) {
        validatePath(path);
        header.append("; Path=");
        header.append(path);
    }
    if (cookie.getSecure()) {
        header.append("; Secure");
    }
    if (cookie.isHttpOnly()) {
        header.append("; HttpOnly");
    }
    return header.toString();
}
Also used : FieldPosition(java.text.FieldPosition) Date(java.util.Date)

Example 47 with FieldPosition

use of java.text.FieldPosition in project j2objc by google.

the class DecimalFormatTest method test_formatDouble_roundingUnnecessaryArithmeticException.

public void test_formatDouble_roundingUnnecessaryArithmeticException() {
    DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
    decimalFormat.setMaximumFractionDigits(0);
    decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);
    try {
        // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY,
        // throw ArithmeticException
        decimalFormat.format(11.5, new StringBuffer(), new FieldPosition(0));
        fail("ArithmeticException expected");
    } catch (ArithmeticException e) {
    // expected
    }
}
Also used : DecimalFormat(java.text.DecimalFormat) FieldPosition(java.text.FieldPosition)

Example 48 with FieldPosition

use of java.text.FieldPosition in project j2objc by google.

the class MessageFormatTest method test_ConstructorLjava_lang_String.

public void test_ConstructorLjava_lang_String() {
    MessageFormat format = new MessageFormat("abc {4,time} def {3,date} ghi {2,number} jkl {1,choice,0#low|1#high} mnop {0}");
    assertTrue("Not a MessageFormat", format.getClass() == MessageFormat.class);
    Format[] formats = format.getFormats();
    assertNotNull("null formats", formats);
    assertTrue("Wrong format count: " + formats.length, formats.length >= 5);
    assertTrue("Wrong time format", formats[0].equals(DateFormat.getTimeInstance()));
    assertTrue("Wrong date format", formats[1].equals(DateFormat.getDateInstance()));
    assertTrue("Wrong number format", formats[2].equals(NumberFormat.getInstance()));
    assertTrue("Wrong choice format", formats[3].equals(new ChoiceFormat("0.0#low|1.0#high")));
    assertNull("Wrong string format", formats[4]);
    Date date = new Date();
    FieldPosition pos = new FieldPosition(-1);
    StringBuffer buffer = new StringBuffer();
    format.format(new Object[] { "123", new Double(1.6), new Double(7.2), date, date }, buffer, pos);
    String result = buffer.toString();
    buffer.setLength(0);
    buffer.append("abc ");
    buffer.append(DateFormat.getTimeInstance().format(date));
    buffer.append(" def ");
    buffer.append(DateFormat.getDateInstance().format(date));
    buffer.append(" ghi ");
    buffer.append(NumberFormat.getInstance().format(new Double(7.2)));
    buffer.append(" jkl high mnop 123");
    assertTrue("Wrong answer:\n" + result + "\n" + buffer, result.equals(buffer.toString()));
    assertEquals("Simple string", "Test message", new MessageFormat("Test message").format(new Object[0]));
    result = new MessageFormat("Don't").format(new Object[0]);
    assertTrue("Should not throw IllegalArgumentException: " + result, "Dont".equals(result));
    try {
        new MessageFormat("Invalid {1,foobar} format descriptor!");
        fail("Expected test_ConstructorLjava_lang_String to throw IAE.");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    try {
        new MessageFormat("Invalid {1,date,invalid-spec} format descriptor!");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    checkSerialization(new MessageFormat(""));
    checkSerialization(new MessageFormat("noargs"));
    checkSerialization(new MessageFormat("{0}"));
    checkSerialization(new MessageFormat("a{0}"));
    checkSerialization(new MessageFormat("{0}b"));
    checkSerialization(new MessageFormat("a{0}b"));
    // Regression for HARMONY-65
    try {
        new MessageFormat("{0,number,integer");
        fail("Assert 0: Failed to detect unmatched brackets.");
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : Format(java.text.Format) DecimalFormat(java.text.DecimalFormat) SimpleDateFormat(java.text.SimpleDateFormat) NumberFormat(java.text.NumberFormat) MessageFormat(java.text.MessageFormat) ChoiceFormat(java.text.ChoiceFormat) DateFormat(java.text.DateFormat) MessageFormat(java.text.MessageFormat) ChoiceFormat(java.text.ChoiceFormat) FieldPosition(java.text.FieldPosition) Date(java.util.Date)

Example 49 with FieldPosition

use of java.text.FieldPosition in project j2objc by google.

the class SimpleDateFormatTest method test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition.

public void test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition() {
    // Test for method java.lang.StringBuffer
    // java.text.SimpleDateFormat.format(java.util.Date,
    // java.lang.StringBuffer, java.text.FieldPosition)
    new Support_SimpleDateFormat("test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition").t_format_with_FieldPosition();
    SimpleDateFormat format = new SimpleDateFormat("", Locale.ENGLISH);
    Calendar cal = new GregorianCalendar(1999, Calendar.JUNE, 2, 15, 3, 6);
    assertFormat(format, " G", cal, " AD", DateFormat.ERA_FIELD);
    assertFormat(format, " GG", cal, " AD", DateFormat.ERA_FIELD);
    assertFormat(format, " GGG", cal, " AD", DateFormat.ERA_FIELD);
    assertFormat(format, " G", new GregorianCalendar(-1999, Calendar.JUNE, 2), " BC", DateFormat.ERA_FIELD);
    // This assumes Unicode behavior where 'y' and 'yyy' don't truncate,
    // which means that it will fail on the RI.
    assertFormat(format, " y", cal, " 1999", DateFormat.YEAR_FIELD);
    assertFormat(format, " yy", cal, " 99", DateFormat.YEAR_FIELD);
    assertFormat(format, " yy", new GregorianCalendar(2001, Calendar.JUNE, 2), " 01", DateFormat.YEAR_FIELD);
    assertFormat(format, " yy", new GregorianCalendar(2000, Calendar.JUNE, 2), " 00", DateFormat.YEAR_FIELD);
    assertFormat(format, " yyy", new GregorianCalendar(2000, Calendar.JUNE, 2), " 2000", DateFormat.YEAR_FIELD);
    assertFormat(format, " yyy", cal, " 1999", DateFormat.YEAR_FIELD);
    assertFormat(format, " yyyy", cal, " 1999", DateFormat.YEAR_FIELD);
    assertFormat(format, " yyyyy", cal, " 01999", DateFormat.YEAR_FIELD);
    assertFormat(format, " M", cal, " 6", DateFormat.MONTH_FIELD);
    assertFormat(format, " M", new GregorianCalendar(1999, Calendar.NOVEMBER, 2), " 11", DateFormat.MONTH_FIELD);
    assertFormat(format, " MM", cal, " 06", DateFormat.MONTH_FIELD);
    assertFormat(format, " MMM", cal, " Jun", DateFormat.MONTH_FIELD);
    assertFormat(format, " MMMM", cal, " June", DateFormat.MONTH_FIELD);
    assertFormat(format, " MMMMM", cal, " J", DateFormat.MONTH_FIELD);
    assertFormat(format, " d", cal, " 2", DateFormat.DATE_FIELD);
    assertFormat(format, " d", new GregorianCalendar(1999, Calendar.NOVEMBER, 12), " 12", DateFormat.DATE_FIELD);
    assertFormat(format, " dd", cal, " 02", DateFormat.DATE_FIELD);
    assertFormat(format, " dddd", cal, " 0002", DateFormat.DATE_FIELD);
    assertFormat(format, " h", cal, " 3", DateFormat.HOUR1_FIELD);
    assertFormat(format, " h", new GregorianCalendar(1999, Calendar.NOVEMBER, 12), " 12", DateFormat.HOUR1_FIELD);
    assertFormat(format, " hh", cal, " 03", DateFormat.HOUR1_FIELD);
    assertFormat(format, " hhhh", cal, " 0003", DateFormat.HOUR1_FIELD);
    assertFormat(format, " H", cal, " 15", DateFormat.HOUR_OF_DAY0_FIELD);
    assertFormat(format, " H", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 4, 0), " 4", DateFormat.HOUR_OF_DAY0_FIELD);
    assertFormat(format, " H", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 12, 0), " 12", DateFormat.HOUR_OF_DAY0_FIELD);
    assertFormat(format, " H", new GregorianCalendar(1999, Calendar.NOVEMBER, 12), " 0", DateFormat.HOUR_OF_DAY0_FIELD);
    assertFormat(format, " HH", cal, " 15", DateFormat.HOUR_OF_DAY0_FIELD);
    assertFormat(format, " HHHH", cal, " 0015", DateFormat.HOUR_OF_DAY0_FIELD);
    assertFormat(format, " m", cal, " 3", DateFormat.MINUTE_FIELD);
    assertFormat(format, " m", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 4, 47), " 47", DateFormat.MINUTE_FIELD);
    assertFormat(format, " mm", cal, " 03", DateFormat.MINUTE_FIELD);
    assertFormat(format, " mmmm", cal, " 0003", DateFormat.MINUTE_FIELD);
    assertFormat(format, " s", cal, " 6", DateFormat.SECOND_FIELD);
    assertFormat(format, " s", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 4, 47, 13), " 13", DateFormat.SECOND_FIELD);
    assertFormat(format, " ss", cal, " 06", DateFormat.SECOND_FIELD);
    assertFormat(format, " ssss", cal, " 0006", DateFormat.SECOND_FIELD);
    assertFormat(format, " S", cal, " 0", DateFormat.MILLISECOND_FIELD);
    Calendar temp = new GregorianCalendar();
    temp.set(Calendar.MILLISECOND, 961);
    assertFormat(format, " SS", temp, " 96", DateFormat.MILLISECOND_FIELD);
    assertFormat(format, " SSSS", cal, " 0000", DateFormat.MILLISECOND_FIELD);
    assertFormat(format, " SS", cal, " 00", DateFormat.MILLISECOND_FIELD);
    assertFormat(format, " E", cal, " Wed", DateFormat.DAY_OF_WEEK_FIELD);
    assertFormat(format, " EE", cal, " Wed", DateFormat.DAY_OF_WEEK_FIELD);
    assertFormat(format, " EEE", cal, " Wed", DateFormat.DAY_OF_WEEK_FIELD);
    assertFormat(format, " EEEE", cal, " Wednesday", DateFormat.DAY_OF_WEEK_FIELD);
    assertFormat(format, " EEEEE", cal, " W", DateFormat.DAY_OF_WEEK_FIELD);
    assertFormat(format, " D", cal, " 153", DateFormat.DAY_OF_YEAR_FIELD);
    assertFormat(format, " DD", cal, " 153", DateFormat.DAY_OF_YEAR_FIELD);
    assertFormat(format, " DDDD", cal, " 0153", DateFormat.DAY_OF_YEAR_FIELD);
    assertFormat(format, " F", cal, " 1", DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD);
    assertFormat(format, " F", new GregorianCalendar(1999, Calendar.NOVEMBER, 14), " 2", DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD);
    assertFormat(format, " FF", cal, " 01", DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD);
    assertFormat(format, " FFFF", cal, " 0001", DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD);
    cal.setMinimalDaysInFirstWeek(1);
    cal.setFirstDayOfWeek(1);
    assertFormat(format, " w", cal, " 23", DateFormat.WEEK_OF_YEAR_FIELD);
    assertFormat(format, " ww", cal, " 23", DateFormat.WEEK_OF_YEAR_FIELD);
    assertFormat(format, " wwww", cal, " 0023", DateFormat.WEEK_OF_YEAR_FIELD);
    assertFormat(format, " W", cal, " 1", DateFormat.WEEK_OF_MONTH_FIELD);
    assertFormat(format, " WW", cal, " 01", DateFormat.WEEK_OF_MONTH_FIELD);
    assertFormat(format, " WWWW", cal, " 0001", DateFormat.WEEK_OF_MONTH_FIELD);
    assertFormat(format, " a", cal, " PM", DateFormat.AM_PM_FIELD);
    assertFormat(format, " a", new GregorianCalendar(1999, Calendar.NOVEMBER, 14), " AM", DateFormat.AM_PM_FIELD);
    assertFormat(format, " a", new GregorianCalendar(1999, Calendar.NOVEMBER, 14, 12, 0), " PM", DateFormat.AM_PM_FIELD);
    assertFormat(format, " aa", cal, " PM", DateFormat.AM_PM_FIELD);
    assertFormat(format, " aaa", cal, " PM", DateFormat.AM_PM_FIELD);
    assertFormat(format, " aaaa", cal, " PM", DateFormat.AM_PM_FIELD);
    assertFormat(format, " aaaaa", cal, " PM", DateFormat.AM_PM_FIELD);
    assertFormat(format, " k", cal, " 15", DateFormat.HOUR_OF_DAY1_FIELD);
    assertFormat(format, " k", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 4, 0), " 4", DateFormat.HOUR_OF_DAY1_FIELD);
    assertFormat(format, " k", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 12, 0), " 12", DateFormat.HOUR_OF_DAY1_FIELD);
    assertFormat(format, " k", new GregorianCalendar(1999, Calendar.NOVEMBER, 12), " 24", DateFormat.HOUR_OF_DAY1_FIELD);
    assertFormat(format, " kk", cal, " 15", DateFormat.HOUR_OF_DAY1_FIELD);
    assertFormat(format, " kkkk", cal, " 0015", DateFormat.HOUR_OF_DAY1_FIELD);
    assertFormat(format, " K", cal, " 3", DateFormat.HOUR0_FIELD);
    assertFormat(format, " K", new GregorianCalendar(1999, Calendar.NOVEMBER, 12), " 0", DateFormat.HOUR0_FIELD);
    assertFormat(format, " KK", cal, " 03", DateFormat.HOUR0_FIELD);
    assertFormat(format, " KKKK", cal, " 0003", DateFormat.HOUR0_FIELD);
    format.applyPattern("'Mkz''':.@5");
    assertEquals("Wrong output", "Mkz':.@5", format.format(new Date()));
    // Test invalid args to format.
    SimpleDateFormat dateFormat = new SimpleDateFormat();
    try {
        dateFormat.format(null, new StringBuffer(), new FieldPosition(1));
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) FieldPosition(java.text.FieldPosition) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 50 with FieldPosition

use of java.text.FieldPosition in project j2objc by google.

the class DecimalFormatTest method test_format_roundingUnnecessaryArithmeticException.

public void test_format_roundingUnnecessaryArithmeticException() {
    final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
    DecimalFormat decimalFormat = new DecimalFormat("00.0#E0", dfs);
    decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);
    try {
        // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY,
        // throw ArithmeticException
        decimalFormat.format(99999, new StringBuffer(), new FieldPosition(0));
        fail("ArithmeticException expected");
    } catch (ArithmeticException e) {
    // expected
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) FieldPosition(java.text.FieldPosition)

Aggregations

FieldPosition (java.text.FieldPosition)100 Date (java.util.Date)31 SimpleDateFormat (java.text.SimpleDateFormat)28 DecimalFormat (java.text.DecimalFormat)12 NumberFormat (java.text.NumberFormat)12 ParsePosition (java.text.ParsePosition)12 IsoDateFormat (alma.acs.util.IsoDateFormat)9 Test (org.junit.Test)9 ILogEntry (com.cosylab.logging.engine.log.ILogEntry)8 Format (java.text.Format)8 MessageFormat (java.text.MessageFormat)8 DateFormat (java.text.DateFormat)6 ACSLogParser (alma.acs.logging.engine.parser.ACSLogParser)5 IOException (java.io.IOException)3 ParseException (java.text.ParseException)3 Calendar (java.util.Calendar)3 GregorianCalendar (java.util.GregorianCalendar)3 Vector (java.util.Vector)3 ExtendedDateFormat (org.jkiss.utils.time.ExtendedDateFormat)3 LogTypeHelper (com.cosylab.logging.engine.log.LogTypeHelper)2