001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class PKParser {
028    
029            public PKParser(String pk) {
030                    if (pk.startsWith(StringPool.OPEN_CURLY_BRACE)) {
031                            pk = pk.substring(1, pk.length());
032                    }
033    
034                    if (pk.endsWith(StringPool.CLOSE_CURLY_BRACE)) {
035                            pk = pk.substring(0, pk.length() - 1);
036                    }
037    
038                    String[] array = StringUtil.split(pk);
039    
040                    for (int i = 0; i < array.length; i++) {
041                            String[] kvp = StringUtil.split(array[i], StringPool.EQUAL);
042    
043                            String key = kvp[0].trim();
044                            String value = kvp[1].trim();
045    
046                            _fields.put(key, value);
047                    }
048            }
049    
050            public boolean getBoolean(String key) {
051                    return GetterUtil.getBoolean(getString(key));
052            }
053    
054            public double getDouble(String key) {
055                    return GetterUtil.getDouble(getString(key));
056            }
057    
058            public int getInteger(String key) {
059                    return GetterUtil.getInteger(getString(key));
060            }
061    
062            public long getLong(String key) {
063                    return GetterUtil.getLong(getString(key));
064            }
065    
066            public short getShort(String key) {
067                    return GetterUtil.getShort(getString(key));
068            }
069    
070            public String getString(String key) {
071                    String value = _fields.get(key);
072    
073                    if (value == null) {
074                            return StringPool.BLANK;
075                    }
076                    else {
077                            return value;
078                    }
079            }
080    
081            private Map<String, String> _fields = new HashMap<String, String>();
082    
083    }