package forum;
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ToHtml { String strHTML=null; public ToHtml() { } public static Connection getConnection(){ Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lerx","root","mysql"); } catch (SQLException e) { e.printStackTrace(); } return conn ; } /** * 根據(jù)指定的文件名和路徑,建立文件 * @param String filePath * @param String fileName */ private static void mkFile(String filePath,String fileName){ File f=new File(filePath,fileName); //創(chuàng)建文件(空的) if(f.exists()) { f.delete(); } } /** * 根據(jù)路徑,用流讀入模板文件 * @param String path * @return StringBuffer template */ private static StringBuffer readTemplate(String path) { StringBuffer sb = new StringBuffer(); File file = new File(path); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } try { String a=""; while ((a = reader.readLine()) != null) { sb.append(a).append("\n"); } } catch (IOException e) { e.printStackTrace(); } return sb; } /** * 將文件寫出成靜態(tài)HTML * @param String fileName 靜態(tài)文件名 * @param StringBuffer content 文件內(nèi)容 * @param String filePath 寫出路徑 * @return boolean isOK */ private static boolean writeStaticFile(String fileName,String content,String filePath){ boolean isOK =false; mkFile(filePath,fileName); //先建立文件,然后在寫出文件 BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filePath+"/"+fileName)); bw.write(content); isOK = true; }catch (IOException e) { e.printStackTrace(); } try { bw.close(); } catch (IOException e) { e.printStackTrace(); } return isOK; } /** * 文章內(nèi)容生成靜態(tài)文件 */ //,String prevPage,String nextPage, public static boolean createhtml(int id,int pi_ClassID,String StaticHTML,String FilePath,String templatePath) { String title="",content=""; int rootNavId=0,styleId=0; /* 取得上一頁 下一頁的鏈接 */ Statement stat = null; try { stat = getConnection().createStatement(); } catch (SQLException e1) { e1.printStackTrace(); } String nextPage = "";
String prevPage = ""; String ps_SQLCmd="select id,title,HtmlCreated,HtmlURLFile,HtmlURLPath from article where id<"+String.valueOf(id) + " and RootNav="+pi_ClassID+" and ArticleType=1 order by id desc limit 0,1"; ResultSet rs; try { rs = stat.executeQuery(ps_SQLCmd); if(rs.next()) { nextPage = rs.getString("HtmlURLFile"); } rs.close(); } catch (SQLException e1) { e1.printStackTrace(); } ps_SQLCmd="select id,title,HtmlCreated,HtmlURLFile,HtmlURLPath from article where id>"+String.valueOf(id) +" and RootNav="+pi_ClassID+ " and ArticleType=1 order by id asc limit 0,1";
try { rs = stat.executeQuery(ps_SQLCmd); if(rs.next()) { prevPage = rs.getString("HtmlURLFile"); } rs.close(); } catch (SQLException e1) { e1.printStackTrace(); } String RealPath=""; String[] PathName=FilePath.split("/"); //建立目錄 for (int i=0;i<PathName.length;i++) { if (!PathName[i].trim().equals("")) { RealPath+="/"+PathName[i]; java.io.File pFilePath =new java.io.File(RealPath.toString()); if(!pFilePath.exists()) { pFilePath.mkdir(); } } } /* 讀取文章內(nèi)容 */ Connection conn = getConnection(); if(conn!=null) { try { stat = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } String sql="select * from article where id="+id; java.sql.ResultSet res = null; try{ res = stat.executeQuery(sql); if(res.next()) { rootNavId = res.getInt("RootNav"); title = res.getString("title"); content = res.getString("body"); System.out.println("查詢到的標(biāo)題:"+title); } } catch (SQLException e) { e.printStackTrace(); } } /* 把文章內(nèi)容解析成分頁數(shù)組 */ String[] tp_content = content.split("\\{\\$\\$page\\$\\$\\}"); /* 讀取模板 */ StringBuffer template = new StringBuffer(""); if(conn!=null) { try { stat = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } String sql="select style from nav where id="+rootNavId; java.sql.ResultSet res = null; try{ res = stat.executeQuery(sql); if(res.next()) { styleId = res.getInt("style"); templatePath =templatePath+"/"+styleId+"/"+styleId+".htm"; //靜態(tài)模板路徑及文件名 } } catch (SQLException e) { e.printStackTrace(); } template = readTemplate(templatePath); System.out.println("模板:"+template); } /*替換模板標(biāo)簽,并自動把文章內(nèi)容分頁存儲,生成多個(gè)靜態(tài)文件 */ /*靜態(tài)模板標(biāo)記:{$$page$$} {$$prePage$$},{$$nextPage$$},{$$title$$},{$$body$$}*/ boolean iswriteOK = false; String[] temp_basename = StaticHTML.split("\\."); String fileBaseName = temp_basename[0]; String fileName = ""; String filePath = FilePath; String pageContent = null; //靜態(tài)頁面內(nèi)容 System.out.println("解析后的文章內(nèi)容長度:"+tp_content.length); for (int i = 0; i < tp_content.length; i++) { pageContent = template.toString(); //靜態(tài)模板內(nèi)容 String preBaseName = prevPage.split("\\.")[0]; String nexBaseName = nextPage.split("\\.")[0]; if(i==0&&tp_content.length==1) //只有一頁內(nèi)容 { pageContent=pageContent.replaceAll("\\{\\$\\$prevPage\\$\\$\\}",preBaseName+"_0.html"); pageContent=pageContent.replaceAll("\\{\\$\\$nextPage\\$\\$\\}",nexBaseName+"_0.html"); pageContent=pageContent.replaceAll("\\{\\$\\$body\\$\\$\\}", tp_content[0]); fileName = fileBaseName+"_0.html";
} else if(i==0&&tp_content.length>1){//第一個(gè)文件 System.out.println("第一個(gè)文件"); pageContent = pageContent.replaceAll("\\{\\$\\$prevPage\\$\\$\\}",preBaseName+"_0.html"); pageContent = pageContent.replaceAll("\\{\\$\\$nextPage\\$\\$\\}",fileBaseName+"_"+1+".html"); pageContent = pageContent.replaceAll("\\{\\$\\$body\\$\\$\\}", tp_content[i]); fileName = fileBaseName+"_0.html"; }else if(i==(tp_content.length-1)&&tp_content.length>1){//最后一個(gè)文件 String prep = fileBaseName+"_"+(i-1)+".html"; //前一頁的文件名 System.out.println("最后一個(gè)文件"); pageContent = pageContent.replaceAll("\\{\\$\\$prevPage\\$\\$\\}",prep); pageContent = pageContent.replaceAll("\\{\\$\\$nextPage\\$\\$\\}",nexBaseName+"_0.html"); //下一頁的文件名,及nextPage pageContent = pageContent.replaceAll("\\{\\$\\$body\\$\\$\\}", tp_content[i]); fileName = fileBaseName+"_"+i+".html"; }else{ //中間生成的文件 System.out.println("中間的文件"); String prep = fileBaseName+"_"+(i-1)+".html"; String nexp = fileBaseName+"_"+(i+1)+".html"; pageContent= pageContent.replaceAll("\\{\\$\\$prevPage\\$\\$\\}",prep); pageContent = pageContent.replaceAll("\\{\\$\\$nextPage\\$\\$\\}",nexp); //下一頁的文件名,及nextPage pageContent = pageContent.replaceAll("\\{\\$\\$body\\$\\$\\}", tp_content[i]); fileName = fileBaseName+"_"+i+".html"; } pageContent = pageContent.replaceAll("\\{\\$\\$title\\$\\$\\}",title); //輸出靜態(tài)文件 iswriteOK = writeStaticFile(fileName,pageContent,filePath); }//end of for(...) return iswriteOK; }//end createHtml() } |
|