/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.infra.database.metadata.url;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.database.metadata.UnrecognizedDatabaseURLException;

import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Standard JDBC URL parser.
 */
public final class StandardJdbcUrlParser {
    
    private static final String SCHEMA_PATTERN = "(?<schema>[\\w\\+:%]+)\\s*";
    
    private static final String AUTHORITY_PATTERN = "(?://(?<authority>[^/?#]*))?\\s*";
    
    private static final String PATH_PATTERN = "(?:/(?!\\s*/)(?<path>[^?#]*))?";
    
    private static final String QUERY_PATTERN = "(?:\\?(?!\\s*\\?)(?<query>[^#]*))?";
    
    private static final Pattern CONNECTION_URL_PATTERN = Pattern.compile(SCHEMA_PATTERN + AUTHORITY_PATTERN + PATH_PATTERN + QUERY_PATTERN, Pattern.CASE_INSENSITIVE);
    
    private static final String AUTHORITY_GROUP_KEY = "authority";
    
    private static final String PATH_GROUP_KEY = "path";
    
    private static final String QUERY_GROUP_KEY = "query";
    
    /**
     * Parse JDBC URL.
     * 
     * @param jdbcURL JDBC URL to be parsed
     * @return parsed JDBC URL
     */
    public JdbcUrl parse(final String jdbcURL) {
        Matcher matcher = CONNECTION_URL_PATTERN.matcher(jdbcURL);
        if (matcher.matches()) {
            String authority = matcher.group(AUTHORITY_GROUP_KEY);
            if (null == authority) {                 
                throw new UnrecognizedDatabaseURLException(jdbcURL, CONNECTION_URL_PATTERN.pattern().replaceAll("%", "%%"));
            }
            return new JdbcUrl(parseHostname(authority), parsePort(authority), matcher.group(PATH_GROUP_KEY), parseQueryProperties(matcher.group(QUERY_GROUP_KEY)));
        }
        throw new UnrecognizedDatabaseURLException(jdbcURL, CONNECTION_URL_PATTERN.pattern().replaceAll("%", "%%"));
    }
    
    private String parseHostname(final String authority) {
        if (!authority.contains(":")) {
            return authority;
        }
        return authority.split(":")[0];
    }
    
    private int parsePort(final String authority) {
        if (!authority.contains(":")) {
            return -1;
        }
        String port = authority.split(":")[1];
        if (port.contains(",")) {
            port = port.split(",")[0];
        }
        return Integer.parseInt(port);
    }
    
    private Properties parseQueryProperties(final String query) {
        if (Strings.isNullOrEmpty(query)) {
            return new Properties();
        }
        Properties result = new Properties();
        for (Entry<String, String> entry : Splitter.on("&").withKeyValueSeparator("=").split(query).entrySet()) {
            result.setProperty(entry.getKey(), entry.getValue());
        }
        return result;
    }
}

@@ -2021,18 +2021,18 @@
 am jdbcU
-RL
+rl
  JDBC UR
@@ -2128,18 +2128,18 @@
 ng jdbcU
-RL
+rl
 ) %7B%0A    
@@ -2196,18 +2196,18 @@
 er(jdbcU
-RL
+rl
 );%0A     
@@ -2341,25 +2341,8 @@
 y) %7B
-                 
 %0A   
@@ -2394,34 +2394,34 @@
 LException(jdbcU
-RL
+rl
 , CONNECTION_URL
@@ -2708,18 +2708,18 @@
 on(jdbcU
-RL
+rl
 , CONNEC

Refactor StandardJdbcUrlParser (#14675)