1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  /**
25   * <a href="PKParser.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class PKParser {
30  
31      public PKParser(String pk) {
32          if (pk.startsWith(StringPool.OPEN_CURLY_BRACE)) {
33              pk = pk.substring(1, pk.length());
34          }
35  
36          if (pk.endsWith(StringPool.CLOSE_CURLY_BRACE)) {
37              pk = pk.substring(0, pk.length() - 1);
38          }
39  
40          String[] array = StringUtil.split(pk);
41  
42          for (int i = 0; i < array.length; i++) {
43              String[] kvp = StringUtil.split(array[i], StringPool.EQUAL);
44  
45              String key = kvp[0].trim();
46              String value = kvp[1].trim();
47  
48              _fields.put(key, value);
49          }
50      }
51  
52      public boolean getBoolean(String key) {
53          return GetterUtil.getBoolean(getString(key));
54      }
55  
56      public double getDouble(String key) {
57          return GetterUtil.getDouble(getString(key));
58      }
59  
60      public int getInteger(String key) {
61          return GetterUtil.getInteger(getString(key));
62      }
63  
64      public long getLong(String key) {
65          return GetterUtil.getLong(getString(key));
66      }
67  
68      public short getShort(String key) {
69          return GetterUtil.getShort(getString(key));
70      }
71  
72      public String getString(String key) {
73          String value = _fields.get(key);
74  
75          if (value == null) {
76              return StringPool.BLANK;
77          }
78          else {
79              return value;
80          }
81      }
82  
83      private Map<String, String> _fields = new HashMap<String, String>();
84  
85  }