- // Sinks for additional levels under /fields/*/relative/ and /fields/*/relativeTime/
-
- /**
- * Make list of simplePatternFmtList, for past and for future.
- * Set a SimpleFormatter for the <style, relative unit, plurality>
- *
- * Fill in values for the particular plural given, e.g., ONE, FEW, OTHER, etc.
- */
- struct RelDateTimeDetailSink : public ResourceTableSink {
- RelDateTimeDetailSink(RelDateTimeFmtDataSink &sink) : outer(sink) {}
- ~RelDateTimeDetailSink();
-
- virtual void put(const char *key, const ResourceValue &value,
- UErrorCode &errorCode) {
- if (U_FAILURE(errorCode)) { return; }
-
- outer.relUnitIndex = relUnitFromGeneric(outer.genericUnit);
- if (outer.relUnitIndex < 0) {
- return;
- }
-
- /* Make two lists of simplePatternFmtList, one for past and one for future.
- * Set a SimpleFormatter pattern for the <style, relative unit, plurality>
- *
- * Fill in values for the particular plural given, e.g., ONE, FEW, OTHER, etc.
- */
- int32_t pluralIndex = StandardPlural::indexOrNegativeFromString(key);
- if (pluralIndex >= 0) {
- SimpleFormatter **patterns =
- outer.outputData.relativeUnitsFormatters[outer.style][outer.relUnitIndex]
- [outer.pastFutureIndex];
- // Only set if not already established.
- if (patterns[pluralIndex] == NULL) {
- patterns[pluralIndex] = new SimpleFormatter(
- value.getUnicodeString(errorCode), 0, 1, errorCode);
- if (patterns[pluralIndex] == NULL) {
- errorCode = U_MEMORY_ALLOCATION_ERROR;
- }
- }
- }
- }
-
- RelDateTimeFmtDataSink &outer;
- } relDateTimeDetailSink;
-
- /*
- * Handles "relativeTime" entries, e.g., under "day", "hour", "minute",
- * "minute-short", etc.
- */
- struct RelativeTimeSink : public ResourceTableSink {
- RelativeTimeSink(RelDateTimeFmtDataSink &sink) : outer(sink) {}
- ~RelativeTimeSink();
-
- virtual ResourceTableSink *getOrCreateTableSink(
- const char *key, int32_t /* initialSize */, UErrorCode& errorCode) {
- if (U_FAILURE(errorCode)) { return NULL; }
- outer.relUnitIndex = relUnitFromGeneric(outer.genericUnit);
- if (outer.relUnitIndex < 0) {
- return NULL;
- }
-
- if (uprv_strcmp(key, "past") == 0) {
- outer.pastFutureIndex = 0;
- } else if (uprv_strcmp(key, "future") == 0) {
- outer.pastFutureIndex = 1;
- } else {
- // Unknown key.
- return NULL;
- }
- return &outer.relDateTimeDetailSink;
- }
-
- RelDateTimeFmtDataSink &outer;
- } relativeTimeSink;
-
- /*
- * Handles "relative" entries, e.g., under "day", "day-short", "fri",
- * "fri-narrow", "fri-short", etc.
- */
- struct RelativeSink : public ResourceTableSink {
- RelativeSink(RelDateTimeFmtDataSink &sink) : outer(sink) {}
- ~RelativeSink();
-
- virtual void put(const char *key, const ResourceValue &value, UErrorCode &errorCode) {
- if (U_FAILURE(errorCode)) { return; }
- int32_t direction = keyToDirection(key);
- if (direction < 0) {
- return;
- }
-
- int32_t relUnitIndex = relUnitFromGeneric(outer.genericUnit);
- if (relUnitIndex == UDAT_RELATIVE_SECONDS &&
- direction == UDAT_DIRECTION_THIS &&
- outer.outputData.absoluteUnits[outer.style][UDAT_ABSOLUTE_NOW]
- [UDAT_DIRECTION_PLAIN].isEmpty()) {
- // Handle "NOW"
- outer.outputData.absoluteUnits[outer.style][UDAT_ABSOLUTE_NOW]
- [UDAT_DIRECTION_PLAIN].fastCopyFrom(value.getUnicodeString(errorCode));
- }
-
- int32_t absUnitIndex = absUnitFromGeneric(outer.genericUnit);
- if (absUnitIndex < 0) {
- return;
- }
- // Only reset if slot is empty.
- if (outer.outputData.absoluteUnits[outer.style][absUnitIndex][direction].isEmpty()) {
- outer.outputData.absoluteUnits[outer.style][absUnitIndex]
- [direction].fastCopyFrom(value.getUnicodeString(errorCode));
- }
- }
-
- RelDateTimeFmtDataSink &outer;
- } relativeSink;
-
- /*
- * Handles entries under "fields", recognizing "relative" and "relativeTime" entries.
- */
- struct UnitSink : public ResourceTableSink {
- UnitSink(RelDateTimeFmtDataSink &sink) : outer(sink) {}
- ~UnitSink();
-
- virtual void put(const char *key, const ResourceValue &value, UErrorCode &errorCode) {
- if (U_FAILURE(errorCode)) { return; }
- if (uprv_strcmp(key, "dn") != 0) {
- return;
- }
-
- // Handle Display Name for PLAIN direction for some units.
- int32_t absUnit = absUnitFromGeneric(outer.genericUnit);
- if (absUnit < 0) {
- return; // Not interesting.
- }
-
- // TODO(Travis Keep): This is a hack to get around CLDR bug 6818.
- UnicodeString displayName = value.getUnicodeString(errorCode);
- if (U_SUCCESS(errorCode)) {
- if (uprv_strcmp("en", outer.sinkLocaleId) == 0) {
- displayName.toLower();
- }
- }
- // end hack
-
- // Store displayname if not set.
- if (outer.outputData.absoluteUnits[outer.style]
- [absUnit][UDAT_DIRECTION_PLAIN].isEmpty()) {
- outer.outputData.absoluteUnits[outer.style]
- [absUnit][UDAT_DIRECTION_PLAIN].fastCopyFrom(displayName);
- return;
- }
- }
-
- virtual ResourceTableSink *getOrCreateTableSink(
- const char *key, int32_t /* initialSize */, UErrorCode &errorCode) {
- if (U_FAILURE(errorCode)) { return NULL; }
- if (uprv_strcmp(key, "relative") == 0) {
- return &outer.relativeSink;
- } else if (uprv_strcmp(key, "relativeTime") == 0) {
- return &outer.relativeTimeSink;
- }
- return NULL;
- }
-
- RelDateTimeFmtDataSink &outer;
- } unitSink;
-
- // For hack for locale "en".
- // TODO(Travis Keep): This is a hack to get around CLDR bug 6818.
- const char* sinkLocaleId;
-