95 733 Internet Technologies JAXB Java API for
95 -733 Internet Technologies • JAXB Java API for XML Binding • Java. Mail
JAXB • The Java Architecture for XML Binding • Notes taken from the JAXB User’s Guide from Sun Microsystems
JAXB • 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 facilities
JAXB • 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 Big. Decimal automatically
The Binding Schema • JAXB requires that we use a DTD AND a “Binding Schema” • The Binding Schema is an XML document. java file(s) DTD JAXB Binding Schema javac. class files
The 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 Big. Decimal – this element’s content is a String – this element should be treated as a class – and so on …
Running JAXB C: Mc. Carthywww95 -733examplesjaxb>java -jar D: jaxb-1. 0 -ealibjaxb-xjc-1. 0 -ea. jar -help Usage: 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 exit
Book. 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 JAXB C: Mc. Carthywww95 -733examplesjaxb>java -jar D: jaxb-1. 0 -ealibjaxb-xjc-1. 0 -ea. jar book 0. dtd book. xjc. Book. java C: Mc. Carthywww95 -733examplesjaxb>
Resulting Book. java file import java. io. IOException; import java. io. Input. Stream; import javax. xml. bind. Conversion. Exception; import javax. xml. bind. Dispatcher; import javax. xml. bind. Invalid. Attribute. Exception; import javax. xml. bind. Local. Validation. Exception; import javax. xml. bind. Marshallable. Root. Element; import javax. xml. bind. Marshaller; import javax. xml. bind. Missing. Content. Exception; import javax. xml. bind. Root. Element; import javax. xml. bind. Structure. Validation. Exception; import javax. xml. bind. Unmarshaller; import javax. xml. bind. Validator; import javax. xml. marshal. XMLScanner; import javax. xml. marshal. XMLWriter;
public class Book extends Marshallable. Root. Element implements Root. Element { private String _Title; private String _Author; private String _Chapter; public String get. Title() { return _Title; } public void set. Title(String _Title) { this. _Title = _Title; if (_Title == null) { invalidate(); } }
public String get. Author() { return _Author; } public void set. Author(String _Author) { this. _Author = _Author; if (_Author == null) { invalidate(); } } public String get. Chapter() { return _Chapter; } public void set. Chapter(String _Chapter) { this. _Chapter = _Chapter; if (_Chapter == null) { invalidate(); } }
public void validate. This() throws Local. Validation. Exception { if (_Title == null) { throw new Missing. Content. Exception("title"); } if (_Author == null) { throw new Missing. Content. Exception("author"); } if (_Chapter == null) { throw new Missing. Content. Exception("chapter"); } } public void validate(Validator v) throws Structure. Validation. Exception { }
public void marshal(Marshaller m) throws IOException { XMLWriter w = m. writer(); w. start("book"); w. leaf("title", _Title. to. String()); w. leaf("author", _Author. to. String()); w. leaf("chapter", _Chapter. to. String()); w. end("book"); } public void unmarshal(Unmarshaller u) throws Unmarshal. Exception { XMLScanner xs = u. scanner(); Validator v = u. validator(); xs. take. Start("book"); while (xs. at. Attribute()) { String an = xs. take. Attribute. Name(); throw new Invalid. Attribute. Exception(an); }
if (xs. at. Start("title")) { xs. take. Start("title"); String s; if (xs. at. Chars(XMLScanner. WS_COLLAPSE)) { s = xs. take. Chars(XMLScanner. WS_COLLAPSE); } else { s = ""; } try { _Title = String. value. Of(s); } catch (Exception x) { throw new Conversion. Exception("title", x); } xs. take. End("title"); } if (xs. at. Start("author")) { xs. take. Start("author"); String s; if (xs. at. Chars(XMLScanner. WS_COLLAPSE)) { s = xs. take. Chars(XMLScanner. WS_COLLAPSE); } else { s = "";
try { _Author = String. value. Of(s); } catch (Exception x) { throw new Conversion. Exception("author", x); } xs. take. End("author"); } if (xs. at. Start("chapter")) { xs. take. Start("chapter"); String s; if (xs. at. Chars(XMLScanner. WS_COLLAPSE)) { s = xs. take. Chars(XMLScanner. WS_COLLAPSE); } else { s = ""; } try { _Chapter = String. value. Of(s); } catch (Exception x) { throw new Conversion. Exception("chapter", x); } xs. take. End("chapter");
xs. take. End("book"); } public static Book unmarshal(Input. Stream in) throws Unmarshal. Exception { return unmarshal(XMLScanner. open(in)); } public static Book unmarshal(XMLScanner xs) throws Unmarshal. Exception { return unmarshal(xs, new. Dispatcher()); } public static Book unmarshal(XMLScanner xs, Dispatcher d) throws Unmarshal. Exception { return ((Book) d. unmarshal(xs, (Book. class))); }
public boolean equals(Object ob) { if (this == ob) { return true; } if (!(ob instanceof Book)) { return false; } Book tob = ((Book) ob); if (_Title!= null) { if (tob. _Title == null) { return false; } if (!_Title. equals(tob. _Title)) { return false; } } else { if (tob. _Title!= null) { return false; } }
if (_Author!= null) { if (tob. _Author == null) { return false; } if (!_Author. equals(tob. _Author)) { return false; } } else { if (tob. _Author!= null) { return false; } } if (_Chapter!= null) { if (tob. _Chapter == null) { return false; } if (!_Chapter. equals(tob. _Chapter)) { return false; }
} else { if (tob. _Chapter!= null) { return false; } } return true; } public int hash. Code() { int h = 0; h = ((127 *h)+((_Title!= null)? _Title. hash. Code(): 0)); h = ((127 *h)+((_Author!= null)? _Author. hash. Code(): 0)); h = ((127 *h)+((_Chapter!= null)? _Chapter. hash. Code(): 0)); return h; }
public String to. String() { String. Buffer sb = new String. Buffer("<<book"); if (_Title!= null) { sb. append(" title="); sb. append(_Title. to. String()); } if (_Author!= null) { sb. append(" author="); sb. append(_Author. to. String()); } if (_Chapter!= null) { sb. append(" chapter="); sb. append(_Chapter. to. String()); } sb. append(">>"); return sb. to. String(); }
public static Dispatcher new. Dispatcher() { Dispatcher d = new Dispatcher(); d. register("book", (Book. class)); d. freeze. Element. Name. Map(); return d; } }
A Driver Program // demo JAXB import java. io. *; public class Test. Jaxb { public static void main(String args[]) throws java. io. File. Not. Found. Exception, javax. xml. bind. Unmarshal. Exception, java. io. IOException { Data. Input. Stream in = new Data. Input. Stream( new Buffered. Input. Stream( new File. Input. Stream(args[0]))); Data. Output. Stream out = new Data. Output. Stream( new File. Output. Stream(args[1]));
// read in the book Book catcher = Book. unmarshal(in); // change its title catcher. set. Title("Gone With The Wind"); // write out the book catcher. marshal(out); } }
Compile and Run C: Mc. Carthywww95 -733examplesjaxb>javac Book. java C: Mc. Carthywww95 -733examplesjaxb>javac Test. Jaxb. java C: Mc. Carthywww95 -733examplesjaxb>java Test. Jaxb book. xml out. xml
Out. xml <? xml version="1. 0" encoding="UTF-8"? > <book> <title>Gone With The Wind</title> <author>J. D. Salinger</author> <chapter>If you really want to hear about it. . . </chapter></book>
Writing Simple Email Clients in Java • Email Using Sockets • Email using the Java. Mail API
Email Using Sockets (Linden Chapter 17) // See page 571 of Linden's Just Java Fifth Edition import java. io. *; import java. net. *; public class Simple. Email. Client { public static void main(String args[]) throws IOException { Socket sock; Data. Input. Stream dis; Print. Stream ps;
// open a socket to an email server on port 25 sock = new Socket("cyrus. andrew. cmu. edu", 25); dis = new Data. Input. Stream(sock. get. Input. Stream()); ps = new Print. Stream(sock. get. Output. Stream()); // talk email ps. println("mail from: mm 6@andrew. cmu. edu"); System. out. println(dis. read. Line()); String Addressee = "mm 6@andrew. cmu. edu"; ps. println("rcpt to: " + Addressee); System. out. println(dis. read. Line());
ps. println("data"); System. out. println(dis. read. Line()); ps. println("A message sent by a Java client using sockets"); ps. println(". "); System. out. println(dis. read. Line()); ps. flush(); sock. close(); } }
Output Echos from Cyrus D: Mc. Carthywww95 -713Java. Mail>java Simple. Email. Client 220 -mail-fe 2. andrew. cmu. edu ESMTP Sendmail 8. 12. 3. Beta 2/8. 12. 3. Beta 2 220 -Mis-identifing the sender of mail is an abuse of computing facilites. 220 ESMTP spoken here 250 2. 1. 0 mm 6@andrew. cmu. edu. . . Sender ok
Using Java. Mail // A simple example using Java. Mail import java. io. *; import java. net. Inet. Address; import java. util. Properties; import java. util. Date; import javax. mail. *; import javax. mail. internet. *; public class Mailer { String to, from, msg;
public Mailer(String to, String from, String msg) { this. to = to; this. from = from; this. msg = msg; } public void send() { Session s = create. Session(); try { Message mess = create. Message(s, msg); Transport. send(mess); } catch(Messaging. Exception e) { System. out. println("Messaging Exception: "+e); }
catch(Exception e ) { System. out. println("Exception thrown" + e); } } private Session create. Session() { Properties p = System. get. Properties(); p. set. Property("mail. transport. protocol", "smtp"); p. set. Property("mail. smtp. host", "cyrus. andrew. cmu. edu"); Session sess = Session. get. Default. Instance(p); return sess; }
private Message create. Message(Session sess, String msg)throws Messaging. Exception{ Message mess = new Mime. Message(sess); mess. set. From(new Internet. Address(from)); mess. set. Recipients(Message. Recipient. Type. TO, Internet. Address. parse(to, false)); mess. set. Subject("Warning"); mess. set. Text(msg); mess. set. Sent. Date(new Date()); return mess; }
public static void main(String a[]) { Mailer mailman = new Mailer("mm 6@andrew. cmu. edu", "mccarthy@cs. pitt. edu", "It's hot"); mailman. send(); } }
Java. Mail Installation Instructions Visit http: //java. sun. com/products/javamail 1. Download the Java. Mail API Implementation Version 1. 3 ea 2. Download the Java. Beans Activation Framework 1. 0. 2 ea 3. Unzip both files. 4. Add the following files to your classpath: – mail. jar (Java. Mail) – activation. jar (JAF file)
- Slides: 40