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

📄 xmlaritheval.java

📁 java xml开发指南(初学者推荐)Java Xml 编程指南书籍源码
💻 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 org.xml.sax.*;
import org.xml.sax.helpers.ParserFactory;
import com.sun.xml.parser.Resolver;


import java.sql.*;   // communicate with database


public class XmlArithEval extends SAXMiniLanguage {
   // the doIt method of each is called in turn; 
   // at the end, that of arithexp is called and
   // we get floatValue set.
   String floatResult="";
public String getFloatResult(){return floatResult;}

public XmlArithEval (String fileName){
   super(fileName);
   initHandlers();
}
public XmlArithEval (Env E){
   super(E);
   initHandlers();
}
   
public void initHandlers(){
    theHandlerSet.put("num",new EndNumHandler());
    theHandlerSet.put("add",new EndAddHandler());
    theHandlerSet.put("mul",new EndMulHandler());
    theHandlerSet.put("div",new EndDivHandler());
    theHandlerSet.put("sub",new EndSubHandler());
    theHandlerSet.put("arithexp",new EndArithExpHandler());
   lg.logIt("handlers placed");
}

class EndOpHandler extends EndHandler{ 

 // just to define this function, shared by add,sub,div,mul:

public void floatOp(String op) throws SAXException{
  CallStackItem cSI=(CallStackItem)theCallStack.pop();
  lg.logIt("EndHandler for "+op+" on "+cSI.name+"; kidnum="+cSI.kidNum);
  int kidNum=cSI.kidNum;  // e.g., 2 for binary operation
  float arg2=((Float)theValStack.pop()).floatValue();
  for(int i=1;i<kidNum;i++){ // just once for binop.
    float arg1=((Float)theValStack.pop()).floatValue();
    if("add".equals(op))arg2=(arg1+arg2);
    else if("mul".equals(op))arg2=(arg1*arg2);
    else if("sub".equals(op))arg2=(arg1-arg2);
    else if("div".equals(op))arg2=(arg1/arg2);
    else throw new SAXException("ArithExp error, op=='"+op+"'");
    }
  pushVal(new Float(arg2));
}
}
class EndNumHandler extends EndHandler{
public void doIt() throws SAXException {
  CallStackItem cSI=(CallStackItem)theCallStack.pop();
  lg.logIt("EndNumHandler for "+cSI.name+"; kidnum="+cSI.kidNum);
  String floatRep=(String)theValStack.pop();
  Float f;
  try{
  f=new Float(floatRep);
  }catch(Exception ex){
    throw new SAXException("invalid float '"+floatRep+"'");
  }
  pushVal(f);
  }
}
class EndAddHandler extends EndOpHandler{
public void doIt() throws SAXException {floatOp("add");}
}
class EndMulHandler extends EndOpHandler{
public void doIt() throws SAXException {floatOp("mul");}
}
class EndSubHandler extends EndOpHandler{
public void doIt() throws SAXException {floatOp("sub");}
}
class EndDivHandler extends EndOpHandler{
public void doIt() throws SAXException {floatOp("div");}
}
class EndArithExpHandler extends EndHandler{
public void doIt() throws SAXException {
  CallStackItem cSI=(CallStackItem)theCallStack.pop();
  lg.logIt("EndArithExpHandler for "+cSI.name+"; kidnum="+cSI.kidNum);
  floatResult=((Float)theValStack.pop()).toString();
}
}


} // end XmlArithEval

⌨️ 快捷键说明

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