Download NewHomework3 - Andrew.cmu.edu

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
95-733 Internet Technologies
Carnegie Mellon University
Lab 3 Due: Wednesday, June 23, 2004
XSDL Code Generation
Part I Working with Microsoft’s Infer.exe
It is assumed that you have downloaded and installed the Microsoft .NET Framework SDK.
1) Download and install Microsoft’s Infer.exe program from
http://apps.gotdotnet.com/xmltools/xsdinference/
2) Read the article on this utility at
http://msdn.microsoft.com/library/default.asp?url=/library/enus/dnxmlnet/html/xsdinference.asp
3) Run the command line Infer.exe tool against the following xml document instance
(book1.xml). Note that this document contains an ISBN element. This was added
after taking the document from the article above.
<book year="1994" xmlns= "www.samplebookstore.com">
<title>TCP/IP Illustrated</title>
<author>Stevens W.</author>
<publisher>Addison-Wesley</publisher>
<price> 65.95</price>
<isbn>0201770598</isbn>
</book>
4) Visit the w3c site http://www.w3.org/TR/xmlschema-2 and change the
xs:unsignedShort type found in the XSDL year attribute to a Gregorian year type.
This is simply to illustrate the fact that the XSDL is changeable. We need not confine
ourselves to the document generated by Infer.exe.
5) Run JAXB against the XSDL document that you created in step 4. The JAXB code
should now contain a getYear() method. What type of data is returned by getYear()?
Is it the same type as that found in the XSDL document? Enter your answer here.
6) Write a Java program that uses the API to read and manipulate the XML. The
program will change the year of the book, and write the new XML document to the
screen and back to disk. You will probably need to consult the JAXB directory under
JWSDP for help with the marshalling and unmarshalling classes. Call this program
ChangeYear.java and submit a listing of the source code. Paste a screen shot here
showing the program running.
7) Modify the program ChangeYear.java and call it ChangeYearFromURL.java. Write
ChangeYearFromURL.java so that it reads the XML source from
1
95-733 Internet Technologies
Carnegie Mellon University
http://www.andrew.cmu.edu/~mm6/bookdoc/book1.xml. It should still change the
year, display the modified file to the screen and write it to disk. It should make no
attempt to change the file as it exists on Andrew. You will probably need to consult
the JAXB directory under JWSDP for help with unmarshalling data using an
internet URL. Submit a listing of the source code and paste a screen shot here of this
program running.
Part II Running Infer.exe on an RSS document
1) Collect an RSS file from MIT. This file has the URL
“http://mitworld.mit.edu/rss/new.xml”.
2) Run Infer.exe using the RSS XML file as input.
3) Run JAXB on the resulting XSDL document.
4) Write a client (MITRSSReader.java) that will search the MIT RSS feed for a
particular topic chosen by the user.
5) Submit a printout of MITRSSReader.java and paste a screen shot here.
Part III JSP Example
In part V you will be asked to build a web application using Java Server Pages. It may be helpful to build a
simpler application first.
1) Create a directory that will hold your application. In this example, I called my directory “jsp2”.
Create two subdirectories called src and web. Under web create a directory called WEB-INF.
Create a subdirectory under src called mybeans.
2) Place the build.properties file under jsp2. Place the build.xml file under jsp2.
3) Create three additional files – web.xml, MyCoolBean.java and Somefile.jsp. These files are shown at the
end of this assignment.
4) Place MyCoolBean.java under src/mybeans and web.xml under web/WEB-INF. Your directory structure
should now look like this:
D:\McCarthy\www\95-733\examples\jsp2>tree /f
Folder PATH listing
Volume serial number is 71FAE346 BA17:BF69
D:.
│ build.properties
│ build.xml
│
├───src
│ └───mybeans
│
MyCoolBean.java
│
└───web
│
Somefile.jsp
│
└───WEB-INF
web.xml
5) You should now be able to startup Tomcat, install and then visit this web application.
2
95-733 Internet Technologies
Carnegie Mellon University
Part IV JSP’s and JAXB
In Part V you will be placing JAXB code within the Java Web Services Developer Pack. Be sure to do the following:
1) Set your JWSD_HOME environment variable to the path of your installation. Mine looks like this:
JWSDP_HOME D:\jwsdp-1.2
2) Set your JAXB_HOME environment variable to JAXB’s path. Mine looks like this:
JAXB_HOME D:\jwsdp-1.2\jaxb
3) After running JAXB, copy all the directories that it creates to the WEB-INF/classes subdirectory
4) Make sure that the build.xml file includes the JAXB jars in the classpath. This is the build.xml file that
is used to install your web application under Tomcat. My build.xml file contains the following:
<path id="compile.classpath">
<!-- Include all elements that Tomcat exposes to applications -->
<pathelement location="${catalina.home}/common/classes"/>
<fileset dir="${catalina.home}/common/endorsed">
<include name="*.jar"/>
</fileset>
<fileset dir="${catalina.home}/common/lib">
<include name="*.jar"/>
</fileset>
<pathelement location="${catalina.home}/shared/classes"/>
<fileset dir="${catalina.home}/shared/lib">
<include name="*.jar"/>
</fileset>
<!-- these have been added for the JAXB related classes -->
<fileset dir="D:\jwsdp-1.2\jaxb\lib" includes="*.jar"/>
<fileset dir="D:\jwsdp-1.2\common\lib" includes="*.jar"/>
<fileset dir="D:\jwsdp-1.2\jaxp\lib\endorsed" includes="*.jar"/>
<fileset dir="D:\jwsdp-1.2\jwsdp-shared\lib" includes="*.jar"/>
<fileset dir="D:\jwsdp-1.2\jaxp\lib" includes="*.jar"/>
<pathelement location="."/>
</path>
5) Make sure that you have the files jaxb.properties and bgm.ser (both generated by JAXB) inside a
subdirectory of WEB-INF/classes. This should have been automatic when you performed step 3.
6) There is additional help from Sun at http://java.sun.com/xml/jaxb/jaxbWithTomcat.html
7) Below is a display of my directory tree that I installed on Tomcat. There are too many files to show
them all but I have added comments describing where things have been placed.
3
95-733 Internet Technologies
Carnegie Mellon University
D:.
├───build
build directory created by build.xml
│ └───WEB-INF
│
├───classes
│
│ ├───docs
│
│ │ └───api
│
│ │
├───resources
│
│ │
└───RSSAPI
│
│ ├───mybeans
│
│ │ ├───docs
│
│ │ │ └───api
│
│ │ │
├───resources
│
│ │ │
└───RSSAPI
│
│ │ ├───org
│
│ │ │ ├───purl
│
│ │ │ │ └───dc
│
│ │ │ │
└───elements
│
│ │ │ │
└───_1
│
│ │ │ │
└───impl
│
│ │ │ └───w3
│
│ │ │
└───_2001
│
│ │ │
└───xmlschema
│
│ │ │
└───impl
│
│ │ └───RSSAPI
│
│ │
└───impl
│
│ │
└───runtime
│
│ ├───org
│
│ │ ├───purl
│
│ │ │ └───dc
│
│ │ │
└───elements
│
│ │ │
└───_1
│
│ │ │
└───impl
│
│ │ └───w3
│
│ │
└───_2001
│
│ │
└───xmlschema
│
│ │
└───impl
│
│ └───RSSAPI
│
│
└───impl
│
│
└───runtime
│
└───lib
├───src
│ └───mybeans ----MITNews.java and RSSRecord.java make calls on JAXB code
└───web
-CollectData.jsp and MIT.jsp are JSP pages that use the MITNews.java bean
└───WEB-INF
└───classes
├───docs -- directory created by the build.xml file that generates the JAXB code
│ └───api
│
├───resources
│
└───RSSAPI
├───org -- JAXB generated code
│ ├───purl
│ │ └───dc
│ │
└───elements
│ │
└───_1
│ │
└───impl
4
95-733 Internet Technologies
Carnegie Mellon University
│ └───w3 -- JAXB generated code
│
└───_2001
│
└───xmlschema
│
└───impl
└───RSSAPI -- JAXB generated code
└───impl
└───runtime
Part V JSP, XSDL and JAXB
1) Develop a web interface for MITRSSReader. This must be written with JSP pages.
2) Submit printouts of your JSP pages and several screen shots showing a browser visiting the site. The browser
user will be able to enter a keyword for the MIT search or make a global selection (perhaps from a pull down
menu) to review all of the news. The user will then be able to click on links that were retrieved by your web
application and that point to the MIT resources.
Summary of Submission Requirements:
You must submit this final project in a large envelope.









Answer to question 5.
5 Points
ChangeYear.java
10 Points
Screen shot of ChangeYear.java
5 Points
ChangeYearFromURL.java
10 Points
Screen shot of ChangeYearFromURL.java
5 Points
MITRSSReader.java
20 Points
Screen shot of MITRSSReader.java
5 Points
JSP pages that incorporate the MITRSSReader class
20 Points
Several browser screen shots showing the search terms
20 Points
and retrieved links
MyCoolBean.java
package mybeans;
import java.io.*;
public class MyCoolBean implements Serializable {
private int next;
public int getNext() { return next++; }
public void setNext(int m) { next = m; }
public MyCoolBean() { next = 0; }
}
5
95-733 Internet Technologies
Carnegie Mellon University
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app></web-app>
SomeFile.jsp
<%@ page language="java" contentType="text/html" %>
<html>
<body>
<h1>Helo Visitor </h1>
<jsp:useBean id = "visit" scope = "application" class="mybeans.MyCoolBean" />
The visit number at the server is <jsp:getProperty name="visit" property="next" />
</body>
</html>
Book1.xml from the article at Microsoft
<book year="1994" xmlns= "www.samplebookstore.com">
<title>TCP/IP Illustrated</title>
<author>Stevens W.</author>
<publisher>Addison-Wesley</publisher>
<price> 65.95</price>
</book>
A JAXB build script:
Build.xml
<?xml version="1.0"?>
<project basedir="." default="run">
<path id="classpath">
<fileset dir="D:\jwsdp-1.3\jaxb\lib" includes="*.jar"/>
<fileset dir="D:\jwsdp-1.3\common\lib" includes="*.jar"/>
<fileset dir="D:\jwsdp-1.3\jaxp\lib\endorsed" includes="*.jar"/>
<fileset dir="D:\jwsdp-1.3\jwsdp-shared\lib" includes="*.jar"/>
<fileset dir="D:\jwsdp-1.3\jaxp\lib" includes="*.jar"/>
<pathelement location="."/>
</path>
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
<classpath refid="classpath" />
</taskdef>
<!-- generate Java source files -->
<target name="generate">
<!-- generate the Java content classes from the schema -->
<echo message="Compiling the schema..."/>
6
95-733 Internet Technologies
Carnegie Mellon University
<xjc schema="Book1.xsd" target="." package="BookAPI"/>
<!-- generate the javadocs from the content classes -->
<mkdir dir="docs/api"/>
<javadoc packagenames="BookAPI"
sourcepath="."
destdir="docs/api"
windowtitle="Generated Interfaces for Book1.xsd">
<classpath refid="classpath" />
</javadoc>
<!-- compile all of the java sources -->
<echo message="Compiling the java source files..."/>
<javac srcdir="." destdir="." debug="on">
<classpath refid="classpath" />
</javac>
</target>
<target name="JustJava">
<!-- compile all of the java sources -->
<echo message="Compiling the java source files..."/>
<javac srcdir="." destdir="." debug="on">
<classpath refid="classpath" />
</javac>
</target>
<target name="JustJavaRun">
<echo message="java runtime"/>
<java classname="Main" fork="true">
<classpath refid="classpath" />
</java>
</target>
<target name="run" depends="generate">
<echo message="Running the sample application..."/>
<java classname="Main" fork="true">
<classpath refid="classpath" />
</java>
</target>
</project>
A Main.java that works with the JAXB generated code
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.util.ValidationEventCollector;
7
95-733 Internet Technologies
Carnegie Mellon University
// import java content classes generated by binding compiler
import BookAPI.*;
public class Main {
// This sample application demonstrates how to enable validation during
// the unmarshal operations.
public static void main( String[] args ) {
try {
// create a JAXBContext capable of handling classes generated into
// the BookAPI package
JAXBContext jc = JAXBContext.newInstance( "BookAPI" );
// create an Unmarshaller
Unmarshaller u = jc.createUnmarshaller();
// enable validation
u.setValidating( true );
// in this example, we will allow the Unmarshaller's default
// ValidationEventHandler to receive notification of warnings
// and errors which will be sent to System.out. The default
// ValidationEventHandler will cause the unmarshal operation
// to fail with an UnmarshalException after encountering the
// first error or fatal error.
// unmarshal an invalid itemList instance document into a tree of Java
// content objects composed of classes from the BookAPI package.
Book book =
(Book)u.unmarshal( new File( "Book1.xml" ) );
System.out.println("Author: " + book.getAuthor());
} catch( UnmarshalException ue ) {
System.out.println( "Caught UnmarshalException" );
} catch( JAXBException je ) {
je.printStackTrace();
}
}
}
8