📄 rowseq.java
字号:
package MyNa.xml;
import java.util.*;
import java.io.*;
import java.sql.*;
import MyNa.utils.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public abstract class RowSeq {
// but leaves the next() method for subclass implementation,
// because only a subclass will know what an element (row) contains.
Env theEnv;
String [] theColumnLabels; // {"Name","Birthdate","LifeStory"...}
String [] theColumnTypes; // {"VARCHAR","DATE","LONGVARCHAR"...}
String [] theColumnValues; // {"JoeSchmoe","9/9/99","born & died"..}
String [] theFieldValueNum; // {"FieldValue1","FieldValue2",...}
int theNumberOfColumns;
Logger lg;
/* theColumnLabels appear in the Env as "columnLabels" and as
"FieldName"; theColumnTypes appear as "columnTypes" and as
"FieldType"; theColumnValues as "columnValues" and as "FieldValue".
theFieldValueNum is not placed in the Env, but each of its names,
i.e. "FieldValue1", "FieldValue2", and so on, is defined as the current
value, i.e. at any given moment
theEnv.getStr(theFieldValueNum[0])
== theEnv.getStr("FieldValue1")
== theEnv.getStrSeq("FieldValue")[0]
== theColumnValues[0];
we also set
theEnv.getStr(theColumnLabels[i])==theColumnValues[i]
and theEnv.getStr("FieldName1")==theColumnLabels[0], and so on.
This is intended to make them easy to access for various purposes,
e.g. ParseSubst in the early part of this book and TreeSubst in
the xml part; on the other hand, it's also easy to prune the
implementation to make it a little faster. It won't help much;
remember that the only important issue is the next()
function itself.
*/
public RowSeq(Env enumInfo)throws Exception {
theEnv=enumInfo;
lg=new Logger();
if(!findStrSeqs(false))return;
setInitEnv();
}
public RowSeq()throws Exception{this(new Env());}
public String[]getColumnLabels(){return theColumnLabels;}
public String[]getColumnTypes(){return theColumnTypes;}
public String[]getColumnValues(){return theColumnValues;}
public String[]getFieldValueNum(){return theFieldValueNum;}
public void setEnv(Env e){theEnv=e;}
public boolean findStrSeqs(boolean mustWork)throws Exception{
String[]colL=theEnv.getStrSeq("columnLabels");
String[]colT=theEnv.getStrSeq("columnTypes");
if(null!=colL)theColumnLabels=colL;
if(null==theColumnLabels)
if(mustWork)throw new Exception("missing columnLabels");
else return false;
if(null!=colT)theColumnTypes=colT;
if(null==theColumnTypes)
if(mustWork)throw new Exception("missing columnTypes");
else return false;
return true;
}
public void setInitEnv()throws Exception{ // call with columnlabels,types okay
if(null==theEnv)throw new Exception("null Environment in RowSeq.setInitEnv");
findStrSeqs(true);
theNumberOfColumns=theColumnLabels.length;
theColumnValues=new String[theNumberOfColumns];
theEnv.put("NumberOfColumns",""+theNumberOfColumns);
theEnv.put("FieldName",theColumnLabels);
theEnv.put("FieldType",theColumnTypes);
theEnv.put("FieldValue",theColumnValues);
theFieldValueNum=new String[theNumberOfColumns];
for(int i=1;i<=theNumberOfColumns;i++){
theEnv.put("FieldName"+i,theColumnLabels[i-1]);
theFieldValueNum[i-1]="FieldValue"+i;
}
}
public void shallowClone(RowSeq re){
theEnv=re.getRow();
theColumnLabels=re.getColumnLabels();
theColumnTypes=re.getColumnTypes();
theColumnValues=re.getColumnValues();
theFieldValueNum=re.getFieldValueNum();
}
public abstract boolean next();
/* the next() method tries to generate a new next-element,
a new row in the sequence, and is "true" if successful.
*/
public Env getRow(){
return theEnv;
}
public void toDB(Connection conn,String tableName,boolean reset)
throws Exception{
Statement stmnt= conn.createStatement();
lg.logIt(MiscDB.createTable(stmnt,tableName,
theColumnLabels,theColumnTypes,reset));
lg.logIt("table "+tableName+" exists");
stmnt.close();
PreparedStatement ps=MiscDB.createInserter(tableName,theColumnLabels,conn);
while(next()){
lg.logIt("sending to db "+tableName+" values "+theColumnValues[0]+"...");
for(int i=0;i<theColumnLabels.length;i++)
ps.setString(i+1,theColumnValues[i]);
ps.executeUpdate();
}
ps.close();
}
public void toDB() throws Exception{
String tableName=theEnv.getStr("tableName","ROWSEQ");
String resetStr=theEnv.getStr("reset","false");
boolean reset=(new Boolean(resetStr)).booleanValue();
String dbDriver=theEnv.getStr("dbDriver","sun.jdbc.odbc.JdbcOdbcDriver");
String dbUrl=theEnv.getStr("dbUrl","jdbc:odbc:PHONEBOOK");
String dbUser=theEnv.getStr("dbUser","JohnSmith");
String dbPwd=theEnv.getStr("dbPwd","password");
lg.logIt("about to get driver "+dbDriver);
Class.forName(dbDriver);
Connection conn=DriverManager.getConnection(dbUrl,dbUser,dbPwd);
toDB(conn,tableName,reset);
conn.close();
}
public void toXML() throws Exception{
lg.logIt("started toXML of RowSeq");
String tableName=theEnv.getStr("tableName","ROWSEQ");
String resetStr=theEnv.getStr("reset","false");
boolean reset=(new Boolean(resetStr)).booleanValue();
Object out=theEnv.get("OutputWriter");
lg.logIt("toXML with table="+tableName+"; resetStr="+resetStr);
if(null==out || !(out instanceof Writer))
toXML(new PrintWriter(System.out),tableName,reset);
else toXML((Writer)out,tableName,reset);
}
public void toXML(Writer out,String tableName,boolean reset)
throws Exception{
lg.logIt("xmlwriting to "+tableName);
String lineSeparator="\n";
out.write("<table name=\"");
out.write(tableName);
out.write("\"");
if(reset)out.write(" reset=\"true\"");
out.write(">"); out.write(lineSeparator);
// so far: <table name="Birthdays" reset="true">
out.write("<headers>");
for(int i=0;i<theColumnLabels.length;i++){
out.write("<header name=\"");
out.write(theColumnLabels[i]);
out.write("\" fieldType=\"");
out.write(theColumnTypes[i]);
out.write("\" />"); out.write(lineSeparator);
}
out.write("</headers>"); out.write(lineSeparator);
while(next()){String S;
lg.logIt("writing another xml line with "+theColumnValues[theColumnValues.length-1]);
out.write("<row>"); out.write(lineSeparator);
for(int i=0;i<theColumnLabels.length;i++){
out.write("<field name=\"");
out.write(null==(S=theColumnLabels[i])?"":S);
out.write("\">");
out.write(null==(S=theColumnValues[i])?"":Misc.htmlEscape(S));
out.write("</field>"); out.write(lineSeparator);
}
out.write("</row>"); out.write(lineSeparator);
}
out.write("</table>"); out.write(lineSeparator);
}
/* Sample XML output:
<table name="Birthdays" reset="true">
<headers>
<header name="Who" fieldType="VARCHAR" />
<header name="When" fieldType="DATE" />
<header name="Addr" fieldType="VARCHAR" />
</headers>
<row>
<field name="Who">Joseph Conrad</field>
<field name="When">12/3/1857</field>
<field name="Addr">tom.myers@worldnet.att.net</field>
</row>
*/
public void toMail()throws Exception{
// the RowSeq must define "msgtoaddr","msgfromaddr", "smtphost",
// "msgsubject", and "msgcontent", but these need not be actual
// columns; they just have to be defined in the Env
// produced by getRow();
java.util.Properties props=System.getProperties();
Session sess= Session.getInstance(props,null);
Transport trans=sess.getTransport("smtp");
lg.logIt("got props, sess, trans for RowSeq.toMail(); trans=null=="+(trans==null)+" Env=null="+(theEnv==null));
String smtphost=theEnv.getStr("smtphost");
lg.logIt("smtphost="+smtphost);
trans.connect(smtphost,"","");
while(next()){
MimeMessage mess=new MimeMessage(sess);
lg.logIt("new mess ==null=="+(mess==null));
String fromaddr=theEnv.getStr("msgfromaddr");
String toaddr=theEnv.getStr("msgtoaddr");
String subject=theEnv.getStr("msgsubject");
String content=theEnv.getStr("msgcontent");
lg.logIt("RowSeq.toMail() sending from "+fromaddr+" to "+toaddr+" subject["+subject+"] content[ "+content+" ] ");
mess.setFrom(new InternetAddress(fromaddr));
mess.addRecipient(Message.RecipientType.TO,
new InternetAddress(toaddr));
mess.setSubject(subject);
mess.setText(content);
trans.sendMessage(mess,mess.getAllRecipients());
}
trans.close();
lg.logIt("mail sent successfully");
}
public void close(){ // usually overridden.
}
} // end RowSeq
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -