What do you want to know?

Tuesday, August 11, 2009

Parsing String value to Specific type

Technorati Tags: ,,

Consider nothing more easier than having a method that accepts a String parameter then return a certain Type which is ready to use in your application. For example, DateTime.Parse(string s) in .NET. But one question: what type of string consider as valid and invalid value for this method to which I can pass. And no magic the author of DateTime class need to take care of what is the valid pattern of the string to be able to parse into DateTime object, What’s happened if user pass invalid string format to the method, how can user applies his/her own format to method to return the correct DateTime according to specific culture.

Home-Based Care (HBC) mobile app is a j2me midlet which listen to sms from a desktop app with phone modem connected. At first this kind of message is coming 1200000611082009AM|1200000311082009PM|…. The message is split into chunks separated by pipe character |, the chunk is read as: In province 12, clinic 000006, on 11 August 2009 morning for 1200000611082009AM. Since the format and length of the chunk is fixed a simple static method to convert from string to StoredEvent would be the solution.

   1: public static StoredEvent fromString(String text) {
   2:     StoredEvent event = new StoredEvent();        
   3:     try {
   4:         int[] data = parse(text);            
   5:         event.setProvinceId(data[0]);
   6:     ..
   7:     return event;
   8: }

And let the parse(String s) method to extract data from the text string.

   1: private static int[] FIELDS = {2, 6, 2, 2, 4, 2};
   2: private static String[][] Formats = new String[][] { null, null, null, null, null, new String[] {"AM", "PM"}};
   3: private static int[][] Values = new int[][] { null, null, null, null, null, new int[] {0, 1}};
   4:  
   5: private static int[] parse(String text) throws Throwable {
   6:     int[] result = new int[FIELDS.length];
   7:     int pos = 0;
   8:     
   9:     try {
  10:         for (int i = 0; i < FIELDS.length; i++) {
  11:             int endPos = pos + FIELDS[i];
  12:             String value = text.substring(pos, endPos);
  13:             if (Formats[i] != null) {
  14:                 for (int j = 0; j < Formats[i].length; j++) {
  15:                     if (value.equals(Formats[i][j])) {
  16:                         result[i] = Values[i][j];
  17:                         break;
  18:                     }
  19:                 }
  20:             } else {
  21:                 result[i] = Integer.parseInt(value);
  22:             }            
  23:             pos = endPos;
  24:         }
  25:     } catch (Exception e) {
  26:         logger.error("Invalid text format", e);
  27:         throw new Throwable("Can not parse text.");
  28:     }
  29:     
  30:     return result;
  31: }

Eventually, the incoming message format changed ProvinceId is omited, PhoneNumber, and EventId are to be added. And the newly added field required to be variable length that would reveal the week design on message format in the early stage.

No comments:

To record problems and solutions on real world cases and share back to the world...

About Me

Software Developer @InSTEDD. Learning and making the technology innovation to improve life...