// i.e. compile did not fail with a PatternSyntaxException.
return regex;
}
private static PhoneMetadata loadCountryMetadata(String regionCode, Element element) {
PhoneMetadata metadata = new PhoneMetadata();
metadata.setId(regionCode);
metadata.setCountryCode(Integer.parseInt(element.getAttribute("countryCode")));
if (element.hasAttribute("leadingDigits")) {
metadata.setLeadingDigits(validateRE(element.getAttribute("leadingDigits")));
}
metadata.setInternationalPrefix(validateRE(element.getAttribute("internationalPrefix")));
if (element.hasAttribute("preferredInternationalPrefix")) {
String preferredInternationalPrefix = element.getAttribute("preferredInternationalPrefix");
metadata.setPreferredInternationalPrefix(preferredInternationalPrefix);
}
String nationalPrefix = "";
String nationalPrefixFormattingRule = "";
String carrierCodeFormattingRule = "";
if (element.hasAttribute("nationalPrefix")) {
nationalPrefix = element.getAttribute("nationalPrefix");
metadata.setNationalPrefix(nationalPrefix);
nationalPrefixFormattingRule =
validateRE(getNationalPrefixFormattingRuleFromElement(element, nationalPrefix));
if (element.hasAttribute("nationalPrefixForParsing")) {
metadata.setNationalPrefixForParsing(
validateRE(element.getAttribute("nationalPrefixForParsing")));
if (element.hasAttribute("nationalPrefixTransformRule")) {
metadata.setNationalPrefixTransformRule(
validateRE(element.getAttribute("nationalPrefixTransformRule")));
}
} else {
metadata.setNationalPrefixForParsing(nationalPrefix);
}
}
if (element.hasAttribute("preferredExtnPrefix")) {
metadata.setPreferredExtnPrefix(element.getAttribute("preferredExtnPrefix"));
}
if (element.hasAttribute("mainCountryForCode")) {
metadata.setMainCountryForCode(true);
}
// Extract availableFormats
NodeList numberFormatElements = element.getElementsByTagName("numberFormat");
int numOfFormatElements = numberFormatElements.getLength();
if (numOfFormatElements > 0) {
for (int i = 0; i < numOfFormatElements; i++) {
Element numberFormatElement = (Element) numberFormatElements.item(i);
NumberFormat format = new NumberFormat();
if (numberFormatElement.hasAttribute("nationalPrefixFormattingRule")) {
format.setNationalPrefixFormattingRule(validateRE(
getNationalPrefixFormattingRuleFromElement(numberFormatElement, nationalPrefix)));
} else {
format.setNationalPrefixFormattingRule(nationalPrefixFormattingRule);
}
if (numberFormatElement.hasAttribute("carrierCodeFormattingRule")) {
format.setDomesticCarrierCodeFormattingRule(validateRE(
getDomesticCarrierCodeFormattingRuleFromElement(numberFormatElement,
nationalPrefix)));
} else {
format.setDomesticCarrierCodeFormattingRule(carrierCodeFormattingRule);
}
setLeadingDigitsPatterns(numberFormatElement, format);
format.setPattern(validateRE(numberFormatElement.getAttribute("pattern")));
NodeList formatPattern = numberFormatElement.getElementsByTagName("format");
if (formatPattern.getLength() != 1) {
LOGGER.log(Level.SEVERE,
"Only one format pattern for a numberFormat element should be defined.");
throw new RuntimeException("Invalid number of format patterns for country: " +
regionCode);
}
format.setFormat(validateRE(formatPattern.item(0).getFirstChild().getNodeValue()));
metadata.addNumberFormat(format);
}
}
NodeList intlNumberFormatElements = element.getElementsByTagName("intlNumberFormat");
int numOfIntlFormatElements = intlNumberFormatElements.getLength();
if (numOfIntlFormatElements > 0) {
for (int i = 0; i < numOfIntlFormatElements; i++) {
Element numberFormatElement = (Element) intlNumberFormatElements.item(i);
NumberFormat format = new NumberFormat();
setLeadingDigitsPatterns(numberFormatElement, format);
format.setPattern(validateRE(numberFormatElement.getAttribute("pattern")));
NodeList formatPattern = numberFormatElement.getElementsByTagName("format");
if (formatPattern.getLength() != 1) {
LOGGER.log(Level.SEVERE,
"Only one format pattern for a numberFormat element should be defined.");
throw new RuntimeException("Invalid number of format patterns for country: " +
regionCode);
}
format.setFormat(validateRE(formatPattern.item(0).getFirstChild().getNodeValue()));
if (numberFormatElement.hasAttribute("carrierCodeFormattingRule")) {
format.setDomesticCarrierCodeFormattingRule(validateRE(
getDomesticCarrierCodeFormattingRuleFromElement(numberFormatElement,
nationalPrefix)));
} else {
format.setDomesticCarrierCodeFormattingRule(carrierCodeFormattingRule);
}
metadata.addIntlNumberFormat(format);
}
}
PhoneNumberDesc generalDesc = new PhoneNumberDesc();
generalDesc = processPhoneNumberDescElement(generalDesc, element, "generalDesc");
metadata.setGeneralDesc(generalDesc);
metadata.setFixedLine(processPhoneNumberDescElement(generalDesc, element, "fixedLine"));
metadata.setMobile(processPhoneNumberDescElement(generalDesc, element, "mobile"));
metadata.setTollFree(processPhoneNumberDescElement(generalDesc, element, "tollFree"));
metadata.setPremiumRate(processPhoneNumberDescElement(generalDesc, element, "premiumRate"));
metadata.setSharedCost(processPhoneNumberDescElement(generalDesc, element, "sharedCost"));
metadata.setVoip(processPhoneNumberDescElement(generalDesc, element, "voip"));
metadata.setPersonalNumber(processPhoneNumberDescElement(generalDesc, element,
"personalNumber"));
metadata.setPager(processPhoneNumberDescElement(generalDesc, element, "pager"));
if (metadata.getMobile().getNationalNumberPattern().equals(
metadata.getFixedLine().getNationalNumberPattern())) {
metadata.setSameMobileAndFixedLinePattern(true);
}
return metadata;
}