95-733 internet technologies jaxb java api for xml binding javamail

40
95-733 Internet Technologies • JAXB Java API for XML Binding • JavaMail

Post on 21-Dec-2015

240 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

95-733 Internet Technologies

• JAXB Java API for XML Binding

• JavaMail

Page 2: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

JAXB

• The Java Architecture for XML Binding

• Notes taken from the JAXB User’s Guide from Sun Microsystems

Page 3: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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

Page 4: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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)

Page 5: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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 automatically

Page 6: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

The Binding Schema

• JAXB requires that we use a DTD AND a “Binding Schema”

• The Binding Schema is an XML document

JAXBDTD

BindingSchema

.java file(s)

javac

.class files

javac

Page 7: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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 BigDecimal

– this element’s content is a String

– this element should be treated as a class

– and so on …

Page 8: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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 exit

Page 9: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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>

Page 10: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

Book.dtd

<!ELEMENT book (title,author, chapter)><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)><!ELEMENT chapter (#PCDATA)>

Page 11: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

A minimal binding schema Book.xjc

<xml-java-binding-schema> <element name = "book" type = "class" root = "true" /></xml-java-binding-schema>

Page 12: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

Run JAXB

C:\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.java

C:\McCarthy\www\95-733\examples\jaxb>

Page 13: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

Resulting Book.java file

import 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;

Page 14: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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(); } }

Page 15: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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(); } }

Page 16: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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 { }

Page 17: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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); }

Page 18: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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 = ""; }

Page 19: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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"); }

Page 20: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

xs.takeEnd("book"); }

public static Book unmarshal(InputStream in) throws UnmarshalException { return unmarshal(XMLScanner.open(in)); }

public static Book unmarshal(XMLScanner xs) throws UnmarshalException { return unmarshal(xs, newDispatcher()); }

public static Book unmarshal(XMLScanner xs, Dispatcher d) throws UnmarshalException { return ((Book) d.unmarshal(xs, (Book.class))); }

Page 21: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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; } }

Page 22: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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; }

Page 23: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

} else { if (tob._Chapter!= null) { return false; } } return true; }

public int hashCode() { int h = 0; h = ((127 *h)+((_Title!= null)?_Title.hashCode(): 0)); h = ((127 *h)+((_Author!= null)?_Author.hashCode(): 0)); h = ((127 *h)+((_Chapter!= null)?_Chapter.hashCode(): 0)); return h; }

Page 24: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

public String toString() { StringBuffer sb = new StringBuffer("<<book"); if (_Title!= null) { sb.append(" title="); sb.append(_Title.toString()); } if (_Author!= null) { sb.append(" author="); sb.append(_Author.toString()); } if (_Chapter!= null) { sb.append(" chapter="); sb.append(_Chapter.toString()); } sb.append(">>"); return sb.toString(); }

Page 25: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

public static Dispatcher newDispatcher() { Dispatcher d = new Dispatcher(); d.register("book", (Book.class)); d.freezeElementNameMap(); return d; }

}

Page 26: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

A Driver Program// demo JAXBimport java.io.*;public class TestJaxb {

public static void main(String args[]) throws java.io.FileNotFoundException, javax.xml.bind.UnmarshalException, java.io.IOException {

DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream(args[0]))); DataOutputStream out = new DataOutputStream( new FileOutputStream(args[1]));

Page 27: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

// read in the book Book catcher = Book.unmarshal(in); // change its title catcher.setTitle("Gone With The Wind"); // write out the book catcher.marshal(out); }}

Page 28: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

Compile and Run

C:\McCarthy\www\95-733\examples\jaxb>javac Book.java

C:\McCarthy\www\95-733\examples\jaxb>javac TestJaxb.java

C:\McCarthy\www\95-733\examples\jaxb>java TestJaxb book.xml out.xml

Page 29: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

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>

Page 30: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

Writing Simple Email Clients in Java

• Email Using Sockets

• Email using the JavaMail API

Page 31: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

Email Using Sockets (Linden Chapter 17)

// See page 571 of Linden's Just Java Fifth Edition

import java.io.*;import java.net.*;

public class SimpleEmailClient {

public static void main(String args[]) throws IOException {

Socket sock; DataInputStream dis; PrintStream ps;

Page 32: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

// open a socket to an email server on port 25 sock = new Socket("cyrus.andrew.cmu.edu",25);

dis = new DataInputStream(sock.getInputStream()); ps = new PrintStream(sock.getOutputStream());

// talk email ps.println("mail from: [email protected]"); System.out.println(dis.readLine());

String Addressee = "[email protected]"; ps.println("rcpt to: " + Addressee);

System.out.println(dis.readLine());

Page 33: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

ps.println("data"); System.out.println(dis.readLine());

ps.println("A message sent by a Java client using sockets"); ps.println(".");

System.out.println(dis.readLine());

ps.flush(); sock.close(); }}

Page 34: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

Output Echos from Cyrus

D:\McCarthy\www\95-713\JavaMail>java SimpleEmailClient220-mail-fe2.andrew.cmu.edu ESMTP Sendmail 8.12.3.Beta2/8.12.3.Beta2220-Mis-identifing the sender of mail is an abuse of computing facilites.220 ESMTP spoken here250 2.1.0 [email protected]... Sender ok

Page 35: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

Using JavaMail// A simple example using JavaMailimport java.io.*;import java.net.InetAddress;import java.util.Properties;import java.util.Date;

import javax.mail.*;import javax.mail.internet.*;

public class Mailer {

String to, from, msg;

Page 36: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

public Mailer(String to, String from, String msg) {

this.to = to; this.from = from; this.msg = msg; }

public void send() { Session s = createSession(); try {

Message mess = createMessage(s, msg); Transport.send(mess);

} catch(MessagingException e) {

System.out.println("Messaging Exception: "+e); }

Page 37: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

catch(Exception e ) { System.out.println("Exception thrown" + e); }

} private Session createSession() {

Properties p = System.getProperties();p.setProperty("mail.transport.protocol", "smtp");p.setProperty("mail.smtp.host","cyrus.andrew.cmu.edu");Session sess = Session.getDefaultInstance(p);return sess;

}

Page 38: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

private Message createMessage(Session sess, String msg)throws MessagingException{

Message mess = new MimeMessage(sess);mess.setFrom(new InternetAddress(from));

mess.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));

mess.setSubject("Warning");mess.setText(msg);mess.setSentDate(new Date());

return mess;}

Page 39: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

public static void main(String a[]) { Mailer mailman = new Mailer("[email protected]", "[email protected]", "It's hot"); mailman.send();

}}

Page 40: 95-733 Internet Technologies JAXB Java API for XML Binding JavaMail

JavaMail Installation Instructions

1. Download the JavaMail API Implementation Version 1.3ea

2. Download the JavaBeans Activation Framework 1.0.2ea

3. Unzip both files.4. Add the following files to your classpath:

– mail.jar (JavaMail)– activation.jar (JAF file)

Visit http://java.sun.com/products/javamail