Offline application example
Example
The following code reads a CSV file line by line, performs user agent matching and saves result in another CSV file. Essentially two new comma-separated values are added to each line that contain detection results. In this case we are interested in the following properties: IsMobile and HardwareVendor . For my test I've used the millions.csv file that can be downloaded from our website.
package processcsv ; import fiftyone.mobile.detection.Match ; import fiftyone.mobile.detection.Provider ; import fiftyone.mobile.detection.factories.MemoryFactory ; import java.io.BufferedReader ; import java.io.FileReader ; import java.io.FileWriter ; import java.io.IOException ; import java.util.logging.Level ; import java.util.logging.Logger ; public class ProcessCSV { private static final String FOD_FILE_PATH = "path_to_dat_file" ; private static final String CSV_FILE_PATH = "path_to_original_csv_file" ; private static final String CSV_FILE_RESULT = "path_to_destination_csv_file" ; /** * @param args the command line arguments */ public static void main ( String [] args ) { Provider p ; FileWriter writer ; BufferedReader br ; String line ; Match m ; try { //MemoryFactory is faster than StreamFactory but requires more memory p = new Provider ( MemoryFactory . create ( FOD_FILE_PATH )); br = new BufferedReader ( new FileReader ( CSV_FILE_PATH )); writer = new FileWriter ( CSV_FILE_RESULT ); while (( line = br . readLine ()) != null ) { m = p . match ( line ); writer . append ( line ); writer . append ( ',' ); writer . append ( m . getValues ( "IsMobile" ). toString ()); writer . append ( ',' ); writer . append ( m . getValues ( "HardwareVendor" ). toString ()); writer . append ( '\n' ); } } catch ( IOException ex ) { Logger . getLogger ( ProcessCSV . class . getName ()). log ( Level . SEVERE , null , ex ); } finally { //Destroy objects. } } }
Originally the CSV file contained records of the following format
:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
"Mozilla/5.0 (iPad; CPU OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B329 Safari/8536.25"
After executing this program
:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0),False,Unknown
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko,False,Unknown
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36",False,Unknown
"Mozilla/5.0 (iPad; CPU OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B329 Safari/8536.25",True,Apple