Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
PhoneNumUtil.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2019 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package org.sleuthkit.autopsy.communications.relationships;
20
21import com.google.i18n.phonenumbers.NumberParseException;
22import com.google.i18n.phonenumbers.PhoneNumberUtil;
23import com.google.i18n.phonenumbers.Phonenumber;
24import java.util.logging.Level;
25import org.sleuthkit.autopsy.coreutils.Logger;
26
31public final class PhoneNumUtil {
32
33 private static final Logger logger = Logger.getLogger(PhoneNumUtil.class.getName());
34
35 private PhoneNumUtil() {
36 }
37
44 public static String getCountryCode(String phoneNumber) {
45 String regionCode = null;
46 try {
47 PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
48 Phonenumber.PhoneNumber phoneNum = phoneNumberUtil.parse(phoneNumber, "");
49 regionCode = phoneNumberUtil.getRegionCodeForNumber(phoneNum);
50
51 if (regionCode == null) {
52 return "";
53 } else {
54 return regionCode;
55 }
56 } catch (NumberParseException ex) {
57 logger.log(Level.WARNING, "Error getting country code, for phone number: {0}", phoneNumber);
58 return "";
59 }
60 }
61
70 public static String convertToE164(String phoneNumber) {
71 PhoneNumberUtil phone_util = PhoneNumberUtil.getInstance();
72 try {
73 Phonenumber.PhoneNumber phoneProto = phone_util.parse(phoneNumber, getCountryCode(phoneNumber));
74 if (phone_util.isValidNumber(phoneProto)) {
75 return phone_util.format(phoneProto, PhoneNumberUtil.PhoneNumberFormat.E164);
76 } else {
77 logger.log(Level.WARNING, "Invalid phone number: {0}", phoneNumber);
78 return phoneNumber;
79 }
80 } catch (NumberParseException e) {
81 logger.log(Level.WARNING, "Error parsing phone number: {0}", phoneNumber);
82 return phoneNumber;
83 }
84 }
85
94 public static String convertToInternational(String phoneNumber) {
95 PhoneNumberUtil phone_util = PhoneNumberUtil.getInstance();
96 try {
97 Phonenumber.PhoneNumber phoneProto = phone_util.parse(phoneNumber, getCountryCode(phoneNumber));
98 if (phone_util.isValidNumber(phoneProto)) {
99 return phone_util.format(phoneProto, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
100 } else {
101 logger.log(Level.WARNING, "Invalid phone number: {0}", phoneNumber);
102 return phoneNumber;
103 }
104 } catch (NumberParseException e) {
105 logger.log(Level.WARNING, "Error parsing phone number: {0}", phoneNumber);
106 return phoneNumber;
107 }
108 }
109
110}
synchronized static Logger getLogger(String name)
Definition Logger.java:124

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.