📄 rowsubst.java
字号:
package MyNa.utils;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Enumeration;
/* given a template file, a resultset, and prior name->value pairs,
we want to substitute values for names, at least supporting:
$$SUBSTD:$: $FieldName1$ for $Parameter1$ is $FieldValue1$
$$SUBSTDERR:$: There was no $FieldName1$ for $Parameter1$.
in this line, the "D" stands for Data, indicating that there
has to be actual query-result data (or update-data, in which
case only NumberAffected will be defined by the query.) Here,
when we look up JoeSchmoe's number 123-4567, the SUBSTD line
will be sent to the browser as a line reading
THENUMBER for JoeSchmoe is 123-4567
For SELECT * FROM PHONEBOOK we probably want
<TABLE BORDER=1>
$$SUBST:$: <TR><TH>$FieldName1$</TH><TH>$FieldName2$</TH></TR>
$$SUBSTD*:$:<TR><TD>$FieldValue1$</TH><TH>$FieldValue2$</TH></TR>
</TABLE>
*/
public class RowSubst {
RowSequence theRows; Env theInitEnv; Logger lg;
String theFileName; BufferedReader theInput;
PrintWriter theOutput; boolean dataFailed;
String theCommandPrefix="$$SUBST";
String SUBST="";
String SUBSTD="D";
String SUBSTDERR="DERR";
String SUBSTDSTAR="D*";
public RowSubst(String fName,RowSequence rows,PrintWriter out) {
this(newReader(fName),rows,out);
theFileName=fName;
}
public static BufferedReader newReader(String fName){
BufferedReader bR;
try{bR=new BufferedReader(new FileReader(fName));}
catch(FileNotFoundException Ex){return null;}
return bR;
}
public RowSubst(BufferedReader brin,RowSequence rows,PrintWriter out) {
theInput=brin; theOutput=out; theRows=rows;
theInitEnv=rows.getRow();
// lg=new Logger("C:\\MyNa\\rs.log"); lg.clearLog();
dataFailed=false;
}
public String interpretLine(String L){
if(L==null)return "";
if(!L.startsWith(theCommandPrefix))return L;
try{
int cmdBegin=theCommandPrefix.length();
int cmdEnd=L.indexOf(':');
int delimEnd=L.indexOf(':',1+cmdEnd);
String cmd=L.substring(cmdBegin,cmdEnd);
String delim=L.substring(1+cmdEnd,delimEnd);
L=L.substring(1+delimEnd,L.length());
if(SUBST.equals(cmd)){
return Misc.stringDelimSubst(L,delim,theInitEnv);
} else if (SUBSTD.equals(cmd)){
if(theRows.next())
return Misc.stringDelimSubst(L,delim,theRows.getRow());
else {dataFailed=true; return "";}
} else if (SUBSTDERR.equals(cmd)){
if(!dataFailed)return "";
else return Misc.stringDelimSubst(L,delim,theInitEnv);
}else if (SUBSTDSTAR.equals(cmd)){
String S="";
while(theRows.next())
S+=Misc.stringDelimSubst(L,delim,theRows.getRow())+"\n";
return S;
} else return L;
}catch(Exception E){}
return L;
}
public void interpret(){
String nextLine;
if(theInput==null)return;
try{
while(null!=(nextLine=theInput.readLine()))
theOutput.println(interpretLine(nextLine));
theInput.close();
// note that theOutput _may_ continue with other stuff.
}catch(IOException e)
{try{theInput.close();}catch(IOException ex){}}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -