XML Script Interpreter Using DOM API (HW3) & SAX2 API (HW4)
Develop a Java interpreter to run XML script files that define variables and functions as per the provided DTD. Complete the missing parts in HW3.java and create HW4.java using SAX2 API.
XML Script Interpreter Using DOM API (HW3) & SAX2 API (HW4)
E N D
Presentation Transcript
Homework 3 & 4 • Reference DTD (xmath.dtd): • http://xml.cs.ncuc.edu.tw/courses/xmlta/xmlta2008/HWs/hw34/xmath.dtd • Reference sample xmath script: • http://xml.cs.ncuc.edu.tw/courses/xmlta/xmlta2008/HWs/hw34/script1.xmath • The above dtd defines a simple script language for defining double variables and single-variable polynomial function.
Sample script : Script1.xml • non-xml version: a = -2 define f(x) = 3x^2 - 5x + 7 define g(x) = 3x^3 -5x^2 +2 combine h = f + g list functions list variables print "The value of f(1) is" f(1) "." print "The value of f(a) is " f(a) "." print "The derivative of g' is " g' "."
Expected output a = -2.0 f(x) = 3x^2 - 5x + 3 g(y) = 3y^3 - 5y^2 + 2 h(x) = 3x^3 - 2x^2 - 5x + 5 The value of f(1) is 1.0. The value of h(a) is -17.0. The derivative of g is g'(y) = 9y^2 - 10y.
Script1.xml : XMLVersion <?xml version="1.0" ?> <script> <assign var="a"><number>-2</number></assign> <define name="f" var="x" > <polynomial var="x" > <term c="3" exp="2" /> <term c="-5" exp="1" /> <term c="3" exp="0" /> </polynomial> </define> <define name="g" var="y" > <polynomial var="y" > <term c="3" exp="3" /> <term c="-5" exp="2" /> < term c="2" exp="0" /> </polynomial></define>
<combine name="h" type="add" fun1="f" fun2="g" /> <list option="variables" /> <list option="functions" /> <print> <literal>The value of f(1) is </literal> <funEval name="f" ><number>1</number></funEval> <literal>.</literal> </print> <print><literal>The value of h(a) is </literal> <funEval name="h"><varRef>a</varRef></funEval> <literal>.</literal></print> <print><literal>The derivative of g is </literal> <derivative>g</derivative><literal>.</literal></print> </script>
HW3 • HW3 : • This homework is about using DOM API to write a Java interpreter that can load a script file in XML format that are valid w.r.t the previous DTD, and then execute it and produce correct result. • We have prepare an almost-completed program HW3.java for you. • Read carefully and try to understand how this program works. • Complete all missing part in HW3.java
HW4 • HW4 : • Same as HW3, but this time use SAX2 API to write a Java program named HW4.java that can load a script file in XML format that are valid w.r.t the previous DTD and then run it and produce correct result. • Your HW4.java file should contain a main public HW4 class (which contains the main(String[]) method) as well as whatever non-public classes like content handler, error handler are needed in your HW4 class. • You can use the template HW4.java I prepared for you.
Notes • We have prepared 3 classes for representing polynomial, terms and functions, respectively. • see http://xml.cs.nccu.edu.tw/courses/xmlta/xmlta2008/hws/hw34/src/xmlta/xmath" • To help grading, please remember that your main java classes should be named as HW3 and HW4, respectively. Both should have a main(String[]) method accepting as input an input file name. So we can invoke them as follows: >java HW3 script1.xmath >java HW4 script2.xmath • I have packaged all related files into one eclipse project hw34.zip, which you can download and import it into your local eclipse workspace. • After completing your homework, you need just submit HW3.java and HW4.java to c3@cs.ncuc.edu.tw.