All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.utils.SiteMapUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
package com.github.dennisit.vplus.data.utils;

import com.github.dennisit.vplus.data.enums.common.FrequentlyType;
import com.spring.boxes.dollar.$;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.List;

@Slf4j
public class SiteMapUtils {

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class UrlNode implements Serializable {

        @ApiModelProperty("站点URL")
        private String loc;

        @ApiModelProperty("优先级")
        private double priority;

        @ApiModelProperty("最后修改时间")
        private String lastmod;

        @ApiModelProperty("更新频次")
        private FrequentlyType changefreq;
    }

    public static String sitemapTxt(List urls) {
        if (CollectionUtils.isEmpty(urls)) {
            return StringUtils.EMPTY;
        }
        StringBuffer siteMap = new StringBuffer(1024);
        for (String url : urls) {
            siteMap.append(url).append("\r\n");
        }
        return siteMap.toString();
    }

    public static void createSiteMapFile(String filePath, String content) {
        try {
            File file = new File(filePath);
            FileUtils.writeStringToFile(file, content, Charset.forName("UTF-8"));
        } catch (IOException e) {
            log.error(e.getLocalizedMessage(), e);
        }
        $.openDir(filePath);
    }

    public static void createSiteMapFile(String filePath, List list) {
        try {
            File file = new File(filePath);
            FileUtils.writeStringToFile(file, getUrlMaps(list), Charset.forName("UTF-8"));
        } catch (IOException e) {
            log.error(e.getLocalizedMessage(), e);
        }
        $.openDir(filePath);
    }


    private static String getUrlMaps(List list) {
        StringBuffer buffer = new StringBuffer("").append("\r\n");
        buffer.append("").append("\r\n");
        buffer.append(getUrlNodes(list));
        buffer.append("");
        return buffer.toString();
    }

    private static String getUrlNodes(List list) {
        if (CollectionUtils.isEmpty(list)) {
            return StringUtils.EMPTY;
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            UrlNode node = list.get(i);
            if (null != node) {
                buffer.append("").append("\r\n");
                buffer.append("\t").append(StringUtils.trim(node.getLoc())).append("").append("\r\n");
                buffer.append("\t").append(StringUtils.trim(node.getLastmod())).append("").append("\r\n");
                buffer.append("\t").append(null != node.getChangefreq() ? node.getChangefreq().getLabel() : FrequentlyType.MONTHLY.getLabel()).append("").append("\r\n");
                buffer.append("\t").append(node.getPriority() > 0 ? node.getPriority() : "0.8").append("").append("\r\n");
                buffer.append("").append("\r\n");
            }
        }
        return buffer.toString();
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy