Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Ivancho participates in a team project with colleagues at SoftUni.
- * They have to develop an application, but something mysterious happened – at the last moment all team members… disappeared!
- * And guess what? He is left alone to finish the project.
- * All that is left to do is to parse the input data and store it in a special way, but Ivancho has no idea how to do that! Can you help him?
- *
- * The Input comes from the console on a variable number of lines and ends when the keyword "END" is received.
- * For each row of the input, the query string contains pairs field=value.
- * Within each pair, the field name and value are separated by an equals sign, '='.
- * The series of pairs are separated by an ampersand, '&'.
- * The question mark is used as a separator and is not part of the query string.
- * A URL query string may contain another URL as value.
- * SPACE is encoded as '+' or "%20". Letters (A-Z and a-z), numbers (0-9), the characters '*', '-', '.', '_'
- * and other non-special symbols are left as-is.
- *
- * Output
- * For each input line, print on the console a line containing the processed string as follows:
- * key=[value]nextkey=[another value] … etc.
- * Multiple whitespace characters should be reduced to one inside value/key names,
- * but there shouldn’t be any whitespaces before/after extracted keys and values.
- * If a key already exists, the value is added with comma and space after other values of the existing key in the current string.
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class QueryMess {
- public static void main(String[] args) throws IOException {
- BufferedReader reader =
- new BufferedReader(new InputStreamReader(System.in));
- String inputLine = reader.readLine();
- while (!inputLine.equals("END")) {
- extractValidKeyValuePairsAndPrintOnOutput(inputLine.split("[?&]"));
- System.out.println();
- inputLine = reader.readLine();
- }
- }
- private static void extractValidKeyValuePairsAndPrintOnOutput(String[] queryPairs) {
- LinkedHashMap<String, ArrayList<String>> keysValuesMap = new LinkedHashMap<>();
- Pattern pairsPattern = Pattern.compile("(\\S+)=(\\S+)");
- Matcher pairMatcher;
- ArrayList<String> keyValues;
- for (String pair : queryPairs) {
- pairMatcher = pairsPattern.matcher(pair);
- if (pairMatcher.find()) {
- String key =
- removeSpaceCharactersFromKeyValuePairs(pairMatcher.group(1));
- String value =
- removeSpaceCharactersFromKeyValuePairs(pairMatcher.group(2));
- keyValues = new ArrayList<>();
- if (keysValuesMap.containsKey(key)) {
- keyValues = keysValuesMap.get(key);
- }
- if (!keyValues.contains(value)) {
- keyValues.add(value);
- }
- keysValuesMap.put(key, keyValues);
- }
- }
- for (String keyField : keysValuesMap.keySet()) {
- System.out.printf("%s=", keyField);
- System.out.print(keysValuesMap.get(keyField));
- }
- }
- private static String removeSpaceCharactersFromKeyValuePairs(String keyWord) {
- StringBuilder resultWord = new StringBuilder();
- String[] wordParts = keyWord.trim().split("(%20)+");
- String[] smallParts;
- StringBuilder sb;
- for (String part : wordParts) {
- smallParts = part.trim().split("\\++");
- sb = new StringBuilder();
- for (String str : smallParts) {
- sb.append(str).append(" ");
- }
- resultWord.append(sb.toString().trim()).append(" ");
- }
- return resultWord.toString().trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement