分享

静态HTML生成模式代码

 Blex 2011-03-28
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 ;
 }
 
 /**
  * 根据指定的文件名和路径,建立文件
  * @param String filePath
  * @param String fileName
  */
 private static void mkFile(String filePath,String fileName){
  
  File f=new File(filePath,fileName); //创建文件(空的)
  if(f.exists())
  {
   f.delete();
  }
 }
 
 /**
  * 根据路径,用流读入模板文件
  * @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;
 }
 
 
 /**
  * 将文件写出成静态HTML
  * @param String fileName 静态文件名
  * @param StringBuffer content 文件内容
  * @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;
 }
 
 
 /**
  * 文章内容生成静态文件
  */
 //,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();
     }
    }
   }
  
   
   /*  读取文章内容  */
   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("查询到的标题:"+title);
     }
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
   
   
   /* 把文章内容解析成分页数组 */
   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"; //静态模板路径及文件名
      
     }
    } catch (SQLException e) {
     e.printStackTrace();
    }
    
    template = readTemplate(templatePath);
    System.out.println("模板:"+template);
   }
   
   
   /*替换模板标签,并自动把文章内容分页存储,生成多个静态文件 */
   /*静态模板标记:{$$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; //静态页面内容
   System.out.println("解析后的文章内容长度:"+tp_content.length);
   
   for (int i = 0; i < tp_content.length; i++)
   {
    pageContent = template.toString(); //静态模板内容
    String preBaseName = prevPage.split("\\.")[0];
    String nexBaseName = nextPage.split("\\.")[0];
    
    if(i==0&&tp_content.length==1) //只有一页内容
    {
     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){//第一个文件
     System.out.println("第一个文件");
     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){//最后一个文件
     String prep = fileBaseName+"_"+(i-1)+".html"; //前一页的文件名
     System.out.println("最后一个文件");
     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);
    
    //输出静态文件
    iswriteOK =  writeStaticFile(fileName,pageContent,filePath);
    
   }//end of for(...)
   
   return iswriteOK;
  
 
 }//end createHtml()
 
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约