csc3530 software technology

21
CSC3530 Software Technology Tutorial 10 Assignment Two (III) Demo Update to Assignment One java.net package nanoxml XSLT,XPath

Upload: nydia

Post on 20-Mar-2016

23 views

Category:

Documents


0 download

DESCRIPTION

CSC3530 Software Technology. Tutorial 10 Assignment Two (III) Demo Update to Assignment One java.net package nanoxml XSLT,XPath. Assignment Two Part III. Demo link http://sparc68.cse.cuhk.edu.hk:8080/examples/servlet/CompareXML - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSC3530 Software Technology

CSC3530 Software Technology

Tutorial 10Assignment Two (III) DemoUpdate to Assignment One

java.net packagenanoxml

XSLT,XPath

Page 2: CSC3530 Software Technology

Assignment Two Part III• Demo link

– http://sparc68.cse.cuhk.edu.hk:8080/examples/servlet/CompareXML– http://sparc68.cse.cuhk.edu.hk:8080/examples/servlet/CompareXML1

• Flow of part III– 1.Post a query to servlet – CompareXML (with check flag on)– 2.For each product found in product table, find those

corresponding supplier in supplying table– 3.Check if the price quote from supplier exceeds a certain value

• 3.1 If yes, post a query to that supplier• 3.2 The URL is obtain in a field URL of supplier table (URL of

CompareXML1)• 3.3 Parse the XML return from query

– 4.Update the price quote– 5.Presents the user with details of products

• Price lower than the current site should not be display

Page 3: CSC3530 Software Technology

Flow diagram of Part III

CompareXML1

CompareXML2

CompareXML

DB

DB1

DB2

internet

1

2,3 check if need to update

3.1

3.3

4 update price quote

5

User post a query

XML data

XML data

XML data

Page 4: CSC3530 Software Technology

code Category On_hand Price111 Printer 10 600abc Scanner 20 200def printer 10 800

Product - CompareXMLcode Category On_hand Price111 Printer 20 800abc Scanner 20 150defg Scanner 50 600

Product – CompareXML1

Product code

Supplier code

price On_hand

Quote

111 t1 500 30 12/11def t2 300 50 12/10

supplying

code name URLt1 company1 http://

sparc68.cse.cuhk.edu.hk8080/example/servlet/CompareXML1

t2 company2 http://sparc68.cse.cuhk.edu.hk8080/example/servlet/CompareXML2

supplier

Get the URL and whenthe quote is updated

1

2

3

4

5

Page 5: CSC3530 Software Technology

Update to assignment one database schema

• Reuse of table– Originally, you have

• Supplying(supplier_code,product_code,price)• Supplier(code,name,address,e-mail,tel)• Product(code,name,category,on_hand,low_limit)

• Update– alter table supplying add (quote date,on_hand int)

• Indicate when the price quote is updated• How many product the supplier has

– alter table supplier add url varchar2(100)• Store the price quote servlet URL of supplier

– alter table product add price number(10,2)• Store the price quote of current company

Page 6: CSC3530 Software Technology

Suggested SQL• select SU.url, S.code from supplying SU, supplier S

where SU.supplier_code=S.code and SU.product_code=‘xxx’ and (SYSDATE-SU.quote)*24*60 > 30;– Find those supplier’s price quote URL which supply product xxx

to us and the price quote is not update for 30 minutes

• update supplying set price=100, quote=SYSDATE where supplier_code=‘xxx’ and product_code=‘yyy’;– Update the price quote of product yyy supplied by supplier xxx

and set the time to current time

• You should not show supplier whose price quote is lower that your company

• SYSDATE is the current date/time• Date arithmetic is in number of days

Page 7: CSC3530 Software Technology

java.net package• How to post query to a CGI in java program?

– Use java.net.URL, java.net.URLConnection• Java will open an http connection for your program

– No need to do socket programming• To use

– import java.net.*;– Import java.io.*; (for reader and writer)

• Key objects– URL – an object to model the url (http://……)– URLConnection – an object to model connection between server

and client– PrintWriter – an object for you to post request to a CGI URL– BufferedReader – and object for you to read the CGI output

(html page)

Page 8: CSC3530 Software Technology

Code fragment• Example

– http://www.cse.cuhk.edu.hk/~kcsia/csc3530/Query.java– Please run in unix machine, and make sure sparc68 is up

URL url=new URL(“http://sparc68.cse.cuhk.edu.hk:8080/examples/servlet/CompareXML”);URLConnection urlconnection=url.openConnection();urlconnection.setDoOutput(true);PrintWriter out=new PrintWriter(urlconnection.getOutputStream());out.print(“field=id&query=111”);out.close();BufferedReader in=new BufferedReader(new

InputStreamReader(urlconnection.getInputStream());String temp;do {

temp=in.readLine();if (temp!=null)

System.out.println(temp);else

break;}in.close();

Page 9: CSC3530 Software Technology

Explanation• Construct an URL object, using the URL of price quote

CGI• openConnection() - to obtain an URLConnection object• setDoOutput(true) - to enable sending data to CGI• new PrintWriter – obtain an writer object for sending

request to CGI• out.println() - send query data to CGI• out.close() – close the writer• … getInputStream() - get the stream for reading CGI

output (xml data)• in.close() – close the reader• Parse the XML … (use nanoXML)

Page 10: CSC3530 Software Technology

XML for exchanging data<products>

<product><id>ABCD</id><description>rogue spear</description><category>computer games</category><company><name>Amazon</name><price>200.2</price><onhand>50</onhand></company></product><product><id>EFGH</id><description>black thorn</description><category>computer games</category><company><name>Amazon</name><price>150.3</price><onhand>200</onhand></company></product>

</products>

Tag Name

Content

Page 11: CSC3530 Software Technology

nanoxml• How to interpret the XML return from other URL

– Use XML parser– http://nanoxml.sourceforge.net/index.html

version 1.6.8– http://www.cse.cuhk.edu.hk/~kcsia/csc3530/nanoxml.jar

• To use– Place nanoxml.jar in the same directory with your code or

set CLASSPATH to include nanoxml.jar– import nanoxml.*;

• XMLElement (a class in nanoxml)– It models a node in the DOM tree– Methods to use

• parseString()• getChildren()• getTagName()• getContents()

Page 12: CSC3530 Software Technology

DOM Tree and XMLElement

products

product

id description category company

price name

All nodes are XMLElement object

getChildren() returns the childnodes contained in a Vectorobject

getTagName() returns the node’s name

getContents(), e.g. when call on price XMLElementit will return 150.3

Page 13: CSC3530 Software Technology

How to use XMLSample: http://www.cse.cuhk.edu.hk/~kcsia/csc3530/QueryXML.java…String xml=“<products><product><company><price>200</price>

</company></product></products>”;XMLElement root=new XMLElement();root.parseString(xml);XMLElement product=findTag(root,"product");if (product!=null) {

XMLElement company=findTag(product,"company");if (company!=null) {XMLElement price=findTag(company,"price");if (price!=null) {System.out.println("The price is:“+price.getContents());}}}

}// findTag is a function that you have written

Page 14: CSC3530 Software Technology

How to use XMLprivate static XMLElement findTag(XMLElement src,String str) {

Vector v = src.getChildren();for (int i = 0; i < v.size(); i++) {XMLElement e = (XMLElement)(v.elementAt(i));if (e.getTagName().compareTo(str) == 0) {return e;} }return null;

}• To use Vector, you should import java.util.*;• getChildren() is a method of XMLElement

– If you call product.getChildren, it will return a vector containing XMLElement: id, description, category and company.

• getTagName() and getContents() (refer to page 9)

Page 15: CSC3530 Software Technology

XSLT, XPath• How to present the XML in a browser?

– Specify a XSL file in the XML– <?xml:stylesheet type="text/xsl"

href="http://www.cse.cuhk.edu.hk/~kcsia/display.xsl"?>– XSL - eXtensible Stylesheet Language

• To transform XML document to HTML (mainly)

• XSL has two standard versions– <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

version="1.0">• version 1.0 (supported by IE 6, need to install msxml 3.0 in replace mode)• http://www.cse.cuhk.edu.hk/~kcsia/csc3530/XmlInst.exe

– <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">• Working draft (supported by IE 5.5)

Page 16: CSC3530 Software Technology

Sample XSL file<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/"><HTML><HEAD><TITLE>Query Result</TITLE></HEAD><BODY><H1>Query Result</H1><xsl:for-each select="/products/product">

Product ID: <xsl:value-of select="id"/><BR/>Product Code: <xsl:value-of select="description"/><BR/>Product Category: <xsl:value-of select="category"/><BR/><TABLE BORDER="1"><TR><TH>company name</TH><TH>price</TH><TH>on hand</TH></TR>

Page 17: CSC3530 Software Technology

Sample XSL file<xsl:for-each select="company"><xsl:sort select="price" data-type="number" order="ascending" /><TR><TD><xsl:value-of select="name"/></TD><TD><xsl:value-of select="price"/></TD><TD><xsl:value-of select="onhand"/></TD></TR></xsl:for-each></TABLE><BR/>

</xsl:for-each></BODY></HTML></xsl:template></xsl:stylesheet>

Page 18: CSC3530 Software Technology

XPath• XPath is to enable the addressing of, or navigation to,

chosen part of XML document• XSL use XPath for testing whether or not an node

matches a pattern• <xsl:template match="/">

– Xsl will process the whole xml file, / means the root• <xsl:for-each select="/products/product">

– XPath: /products/product– Find the nodes named product, with parent node named products– For-each loop will take out these node and do an iteration

• <xsl:sort select="id" data-type="text" order="ascending" />– Sort the selected nodes according to the content in child node id– Sort function is supported in XSLT version 1.0

• <xsl:value-of select="name"/>– Display the value (content) in node named “name”

Page 19: CSC3530 Software Technology

XPath• Describe a path through the XML hierarchy with a slash-

separated list of child element names• Identify all the elements that match the path• Simple query mechanism

authors/*/nameauthors

author (period)

name nationality

authors/author/*authors/author[nationality=‘Russian’]/nameauthors/author/[@period=“classical”]

Page 20: CSC3530 Software Technology

IE Setting Prompt or Enable

Page 21: CSC3530 Software Technology

Reference• TopXML

– http://www.topxml.com/default.asp• Servlet 2.1 Documentation

– http://java.sun.com/products/servlet/2.1/api/packages.html• JDK1.3 Documentation

– http://java.sun.com/j2se/1.3/docs/api/index.html• Java Tutorial

– http://java.sun.com/docs/books/tutorial/