+
+static const int32_t PATH_PREFIX_LENGTH = 17;
+static const UChar PATH_PREFIX[] = {SOLIDUS, CAP_L, CAP_O, CAP_C, CAP_A, CAP_L, CAP_E, SOLIDUS,
+ LOW_C, LOW_A, LOW_L, LOW_E, LOW_N, LOW_D, LOW_A, LOW_R, SOLIDUS};
+static const int32_t PATH_SUFFIX_LENGTH = 16;
+static const UChar PATH_SUFFIX[] = {SOLIDUS, LOW_I, LOW_N, LOW_T, LOW_E, LOW_R, LOW_V, LOW_A,
+ LOW_L, CAP_F, LOW_O, LOW_R, LOW_M, LOW_A, LOW_T, LOW_S};
+
+/**
+ * Sink for enumerating all of the date interval skeletons.
+ */
+struct DateIntervalInfo::DateIntervalSink : public ResourceSink {
+
+ // Output data
+ DateIntervalInfo &dateIntervalInfo;
+
+ // Next calendar type
+ UnicodeString nextCalendarType;
+
+ DateIntervalSink(DateIntervalInfo &diInfo, const char *currentCalendarType)
+ : dateIntervalInfo(diInfo), nextCalendarType(currentCalendarType, -1, US_INV) { }
+ virtual ~DateIntervalSink();
+
+ virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &errorCode) {
+ if (U_FAILURE(errorCode)) { return; }
+
+ // Iterate over all the calendar entries and only pick the 'intervalFormats' table.
+ ResourceTable dateIntervalData = value.getTable(errorCode);
+ if (U_FAILURE(errorCode)) { return; }
+ for (int32_t i = 0; dateIntervalData.getKeyAndValue(i, key, value); i++) {
+ if (uprv_strcmp(key, gIntervalDateTimePatternTag) != 0) {
+ continue;
+ }
+
+ // Handle aliases and tables. Ignore the rest.
+ if (value.getType() == URES_ALIAS) {
+ // Get the calendar type for the alias path.
+ const UnicodeString &aliasPath = value.getAliasUnicodeString(errorCode);
+ if (U_FAILURE(errorCode)) { return; }
+
+ nextCalendarType.remove();
+ getCalendarTypeFromPath(aliasPath, nextCalendarType, errorCode);
+
+ if (U_FAILURE(errorCode)) {
+ resetNextCalendarType();
+ }
+ break;
+
+ } else if (value.getType() == URES_TABLE) {
+ // Iterate over all the skeletons in the 'intervalFormat' table.
+ ResourceTable skeletonData = value.getTable(errorCode);
+ if (U_FAILURE(errorCode)) { return; }
+ for (int32_t j = 0; skeletonData.getKeyAndValue(j, key, value); j++) {
+ if (value.getType() == URES_TABLE) {
+ // Process the skeleton
+ processSkeletonTable(key, value, errorCode);
+ if (U_FAILURE(errorCode)) { return; }
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ /**
+ * Processes the patterns for a skeleton table
+ */
+ void processSkeletonTable(const char *key, ResourceValue &value, UErrorCode &errorCode) {
+ if (U_FAILURE(errorCode)) { return; }
+
+ // Iterate over all the patterns in the current skeleton table
+ const char *currentSkeleton = key;
+ ResourceTable patternData = value.getTable(errorCode);
+ if (U_FAILURE(errorCode)) { return; }
+ for (int32_t k = 0; patternData.getKeyAndValue(k, key, value); k++) {
+ if (value.getType() == URES_STRING) {
+ // Process the key
+ UCalendarDateFields calendarField = validateAndProcessPatternLetter(key);
+
+ // If the calendar field has a valid value
+ if (calendarField < UCAL_FIELD_COUNT) {
+ // Set the interval pattern
+ setIntervalPatternIfAbsent(currentSkeleton, calendarField, value, errorCode);
+ if (U_FAILURE(errorCode)) { return; }
+ }
+ }
+ }
+ }
+
+ /**
+ * Extracts the calendar type from the path.
+ */
+ static void getCalendarTypeFromPath(const UnicodeString &path, UnicodeString &calendarType,
+ UErrorCode &errorCode) {
+ if (U_FAILURE(errorCode)) { return; }
+
+ if (!path.startsWith(PATH_PREFIX, PATH_PREFIX_LENGTH) || !path.endsWith(PATH_SUFFIX, PATH_SUFFIX_LENGTH)) {
+ errorCode = U_INVALID_FORMAT_ERROR;
+ return;
+ }
+
+ path.extractBetween(PATH_PREFIX_LENGTH, path.length() - PATH_SUFFIX_LENGTH, calendarType);
+ }
+
+ /**
+ * Validates and processes the pattern letter
+ */
+ UCalendarDateFields validateAndProcessPatternLetter(const char *patternLetter) {
+ // Check that patternLetter is just one letter
+ char c0;
+ if ((c0 = patternLetter[0]) != 0 && patternLetter[1] == 0) {
+ // Check that the pattern letter is accepted
+ if (c0 == 'G') {
+ return UCAL_ERA;
+ } else if (c0 == 'y') {
+ return UCAL_YEAR;
+ } else if (c0 == 'M') {
+ return UCAL_MONTH;
+ } else if (c0 == 'd') {
+ return UCAL_DATE;
+ } else if (c0 == 'a') {
+ return UCAL_AM_PM;
+ } else if (c0 == 'h' || c0 == 'H') {
+ return UCAL_HOUR;
+ } else if (c0 == 'm') {
+ return UCAL_MINUTE;
+ }// TODO(ticket:12190): Why icu4c doesn't accept the calendar field "s" but icu4j does?
+ }
+ return UCAL_FIELD_COUNT;
+ }
+
+ /**
+ * Stores the interval pattern for the current skeleton in the internal data structure
+ * if it's not present.
+ */
+ void setIntervalPatternIfAbsent(const char *currentSkeleton, UCalendarDateFields lrgDiffCalUnit,
+ const ResourceValue &value, UErrorCode &errorCode) {
+ // Check if the pattern has already been stored on the data structure
+ IntervalPatternIndex index =
+ dateIntervalInfo.calendarFieldToIntervalIndex(lrgDiffCalUnit, errorCode);
+ if (U_FAILURE(errorCode)) { return; }
+
+ UnicodeString skeleton(currentSkeleton, -1, US_INV);
+ UnicodeString* patternsOfOneSkeleton =
+ (UnicodeString*)(dateIntervalInfo.fIntervalPatterns->get(skeleton));
+
+ if (patternsOfOneSkeleton == NULL || patternsOfOneSkeleton[index].isEmpty()) {
+ UnicodeString pattern = value.getUnicodeString(errorCode);
+ dateIntervalInfo.setIntervalPatternInternally(skeleton, lrgDiffCalUnit,
+ pattern, errorCode);
+ }
+ }
+
+ const UnicodeString &getNextCalendarType() {
+ return nextCalendarType;
+ }
+
+ void resetNextCalendarType() {
+ nextCalendarType.setToBogus();
+ }
+};
+
+// Virtual destructors must be defined out of line.
+DateIntervalInfo::DateIntervalSink::~DateIntervalSink() {}
+
+
+
+void
+DateIntervalInfo::initializeData(const Locale& locale, UErrorCode& status)