EN · DE · RU · FR · ES

#1381: PhoneNumberUtils.kt

projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt Kotlin class, projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt 289 lines · 165 code · 84 comments · 40 blank
Purpose: Source file: projectforge/framework/utils/PhoneNumberUtils.kt. PhoneNumberUtils.kt is part of the ProjectForge open-source project management application.

Source (first 100 lines)

/////////////////////////////////////////////////////////////////////////////
//
// Project ProjectForge Community Edition
//         www.projectforge.org
//
// Copyright (C) 2001-2026 Micromata GmbH, Germany (www.micromata.com)
//
// ProjectForge is dual-licensed.
//
// This community edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 3 of the License.
//
// This community edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see http://www.gnu.org/licenses/.
//
/////////////////////////////////////////////////////////////////////////////

package org.projectforge.framework.utils

import org.projectforge.framework.configuration.Configuration
import org.projectforge.framework.configuration.ConfigurationParam

/**
 * Utility functions for phone number normalization and formatting.
 *
 * @author Kai Reinhard (k.reinhard@micromata.de)
 */
object PhoneNumberUtils {

    // Common country codes (1-3 digits)
    private val KNOWN_COUNTRY_CODES = setOf(
        "1",   // USA, Canada
        "7",   // Russia, Kazakhstan
        "20", "27",  // Egypt, South Africa
        "30", "31", "32", "33", "34", "36", "39", "40", "41", "43", "44", "45", "46", "47", "48", "49", // Europe
        "51", "52", "53", "54", "55", "56", "57", "58", // South America
        "60", "61", "62", "63", "64", "65", "66", // Asia/Oceania
        "81", "82", "84", "86", "90", "91", "92", "93", "94", "95", "98", // Asia
        "212", "213", "216", "218", "220", "221", "222", "223", "224", "225", "226", "227", "228", "229", // Africa
        "230", "231", "232", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "243", "244", "245", "246", "247", "248", "249", // Africa
        "250", "251", "252", "253", "254", "255", "256", "257", "258", "260", "261", "262", "263", "264", "265", "266", "267", "268", "269", // Africa
        "290", "291", "297", "298", "299", // Atlantic
        "350", "351", "352", "353", "354", "355", "356", "357", "358", "359", // Europe
        "370", "371", "372", "373", "374", "375", "376", "377", "378", "380", "381", "382", "383", "385", "386", "387", "389", // Europe
        "420", "421", "423", // Europe
        "500", "501", "502", "503", "504", "505", "506", "507", "508", "509", "590", "591", "592", "593", "594", "595", "596", "597", "598", "599", // Americas
        "670", "671", "672", "673", "674", "675", "676", "677", "678", "679", "680", "681", "682", "683", "684", "685", "686", "687", "688", "689", "690", "691", "692", // Pacific
        "850", "852", "853", "855", "856", "880", "886", // Asia
        "960", "961", "962", "963", "964", "965", "966", "967", "968", "970", "971", "972", "973", "974", "975", "976", "977", "992", "993", "994", "995", "996", "998" // Asia/Middle East
    )

    /**
     * Normalizes a phone number to a standardized, readable format.
     *
     * Format: +[Country] [Area] [Number][-Extension]
     *
     * Examples:
     * - "0561 316793-0" → "+49 561 316793-0"
     * - "+49 (0) 561 / 316793-0" → "+49 561 316793-0"
     * - "0170 1234567" → "+49 170 1234567"
     * - "+1 415 555-1234" → "+1 415 5551234"
     *
     * @param phoneNumber The phone number to normalize (may contain spaces, slashes, parentheses, etc.)
     * @param defaultCountryPrefix The default country prefix to use if none is present (default: from config or "+49")
     * @return Normalized phone number in format "+CC AAA NNNNNNN[-EXT]" or null if input is null/empty
     */
    fun normalizePhoneNumber(
        phoneNumber: String?,
        defaultCountryPrefix: String = getDefaultCountryPrefix()
    ): String? {
        if (phoneNumber.isNullOrBlank()) {
            return null
        }

        var cleaned = phoneNumber.trim()

        // Remove prefixes like "Tel:", "Phone:", "Fax:", etc.
        cleaned = cleaned.replace(Regex("^(?:Tel\\.?:|Telefon:|Phone:|Mobil:|Mobile:|Fax:)\\s*", RegexOption.IGNORE_CASE), "")

        // Remove UTF control chars
        cleaned = cleaned.replace("\\p{C}".toRegex(), "")

        // Remove (0) in +49 (0) 123456789
        cleaned = cleaned.replace(Regex("\\(0\\)"), "")

        // Remove parentheses and slashes early for country code detection
        cleaned = cleaned.replace(Regex("[/()]"), "")

        if (cleaned.isEmpty()) {
            return null
        }

        // Return very short numbers (3 digits or less) unchanged - emergency numbers, short codes, etc.
        // Examples: 0, 00, 110, 112

Git History

868d6abb7 2025 -> 2026
2b9888261 Numberformatting in Sipgate and vcard handling refactored.
a2608ee2e PhoneNumberUtils...
c624d79b8 PhoneNumberUtils.formatPhoneNumber and AddressTextParser

868d6abb7

2025 -> 2026
868d6abb75cd191a892911ac8e45058932cf9074
diff --git a/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt b/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
index 320fb1c99..37ee6ed30 100644
--- a/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
+++ b/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
@@ -3,7 +3,7 @@
 // Project ProjectForge Community Edition
 //         www.projectforge.org
 //
-// Copyright (C) 2001-2025 Micromata GmbH, Germany (www.micromata.com)
+// Copyright (C) 2001-2026 Micromata GmbH, Germany (www.micromata.com)
 //
 // ProjectForge is dual-licensed.
 //

2b9888261

Numberformatting in Sipgate and vcard handling refactored.
2b9888261f5880a0d9bda8d7343c4367bd32d7c9
diff --git a/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt b/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
index 506ea65ab..320fb1c99 100644
--- a/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
+++ b/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
@@ -89,21 +89,50 @@ object PhoneNumberUtils {
         // Remove (0) in +49 (0) 123456789
         cleaned = cleaned.replace(Regex("\\(0\\)"), "")
 
+        // Remove parentheses and slashes early for country code detection
+        cleaned = cleaned.replace(Regex("[/()]"), "")
+
         if (cleaned.isEmpty()) {
             return null
         }
 
+        // Return very short numbers (3 digits or less) unchanged - emergency numbers, short codes, etc.
+        // Examples: 0, 00, 110, 112
+        // But NOT if they start with 00X (international prefix) or +X
+        val digitCount = cleaned.count { it.isDigit() }
+        if (digitCount in 1..3) {
+            // Allow "0", "00", "110", "112" etc. to pass through unchanged
+            // But block "001", "0049" etc. (international format) - they need processing
+            if (cleaned == "0" || cleaned == "00" || !cleaned.startsWith("00") && !cleaned.startsWith("+")) {
+                return cleaned
+            }
+        }
+
         // Extract country code
-        val (countryCode, remainingPart) = extractCountryCode(cleaned, defaultCountryPrefix)
+        val (countryCode, remainingPart, hasPrefix) = extractCountryCode(cleaned, defaultCountryPrefix)
+
+        // If no prefix was detected (no +, no 00, no leading 0), return cleaned number unchanged
+        if (!hasPrefix) {
+            // Just clean up special chars and normalize spaces
+            val result = cleaned
+                .replace(".", " ")
+                .replace(Regex("\\s+"), " ")
+                .trim()
+            // But validate it contains at least some digits
+            if (!result.contains(Regex("\\d"))) {
+                return null
+            }
+            return result
+        }
 
         if (remainingPart.isBlank()) {
-            return null
+            // If only country code exists (e.g., "001" -> "+1"), return just the country code
+            return countryCode
         }
 
-        // Clean up remaining part: replace dots with spaces (separator), remove slashes/parentheses, then normalize spaces
+        // Clean up remaining part: replace dots with spaces (separator), then normalize spaces
         var remaining = remainingPart.trim()
             .replace(".", " ") // Dot is a separator between number blocks
-            .replace(Regex("[/()]"), "") // Remove slashes, parentheses
             .replace(Regex("\\s+"), " ") // Normalize multiple spaces to single space
             .trim()
 

a2608ee2e

PhoneNumberUtils...
a2608ee2e9bf20343a26a5543fc0f06176735862
diff --git a/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt b/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
index 41a89f714..506ea65ab 100644
--- a/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
+++ b/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
@@ -100,9 +100,10 @@ object PhoneNumberUtils {
             return null
         }
 
-        // Clean up remaining part: remove slashes, dots, parentheses first, then normalize spaces
+        // Clean up remaining part: replace dots with spaces (separator), remove slashes/parentheses, then normalize spaces
         var remaining = remainingPart.trim()
-            .replace(Regex("[/().]"), "") // Remove slashes, parentheses, dots
+            .replace(".", " ") // Dot is a separator between number blocks
+            .replace(Regex("[/()]"), "") // Remove slashes, parentheses
             .replace(Regex("\\s+"), " ") // Normalize multiple spaces to single space
             .trim()
 

c624d79b8

PhoneNumberUtils.formatPhoneNumber and AddressTextParser
c624d79b8c005f4f746148f647e9eb1dbff3226f
diff --git a/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt b/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
new file mode 100644
index 000000000..41a89f714
--- /dev/null
+++ b/projectforge-business/src/main/kotlin/org/projectforge/framework/utils/PhoneNumberUtils.kt
@@ -0,0 +1,256 @@
+/////////////////////////////////////////////////////////////////////////////
+//
+// Project ProjectForge Community Edition
+//         www.projectforge.org
+//
+// Copyright (C) 2001-2025 Micromata GmbH, Germany (www.micromata.com)
+//
+// ProjectForge is dual-licensed.
+//
+// This community edition is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as published
+// by the Free Software Foundation; version 3 of the License.
+//
+// This community edition is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+// Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, see http://www.gnu.org/licenses/.
+//
+/////////////////////////////////////////////////////////////////////////////
+
+package org.projectforge.framework.utils
+
+import org.projectforge.framework.configuration.Configuration
+import org.projectforge.framework.configuration.ConfigurationParam
+
+/**
+ * Utility functions for phone number normalization and formatting.
+ *
+ * @author Kai Reinhard (k.reinhard@micromata.de)
+ */
+object PhoneNumberUtils {
+
+    // Common country codes (1-3 digits)
+    private val KNOWN_COUNTRY_CODES = setOf(
+        "1",   // USA, Canada
+        "7",   // Russia, Kazakhstan
+        "20", "27",  // Egypt, South Africa
+        "30", "31", "32", "33", "34", "36", "39", "40", "41", "43", "44", "45", "46", "47", "48", "49", // Europe
+        "51", "52", "53", "54", "55", "56", "57", "58", // South America
+        "60", "61", "62", "63", "64", "65", "66", // Asia/Oceania
+        "81", "82", "84", "86", "90", "91", "92", "93", "94", "95", "98", // Asia
+        "212", "213", "216", "218", "220", "221", "222", "223", "224", "225", "226", "227", "228", "229", // Africa
+        "230", "231", "232", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "243", "244", "245", "246", "247", "248", "249", // Africa
+        "250", "251", "252", "253", "254", "255", "256", "257", "258", "260", "261", "262", "263", "264", "265", "266", "267", "268", "269", // Africa
+        "290", "291", "297", "298", "299", // Atlantic
+        "350", "351", "352", "353", "354", "355", "356", "357", "358", "359", // Europe
+        "370", "371", "372", "373", "374", "375", "376", "377", "378", "380", "381", "382", "383", "385", "386", "387", "389", // Europe
+        "420", "421", "423", // Europe
+        "500", "501", "502", "503", "504", "505", "506", "507", "508", "509", "590", "591", "592", "593", "594", "595", "596", "597", "598", "599", // Americas
+        "670", "671", "672", "673", "674", "675", "676", "677", "678", "679", "680", "681", "682", "683", "684", "685", "686", "687", "688", "689", "690", "691", "692", // Pacific