mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-13 17:01:16 +00:00
ICU-3487 support running under java 1.4
X-SVN-Rev: 22418
This commit is contained in:
parent
a0088597b1
commit
6de3ab2b51
2 changed files with 57 additions and 24 deletions
|
@ -165,4 +165,25 @@ public class ICUDurationTest extends TestFmwk {
|
|||
}
|
||||
}
|
||||
// END JDK>1.5
|
||||
|
||||
|
||||
|
||||
public void TestBadObjectError() {
|
||||
Runtime r = Runtime.getRuntime();
|
||||
DurationFormat df = DurationFormat.getInstance(new ULocale("en"));
|
||||
String output = null;
|
||||
try {
|
||||
output = df.format(r);
|
||||
errln("FAIL: did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
logln("PASS: expected: Caught iae: " + iae.toString() );
|
||||
}
|
||||
// try a second time, because it is a different code path for java < 1.5
|
||||
try {
|
||||
output = df.format(r);
|
||||
errln("FAIL: [#2] did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
logln("PASS: [#2] expected: Caught iae: " + iae.toString() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,10 @@ public class BasicDurationFormat extends DurationFormat {
|
|||
return new BasicDurationFormat(locale);
|
||||
}
|
||||
|
||||
// BEGIN JDK>1.5
|
||||
private static boolean checkXMLDuration = true;
|
||||
// END JDK>1.5
|
||||
|
||||
public StringBuffer format(Object object, StringBuffer toAppend, FieldPosition pos) {
|
||||
if(object instanceof Long) {
|
||||
String res = formatDurationFromNow(((Long)object).longValue());
|
||||
|
@ -39,14 +43,21 @@ public class BasicDurationFormat extends DurationFormat {
|
|||
} else if(object instanceof Date) {
|
||||
String res = formatDurationFromNowTo(((Date)object));
|
||||
return toAppend.append(res);
|
||||
// BEGIN JDK>1.5
|
||||
} else if(object instanceof javax.xml.datatype.Duration) {
|
||||
String res = formatDuration(object);
|
||||
return toAppend.append(res);
|
||||
// END JDK>1.5
|
||||
} else {
|
||||
throw new IllegalArgumentException("Cannot format given Object as a Duration");
|
||||
}
|
||||
// BEGIN JDK>1.5
|
||||
if(checkXMLDuration) try {
|
||||
if(object instanceof javax.xml.datatype.Duration) {
|
||||
String res = formatDuration(object);
|
||||
return toAppend.append(res);
|
||||
}
|
||||
} catch ( NoClassDefFoundError ncdfe ) {
|
||||
System.err.println("Skipping XML capability");
|
||||
checkXMLDuration = false; // don't try again
|
||||
}
|
||||
// END JDK>1.5
|
||||
|
||||
throw new IllegalArgumentException("Cannot format given Object as a Duration");
|
||||
|
||||
}
|
||||
|
||||
public BasicDurationFormat() {
|
||||
|
@ -86,22 +97,6 @@ public class BasicDurationFormat extends DurationFormat {
|
|||
}
|
||||
|
||||
// BEGIN JDK>1.5
|
||||
private static final javax.xml.datatype.DatatypeConstants.Field inFields[] = {
|
||||
javax.xml.datatype.DatatypeConstants.YEARS,
|
||||
javax.xml.datatype.DatatypeConstants.MONTHS,
|
||||
javax.xml.datatype.DatatypeConstants.DAYS,
|
||||
javax.xml.datatype.DatatypeConstants.HOURS,
|
||||
javax.xml.datatype.DatatypeConstants.MINUTES,
|
||||
javax.xml.datatype.DatatypeConstants.SECONDS,
|
||||
};
|
||||
private static final TimeUnit outFields[] = {
|
||||
TimeUnit.YEAR,
|
||||
TimeUnit.MONTH,
|
||||
TimeUnit.DAY,
|
||||
TimeUnit.HOUR,
|
||||
TimeUnit.MINUTE,
|
||||
TimeUnit.SECOND,
|
||||
};
|
||||
/**
|
||||
* JDK 1.5+ only
|
||||
* @param o
|
||||
|
@ -109,7 +104,24 @@ public class BasicDurationFormat extends DurationFormat {
|
|||
* @see http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/datatype/Duration.html
|
||||
*/
|
||||
public String formatDuration(Object obj) {
|
||||
javax.xml.datatype.Duration inDuration = (javax.xml.datatype.Duration)obj;
|
||||
javax.xml.datatype.DatatypeConstants.Field inFields[] = {
|
||||
javax.xml.datatype.DatatypeConstants.YEARS,
|
||||
javax.xml.datatype.DatatypeConstants.MONTHS,
|
||||
javax.xml.datatype.DatatypeConstants.DAYS,
|
||||
javax.xml.datatype.DatatypeConstants.HOURS,
|
||||
javax.xml.datatype.DatatypeConstants.MINUTES,
|
||||
javax.xml.datatype.DatatypeConstants.SECONDS,
|
||||
};
|
||||
TimeUnit outFields[] = {
|
||||
TimeUnit.YEAR,
|
||||
TimeUnit.MONTH,
|
||||
TimeUnit.DAY,
|
||||
TimeUnit.HOUR,
|
||||
TimeUnit.MINUTE,
|
||||
TimeUnit.SECOND,
|
||||
};
|
||||
|
||||
javax.xml.datatype.Duration inDuration = (javax.xml.datatype.Duration)obj;
|
||||
Period p = null;
|
||||
javax.xml.datatype.Duration duration = inDuration;
|
||||
boolean inPast = false;
|
||||
|
|
Loading…
Add table
Reference in a new issue