📄 docwalker.java
字号:
package MyNa.xml;
import MyNa.utils.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;
import java.net.URL;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Attr;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.sun.xml.parser.Resolver;
import com.sun.xml.tree.XmlDocument;
import org.w3c.dom.Element;
public class DocWalker {
/* An DocWalker is started up empty; it takes input
commands from an Env, and it has a Node which
is an Xml node; the usual interface is doCommand,
which tries to execute a command and return a
summary value--an error message if appropriate, or
the (new) node's name and type.
If theNode has a child, and we call on doCommand where
defs.getStr("Command") is "firstChild",
then afterwards theNode is that first child and the
result returned by doCommand will be name:type. If
there is no such child, theNode will be unchanged
but the result will be an error message.
In general, its response is to modify theNode and
the Env; it also has a writeNode method which can
be used to show the current node's content.
defs.getStr("Error") is an error message. "firstChild"
works just like parentNode, nextSibling, previousSibling,
and lastChild; we also have reset which sets the node
to whatever it was set to by the most recent parseFile
(with or without validation). DocWalker is used in the
DocWalkerServlet class which produces
HTML output for a dataframe.
*/
Logger lg;
Env defs;
Node theNode;
Node originalNode; // used in reset() to restore original.
public DocWalker(){
lg=new Logger();
defs=new Env();
theNode=originalNode=null;
lg.logIt("DocWalker default initialization");
}
public void setDefs(Env e){defs=e;}
public Env respond(Env E){
defs.addHashtable(E);
return defs;
}
public String addError(String S){
defs.put("Error",S);
return S;
}
public String doCommand(){
String cmd=defs.getStr("Command");
if(null==cmd)return addNodeData("No 'Command' given");
if(cmd.equals("parseFile"))
return initFile(defs.getStr("theFile"),false);
if(cmd.equals("parseValidateFile"))
return initFile(defs.getStr("theFile"),true);
if(cmd.equals("firstChild"))return getFirstChild();
if(cmd.equals("parentNode"))return getParentNode();
if(cmd.equals("nextSibling"))return getNextSibling();
if(cmd.equals("previousSibling"))return getPreviousSibling();
if(cmd.equals("lastChild"))return getLastChild();
if(cmd.equals("reset"))return reset();
return addNodeData("Command '"+cmd+"' not understood");
}
public String addNodeData(Node theNode,String errMess){
// sets Error, nodeName,nodeType,status in defs;
if(null==theNode)
return addErrNameTypeStat("Null Node","NULL","NULL","NULLSTATUS");
if(null==errMess)errMess="";
String theName=theNode.getNodeName();
String theType=getNodeTypeName(theNode);
String theStatus=(0==errMess.length())?"OKSTATUS":"ERRSTATUS";
return addErrNameTypeStat(errMess,theName,theType,theStatus);
}
public String addNodeData(Node theNode){
return addNodeData(theNode,"");
}
public String addNodeData(String err){
return addNodeData(theNode,err);
}
public String addErrNameTypeStat(String errMess, String theName,
String theType,String theStatus){
defs.put("Error",errMess);
defs.put("nodeName",theName);
defs.put("nodeType",theType);
defs.put("status",theStatus);
if(0==errMess.length())return theType;
return errMess;
}
public String getFirstChild(){
if(null==theNode)return addNodeData("cannot getFirstChild of null");
Node kid=theNode.getFirstChild();
if(null==kid)return addNodeData("cannot getFirstChild of node");
theNode=kid;
return addNodeData(theNode);
}
public String getParentNode(){
if(null==theNode)return addNodeData("cannot getParentNode of null");
Node kid=theNode.getParentNode();
if(null==kid)return addNodeData("cannot getParentNode of node");
theNode=kid;
return addNodeData(theNode);
}
public String getNextSibling(){
if(null==theNode)return addNodeData("cannot getNextSibling of null");
Node kid=theNode.getNextSibling();
if(null==kid)return addNodeData("cannot getNextSibling of node");
theNode=kid;
return addNodeData(theNode);
}
public String getPreviousSibling(){
if(null==theNode)return addNodeData("cannot getPreviousSibling of null");
Node kid=theNode.getPreviousSibling();
if(null==kid)return addNodeData("cannot getPreviousSibling of node");
theNode=kid;
return addNodeData(theNode);
}
public String getLastChild(){
if(null==theNode)return addNodeData("cannot getLastChild of null");
Node kid=theNode.getLastChild();
if(null==kid)return addNodeData("cannot getLastChild of node");
theNode=kid;
return addNodeData(theNode);
}
public String reset(){
theNode=originalNode;
return addNodeData(theNode);
}
// the core strategy is to echo an xml document; then echo with changes.
// here are the node types we have to deal with
static final String[]NODETYPENAMES=
{"ELEMENT_NODE","ATTRIBUTE_NODE","TEXT_NODE","CDATA_SECTION_NODE",
"ENTITY_REFERENCE_NODE","ENTITY_NODE","PROCESSING_INSTRUCTION_NODE",
"COMMENT_NODE","DOCUMENT_NODE","DOCUMENT_TYPE_NODE",
"DOCUMENT_FRAGMENT_NODE","NOTATION_NODE"};
public String getNodeTypeName(Node theNode){
if(null==theNode)return "null node has no type";
short nT=theNode.getNodeType();
if(0<nT && nT<=NODETYPENAMES.length)
return NODETYPENAMES[nT-1];
return "invalid Nodetype "+nT+" for node";
}
public String getNodeTypeNameSwitcher(Node theNode){
if(null==theNode)return "null node has no type";
short code=theNode.getNodeType();
switch(code){
case Node.ATTRIBUTE_NODE:
return "Node.ATTRIBUTE_NODE";
case Node.CDATA_SECTION_NODE:
return "Node.CDATA_SECTION_NODE";
case Node.COMMENT_NODE:
return "Node.COMMENT_NODE";
case Node.DOCUMENT_FRAGMENT_NODE:
return "Node.DOCUMENT_FRAGMENT_NODE";
case Node.DOCUMENT_NODE:
return "Node.DOCUMENT_NODE";
case Node.DOCUMENT_TYPE_NODE:
return "Node.DOCUMENT_TYPE_NODE";
case Node.ELEMENT_NODE:
return "Node.ELEMENT_NODE";
case Node.ENTITY_NODE:
return "Node.ENTITY_NODE";
case Node.ENTITY_REFERENCE_NODE:
return "Node.ENTITY_REFERENCE_NODE";
case Node.NOTATION_NODE :
return "Node.NOTATION_NODE";
case Node.PROCESSING_INSTRUCTION_NODE:
return "Node.PROCESSING_INSTRUCTION_NODE";
case Node.TEXT_NODE:
return "Node.TEXT_NODE";
default: return "invalid Nodetype "+code+" for node";
}
}
/* BEGIN basic echo functionality: */
public void echoChildren(Node theFirst, Writer out)
throws IOException {
while(null!=theFirst){
echoNode(theFirst,out);
theFirst=theFirst.getNextSibling();
}
}
public void echoDTD(DocumentType theDTD,String theName,Writer out)
throws IOException {
// sun implementation does not provide full access, so we just
// produce the name.
out.write("<!DOCTYPE "); out.write(theName);
out.write(" />\n");
}
public void echoAttributes(NamedNodeMap attrs,Writer out)
throws IOException {
int N=attrs.getLength();
for(int i=0; i<N; i++)echoNode(attrs.item(i),out);
}
public void echoNode(Node theNode,Writer out)
throws IOException {
if(null==theNode){lg.logIt("echoNode null");return;}
else lg.logIt("echoNode "+theNode.getNodeName()+"; type "+getNodeTypeName(theNode));
short code=theNode.getNodeType();
switch(code){
case Node.ATTRIBUTE_NODE:
Attr at=(Attr)theNode;
out.write(" ");
out.write(at.getName());
out.write("='");
out.write(at.getValue());
out.write("'");
return;
case Node.CDATA_SECTION_NODE:
out.write("<CDATA[[");
out.write(theNode.getNodeValue());
out.write("]]>");
return;
case Node.COMMENT_NODE:
out.write("<!-- ");
out.write(theNode.getNodeValue());
out.write("-->\n");
return;
case Node.DOCUMENT_FRAGMENT_NODE: // we can't reconstitute this; dump it
case Node.DOCUMENT_NODE:
echoChildren(theNode.getFirstChild(),out);
return;
case Node.DOCUMENT_TYPE_NODE:
DocumentType dT=(DocumentType)theNode;
echoDTD(dT,dT.getName(),out);
return;
case Node.ELEMENT_NODE:
out.write("<"); out.write(theNode.getNodeName());
echoAttributes(theNode.getAttributes(),out);
Node kid=theNode.getFirstChild();
if(kid==null) out.write("/>\n");
else {
out.write(">");
echoChildren(kid,out);
out.write("</");
out.write(theNode.getNodeName());
out.write(">\n");
}
return;
case Node.ENTITY_NODE:
out.write("<ENTITY: "); out.write(theNode.getNodeName());
out.write(">"); // out.write(theNode.getNodeValue());
out.write("</"); out.write(theNode.getNodeName());
out.write(">\n");
return;
case Node.ENTITY_REFERENCE_NODE:
out.write("<ENTITY_REF: "); out.write(theNode.getNodeName());
out.write("/>\n");
return;
case Node.NOTATION_NODE :
out.write("<NOTATION: "); out.write(theNode.getNodeName());
out.write("/>\n");
return;
case Node.PROCESSING_INSTRUCTION_NODE:
out.write("<? "); out.write(theNode.getNodeName());
out.write(" "); out.write(theNode.getNodeValue());
out.write(" ?>\n");
return;
case Node.TEXT_NODE:
out.write(theNode.getNodeValue());
return;
default:return;
}
}
public String writeNode() throws IOException{
StringWriter sW=new StringWriter();
PrintWriter pW=new PrintWriter(sW);
try {
echoNode(theNode,pW);
} catch (IOException t) {t.printStackTrace (pW);}
return sW.toString();
}
public String initFile(String fileName,boolean validate)
{
InputSource input;
XmlDocument doc;
if(null==fileName)return addNodeData("cannot initFile of null");
theNode=originalNode=null;
try{
try {
lg.logIt("initFile "+fileName);
if(fileName.startsWith("http:")){
URL uri=new URL(fileName);
lg.logIt("got URL");
input=Resolver.createInputSource(uri,true);
}else {
File inFile=new File(fileName);
lg.logIt("got file");
input=Resolver.createInputSource(inFile);
}
doc = XmlDocument.createXmlDocument (input, validate);
theNode=originalNode=doc;
lg.logIt("initFile succeeded");
return addNodeData(theNode);
} catch (SAXParseException err) {
lg.logIt ("** Parsing error"
+ ", line " + err.getLineNumber ()
+ ", uri " + err.getSystemId ()
+ "\n");
lg.logIt(" " + err.getMessage () + "\n");
return addNodeData(""+err);
} catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
return addNodeData(""+e);
}
} catch (IOException t) {
t.printStackTrace (); return addNodeData(""+t);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -