Esempio n. 1
0
        /**
         * Extracts the pattern for the national format.
         *
         * @throws  RuntimeException if multiple or no formats have been encountered.
         */
        // @VisibleForTesting
        internal static void loadNationalFormat(PhoneMetadata.Builder metadata, XmlElement numberFormatXmlElement,
                                                NumberFormat.Builder format)
        {
            setLeadingDigitsPatterns(numberFormatXmlElement, format);
            format.setPattern(validateRE(numberFormatXmlElement.GetAttribute(PATTERN)));

            var formatPattern     = numberFormatXmlElement.GetElementsByTagName(FORMAT);
            int numFormatPatterns = formatPattern.Count;

            if (numFormatPatterns != 1)
            {
//      LOGGER.log(Level.SEVERE, "One format pattern for a numberFormat element should be defined.");
                String countryId = metadata.getId().Length > 0 ?
                                   metadata.getId() : metadata.getCountryCode().ToString();
                throw new Exception("Invalid number of format patterns (" + numFormatPatterns +
                                    ") for country: " + countryId);
            }
            format.setFormat(formatPattern.Item(0).FirstChild.Value);
        }
Esempio n. 2
0
        /**
         * Extracts the pattern for international format. If there is no intlFormat, default to using the
         * national format. If the intlFormat is set to "NA" the intlFormat should be ignored.
         *
         * @throws  RuntimeException if multiple intlFormats have been encountered.
         * @return  whether an international number format is defined.
         */
        // @VisibleForTesting
        internal static bool loadInternationalFormat(PhoneMetadata.Builder metadata,
                                                     XmlElement numberFormatXmlElement,
                                                     NumberFormat nationalFormat)
        {
            NumberFormat.Builder intlFormat  = NumberFormat.newBuilder();
            var intlFormatPattern            = numberFormatXmlElement.GetElementsByTagName(INTL_FORMAT);
            var hasExplicitIntlFormatDefined = false;

            if (intlFormatPattern.Count > 1)
            {
//      LOGGER.log(Level.SEVERE,
//                 "A maximum of one intlFormat pattern for a numberFormat element should be " +
//                 "defined.");
                String countryId = metadata.getId().Length > 0 ?
                                   metadata.getId() : metadata.getCountryCode().ToString();
                throw new Exception("Invalid number of intlFormat patterns for country: " + countryId);
            }
            else if (intlFormatPattern.Count == 0)
            {
                // Default to use the same as the national pattern if none is defined.
                intlFormat.mergeFrom(nationalFormat);
            }
            else
            {
                intlFormat.setPattern(numberFormatXmlElement.GetAttribute(PATTERN));
                setLeadingDigitsPatterns(numberFormatXmlElement, intlFormat);
                String intlFormatPatternValue = intlFormatPattern.Item(0).FirstChild.Value;
                if (!intlFormatPatternValue.Equals("NA"))
                {
                    intlFormat.setFormat(intlFormatPatternValue);
                }
                hasExplicitIntlFormatDefined = true;
            }

            if (intlFormat.HasFormat())
            {
                metadata.addIntlNumberFormat(intlFormat);
            }
            return(hasExplicitIntlFormatDefined);
        }