Advertisement
Guest User

LogObjectParser

a guest
Nov 7th, 2012
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3.  
  4. public class Parse
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.     String toParse = "ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife&Content=racroi&TimeStamp=2012-11-05T17:20:06.056";
  9.     String[] fields = toParse.split("&");
  10.     String[] t;
  11.  
  12.     HashMap<String, String> things = new HashMap<String, String>();
  13.  
  14.  
  15.     for (int i = 0; i < fields.length; ++i)
  16.     {
  17.         t = fields[i].split("=");
  18.         if (2 == t.length)
  19.         {
  20.         things.put(t[0], t[1]);
  21.         }
  22.     }
  23.  
  24.  
  25.     // NOTE this will fail if the keys are not present in the dict.
  26.     // Remember to sanitise your input!
  27.     LogObject logObject = new LogObject(things.get("ObjectGUId"),
  28.                         things.get("ObjectType"),
  29.                         things.get("ObjectTitle"),
  30.                         things.get("Content"),
  31.                         things.get("TimeStamp"));
  32.  
  33.     // NOTE this is not guaranteed to print in sorted order
  34.     for (String k : things.keySet())
  35.         System.out.println(k + ": " + things.get(k));
  36.  
  37.     System.out.println(logObject);
  38.     }
  39.  
  40.     static class LogObject
  41.     {
  42.     private String ObjectGUId;
  43.     private String ObjectType;
  44.     private String ObjectTitle;
  45.     private String Content;
  46.     private String TimeStamp;
  47.  
  48.     public LogObject(String guid, String type, String title,
  49.              String content, String timestamp)
  50.     {
  51.         ObjectGUId = guid;
  52.         ObjectType = type;
  53.         ObjectTitle = title;
  54.         Content = content;
  55.         TimeStamp = timestamp;
  56.     }
  57.  
  58.     public String toString()
  59.     {
  60.         return ObjectGUId + ", " + ObjectType + ", " + ObjectTitle + ", "
  61.         + Content + ", " + TimeStamp;
  62.     }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement