Presentation is loading. Please wait.

Presentation is loading. Please wait.

95-733 Internet Technologies 1 Lecture 1 Introduction.

Similar presentations


Presentation on theme: "95-733 Internet Technologies 1 Lecture 1 Introduction."— Presentation transcript:

1 95-733 Internet Technologies 1 Lecture 1 Introduction

2 95-733 Internet Technologies 2 Course Web Site http://www.andrew.cmu.edu/~mm6

3 95-733 Internet Technologies 3 Course TA TBA

4 95-733 Internet Technologies 4 Structure of the Course Lectures / class participation Homeworks (programming) Final exam

5 95-733 Internet Technologies 5 Readings The required text is “XML and Java” Second edition Addison-Wesley Readings will be assigned each week. Readings from the web will also be assigned.

6 95-733 Internet Technologies 6 Grading Homework(3-4) 60% Final Exam 40%

7 95-733 Internet Technologies 7 Selected Course Topics DTD’s XSDL and JAXB XSLT XPath, Xlink Java Server Faces XML Encryption and XML Digital Signature RDF and RDFS OWL

8 95-733 Internet Technologies 8 Introduction XML and Java Chapter 1 Web Applications, XML, and Java

9 95-733 Internet Technologies 9 Introduction In this course we will be concerned with B2B applications. We will study how applications can send, receive, verify and manipulate XML documents. Definition: By Web Application we mean a distributed application based on Web technologies: HTTP, HTML, and the family of XML technologies.

10 10 Introduction Definition: By traditional three-tier application we mean applications consisting of: First tier – Browsers that act as a human interface Second tier – a program running in a web server Third tier – backend systems that provide databases and transaction services Let’s replace the browser with a program…

11 95-733 Internet Technologies 11 From web for eyeballs to web for programs Consider a PowerWarning application allows users To register their geographical position and their temperature concerns. Users will receive e-mail when the temperature exceeds the user specified parameters.

12 95-733 Internet Technologies 12 [1] [2] [3] Weather Report [4] [5] [6] Weather Report -- White Plains, NY [7] [8] Date/Time 11 AM EDT Sat Jul 25 1998 [9] Current Tem. 70° [10] Today’s High 82° [11] Today’s Low 62° [12] [13] [14] Suppose that we know that the weather information is available from the web at http://www.xweather.com/White_Plains_NY_US.html.

13 95-733 Internet Technologies 13 Strategy 1: For the current temperature of White Plains, go to line 9, column 46 of the page and continue until reaching the next ampersand. Strategy 2: For the current temperature of the White Plains, go to the first tag, then go to the second tag within the table, and then go to the second tag within the row. Neither of these seems very appealing…

14 95-733 Internet Technologies 14 <!DOCTYPE WeatherReport SYSTEM “http>//www.xweather.com/WeatherReport.dtd”> White Plains NY Sat Jul 25 1998 11 AM EDT 70 82 62 XML would help

15 95-733 Internet Technologies 15 Strategy 3: For the current temperature of White Plains, N.Y., go to the tag.

16 16 XML Mobile users PC users Http://www.xweather.com WeatherReport application WML HTML PowerWarning application Application programs XML Email notifications Registrations XML XSLT

17 95-733 Internet Technologies 17 XML Extensible Markup Language (really a meta- language) Generic syntax (not like HTML) Simple, human-readable tags From Web for eyeballs to Web for programs Tools exist that allows us to easily process any type of XML document

18 95-733 Internet Technologies 18 Introduction XML and Java Chapter 2 Basics of Parsing Documents

19 95-733 Internet Technologies 19 Department.xml John Doe John.Doe@foo.com Bob Smith Bob.Smith@foo.com

20 95-733 Internet Technologies 20 Alice Miller

21 21 SimpleParse.java // Download Xerces from http://xml.apache.org/xerces2-j/index.html // Two Jar files are needed- xercesImpl.jar and xmlParserAPIs.jar // add these to the classpath /** * From XML and Java Chapter 2 SimpleParse.java **/ import org.w3c.dom.Document; import org.apache.xerces.parsers.DOMParser; import org.xml.sax.SAXException; import java.io.IOException;

22 95-733 Internet Technologies 22 public class SimpleParse { public static void main(String[] argv) { if (argv.length != 1) { System.err.println( "Usage: java SimpleParse "); System.exit(1); } try { // Creates a parser object DOMParser parser = new DOMParser(); // Parses an XML Document parser.parse(argv[0]); // Gets a Document object Document doc = parser.getDocument();

23 95-733 Internet Technologies 23 } catch (SAXException se) { System.out.println("Parser error found: " +se.getMessage()); System.exit(1); } catch (IOException ioe) { System.out.println("IO error found: " + ioe.getMessage()); System.exit(1); } D:\McCarthy\www\95-733\examples\chap02>java SimpleParse department.xml D:\McCarthy\www\95-733\examples\chap02>

24 95-733 Internet Technologies 24 Let’s Introduce an Error John Doe John.Doe@foo.com Bob Smith Bob.Smith@foo.com This code is OK.

25 95-733 Internet Technologies 25 Alice Miller Missing closing tag on name. The document is not well-formed.

26 95-733 Internet Technologies 26 D:\McCarthy\www\95-733\examples\chap02>java SimpleParse department.xml [Fatal Error] department.xml:16:5: The element type "name" must be terminated by the matching end-tag " ". Parser error found: The element type "name" must be terminated by the matching e nd-tag " ". After displaying the error status an exception was throw to the application

27 95-733 Internet Technologies 27 Simple Validation

28 95-733 Internet Technologies 28 department-dtd.xml John Doe John.Doe@foo.com Bob Smith Bob.Smith@foo.com

29 95-733 Internet Technologies 29 Alice Miller

30 95-733 Internet Technologies 30 department.dtd

31 95-733 Internet Technologies 31 SimpleParseWithValidation.java /** * SimpleParseWithValidation.java **/ import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.ErrorHandler; import org.apache.xerces.parsers.DOMParser; import java.io.IOException;

32 95-733 Internet Technologies 32 public class SimpleParseWithValidation { public static void main(String[] argv) { if (argv.length != 1) { System.err.println("Usage: java "+ "SimpleParseWigthValidation "); System.exit(1); } try { // Creates parser object DOMParser parser = new DOMParser(); // Sets an ErrorHandler parser.setErrorHandler(new MyErrorHandler());

33 95-733 Internet Technologies 33 // Tells the parser to validate documents parser.setFeature( "http://xml.org/sax/features/validation", true); // Parses an XML Document parser.parse(argv[0]); // Gets a Document object Document doc = parser.getDocument(); // Does something } catch (Exception e) { e.printStackTrace(); }

34 95-733 Internet Technologies 34 MyHandler.java import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class MyErrorHandler implements ErrorHandler { /** Constructor. */ public MyErrorHandler(){ }

35 35 /** Warning. */ public void warning(SAXParseException ex) { System.err.println("[Warning] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Error. */ public void error(SAXParseException ex) { System.err.println("[Error] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Fatal error. */ public void fatalError(SAXParseException ex) { System.err.println("[Fatal Error] "+ getLocationString(ex)+": "+ ex.getMessage()); }

36 36 /** Returns a string of the location. */ private String getLocationString(SAXParseException ex) { StringBuffer str = new StringBuffer(); String systemId = ex.getSystemId(); if (systemId != null) { int index = systemId.lastIndexOf('/'); if (index != -1) systemId = systemId.substring(index + 1); str.append(systemId); } str.append(':'); str.append(ex.getLineNumber()); str.append(':'); str.append(ex.getColumnNumber()); return str.toString(); } Get the file name after the /’s.

37 95-733 Internet Technologies 37 Runs without error D:\McCarthy\www\95-733\examples\chap02>java SimpleParseWithValidation department-dtd.xml D:\McCarthy\www\95-733\examples\chap02>

38 95-733 Internet Technologies 38 Using the same dtd…

39 39 Introduce an error John Doe John.Doe@foo.com Bob Smith Bob.Smith@foo.com

40 95-733 Internet Technologies 40 Alice Miller D:\McCarthy\www\95-733\examples\chap02>java SimpleParseWithValidation department-dtd2.xml [Error] department-dtd2.xml:4:13: Attribute "id" is required and must be specifi ed for element type "employee". The parser does not throw an exception.

41 41 Make a fatal error (not well- formed) John Doe John.Doe@foo.com Bob Smith id="A.M"> Bob.Smith@foo.com

42 95-733 Internet Technologies 42 D:\McCarthy\www\95-733\examples\chap02>java SimpleParseWithValidation department-dtd2.xml [Error] department-dtd2.xml:10:39: Element type "someTag" must be declared. [Fatal Error] department-dtd2.xml:12:5: The element type "someTag" must be termi nated by the matching end-tag " ". org.xml.sax.SAXParseException: The element type "someTag" must be terminated by the matching end-tag " ". at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at SimpleParseWithValidation.main(SimpleParseWithValidation.java:31) An exception is thrown to the caller for this Fatal Error.

43 43 JAXP An API but really an abstraction layer Does not provide a new means of parsing XML Does not add to DOM, SAX or XSLT Does not provide new functionality in handling XML JAXP enables applications to parse and transform XML documents independent of a particular XML processing implementation. Just change a system variable or classpath setting and you change parsers. Six classes included in JDK1.4 in javax.xml.parsers package

44 44 SimpleParseJAXP.java /** * SimpleParseJAXP.java **/ import java.io.IOException; import org.w3c.dom.Document; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; Note: no mention of xerces.

45 95-733 Internet Technologies 45 public class SimpleParseJAXP { public static void main(String[] argv) { if (argv.length != 1) { System.err.println( "Usage: java SimpleParseJAXP "); System.exit(1); } try { // Creates document builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Tells the parser to validate documents factory.setValidating(true);

46 46 // Tells the parser to be aware namespaces factory.setNamespaceAware(true); // Creates builder object DocumentBuilder builder = factory.newDocumentBuilder(); // Sets an ErrorHandler builder.setErrorHandler(new MyErrorHandler()); // Parses the document Document doc = builder.parse(argv[0]); } catch (ParserConfigurationException pe) { pe.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); }

47 95-733 Internet Technologies 47 Suppose we want Xerces to be our parser D:\McCarthy\www\95-733\examples\chap02>java -Djavax.xml.parsers.DocumentBuilderFactory= org.apache.xerces.jaxp.DocumentBuilderFactoryImpl SimpleParseJAXP department-dtd.xml


Download ppt "95-733 Internet Technologies 1 Lecture 1 Introduction."

Similar presentations


Ads by Google