Java tutorial
/** * * Licensed under the Apache License, Version 2.0 (the "License"); * * Founder admin@love320.com * * http://www.love320.com */ package cms.service.account; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import cms.entity.account.Archives; import cms.entity.account.Arctype; import cms.entity.account.Sgpage; import cms.entity.account.Shop; //???? @Component public class HTMLAllPageManager { private HTMLGenerator hg; // ???? private ServicesManager smf;//? private SgpageManager sm;// ?? private ConfigManager cm;// ? private String htmlFileMsg;//?? // ?: private String wwwUrl = org.apache.struts2.ServletActionContext.getRequest().getRemoteHost(); // ?: private int wwwPort = org.apache.struts2.ServletActionContext.getRequest().getLocalPort(); // : private String wwwPath = org.apache.struts2.ServletActionContext.getRequest().getContextPath(); // ??? private String webAppHostPath; HTMLAllPageManager() { wwwUrl = org.apache.struts2.ServletActionContext.getRequest().getServerName(); // ?: wwwPort = org.apache.struts2.ServletActionContext.getRequest().getServerPort(); // : wwwPath = org.apache.struts2.ServletActionContext.getRequest().getContextPath(); // ??? webAppHostPath = "http://" + wwwUrl + ":" + wwwPort + wwwPath + "/"; //System.out.println("?-URL"+ webAppHostPath); } //JS private void saveJs(String outLinkStr, String htmlName) { htmlFileMsg += "|" + htmlName;//?? //System.out.println(":"+outLinkStr +" ??:"+htmlName); String src = "<script language=\"javascript\" type=\"text/javascript\"> " + "window.location.href='" + outLinkStr + "'</script>"; File file = new File(this.getClass().getResource("/").getPath() + "../../html/" + htmlName); //System.out.println(file.getPath()); try { FileUtils.writeStringToFile(file, src, "UTF-8"); } catch (IOException e) { e.printStackTrace(); } } //???? private int saveHtml(String urlstr, String htmlName) { htmlFileMsg += "|" + htmlName;//?? /* //?PrintWriter,?,?? HttpServletResponse response = ServletActionContext.getResponse(); try { PrintWriter out = response.getWriter(); out.println(">>> "+htmlName+"<br>");//Ajax?? } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ return hg.save(urlstr, htmlName); } // ? public String htmlHomePage() { //??? String sysRunMode = cm.getConfigFile().getMode(); //???? if (sysRunMode.equals("1") || sysRunMode.equals("3")) { saveJs("index/", "../index.html"); saveJs("index/", "index.html"); } else { saveHtml(webAppHostPath + "html/homepage.action", "../index.html");// saveHtml(webAppHostPath + "html/homepage.action", "index.html");// html } //System.out.println("?URL"+ webAppHostPath); return htmlFileMsg; } // ?? public String htmlType() { //??? String sysRunMode = cm.getConfigFile().getMode(); ArctypeManager atm = smf.getArctypeManager();//?? // ??List List<Arctype> typeids = atm.getAllArctype(); // ?? for (Arctype arctype : typeids) { //System.out.println(":"+arctype.getArchivesList().size()); int arctypeListarcs = arctype.getArchivesList().size();//? if (arctype.getIspart() != 2) { //???? if (sysRunMode.equals("1") || sysRunMode.equals("3")) { saveJs("list-" + arctype.getId() + "/", "list-" + arctype.getId() + ".html"); } else { saveHtml(webAppHostPath + "html/list.action?id=" + arctype.getId(), "list-" + arctype.getId() + ".html"); // System.out.println("????:"+"list-"+arctype.getId()+".html"); } int pageNo = 1;// ?? int thepageNo = 1;// ?? int beforepageNo = 1;// ?? while (0 < thepageNo) { thepageNo = saveHtml( webAppHostPath + "html/list.action?id=" + arctype.getId() + "&pageNo=" + pageNo, "list-" + arctype.getId() + "-" + pageNo + ".html"); // System.out.println("????:"+"list-"+arctype.getId()+"-"+pageNo+".html"); //???? if (sysRunMode.equals("1") || sysRunMode.equals("3")) { saveJs("list-" + arctype.getId() + "-" + pageNo + "/", "list-" + arctype.getId() + "-" + pageNo + ".html"); } //System.out.println(":"+thepageNo+">>"+arctype.getTypename()+"-"+pageNo); pageNo++; if (Integer.parseInt(cm.getConfigFile().getHtmlgeneration()) < pageNo) break; if ((beforepageNo == thepageNo) || (pageNo > arctypeListarcs)) break;//??? beforepageNo = thepageNo; } } if (arctype.getIspart() == 2) { // System.out.println(":"+arctype.getTypedir()); // URL Pattern pattern = Pattern.compile("(http://|https://){1}[\\w\\.\\-/:]+"); Matcher matcher = pattern.matcher(arctype.getTypedir()); if (matcher.find()) { //System.out.println("?:" + arctype.getTypedir());// -js //System.out.println("?ID:" + arctype.getId()); saveJs(arctype.getTypedir(), "list-" + arctype.getId() + ".html"); } else { // ?? pattern = Pattern.compile("sgpage"); matcher = pattern.matcher(arctype.getTypedir()); if (matcher.find()) { //System.out.println("??"+arctype.getTypedir()); //???? if (sysRunMode.equals("1") || sysRunMode.equals("3")) { saveJs("list-" + arctype.getId() + "/", "list-" + arctype.getId() + ".html"); } else { saveHtml(webAppHostPath + "html/" + arctype.getTypedir(), "list-" + arctype.getId() + ".html"); } } else { // // <script language="javascript" type="text/javascript"> // window.location.href="login.jsp?backurl="+window.location.href; // </script> saveJs(arctype.getTypedir(), "list-" + arctype.getId() + ".html"); } } } } return htmlFileMsg; } // ? public void htmlArticle() { //??? String sysRunMode = cm.getConfigFile().getMode(); ArchivesManager am = smf.getArchivesManager();//? // ? List<Archives> arcicleses = am.getAllArchives(); // ? for (Archives archives : arcicleses) { //???? if (sysRunMode.equals("1") || sysRunMode.equals("3")) { /* * saveJs("article-" + archives.getId() + "/" ,"article-" + archives.getId() + ".html"); */ } else { saveHtml(webAppHostPath + "html/article.action?id=" + archives.getId(), "article-" + archives.getId() + ".html"); // System.out.println("???:"+"list-"+archives.getId()+".html"); } } } // ?? public void htmlShop() { //??? String sysRunMode = cm.getConfigFile().getMode(); ShopManager shopM = smf.getShopManager();//? // ? List<Shop> shops = shopM.getAll(); // ? for (Shop shop : shops) { //???? if (sysRunMode.equals("1") || sysRunMode.equals("3")) { /* saveJs("shop-" + shop.getId() + "/" ,"shop-" + shop.getId() + ".html"); */ } else { saveHtml(webAppHostPath + "html/shop.action?id=" + shop.getId(), "shop-" + shop.getId() + ".html"); // System.out.println("???:"+"list-"+archives.getId()+".html"); } } } //? public void htmlOneArticle(Long id) { //??? String sysRunMode = cm.getConfigFile().getMode(); //???? if (sysRunMode.equals("1") || sysRunMode.equals("3")) { /* saveJs("article-" + id + "/" ,"article-" + id + ".html"); */ } else { saveHtml(webAppHostPath + "html/article.action?id=" + id, "article-" + id + ".html"); // System.out.println("???:"+"list-"+id+".html"); } } // ??? public void htmlSgpage() { //??? String sysRunMode = cm.getConfigFile().getMode(); // ??? List<Sgpage> sgpages = sm.getAllSgpage(); // ????? /* for (Sgpage sgpage : sgpages) { if(sysRunMode.equals("1")||sysRunMode.equals("3")){ saveJs("sgpage-" + sgpage.getId() + "/","sgpage-" + sgpage.getId() + ".html"); }else{ saveHtml(webAppHostPath + "html/sgpage.action?id=" + sgpage.getId(), "sgpage-" + sgpage.getId() + ".html"); // System.out.println("?????:"+"sgpage-"+sgpage.getId()+".html"); } } */ //????? for (Sgpage sgpage : sgpages) { saveHtml(webAppHostPath + "html/sgpage.action?id=" + sgpage.getId(), "sgpage-" + sgpage.getId() + ".html"); } } //??? public void htmlOneSgpage(Long id) { //??? String sysRunMode = cm.getConfigFile().getMode(); //???? if (sysRunMode.equals("1") || sysRunMode.equals("3")) { saveJs("sgpage-" + id + "/", "sgpage-" + id + ".html"); } else { saveHtml(webAppHostPath + "html/sgpage.action?id=" + id, "sgpage-" + id + ".html"); } } @Autowired public void setHg(HTMLGenerator hg) { this.hg = hg; } @Autowired public void setSmf(ServicesManager smf) { this.smf = smf; } @Autowired public void setCm(ConfigManager cm) { this.cm = cm; } @Autowired public void setSm(SgpageManager sm) { this.sm = sm; } public String getHtmlFileMsg() { return htmlFileMsg; } public void setHtmlFileMsg(String htmlFileMsg) { this.htmlFileMsg = htmlFileMsg; } }