AddPossibleLength() 공개 메소드

public AddPossibleLength ( int value ) : Builder
value int
리턴 Builder
예제 #1
0
        /**
         * Processes a phone number description element from the XML file and returns it as a
         * PhoneNumberDesc. If the description element is a fixed line or mobile number, the parent
         * description will be used to fill in the whole element if necessary, or any components that are
         * missing. For all other types, the parent description will only be used to fill in missing
         * components if the type has a partial definition. For example, if no "tollFree" element exists,
         * we assume there are no toll free numbers for that locale, and return a phone number description
         * with "NA" for both the national and possible number patterns.
         *
         * @param generalDesc  a generic phone number description that will be used to fill in missing
         *                     parts of the description
         * @param countryElement  the XML element representing all the country information
         * @param numberType  the name of the number type, corresponding to the appropriate tag in the XML
         *                    file with information about that type
         * @return  complete description of that phone number type
         */
        public static PhoneNumberDesc.Builder ProcessPhoneNumberDescElement(PhoneNumberDesc parentDesc,
                                                                            XElement countryElement, string numberType)
        {
            if (parentDesc == null)
            {
                parentDesc = new PhoneNumberDesc.Builder().Build();
            }
            var phoneNumberDescList = countryElement.GetElementsByTagName(numberType).ToList();
            var numberDesc          = new PhoneNumberDesc.Builder();

            if (phoneNumberDescList.Count == 0)
            {
                // -1 will never match a possible phone number length, so is safe to use to ensure this never
                // matches. We don't leave it empty, since for compression reasons, we use the empty list to
                // mean that the generalDesc possible lengths apply.
                numberDesc.AddPossibleLength(-1);
                return(numberDesc);
            }
            if (phoneNumberDescList.Count > 0)
            {
                if (phoneNumberDescList.Count > 1)
                {
                    throw new Exception($"Multiple elements with type {numberType} found.");
                }
                var element = phoneNumberDescList[0];

                if (parentDesc != null)
                {
                    // New way of handling possible number lengths. We don't do this for the general
                    // description, since these tags won't be present; instead we will calculate its values
                    // based on the values for all the other number type descriptions (see
                    // setPossibleLengthsGeneralDesc).
                    var lengths          = new SortedSet <int>();
                    var localOnlyLengths = new SortedSet <int>();
                    PopulatePossibleLengthSets(element, lengths, localOnlyLengths);
                    SetPossibleLengths(lengths, new SortedSet <int>(), parentDesc, numberDesc);
                }

                var validPattern = element.GetElementsByTagName(NATIONAL_NUMBER_PATTERN).ToList();
                if (validPattern.Any())
                {
                    numberDesc.SetNationalNumberPattern(ValidateRE(validPattern.First().Value, true));
                }

                var exampleNumber = element.GetElementsByTagName(EXAMPLE_NUMBER).ToList();
                if (exampleNumber.Any())
                {
                    numberDesc.SetExampleNumber(exampleNumber.First().Value);
                }
            }
            return(numberDesc);
        }
        /**
         * Processes a phone number description element from the XML file and returns it as a
         * PhoneNumberDesc. If the description element is a fixed line or mobile number, the parent
         * description will be used to fill in the whole element if necessary, or any components that are
         * missing. For all other types, the parent description will only be used to fill in missing
         * components if the type has a partial definition. For example, if no "tollFree" element exists,
         * we assume there are no toll free numbers for that locale, and return a phone number description
         * with no national number data and [-1] for the possible lengths. Note that the parent
         * description must therefore already be processed before this method is called on any child
         * elements.
         *
         * @param generalDesc  a generic phone number description that will be used to fill in missing
         *                     parts of the description
         * @param countryElement  the XML element representing all the country information
         * @param numberType  the name of the number type, corresponding to the appropriate tag in the XML
         *                    file with information about that type
         * @return  complete description of that phone number type
         */
        public static PhoneNumberDesc.Builder ProcessPhoneNumberDescElement(PhoneNumberDesc parentDesc,
                                                                            XElement countryElement, string numberType)
        {
            var phoneNumberDescList = countryElement.Elements(numberType).ToList();
            var numberDesc          = new PhoneNumberDesc.Builder();

            if (phoneNumberDescList.Count == 0)
            {
                // -1 will never match a possible phone number length, so is safe to use to ensure this never
                // matches. We don't leave it empty, since for compression reasons, we use the empty list to
                // mean that the generalDesc possible lengths apply.
                numberDesc.AddPossibleLength(-1);
                return(numberDesc);
            }
            if (phoneNumberDescList.Count > 1)
            {
                throw new Exception($"Multiple elements with type {numberType} found.");
            }
            var element = phoneNumberDescList[0];

            parentDesc ??= new PhoneNumberDesc();
            var lengths          = new SortedSet <int>();
            var localOnlyLengths = new SortedSet <int>();

            PopulatePossibleLengthSets(element.Elements(POSSIBLE_LENGTHS), lengths, localOnlyLengths);
            SetPossibleLengths(lengths, localOnlyLengths, parentDesc, numberDesc);

            var validPattern = element.Element(NATIONAL_NUMBER_PATTERN);

            if (validPattern != null)
            {
                numberDesc.SetNationalNumberPattern(ValidateRE(validPattern.Value, true));
            }

            var exampleNumber = element.Element(EXAMPLE_NUMBER);

            if (exampleNumber != null)
            {
                numberDesc.SetExampleNumber(exampleNumber.Value);
            }

            return(numberDesc);
        }