Unformatted text preview:

95-733 Internet TechnologiesJAXBSlide 3Slide 4Slide 5The Binding SchemaSlide 7Running JAXBBook.xmlBook.dtdA minimal binding schema Book.xjcRun JAXBResulting Book.java fileSlide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25A Driver ProgramSlide 27Compile and RunOut.xmlWriting Simple Email Clients in JavaEmail Using Sockets (Linden Chapter 17)Slide 32Slide 33Output Echos from CyrusUsing JavaMailSlide 36Slide 37Slide 38Slide 39Slide 4095-733 Internet Technologies•JAXB Java API for XML Binding•JavaMailJAXB•The Java Architecture for XML Binding•Notes taken from the JAXB User’s Guide from Sun MicrosystemsJAXB•Like SAX and DOM in that the application developer does not have to parse the XML•Unlike SAX and DOM it is tied to a particular document schema•Fast like SAX•In memory representation like DOM but without all the general tree manipulation facilitiesJAXB•An XML Document is an instance of a schema•A Java object is an instance of a class•JAXB reads a schema and produces a class•Currently, the only schema supported is the Document Type Definition (DTD)JAXB•Augments the standard DTD by providing type conversion <!ELEMENT notional (#PCDATA)> : : <notional>100.00</notional>•With JAXB we can make sure that Java converts this to a BigDecimal automaticallyThe Binding Schema•JAXB requires that we use a DTD AND a “Binding Schema”•The Binding Schema is an XML documentJAXBDTDBindingSchema.java file(s)javac.class filesjavacThe Binding Schema•The binding schema contains instructions on how to bind a schema to classes.•It might say that :–this attribute (in the XML document) should be treated as an int–this element’s content is a BigDecimal–this element’s content is a String–this element should be treated as a class–and so on …Running JAXBC:\McCarthy\www\95-733\examples\jaxb>java -jar D:\jaxb\jaxb-1.0-ea\lib\jaxb-xjc-1.0-ea.jar -helpUsage: xjc <schema> [ <binding-schema> ] [ -d <directory> ] [ -roots <element-list> ]Options: -d <directory> Specify the destination directory for the Java output -roots <element-list> Designate one or more root elements (comma separated) -version Print the compiler's version number and exit -help Print this message and exitBook.xml<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE book SYSTEM "book.dtd"><book> <title>The Catcher in the Rye </title> <author>J.D. Salinger </author> <chapter>If you really want to hear about it... </chapter></book>Book.dtd<!ELEMENT book (title,author, chapter)><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)><!ELEMENT chapter (#PCDATA)>A minimal binding schema Book.xjc<xml-java-binding-schema> <element name = "book" type = "class" root = "true" /></xml-java-binding-schema>Run JAXBC:\McCarthy\www\95-733\examples\jaxb>java -jar D:\jaxb\jaxb-1.0-ea\lib\jaxb-xjc-1.0-ea.jar book0.dtd book.xjc.\Book.javaC:\McCarthy\www\95-733\examples\jaxb>Resulting Book.java fileimport java.io.IOException;import java.io.InputStream;import javax.xml.bind.ConversionException;import javax.xml.bind.Dispatcher;import javax.xml.bind.InvalidAttributeException;import javax.xml.bind.LocalValidationException;import javax.xml.bind.MarshallableRootElement;import javax.xml.bind.Marshaller;import javax.xml.bind.MissingContentException;import javax.xml.bind.RootElement;import javax.xml.bind.StructureValidationException;import javax.xml.bind.UnmarshalException;import javax.xml.bind.Unmarshaller;import javax.xml.bind.Validator;import javax.xml.marshal.XMLScanner;import javax.xml.marshal.XMLWriter;public class Book extends MarshallableRootElement implements RootElement{ private String _Title; private String _Author; private String _Chapter; public String getTitle() { return _Title; } public void setTitle(String _Title) { this._Title = _Title; if (_Title == null) { invalidate(); } }public String getAuthor() { return _Author; } public void setAuthor(String _Author) { this._Author = _Author; if (_Author == null) { invalidate(); } } public String getChapter() { return _Chapter; } public void setChapter(String _Chapter) { this._Chapter = _Chapter; if (_Chapter == null) { invalidate(); } }public void validateThis() throws LocalValidationException { if (_Title == null) { throw new MissingContentException("title"); } if (_Author == null) { throw new MissingContentException("author"); } if (_Chapter == null) { throw new MissingContentException("chapter"); } } public void validate(Validator v) throws StructureValidationException { }public void marshal(Marshaller m) throws IOException { XMLWriter w = m.writer(); w.start("book"); w.leaf("title", _Title.toString()); w.leaf("author", _Author.toString()); w.leaf("chapter", _Chapter.toString()); w.end("book"); } public void unmarshal(Unmarshaller u) throws UnmarshalException { XMLScanner xs = u.scanner(); Validator v = u.validator(); xs.takeStart("book"); while (xs.atAttribute()) { String an = xs.takeAttributeName(); throw new InvalidAttributeException(an); }if (xs.atStart("title")) { xs.takeStart("title"); String s; if (xs.atChars(XMLScanner.WS_COLLAPSE)) { s = xs.takeChars(XMLScanner.WS_COLLAPSE); } else { s = ""; } try { _Title = String.valueOf(s); } catch (Exception x) { throw new ConversionException("title", x); } xs.takeEnd("title"); } if (xs.atStart("author")) { xs.takeStart("author"); String s; if (xs.atChars(XMLScanner.WS_COLLAPSE)) { s = xs.takeChars(XMLScanner.WS_COLLAPSE); } else { s = ""; }try { _Author = String.valueOf(s); } catch (Exception x) { throw new ConversionException("author", x); } xs.takeEnd("author"); } if (xs.atStart("chapter")) { xs.takeStart("chapter"); String s; if (xs.atChars(XMLScanner.WS_COLLAPSE)) { s = xs.takeChars(XMLScanner.WS_COLLAPSE); } else { s = ""; } try { _Chapter = String.valueOf(s); } catch (Exception x) { throw new ConversionException("chapter", x); } xs.takeEnd("chapter"); }xs.takeEnd("book"); } public static Book unmarshal(InputStream in) throws


View Full Document

CMU ISM 95733 - JAXBJavaMailJAXM

Download JAXBJavaMailJAXM
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view JAXBJavaMailJAXM and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view JAXBJavaMailJAXM 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?