<commit_before>package cn.cerc.mis.core;

import cn.cerc.core.DataSet;
import cn.cerc.core.Record;
import cn.cerc.core.TDate;
import cn.cerc.core.TDateTime;
import cn.cerc.core.Utils;
import cn.cerc.db.cache.Buffer;
import cn.cerc.db.core.ServerConfig;
import cn.cerc.mis.cdn.CDN;
import cn.cerc.mis.language.R;
import cn.cerc.ui.core.Component;
import cn.cerc.ui.core.HtmlContent;
import cn.cerc.ui.core.HtmlWriter;
import cn.cerc.ui.menu.MenuList;
import cn.cerc.ui.menu.MenuModel;
import cn.cerc.ui.parts.UIComponent;
import cn.cerc.ui.parts.UIContent;
import cn.cerc.ui.parts.UIDocument;
import cn.cerc.ui.parts.UIFooter;
import cn.cerc.ui.parts.UIHeader;
import cn.cerc.ui.parts.UISheetHelp;
import cn.cerc.ui.parts.UIToolbar;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public abstract class AbstractJspPage extends UIComponent implements IPage {
    private String jspFile;
    private IForm form;
    private List<String> cssFiles = new ArrayList<>();
    private List<String> jsFiles = new ArrayList<>();
    private List<HtmlContent> scriptFunctions = new ArrayList<>();
    private List<HtmlContent> scriptCodes = new ArrayList<>();
    // 头部：广告+菜单
    private UIHeader header;
    // 主体: 控制区(可选)+内容+消息区
    private UIDocument document;
    // 工具面板：多页形式
    private UIToolbar toolBar;
    // 状态栏：快捷操作+按钮组
    private UIFooter footer;

    public AbstractJspPage() {
        super();
    }

    @Override
    public final IForm getForm() {
        return form;
    }

    @Override
    public final void setForm(IForm form) {
        this.form = form;
        this.add("cdn", CDN.getSite());
        this.add("version", HTMLResource.getVersion());
        if (form != null) {
            this.put("jspPage", this);
            // 为兼容而设计
            ServerConfig config = ServerConfig.getInstance();
            this.add("summer_js", CDN.get(config.getProperty("summer.js", "js/summer.js")));
            this.add("myapp_js", CDN.get(config.getProperty("myapp.js", "js/myapp.js")));
        }
    }

    @Override
    public void addComponent(Component component) {
        if (component.getId() != null) {
            this.put(component.getId(), component);
        }
        super.addComponent(component);
    }

    @Override
    public String execute() throws ServletException, IOException {
        return this.getViewFile();
    }

    public final String getJspFile() {
        return jspFile;
    }

    public final void setJspFile(String jspFile) {
        this.jspFile = jspFile;
    }

    protected void put(String id, Object value) {
        getRequest().setAttribute(id, value);
    }

    public final String getMessage() {
        return form.getParam("message", null);
    }

    public final void setMessage(String message) {
        form.setParam("message", message);
    }

    public final String getViewFile() {
        String jspFile = this.getJspFile();
        if (getRequest() == null || jspFile == null) {
            return jspFile;
        }
        if (!jspFile.contains(".jsp")) {
            return jspFile;
        }

        String rootPath = String.format("/WEB-INF/%s/", Application.getAppConfig().getPathForms());
        String fileName = jspFile.substring(0, jspFile.indexOf(".jsp"));
        String extName = jspFile.substring(jspFile.indexOf(".jsp") + 1);

        // 检查是否存在 PC 专用版本的jsp文件
        String newFile = String.format("%s-%s.%s", fileName, "pc", extName);
        if (!this.getForm().getClient().isPhone() && fileExists(rootPath + newFile)) {
            // 检查是否存在相对应的语言版本
            String langCode = form == null ? Application.App_Language : R.getLanguage(form.getHandle());
            String langFile = String.format("%s-%s-%s.%s", fileName, "pc", langCode, extName);
            if (fileExists(rootPath + langFile)) {
                return langFile;
            }
            return newFile;
        }

        // 检查是否存在相对应的语言版本
        String langCode = form == null ? Application.App_Language : R.getLanguage(form.getHandle());
        String langFile = String.format("%s-%s.%s", fileName, langCode, extName);
        if (fileExists(rootPath + langFile)) {
            return langFile;
        }

        // 发送消息
        String msg = form.getParam("message", "");
        getRequest().setAttribute("message", msg == null ? "" : msg.replaceAll("\r\n", "<br/>"));

        return jspFile;
    }

    protected boolean fileExists(String fileName) {
        URL url = AbstractJspPage.class.getClassLoader().getResource("");
        if (url == null) {
            return false;
        }
        String filepath = url.getPath();
        String appPath = filepath.substring(0, filepath.indexOf("/WEB-INF"));
        String file = appPath + fileName;
        File f = new File(file);
        return f.exists();
    }

    // 从请求或缓存读取数据
    public final String getValue(Buffer buff, String reqKey) {
        String result = getRequest().getParameter(reqKey);
        if (result == null) {
            String val = buff.getString(reqKey).replace("{}", "");
            if (Utils.isNumeric(val) && val.endsWith(".0")) {
                result = val.substring(0, val.length() - 2);
            } else {
                result = val;
            }
        } else {
            result = result.trim();
            buff.setField(reqKey, result);
        }
        this.add(reqKey, result);
        return result;
    }

    public final List<String> getCssFiles() {
        return cssFiles;
    }

    public final List<String> getJsFiles() {
        return jsFiles;
    }

    public final List<HtmlContent> getScriptCodes() {
        return scriptCodes;
    }

    public final void addCssFile(String file) {
        file = CDN.get(file);
        cssFiles.add(file);
    }

    public final void addScriptFile(String file) {
        file = CDN.get(file);
        jsFiles.add(file);
    }

    public final void addScriptCode(HtmlContent scriptCode) {
        scriptCodes.add(scriptCode);
    }

    public void addScriptFunction(HtmlContent scriptCode) {<commit_msg>增加添加线上JavaScript函数 addOnlineScript<commit_after>package cn.cerc.mis.core;

import cn.cerc.core.DataSet;
import cn.cerc.core.Record;
import cn.cerc.core.TDate;
import cn.cerc.core.TDateTime;
import cn.cerc.core.Utils;
import cn.cerc.db.cache.Buffer;
import cn.cerc.db.core.ServerConfig;
import cn.cerc.mis.cdn.CDN;
import cn.cerc.mis.language.R;
import cn.cerc.ui.core.Component;
import cn.cerc.ui.core.HtmlContent;
import cn.cerc.ui.core.HtmlWriter;
import cn.cerc.ui.menu.MenuList;
import cn.cerc.ui.menu.MenuModel;
import cn.cerc.ui.parts.UIComponent;
import cn.cerc.ui.parts.UIContent;
import cn.cerc.ui.parts.UIDocument;
import cn.cerc.ui.parts.UIFooter;
import cn.cerc.ui.parts.UIHeader;
import cn.cerc.ui.parts.UISheetHelp;
import cn.cerc.ui.parts.UIToolbar;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public abstract class AbstractJspPage extends UIComponent implements IPage {
    private String jspFile;
    private IForm form;
    private List<String> cssFiles = new ArrayList<>();
    private List<String> jsFiles = new ArrayList<>();
    private List<HtmlContent> scriptFunctions = new ArrayList<>();
    private List<HtmlContent> scriptCodes = new ArrayList<>();
    // 头部：广告+菜单
    private UIHeader header;
    // 主体: 控制区(可选)+内容+消息区
    private UIDocument document;
    // 工具面板：多页形式
    private UIToolbar toolBar;
    // 状态栏：快捷操作+按钮组
    private UIFooter footer;

    public AbstractJspPage() {
        super();
    }

    @Override
    public final IForm getForm() {
        return form;
    }

    @Override
    public final void setForm(IForm form) {
        this.form = form;
        this.add("cdn", CDN.getSite());
        this.add("version", HTMLResource.getVersion());
        if (form != null) {
            this.put("jspPage", this);
            // 为兼容而设计
            ServerConfig config = ServerConfig.getInstance();
            this.add("summer_js", CDN.get(config.getProperty("summer.js", "js/summer.js")));
            this.add("myapp_js", CDN.get(config.getProperty("myapp.js", "js/myapp.js")));
        }
    }

    @Override
    public void addComponent(Component component) {
        if (component.getId() != null) {
            this.put(component.getId(), component);
        }
        super.addComponent(component);
    }

    @Override
    public String execute() throws ServletException, IOException {
        return this.getViewFile();
    }

    public final String getJspFile() {
        return jspFile;
    }

    public final void setJspFile(String jspFile) {
        this.jspFile = jspFile;
    }

    protected void put(String id, Object value) {
        getRequest().setAttribute(id, value);
    }

    public final String getMessage() {
        return form.getParam("message", null);
    }

    public final void setMessage(String message) {
        form.setParam("message", message);
    }

    public final String getViewFile() {
        String jspFile = this.getJspFile();
        if (getRequest() == null || jspFile == null) {
            return jspFile;
        }
        if (!jspFile.contains(".jsp")) {
            return jspFile;
        }

        String rootPath = String.format("/WEB-INF/%s/", Application.getAppConfig().getPathForms());
        String fileName = jspFile.substring(0, jspFile.indexOf(".jsp"));
        String extName = jspFile.substring(jspFile.indexOf(".jsp") + 1);

        // 检查是否存在 PC 专用版本的jsp文件
        String newFile = String.format("%s-%s.%s", fileName, "pc", extName);
        if (!this.getForm().getClient().isPhone() && fileExists(rootPath + newFile)) {
            // 检查是否存在相对应的语言版本
            String langCode = form == null ? Application.App_Language : R.getLanguage(form.getHandle());
            String langFile = String.format("%s-%s-%s.%s", fileName, "pc", langCode, extName);
            if (fileExists(rootPath + langFile)) {
                return langFile;
            }
            return newFile;
        }

        // 检查是否存在相对应的语言版本
        String langCode = form == null ? Application.App_Language : R.getLanguage(form.getHandle());
        String langFile = String.format("%s-%s.%s", fileName, langCode, extName);
        if (fileExists(rootPath + langFile)) {
            return langFile;
        }

        // 发送消息
        String msg = form.getParam("message", "");
        getRequest().setAttribute("message", msg == null ? "" : msg.replaceAll("\r\n", "<br/>"));

        return jspFile;
    }

    protected boolean fileExists(String fileName) {
        URL url = AbstractJspPage.class.getClassLoader().getResource("");
        if (url == null) {
            return false;
        }
        String filepath = url.getPath();
        String appPath = filepath.substring(0, filepath.indexOf("/WEB-INF"));
        String file = appPath + fileName;
        File f = new File(file);
        return f.exists();
    }

    // 从请求或缓存读取数据
    public final String getValue(Buffer buff, String reqKey) {
        String result = getRequest().getParameter(reqKey);
        if (result == null) {
            String val = buff.getString(reqKey).replace("{}", "");
            if (Utils.isNumeric(val) && val.endsWith(".0")) {
                result = val.substring(0, val.length() - 2);
            } else {
                result = val;
            }
        } else {
            result = result.trim();
            buff.setField(reqKey, result);
        }
        this.add(reqKey, result);
        return result;
    }

    public final List<String> getCssFiles() {
        return cssFiles;
    }

    public final List<String> getJsFiles() {
        return jsFiles;
    }

    public final List<HtmlContent> getScriptCodes() {
        return scriptCodes;
    }

    public final void addCssFile(String file) {
        file = CDN.get(file);
        cssFiles.add(file);
    }

    public final void addOnlineScript(String address) {
        jsFiles.add(address);
    }

    public final void addScriptFile(String file) {
        file = CDN.get(file);
        jsFiles.add(file);
    }

    public final void addScriptCode(HtmlContent scriptCode) {
        scriptCodes.add(scriptCode);
    }

    public void addScriptFunction(HtmlContent scriptCode) {
{'fraction_non_alphanumeric': 0.1289348171701113, 'fraction_numerical': 0.0014308426073131955, 'mean_word_length': 2.473495306460519, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5011219', 'n_tokens_mistral': 4009, 'n_tokens_neox': 4042, 'n_words': 823}