⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 htmlwrapper.java

📁 java xml开发指南(初学者推荐)Java Xml 编程指南书籍源码
💻 JAVA
字号:
package MyNa.utils;
import java.io.*;
import java.util.Enumeration;
import org.apache.ecs.*; // used only for wrapPageECS,wrapTablePageECS


public class HtmlWrapper {
  PrintWriter out=null; 
  Logger lg=null;

  public HtmlWrapper (PrintWriter o){out=o; lg=new Logger();}

  public void wrapBodyPage(String title,String body) throws IOException {
    wrapHeader(title);
    openBody();
    wrapHeading(title,1);
    out.println(body);
    closeBody();
    out.close();
  }
  public void wrapTablePage(String title,String[][]tab)
                      throws IOException{
    wrapHeader(title);
    openBody();
    wrapHeading(title,1);
    wrapTable(tab);
    closeBody();
    out.close();
  }
 public void wrapTablePage(String title,String[][]tab,String xtra)
                      throws IOException{
    wrapHeader(title,xtra);
    openBody();
    wrapHeading(title,1);
    wrapTable(tab);
    closeBody();
    out.close();
  }
 public void wrapEnvResultPage(Env resEnv) throws IOException{
  String numRows=resEnv.getStr("NumberOfRowsAffected");
  if(numRows!=null) wrapPage("Rows Affected:",numRows);
  else wrapTablePage("Query Response",
                      (String[][])resEnv.get("ResultTable"));
 }


// the two functions wrapRowsPage and wrapEnvPage are intended
// to show file-line substition as a basis for HTML generation. 
// They could also be implemented in any of the other three
// styles we use, but would require more explicit control if
// done with ecs or ad-hoc HTML generation, and would require
// more sophistication if done with jsp. 


 public void wrapRowsPage(RowSequence dbRows,Env rqEnv){
  String fName=rqEnv.getStr("templateFile");
  String fValue=null;
  try{
    fValue=MiscFile.fileSubstByTag(fName,dbRows);
    }catch(Exception ex){
      lg.logIt(""+ex);ex.printStackTrace(out);return;
      }
    out.println(fValue);
    out.close();
  }

 public void wrapEnvPage(Env E){
 String fName=E.getStr("templateFile");
 String fValue=null;
 try{
   fValue=MiscFile.fileSubstByTag(fName,E);
   if(null==fValue)throw new Exception("no file from "+fName);
  }catch(Exception ex){
    ex.printStackTrace(out);
    lg.logIt("HtmlWrapper.wrapEnvPage: ",ex);return;
  }
 out.println(fValue);
 out.close();
  }

  public void wrapHeader(String title)throws IOException{
    out.println("<HTML><HEAD><TITLE> "+title+" </TITLE></HEAD>");
  }
 public void wrapHeader(String title,String xtra)throws IOException{
    out.println("<HTML><HEAD><TITLE> "+title+" </TITLE>\n"+
                 xtra+"</HEAD>");
  }
  public void wrapHeading(String H,int level)throws IOException{
    out.println("<H"+level+">"+H+"</H"+level+">");
  }
  public void openBody()throws IOException{
    out.println("<BODY>");
  }
  public void closeBody()throws IOException{
    out.println("</BODY></HTML>");
  }

  public void wrapTable(String [][]tab)throws IOException{
    if(tab==null)return;
    openTable();
    wrapTableHeaders(tab[0]);
    for(int i=1;i<tab.length;i++)wrapTableRow(tab[i]);
    closeTable();
  }
  public void wrapTableHeaders(String [] H)throws IOException{
     out.println("<TH>"+Misc.stringArrayJoin(H,"</TH><TH>")+"</TH>");
  } 
  public void wrapTableRow(String [] R)throws IOException{
    out.println("<TR>");
    out.println("<TD>"+Misc.stringArrayJoin(R,"</TD><TD>")+"</TD>");
    out.println("</TR>");
  } 
  public void openTable()throws IOException{
    out.println("<TABLE>");
  }
  public void closeTable()throws IOException{
    out.println("</TABLE>");
  } 

// we define "wrapPage", the simplest form, as a name for wrapBodyPage:

  public void wrapPage(String title,String body) throws IOException {
    wrapBodyPage(title,body);
   }


  public void wrapPageECS(String title,String body) throws IOException {
    Html thePage=new Html()
              .addElement(new Head()
                 .addElement(new Title(title)))
              .addElement(new Body()
                 .addElement(new H1(title))
                 .addElement(body));
    out.println(thePage.toString());
    out.close();
 }
  public void wrapTablePageECS(String title,String[][]tab)
                      throws IOException{
    // wrapHeader(title);
    Table theTable=new Table();
    TR heads=new TR();
    for(int i=0;i<tab[0].length;i++)
      heads.addElement(new TH().addElement(tab[0][i]));
    theTable.addElement(heads);
    for(int j=1;j<tab.length;j++){
      TR theRow=new TR();
      for(int i=0;i<tab[j].length;i++)
        theRow.addElement(new TD().addElement(tab[j][i]));
      theTable.addElement(theRow);
      }
    Html thePage=
      new Html()  
        .addElement(new Head()
           .addElement(new Title(title)))
        .addElement(new Body()
           .addElement(new H1(title))
           .addElement(theTable));
    out.println(thePage.toString());
    out.close();
   }

/* 
  here are line-oriented versions of wrapRowsPage and wrapEnvPage;
  they can be used with line-oriented template files, in which
  lines for substitution are marked with $$SUBST... rather than
  having <myna:SUBST... tags. We include them as a simpler logic
  of almost-equal power; our template file system thus begins
  here, leads on through the ParseSubst class, and on to XML.
  See RowSubst.java for sample, as well as the interpret() method.
*/ 
 public void wrapRowsPageLines(RowSequence dbRows,Env rqEnv){
  RowSubst rSub=new RowSubst(rqEnv.getStr("templateFile"),dbRows,out);
  rSub.interpret();
    out.close();
  }

 public void wrapEnvPageLines(Env E){
 String fName=E.getStr("templateFile");
 String fValue=MiscFile.substLines(fName,E);
 out.println(fValue);
    out.close();
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -