Skip to content

Commit

Permalink
Refresh code and added maven
Browse files Browse the repository at this point in the history
  • Loading branch information
kalenchukov committed Mar 4, 2022
1 parent 6574512 commit f4e6670
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/.idea
/out
/*.iml
/target

2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Использование
### Сборщик параметров
```java
Map<String, List<String>> uriQueryMap = new LinkedHashMap<>(
Map<String, List<String>> uriQueryMap = new HashMap<>(
Map.ofEntries(
Map.entry("param1", List.of("value1.1")),
Map.entry("param2", List.of("значение2.1")),
Expand Down
43 changes: 43 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>dev.kalenchukov</groupId>
<artifactId>uriquery</artifactId>

<name>URIQuery</name>
<description>Сборщик / разборщик параметров URI</description>
<version>1.0.0</version>
<url>https://github.com/kalenchukov/URIQuery</url>

<licenses>
<license>
<name>MIT License</name>
<url>https://opensource.org/licenses/MIT</url>
</license>
</licenses>

<developers>
<developer>
<id>kalenchukov</id>
<name>Алексей Каленчуков</name>
<email>[email protected]</email>
</developer>
</developers>

<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<encoding>UTF-8</encoding>
</properties>

<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>23.0.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class UriQuery
{
Expand All @@ -22,45 +24,42 @@ private UriQuery() {}
*
* @param uriQueryEncode закодированные параметры URI
* @return карту параметров и их значений
* @throws IllegalArgumentException если переданы некорректный формат параметров URI
*/
public static Map<String, List<String>> parse(String uriQueryEncode) throws IllegalArgumentException
public static @NotNull Map<String, List<String>> parse(@Nullable String uriQueryEncode)
{
Map<String, List<String>> params = new LinkedHashMap<>();
Map<String, List<String>> queryParams = new HashMap<>();

if (uriQueryEncode != null && uriQueryEncode.length() > 0)
{
for (String groupParam : uriQueryEncode.split("&"))
{
Pattern pattern = Pattern.compile("(?<key>.+(\\[\\])?)=(?<value>.*)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(groupParam);
if (uriQueryEncode == null || uriQueryEncode.length() < 3) {
return queryParams;
}

if (!matcher.matches())
{
throw new IllegalArgumentException("Not correct query");
}
for (String groupParam : uriQueryEncode.split("&"))
{
Pattern pattern = Pattern.compile("(?<param>[a-z0-9_\\-.+,|:]+(\\[\\])?)=(?<value>.*)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(groupParam);

String keyGroup = URLDecoder.decode(matcher.group("key"), StandardCharsets.UTF_8);
if (matcher.matches())
{
String paramGroup = matcher.group("param").toLowerCase();
String valueGroup = URLDecoder.decode(matcher.group("value"), StandardCharsets.UTF_8);

List<String> paramValues = new ArrayList<>();
List<String> queryParamValues = new ArrayList<>();

if (keyGroup.contains("[]"))
if (paramGroup.contains("[]"))
{
keyGroup = keyGroup.replace("[]", "");
paramGroup = paramGroup.replace("[]", "");

if (params.containsKey(keyGroup))
{
paramValues = params.get(keyGroup);
if (queryParams.containsKey(paramGroup)) {
queryParamValues = queryParams.get(paramGroup);
}
}

paramValues.add(valueGroup);
params.put(keyGroup, paramValues);
queryParamValues.add(valueGroup);
queryParams.put(paramGroup, queryParamValues);
}
}

return params;
return queryParams;
}

/**
Expand All @@ -69,35 +68,31 @@ public static Map<String, List<String>> parse(String uriQueryEncode) throws Ille
* @param params карта параметров и их значений
* @return закодированные параметры URI
*/
public static String compose(Map<String, List<String>> params)
public static @NotNull String compose(@NotNull Map<String, List<String>> params)
{
StringBuilder uriQuery = new StringBuilder();

boolean needSeparator = false;

for (Map.Entry<String, List<String>> groupParam : params.entrySet())
{
if (needSeparator)
{
if (needSeparator) {
uriQuery.append("&");
}

for (int elm = 0; elm < groupParam.getValue().size(); elm++)
{
if (elm > 0)
{
if (elm > 0) {
uriQuery.append("&");
}

uriQuery.append(URLEncoder.encode(groupParam.getKey(), StandardCharsets.UTF_8));

if (groupParam.getValue().size() > 1)
{
if (groupParam.getValue().size() > 1) {
uriQuery.append("[]");
}

uriQuery.append("=");

uriQuery.append(URLEncoder.encode(groupParam.getValue().get(elm), StandardCharsets.UTF_8));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/*
* Copyright © 2022 Алексей Каленчуков
* GitHub: https://github.com/kalenchukov
* E-mail: mailto:[email protected]
*/

/**
* Сборщик / разборщик параметров URI
*
* @author <a href="mailto:[email protected]">Алексей Каленчуков</a>
* @copyright © 2021 Алексей Каленчуков
* @package URLQuery
* @version 21.8.24
* @package Lemna
* @version 1.1.0
*/
package dev.kalenchukov.uriquery;
File renamed without changes.

0 comments on commit f4e6670

Please sign in to comment.