📄 saxminilanguage.java
字号:
package MyNa.xml;
import MyNa.utils.Env;
import MyNa.utils.Logger;
import java.io.*;
import java.util.Stack;
import java.util.Hashtable;
import java.util.Enumeration;
import java.net.*;
import org.xml.sax.*;
import org.xml.sax.helpers.ParserFactory;
import com.sun.xml.parser.Resolver;
public class SAXMiniLanguage extends HandlerBase {
private PrintWriter out;
Stack theCallStack=null;
Stack theValStack=null;
String theFileName=null;
Env theEnv=null;
HandlerSet theHandlerSet; // keeps track of end-handlers for tagnames.
Logger lg;
String parserName="com.sun.xml.parser.Parser";
public void setEnv(Env e){theEnv=e;}
public void setParserName(String S){parserName=S;}
public SAXMiniLanguage (String fileName){
lg=new Logger(); lg.logIt("inside SAXMiniLanguage with "+fileName);
theCallStack=new Stack(); theValStack=new Stack();
theFileName=fileName;
Hashtable H=new Hashtable();
lg.logIt("hashtable okay");
theEnv=new Env(); // out=new PrintWriter(outW);
lg.logIt("new env okay");
theHandlerSet=new HandlerSet();
lg.logIt("new handlerset okay");
}
public SAXMiniLanguage(){this("default.xml");}
public SAXMiniLanguage(Env e){
this();
setEnv(e);
theFileName=e.getStr("theFileName");
}
public void doIt(){
try{
InputSource in=createInputSource();
Parser parser=ParserFactory.makeParser(parserName);
parser.setDocumentHandler(this);
parser.parse(in);
}catch (SAXParseException t){lg.logIt("saxparserr ",t);
lg.logIt("error at "+t.getLineNumber ());
t.printStackTrace();}
catch (Throwable t){lg.logIt("initerr ",t);
t.printStackTrace();}
}
public InputSource createInputSource()throws Exception{
String inType=theEnv.getStr("theInputSourceType");
if("String".equals(inType)){
String S=theEnv.getStr("theXMLString");
InputSource in=new InputSource(new StringReader(S));
S=theEnv.getStr("theSystemID");
if(null!=S)in.setSystemId(S);
return in;
}else if("Socket".equals(inType)){
String theHostName=theEnv.getStr("theHostName");
if(null==theHostName)theHostName="127.0.0.1";
int thePort=theEnv.getInt("thePort",56789);
Socket theSocket=new Socket(theHostName,thePort);
String ct=theEnv.getStr("theContentType");
if(null==ct)ct="text/xml;charset=utf-8";
String protocol=theEnv.getStr("theProtocol"); //null ok.
InputStream is=theSocket.getInputStream();
InputSource in=Resolver.createInputSource(ct,is,true,protocol);
String S=theEnv.getStr("theSystemID");
lg.logIt("system ID="+S);
if(null!=S)in.setSystemId(S);
return in;
}else if(theFileName.startsWith("http:"))
return Resolver.createInputSource(new URL(theFileName),true);
else return Resolver.createInputSource(new File(theFileName));
}
public void OlderdoIt(){
InputSource in=null;
try{
if(theFileName.startsWith("http:")){
URL uri=new URL(theFileName);
lg.logIt("got URL");
in=Resolver.createInputSource(uri,true);
}else {
File inFile=new File(theFileName);
lg.logIt("got file");
in=Resolver.createInputSource(inFile);
}
lg.logIt("got inputSource");
Parser parser=ParserFactory.makeParser("com.sun.xml.parser.Parser");
parser.setDocumentHandler(this);
parser.parse(in);
}catch (SAXParseException t){lg.logIt("saxparserr ",t);
lg.logIt("error at "+t.getLineNumber ());
t.printStackTrace();}
catch (Throwable t){lg.logIt("initerr ",t);
t.printStackTrace();}
}
public void startDocument ()throws SAXException {
lg.logIt("startDocument");
}
public void endDocument () throws SAXException {
lg.logIt("endDocument");
}
public void startElement (String tag, AttributeList attrs)
throws SAXException {
lg.logIt("startElement "+tag+": "+attrs.getLength()+" attrs");
theCallStack.push(new CallStackItem(tag,attrs));
}
public void endElement (String tag) throws SAXException {
lg.logIt("endElement "+tag);
theHandlerSet.get(tag).doIt();
}
public void characters (char buf [], int offset, int len)
throws SAXException {
String s = new String(buf, offset, len);
lg.logIt("chars: "+s);
theValStack.push(s);
CallStackItem cSI=(CallStackItem)(theCallStack.peek());
cSI.kidNum+=1;
}
// Wrap I/O exceptions in SAX exceptions, to
// suit handler signature requirements
/*
private void emit (String s) throws SAXException {
try {
out.write (s); out.flush ();
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
private void nl () throws SAXException {
try {out.write (lineEnd);
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
*/
public class CallStackItem{ // helper/holder class, conceals nothing
public String tagName; // the <tagname> for the xml item
public String name; // its name="nameAttr" if present, else tagName
public int kidNum; // number of children on valStack
public Hashtable attrs; // attributes, not including name
public CallStackItem(String tag,AttributeList aL){
kidNum=0; tagName=tag; attrs=null;
int N=aL.getLength(); int nonName = N;
name=aL.getValue("name");
if(name==null)name=tag; else nonName--;
if(nonName==0)return;
attrs=new Hashtable(nonName);
for(int i=0;i<N;i++){
String key=aL.getName(i); if("name".equals(key))continue;
String val=aL.getValue(i);
attrs.put(key,val);
}
}
} // end CallStackItem helper class
class HandlerSet{
Hashtable table; EndHandler defaultHandler;
public HandlerSet(){
defaultHandler=new EndHandler();
table=new Hashtable();
/* table.put("row",new EndRowHandler());
table.put("user-info",new EndUser_Info());
table.put("header",new EndHeaderHandler());
lg.logIt("got through end, row, userinfo, header");
table.put("dbdata",new EnddbDataHandler());
table.put("table",new EndTableHandler());
table.put("headers",new EndHeadersHandler());
lg.logIt("handlerset finished");
*/
}
public void put(String tagName,EndHandler eH){
table.put(tagName,eH);
}
public EndHandler get(String tagName){
EndHandler eH=(EndHandler)table.get(tagName);
if(eH==null)return defaultHandler;
return eH;
}
}
public class EndHandler {
// an object which will handle a /tagname, popping off stacks.
// the default is to add attrs to theEnv except for name;
// name is associated with value, i.e. item from theValStack,
// if exactly one. override doIt() for each special /tagname.
public EndHandler(){} // nothing to do in initialization
public void popAll(CallStackItem cSI){
lg.logIt("popping "+cSI.kidNum+" from valstack");
for(int N=cSI.kidNum;N>0;N--)theValStack.pop();
}
public void pushVal(Object v){
theValStack.push(v);
((CallStackItem)theCallStack.peek()).kidNum++;
}
public void assocVal(CallStackItem cSI){
theEnv.put(cSI.name,(String)theValStack.peek());
lg.logIt("theEnv."+cSI.name+"=="+((String)theValStack.peek()));
}
public void addAttrsToEnv(CallStackItem cSI){
Hashtable H=cSI.attrs;
lg.logIt("addAttrsToEnv: "+((H==null)?"null":(""+(H.size()))));
if(H!=null) theEnv.addHashtable(H);
lg.logIt("added hashtable");
lg.logIt("Env=["+theEnv.toString()+"]");
}
public void doIt() throws SAXException { // default;
// does not leave any result on the valStack.
CallStackItem cSI=(CallStackItem)theCallStack.pop();
lg.logIt("EndHandler doIt on "+cSI.name+"; kidnum="+cSI.kidNum);
if(cSI.kidNum==1)assocVal(cSI);
addAttrsToEnv(cSI);
popAll(cSI);
lg.logIt("ended endHandler on "+cSI.name);
}
}
} // end SAXMiniLanguage class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -