300 likes | 425 Views
Financial Engineering Project Course. Lecture 5. Validation against a DTD More Java Details Validating the swap agreement using Sun’s JAXP API. Checking the Structure of an XML document with an XML parser. Document. Valid XML. XML Rules Checker (Parser). Structure Rules
E N D
Financial Engineering Project Course Summer A-2000, Project Course--Carnegie Mellon University
Lecture 5 • Validation against a DTD • More Java Details • Validating the swap agreement using Sun’s • JAXP API Summer A-2000, Project Course--Carnegie Mellon University
Checking the Structure of an XML document with an XML parser Document Valid XML XML Rules Checker (Parser) Structure Rules (DTD) Invalid XML Summer A-2000, Project Course--Carnegie Mellon University
Operation of a Tree-based Parser XML DTD Document Tree Tree-Based Parser Application Logic Valid XML Document Summer A-2000, Project Course--Carnegie Mellon University
The Agreement.xml file <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"> <FixedFloatSwap> <Notional>100</Notional> <Fixed_Rate>5</Fixed_Rate> <NumYears>3</NumYears> <NumPayments>6</NumPayments> </FixedFloatSwap> This document declares itself as conforming to this dtd. Summer A-2000, Project Course--Carnegie Mellon University
The FixedFloatSwap.dtd <?xml version="1.0" encoding="utf-8"?> <!ELEMENT FixedFloatSwap (Notional, Fixed_Rate, NumYears, NumPayments) > <!ELEMENT Notional (#PCDATA) > <!ELEMENT Fixed_Rate (#PCDATA) > <!ELEMENT NumYears (#PCDATA) > <!ELEMENT NumPayments (#PCDATA) > Summer A-2000, Project Course--Carnegie Mellon University
The FixedFloatSwap.dtd <?xml version="1.0" encoding="utf-8"?> <!ELEMENT FixedFloatSwap (Notional, Fixed_Rate, NumYears, NumPayments) > A document that conforms to this dtd must have these elements in this order. Summer A-2000, Project Course--Carnegie Mellon University
The FixedFloatSwap.dtd The tag names in the xml document. The content of each element <!ELEMENT Notional (#PCDATA) > <!ELEMENT Fixed_Rate (#PCDATA) > <!ELEMENT NumYears (#PCDATA) > <!ELEMENT NumPayments (#PCDATA) > Summer A-2000, Project Course--Carnegie Mellon University
Before we validate we have to cover some more Java • Inner classes • Exception Handling Summer A-2000, Project Course--Carnegie Mellon University
Inner Classes • Nested Top Level Classes (not inner) • Member Classes • Local Classes • Anonymous Classes Summer A-2000, Project Course--Carnegie Mellon University
Nested Top Level Class • Nested top-level classes are not inner classes. • Use as a convenient way to group related classes • Since the class must be static and has no 'this' pointer, it • has no access to the instance data of objects for its • enclosing class. • It behaves just like a 'normal' class or interface. Summer A-2000, Project Course--Carnegie Mellon University
//NestedTopLevelExample.java class Top { int i,j; static class SomeClass { // static makes it top-level nested int k; SomeClass() { System.out.println("Constructing SomeClass"); } void foo() { System.out.println("Hello"); } } Top() { System.out.println("Constructing a Top object"); } } Summer A-2000, Project Course--Carnegie Mellon University
public class NestedTopLevelExample { public static void main(String args[]) { Top myTop = new Top(); Top.SomeClass myObject = new Top.SomeClass(); myObject.foo(); } } Output Constructing a Top object Constructing SomeClass Hello Summer A-2000, Project Course--Carnegie Mellon University
Member Classes • Member classes (there is no such thing as a 'member‘ • interface) • This inner class (it's not a top-level class) has no static • keyword and can access the members of each object of • its outer class. • The class 'appears in every instance'. Summer A-2000, Project Course--Carnegie Mellon University
The parent class must declare an instance of an inner class, before it can invoke the inner class methods, assign to data fields (including private ones), and so on. • Unlike nested top-level classes, inner classes are not directly part of a package and are not visible outside the class in which they are nested. • Inner classes are often used for GUI event handlers. Summer A-2000, Project Course--Carnegie Mellon University
// MemberClassExample.java class Top { int i = 33; public class SomeClass { // access the outer object's state. private int k = i; SomeClass() { System.out.println("Constructing SomeClass"); } void foo() { System.out.println("Hello"); } } Top() { System.out.println("Constructing a Top object"); SomeClass sc = new SomeClass(); System.out.println(sc.k); } } Summer A-2000, Project Course--Carnegie Mellon University
public class MemberClassExample { public static void main(String args[]) { Top myObject = new Top(); } } // OUTPUT Constructing a Top object Constructing SomeClass 33 Summer A-2000, Project Course--Carnegie Mellon University
Local Classes • A Local class is an inner class. Typically, a local class • is declared within a method. It is not a member of an • enclosing class. It is visible only within the block. • These classes are used primarily as "adapter classes". • For example, a block of code that creates a Button object • could use a local class to define a simple implementation • of the ActionListener Interface. Then it could instantiate • this simple implementation and pass the resulting object • to the button's ActionListener method, thereby connecting • the button to the "callback" code that is executed when • the button is pressed. Summer A-2000, Project Course--Carnegie Mellon University
// Local Class example class Top { int i = 33; Top() { System.out.println("Constructing a Top object"); // define a class within a method class Wow { int t; Wow() { System.out.println("Building a Wow"); i = 8; t = 9; } } Wow h = new Wow(); System.out.println(" h.t == " + h.t); System.out.println(" i == " + i); } } Summer A-2000, Project Course--Carnegie Mellon University
public class LocalExample { public static void main(String args[]) { Top myObject = new Top(); } } // OUTPUT Constructing a Top object Building a Wow h.t == 9 i == 8 Summer A-2000, Project Course--Carnegie Mellon University
Anonymous Classes • An anonymous class is refinement of inner classes. • It allows you to combine the definition of the class • with the instance allocation. • Since it is instantiated in the same expression that defines • it, it can only be instantiated once. This is very similar to • local classes. • When writing a simple adapter class, the choice between • a named local class and an unnamed anonymous class • typically comes down to a matter of style and code clarity, • rather than any difference in functionality. • The new class can't have a constructor. Summer A-2000, Project Course--Carnegie Mellon University
// Anonymous.java interface SmallClass { public void foo(); } class Top { int i = 33; void someMethod(SmallClass s) { s.foo(); } void anotherMethod() { someMethod(new SmallClass() { public void foo() { System.out.println("Really fun"); } }); } Summer A-2000, Project Course--Carnegie Mellon University
Top() { System.out.println("Constructing a Top object"); someMethod(new SmallClass() { public void foo() { System.out.println("Strange but fun"); } }); } } Summer A-2000, Project Course--Carnegie Mellon University
public class Anonymous { public static void main(String args[]) { // We can't create interface objects // error: SmallClass s = new SmallClass(); Top myObject = new Top(); myObject.anotherMethod(); } } // OUTPUT Constructing a Top object Strange but fun Really fun Summer A-2000, Project Course--Carnegie Mellon University
Validating two Agreement.xml files import java.io.File; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; Summer A-2000, Project Course--Carnegie Mellon University
Validating two Agreement.xml files // imports as before Are both xml files on the command line? public class Simulator4 { public static void main(String argv[]) { if(argv.length != 2 ) { System.err.println("usage: java Simulator4” + “document1Name document2Name"); System.exit(1); } Summer A-2000, Project Course--Carnegie Mellon University
Validating two Agreement.xml files try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); This factory will produce parsers that validate! Summer A-2000, Project Course--Carnegie Mellon University
docBuilder.setErrorHandler( new org.xml.sax.ErrorHandler() { public void fatalError(SAXParseException e) throws SAXException {} public void error(SAXParseException e) throws SAXParseException { throw e; } public void warning(SAXParseException err) throws SAXParseException { System.out.println("** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); } } ); Document doc1 = docBuilder.parse(new File(argv[0])); Document doc2 = docBuilder.parse(new File(argv[1])); The new object to handle validation errors End of method call Summer A-2000, Project Course--Carnegie Mellon University
docBuilder.setErrorHandler( new org.xml.sax.ErrorHandler() { public void fatalError(SAXParseException e) throws SAXException {} public void error(SAXParseException e) throws SAXParseException { throw e; } public void warning(SAXParseException err) throws SAXParseException { System.out.println("** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); } } ); Document doc1 = docBuilder.parse(new File(argv[0])); Document doc2 = docBuilder.parse(new File(argv[1])); Validation errors caught here. Summer A-2000, Project Course--Carnegie Mellon University
Lab Exercise: I would like everyone to be checked off for writing A short Java program that validates the agreement.xml file against the FixedFloatSwap.dtd. If, for example, the notional tag appears twice in the xml file your program should display “oops” before terminating. Summer A-2000, Project Course--Carnegie Mellon University