Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Financial Engineering Project Course Summer A-2000, Project Course-Carnegie Mellon University 1 Lecture 6 • Java Exceptions (Definitions from JavaSoft) • Validation revisited • A “Real” FpML document – Vanilla FixedFloat Swap • A “Real” FpML DTD – fpml.dtd • Getting the document and dtd from a server in Java • Results Summer A-2000, Project Course-Carnegie Mellon University 2 Exceptions--Definitions exception An event during program execution that prevents the program from continuing normally; generally, an error. The Java programming language supports exceptions with the try, catch, and throw keywords. exception handler A block of code that reacts to a specific type of exception. If the exception is for an error that the program can recover from, the program can resume executing after the exception handler has executed. Summer A-2000, Project Course-Carnegie Mellon University 3 Exceptions-- Definitions catch A Java programming language keyword used to declare a block of statements to be executed in the event that a Java exception, or run time error, occurs in a preceding "try" block. throw A Java programming language keyword that allows the user to throw an exception or any class that implements the "throwable" interface. Summer A-2000, Project Course-Carnegie Mellon University 4 Exceptions-- Definitions throws A Java programming language keyword used in method declarations that specify which exceptions are not handled within the method but rather passed to the next higher level of the program. try A Java programming language keyword that defines a block of statements that may throw a Java language exception. If an exception is thrown, an optional "catch" block can handle specific exceptions thrown within the "try" block. Also, an optional "finally" block will be executed regardless of whether an exception is thrown or not. Summer A-2000, Project Course-Carnegie Mellon University 5 Exceptions-- Definitions finally A Java programming language keyword that executes a block of statements regardless of whether a Java Exception, or run time error, occurred in a block defined previously by the "try" keyword. Summer A-2000, Project Course-Carnegie Mellon University 6 An exception may be thrown from inside the try block. General Form try { These statements are executed if an exception of a particular type occurs within the try block statement(s) } catch (exceptiontype name) { statement(s) } finally { statement(s) } These statements are executed regardless of whether or not an error occurs within the try block. Summer A-2000, Project Course-Carnegie Mellon University 7 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 8 An Invalid 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> <Notional>100</Notional> A second notional? </FixedFloatSwap> Validation errors do not necessarily cause exceptions. Summer A-2000, Project Course-9 Carnegie Mellon University Validating the Agreement.xml file // Imports as before public static void main(String args[]) { : : Summer A-2000, Project Course-Carnegie Mellon University 10 Validating the Agreement.xml file 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 11 docBuilder.setErrorHandler( Validation errors force a call on this method. The method new org.xml.sax.ErrorHandler() { may or may not throw an exception. 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()); This method has System.out.println(" " + err.getMessage()); chosen to throw an } exception. If it does } docBuilder makes use of ); the ErrorHandler object Document doc1 = docBuilder.parse(new File(argv[0])); Summer A-2000, Project Course-Carnegie Mellon University not throw one then the catch clause does not execute. 12 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()); } Unlike error, after fatalError Executed if the .dtd } file is not found or “SYSTEM” is not Document doc1 = docBuilder.parse(new File(argv[0])); spelled correctly, etc. ); is called an exception IS thrown by the parser. Summer A-2000, Project Course-Carnegie Mellon University 13 : : catch(SAXParseException err) { System.out.println("Catching raised exception"); System.out.println("Parsing error" + ", line " + err.getLineNumber() + ", URI " + err.getSystemId()); System.out.println(" " + err.getMessage()); } catch(SAXException e) { Exception x = e.getException(); ((x == null) ? e : x).printStackTrace(); } catch (Throwable t) { t.printStackTrace(); Summer A-2000, Project Course-} Carnegie Mellon University This catch clause catches the exception and the program terminates by finishing main. 14 Vanilla Fixed-Float Swap • See the file vanillaFixedFloat.xml from fpml.org • See the file vanillaFixedFloat.dtd from fpml.org Summer A-2000, Project Course-Carnegie Mellon University 15 A Java Program that reads fpml from a server and performs validation against the server based DTD. The program then displays the fpml file by traversing the DOM tree. Summer A-2000, Project Course-Carnegie Mellon University 16 import org.xml.sax.*; public class Simulator6 { public static void main(String argv[]) { try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Summer A-2000, Project Course-Carnegie Mellon University 17 docBuilder.setErrorHandler( new org.xml.sax.ErrorHandler() { public void fatalError(SAXParseException e) throws SAXException { System.out.println("Fatal error"); // an exception will be thrown } public void error(SAXParseException e) throws SAXParseException { System.out.println("Validity error"); throw e; } Summer A-2000, Project Course-Carnegie Mellon University 18 public void warning(SAXParseException err) throws SAXParseException { System.out.println("** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); throw err; } } ); Summer A-2000, Project Course-Carnegie Mellon University 19 InputSource is = new InputSource("http://hempel.heinz.cmu.edu/fpml/vanillaFixedFloat.xml"); Document doc = docBuilder.parse(is); System.out.println("No Problems found"); TreePrinter tp = new TreePrinter(doc); tp.print(); } Summer A-2000, Project Course-Carnegie Mellon University 20 catch(SAXParseException err) { System.out.println("Catching raised exception"); System.out.println("Parsing error" + ", line " + err.getLineNumber() + ", URI " + err.getSystemId()); System.out.println(" " + err.getMessage()); } catch(SAXException e) { System.out.println("Catch clause 2"); Exception x = e.getException(); ((x == null) ? e : x).printStackTrace(); } catch (Throwable t) { System.out.println("Catch clause 3"); t.printStackTrace(); } System.exit(0); } } Summer A-2000, Project Course-Carnegie Mellon University 21 TreePrint Class import org.w3c.dom.*; public class TreePrinter { private Document doc; private int currentIndent; public TreePrinter(Document d) { currentIndent = 2; doc = d; } public void print() { privatePrint(doc,currentIndent); } } Summer A-2000, Project Course-Carnegie Mellon University 22 public void privatePrint(Node n, int indent) { for(int i = 0; i < indent; i++) System.out.print(" "); switch( n.getNodeType()) { // Print information as each node type is encountered case n.DOCUMENT_NODE : System.out.println(n.getNodeName() + "...Document Node") break; case n.ELEMENT_NODE : System.out.println(n.getNodeName() + "...Element Node"); break; case n.TEXT_NODE : System.out.println(n.getNodeName() + "...Text Node"); break; case n.CDATA_SECTION_NODE: System.out.println(n.getNodeName() + "...CDATA Node"); break; case n.PROCESSING_INSTRUCTION_NODE: System.out.println("<?"+n.getNodeName()+"...?>"+ "...PI Node"); break; Summer A-2000, Project Course-Carnegie Mellon University 23 case n.COMMENT_NODE: System.out.println("<!--"+n.getNodeValue()+"-->" + "...Comment node"); break; case n.ENTITY_NODE: System.out.println("ENTITY "+ n.getNodeName()+ "...Entity Node"); break; case n.ENTITY_REFERENCE_NODE: System.out.println("&"+n.getNodeName()+";" + "...Entity Reference Node"); break; case n.DOCUMENT_TYPE_NODE: System.out.println("DOCTYPE"+n.getNodeName()+ "...Document Type Node"); break; default: System.out.println("?" + n.getNodeName()); } Node child = n.getFirstChild(); while(child != null) { privatePrint(child, indent+currentIndent); child = child.getNextSibling(); } } } Summer A-2000, Project Course-Carnegie Mellon University 24 See the Output of the program in Results.txt Summer A-2000, Project Course-Carnegie Mellon University 25 Homework Read and validate the vanillaFixedFloat.xml file from my server “hempel.heinz.cmu.edu”. Perform some simple financial processing on the data and print the results. Turn in all of the code and printouts by the end of class next week. The processing need not be difficult. Simply demonstrate that you can read, validate and compute with financial data from the web. Summer A-2000, Project Course-Carnegie Mellon University 26