Java tutorial
//package com.java2s; /* This file is part of Ustad Mobile. Ustad Mobile Copyright (C) 2011-2014 UstadMobile Inc. Ustad Mobile is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version with the following additional terms: All names, links, and logos of Ustad Mobile and Toughra Technologies FZ LLC must be kept as they are in the original distribution. If any new screens are added you must include the Ustad Mobile logo as it has been used in the original distribution. You may not create any new functionality whose purpose is to diminish or remove the Ustad Mobile Logo. You must leave the Ustad Mobile logo as the logo for the application to be used with any launcher (e.g. the mobile app launcher). If you want a commercial license to remove the above restriction you must contact us. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Ustad Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ import java.util.Hashtable; public class Main { /** * Parse a deliminated string with keys and values like Content-Type parameters * and cache-control headers. Keys can be present on their own e.g. * no-cache in which case the no-cache key will be in the hashtable with a * blank string value. It can also have an = sign with quoted or unquoted * text e.g. maxage=600 or maxage="600" * * @param str String to parse * @param deliminator deliminator character * @return Hashtable of parameters and values found */ public static Hashtable parseParams(String str, char deliminator) { String paramName = null; Hashtable params = new Hashtable(); boolean inQuotes = false; int strLen = str.length(); StringBuffer sb = new StringBuffer(); char c; char lastChar = 0; for (int i = 0; i < strLen; i++) { c = str.charAt(i); if (c == '"') { if (!inQuotes) { inQuotes = true; } else if (inQuotes && lastChar != '\\') { inQuotes = false; } } if ((isWhiteSpace(c) && !inQuotes) || (c == '"' && i < strLen - 1)) { //do nothing more } else if ((c == deliminator || i == strLen - 1)) { //check if we are here because it's the end... then we add this to bufer if (i == strLen - 1 && c != '"') { sb.append(c); } if (paramName != null) { //this is a parameter with a value params.put(paramName, sb.toString()); } else { //this is a parameter on its own params.put(sb.toString(), ""); } sb = new StringBuffer(); paramName = null; } else if (c == '=') { paramName = sb.toString(); sb = new StringBuffer(); } else { sb.append(c); } lastChar = c; } return params; } public static final boolean isWhiteSpace(char c) { if (c == ' ' || c == '\n' || c == '\t' || c == '\r') { return true; } else { return false; } } }