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)容分頁存儲,生成多個靜態(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){//第一個文件
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);
//輸出靜態(tài)文件
iswriteOK = writeStaticFile(fileName,pageContent,filePath);
}//end of for(...)
return iswriteOK;
}//end createHtml()
}