001    /**
002     * Copyright (c) 2000-2013 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.CharPool;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class PKParser {
029    
030            public PKParser(String pk) {
031                    if (pk.startsWith(StringPool.OPEN_CURLY_BRACE)) {
032                            pk = pk.substring(1);
033                    }
034    
035                    if (pk.endsWith(StringPool.CLOSE_CURLY_BRACE)) {
036                            pk = pk.substring(0, pk.length() - 1);
037                    }
038    
039                    String[] array = StringUtil.split(pk);
040    
041                    for (int i = 0; i < array.length; i++) {
042                            String[] kvp = StringUtil.split(array[i], CharPool.EQUAL);
043    
044                            String key = kvp[0].trim();
045                            String value = kvp[1].trim();
046    
047                            _fields.put(key, value);
048                    }
049            }
050    
051            public boolean getBoolean(String key) {
052                    return GetterUtil.getBoolean(getString(key));
053            }
054    
055            public double getDouble(String key) {
056                    return GetterUtil.getDouble(getString(key));
057            }
058    
059            public int getInteger(String key) {
060                    return GetterUtil.getInteger(getString(key));
061            }
062    
063            public long getLong(String key) {
064                    return GetterUtil.getLong(getString(key));
065            }
066    
067            public short getShort(String key) {
068                    return GetterUtil.getShort(getString(key));
069            }
070    
071            public String getString(String key) {
072                    String value = _fields.get(key);
073    
074                    if (value == null) {
075                            return StringPool.BLANK;
076                    }
077                    else {
078                            return value;
079                    }
080            }
081    
082            private Map<String, String> _fields = new HashMap<String, String>();
083    
084    }