use of java.text.FieldPosition in project robovm by robovm.
the class Support_Format method t_FormatWithField.
protected void t_FormatWithField(int count, Format format, Object object, String text, Format.Field field, int begin, int end) {
StringBuffer buffer = new StringBuffer();
FieldPosition pos = new FieldPosition(field);
format.format(object, buffer, pos);
if (text == null) {
assertEquals("Test " + count + ": incorrect formatted text", this.text, buffer.toString());
} else {
assertEquals("Test " + count + ": incorrect formatted text", text, buffer.toString());
}
assertEquals("Test " + count + ": incorrect begin index for field " + field, begin, pos.getBeginIndex());
assertEquals("Test " + count + ": incorrect end index for field" + field, end, pos.getEndIndex());
}
use of java.text.FieldPosition in project baseio by generallycloud.
the class CookieUtil method appendCookieValue.
// -------------------- Cookie parsing tools
// TODO RFC2965 fields also need to be passed
public static void appendCookieValue(StringBuilder headerBuf, int version, String name, String value, String path, String domain, String comment, int maxAge, boolean isSecure) {
StringBuffer buf = new StringBuffer();
// Servlet implementation checks name
buf.append(name);
buf.append("=");
// Servlet implementation does not check anything else
version = maybeQuote2(version, buf, value, true);
// Add version 1 specific information
if (version == 1) {
// Version=1 ... required
buf.append("; Version=1");
// Comment=comment
if (comment != null) {
buf.append("; Comment=");
maybeQuote2(version, buf, comment);
}
}
// Add domain information, if present
if (domain != null) {
buf.append("; Domain=");
maybeQuote2(version, buf, domain);
}
// TODO RFC2965 Discard
if (maxAge >= 0) {
if (version > 0) {
buf.append("; Max-Age=");
buf.append(maxAge);
}
// They do understand Expires, even with V1 cookies!
if (version == 0 || ALWAYS_ADD_EXPIRES) {
// Wdy, DD-Mon-YY HH:MM:SS GMT ( Expires Netscape format )
buf.append("; Expires=");
// To expire immediately we need to set the time in past
if (maxAge == 0) {
buf.append(ancientDate);
} else {
OLD_COOKIE_PATTERN_FORMAT.format(new Date(System.currentTimeMillis() + maxAge * 1000L), buf, new FieldPosition(0));
}
}
}
// Path=path
if (path != null) {
buf.append("; Path=");
if (version == 0) {
maybeQuote2(version, buf, path);
} else {
maybeQuote2(version, buf, path, CookieUtil.tspecials2NoSlash, false);
}
}
// Secure
if (isSecure) {
buf.append("; Secure");
}
headerBuf.append(buf);
}
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();
}
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
}
}
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
}
}
Aggregations