Języki i Techniki Programowania II Wykład 13 TRaX, Applety, Java Security
XPath javax.xml.xpath XPathFactory factory = XPathFactory.newInstance() (DEFAULT_OBJECT_MODEL_URI) XPath xp = factory.newxpath() xp.evaluate(string expression, InputSource is)
XPath /step/step... bądź step/step... step jest postaci axisname::nodetest[predicate] axis: ancestor, ancestor-or-self, attribute, child, descendant, descendant-or-self, following, following-sibling, parent, preceding, precedingsibling, self test: nazwa, wyrażenie albo text(), node()
XPath <?xml version="1.0" encoding="iso 8859 1"?> <bookstore> <book> <title lang="eng">harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">learning XML</title> <price>39.95</price> </book> </bookstore>
XPath Przykłady: xmldoc.selectnodes("bookstore") dzieci bookstore xmldoc.selectnodes("/bookstore") bookstore xmldoc.selectnodes("/bookstore/book") dzieci typu book xmldoc.selectnodes("//book") jakiekolwiek book xmldoc.selectnodes("bookstore//book ) jakiekolwiek book o przodku bookstore xmldoc.selectnodes("//@lang ) wszystkie atrybuty lang
XPath Przykłady z warunkami: xmldoc.selectnodes("/bookstore/book[0]") pierwszy book xmldoc.selectnodes("/bookstore/book[last()]") ostatni book xmldoc.selectnodes("/bookstore/book[price>35]/price") xmldoc.selectnodes("/bookstore/book[price>35]/title") xmldoc.selectnodes("/bookstore/book[@category='cooking'] [@available='true'] ) xmldoc.selectnodes("/bookstore/*") jakiekolwiek dziecko xmldoc.selectnodes("///book[@*]") book ktory ma jakikolwiek atrybut xmldoc.selectnodes("//book //title") unia wezlow
XPath Przykłady z osiami: node.selectnodes("child::book") potomkowie book node.selectnodes("sibling::*") rodzenstwo obecnego wezla node.selectnodes("following::*") wszystkie wezly dalej w dokumencie node.selectnodes("following sibling::*") wszystkie wezly rodzenstwo dalej w dokumencie node.selectnodes("descendant::*") wszyscy potomkowie node.selectnodes("parent::*") przodek
Transformacje XSLT Transformacje: zamieniają dokument XML przy użyciu dokumentu XSL <xsl:template> - value match określa przez XPath jaką cześć dokumentu XML zastosować <xsl:template match="/"><html>...</html></xsl:template match> <xsl:value-of select="catalog/cd/title"/> - wyciąga przez XPath wartość z dokumentu XML <xsl:for-each select="catalog/cd"> - stosowany jest dla każdego węzła z podanego zbioru <xsl:sort select= artist > - sortuje wewnątrz for-each
Transformacje XSLT <xsl:choose> <xsl:when test="expression">... some output... </xsl:when> <xsl:otherwise>... some output... </xsl:otherwise> </xsl:choose> <xsl:if test=... >...</xsl:if>
XML Przykładowy <?xml version="1.0" encoding="iso 8859 1"?> <?xml stylesheet type="text/xsl" href="cdcatalog.xsl"?> <catalog> <cd> <title>empire Burlesque</title> <artist>bob Dylan</artist> <country>usa</country> <company>columbia</company> <price>10.90</price> <year>1985</year> </cd>... </catalog>
XSL Style Sheet <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <body> <h2>my CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">title</th> <th align="left">artist</th> </tr>
XSL Style Sheet <xsl:for each select="catalog/cd"> <tr> <td><xsl:value of select="title"/></td> <td><xsl:value of select="artist"/></td> </tr> </xsl:for each> </table> </body> </html> </xsl:template>
XSL Style Sheet <xsl:sort select="artist"/> <xsl:if test="price > 10">......some output if the expression is true...... </xsl:if>
XSL Style Sheet <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"> <xsl:value of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value of select="artist"/></td> </xsl:otherwise> </xsl:choose>
XSL Style Sheet <?xml version="1.0" encoding="iso 8859 1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <body> <h2>my CD Collection</h2> <xsl:apply templates/> </body> </html> </xsl:template>
XSL Style Sheet <xsl:template match="cd"> <p> <xsl:apply templates select="title"/> <xsl:apply templates select="artist"/> </p> </xsl:template> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value of select="."/></span> <br /> </xsl:template> </xsl:stylesheet>
Transformacje XSLT TransformerFactory factory = TransformerFactory.newInstance(); factory.seterrorresolver(myerrorresolver); factory.seturiresolver(myuriresolver); Transformer transformer = factory.newtransformer(new StreamSource("sheet.xsl")); transfomer.transform(new StreamSource("documentA.xml"), new StreamResult(System.out));SAX
Transformacje XSLT Dopuszczalne typy rezultatów: SAXResult DOMResult StreamResult Zmienia najczęściej jeden dokument XML w drugi
Applety <applet name="jsapplet" [niewymagany] archive="jsapplet.jar" code="helloworldapplet" width="300" MAYSCRIPT [niewymagany] height="300" codebase=url [niewymagany] alt=text [niewymagany] > <param name=parametr1 value= 4 >... </applet>
Applety <object classid="clsid:8ad9c840 044E 11D1 B3E9 00805F499D93" width= "290" height= "290" style="border width:0;" id="rup" name="rup" codebase="http://java.sun.com/products/plugin/autodl/jinstall 1_4_1 windows i586.cab#version=1,4,1"> <param name="archive" value="jsapplet.jar"> <param name="code" value="com.raditha.articles.jshelloworld"> <param name="mayscript" value="yes"> <param name="scriptable" value="true"> <param name="name" value="jsapplet"> </object>
Applety import javax.swing.japplet; import java.awt.graphics; public class HelloWorld extends JApplet { public void paint(graphics g) { g.drawrect(0, 0, getsize().width 1, getsize().height 1); g.drawstring("hello world!", 5, 15); } }
Applety import java.applet.applet; import java.awt.graphics; public class Simple extends Applet { StringBuffer buffer; public void init() { buffer = new StringBuffer(); additem("initializing... "); } public void start() { additem("starting... "); }
Applety public void stop() { additem("stopping... "); } public void destroy() { additem("preparing for unloading..."); } private void additem(string newword) { System.out.println(newWord); buffer.append(newword); repaint(); }
Applety public void paint(graphics g) { //Draw a Rectangle around the applet's display area. g.drawrect(0, 0, getwidth() 1, getheight() 1); //Draw the current string inside the rectangle. g.drawstring(buffer.tostring(), 5, 15); } }
Applety public void paint(graphics g) { //Draw a Rectangle around the applet's display area. g.drawrect(0, 0, getwidth() 1, getheight() 1); //Draw the current string inside the rectangle. g.drawstring(buffer.tostring(), 5, 15); } }
Applety Applety nie mogą: Ładować bibliotek lokalnych i wywoływać metod natywnych Czytać i pisać do plików Łączyć się z innym hostem niż ten z którego zostały ściągnięte Uruchamiać lokalnie poleceń Okna otwierane przez applet wyglądają inaczej niż te otwierane przez aplikację
Applety Applety: Uruchamiane mogą być są przez appletviewer Odmalowywanie odbywa się poprzez: wywołanie paint(graphics g) przez wątek obsługi zdarzeń (np. gdy zostanie odsłonięty ekran) można przeciążyć wywołanie repaint() które informuje wątek obsługi zdarzeń by wywołał update() update(graphics g) czyści kontekst, woła paint można przeciążyć
Applety Double buffering init(): offscreen = createimage(dim.width,dim.height); buffergraphics = offscreen.getgraphics(); paint(): buffergraphics.clearrect (0,0,dim.width,dim.width); buffergraphics.fillrect(curx,cury,20,20);... update składa sie wtedy tylko z paint()
Applety Applety mają: Dostęp do przeglądarki w której zostały uruchomione getappletcontext().showdocument(url); getappletcontext().showdocument(url,target); Dostęp do metod publicznych appletów na tej samej stronie Applet2 applet2 = (Applet2)getAppletContext().getApplet ("applet2"); Możliwość wywoływać JavaScript import netscape.javascript.jsobject;//!!!!! call(string,object[]) eval(string)
Bezpieczeństwo Appletów policytool - java.home\lib\security\java.security
Podpisywanie Appletów Czynności nadawcy: keytool -genkey służy do generowania self-signed certificate jarsigner służy do podpisywania appletów keytool -export służy do przekazania klucza publicznego odbiorcy Czynności odbiorcy: keytool -import importuje klucz zmiana policy przez podanie odpowiedniego klucza
Podpisywanie Appletów Czynności nadawcy: keytool -certreq stworzenie Certificate Signing Request, i przesłanie do Certificate Authority (np. Verisign) keytool -import zaimportowanie odpowiedzi CA jarsigner Czynności odbiorcy: