net.duckling.ddl.service.url.DefaultURLConstructor.java Source code

Java tutorial

Introduction

Here is the source code for net.duckling.ddl.service.url.DefaultURLConstructor.java

Source

/*
 * Copyright (c) 2008-2016 Computer Network Information Center (CNIC), Chinese Academy of Sciences.
 * 
 * This file is part of Duckling project.
 *
 * Licensed 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 net.duckling.ddl.service.url;

import java.net.MalformedURLException;
import java.net.URL;

import net.duckling.ddl.util.TextUtil;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @date Mar 2, 2010
 * @author xiejj@cnic.cn
 */
public class DefaultURLConstructor implements URLConstructor {
    private String teamName = "";
    private String basePath = "";
    private String baseURL = "";
    private boolean isRelative = false;

    public DefaultURLConstructor(String baseURL, String teamCode) {
        this.baseURL = baseURL;
        try {
            URL url = new URL(baseURL);
            basePath = url.getPath();
        } catch (MalformedURLException e) {
            basePath = "/ddl";
        }
        this.isRelative = true;
        teamName = teamCode;
    }

    private String buildURLWithFullParam(String action, String name, String params, boolean absolute) {
        if (UrlPatterns.PLAIN.equals(action) && !StringUtils.isEmpty(params)) {

            params = (name.indexOf('?') != -1) ? "&" : "?" + params;
        }
        String urlPattern = UrlPatterns.getInstance().findUrlPattern(action);
        if (urlPattern == null) {
            urlPattern = UrlPatterns.getInstance().findUrlPattern(UrlPatterns.T_PAGE);
        }
        String url = doReplace(urlPattern, name, absolute);
        if (!StringUtils.isEmpty(params)) {
            if (url.indexOf('?') != -1) {
                url = url + "&" + params;
            } else {
                url = url + "?" + params;
            }
        }

        return url;
    }

    /**
     * ??URL
     * URLPattern?:
     * %u      ?basepath?,??baseURL?
     * %U      ??BaseURL?
     * %p      basePath?
     * %n      page?
     * %v      ViewPort?(page/)
     * %s      ?
     * %t      Team ID
     * 
     * @param urlpattern url?
     * @param page ?
     * @param absolute ???
     * @return ???URL
     */
    private String doReplace(String urlpattern, String page, boolean absolute) {
        String url = urlpattern;
        if (absolute) {
            url = TextUtil.replaceString(url, "%u", baseURL);
        } else {
            url = TextUtil.replaceString(url, "%u", basePath);
        }
        url = TextUtil.replaceString(url, "%U", baseURL);
        url = TextUtil.replaceString(url, "%p", basePath);
        if (page != null) {
            url = TextUtil.replaceString(url, "%n", page);
        } else {
            url = TextUtil.replaceString(url, "%n", "");
        }
        if (teamName != null) {
            url = TextUtil.replaceString(url, "%t", teamName);
        }

        return url;
    }

    public String makeURL(String action, String resourceId, String params) {
        return buildURLWithFullParam(action, resourceId, params, !isRelative);
    }

    public String makeURL(String action, String name, String params, boolean absolute) {
        return buildURLWithFullParam(action, name, params, absolute);
    }

    public void setTeamCode(String name) {
        this.teamName = name;
    }
}