1. Trang chủ
  2. » Công Nghệ Thông Tin

Oracle XSQL- P29 ppt

20 99 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Oracle. XSQL Combining SQL, Oracle Text,XSLT, and Java to Publish Dynamic Web Content

    • Cover

  • Contents

  • About the Author

  • Chapter 1 Introducing Oracle XSQL

  • Chapter 2 Getting Started with XSQL

  • Chapter 3 Hello, XSQL!

  • Chapter 4 XSQL Architecture

  • Chapter 5 Writing XSQL Pages

  • Chapter 6 XSQL Parameters

  • Chapter 7 Database Modifications with XSQL

  • Chapter 8 Oracle SQL

  • Chapter 9 PL/SQL

  • Chapter 10 Using Oracle Text 253

  • Chapter 11 Retrieving XML

  • Chapter 12 XSLT

  • Chapter 13 XSLT In-Depth

  • Chapter 14 Building XSQL Web Applications

  • Chapter 15 Command Line Utility 443

  • Chapter 16 Web Services with XSQL

  • Chapter 17 XSQL Beyond Web Browsing

  • Chapter 18 Custom Action Handlers

  • Chapter 19 Serializers

  • Appendix A Resources

  • Appendix B Related Standards

  • Index

  • Team DDU

Nội dung

Figure 19.7 Architecture for the AWT custom serializer. At this point, you know what your serializer should accept as input, so you can go ahead and start coding it. Here is the requisite class information and the serialize method: import oracle.xml.xsql.XSQLPageRequest; import oracle.xml.xsql.XSQLDocumentSerializer; import oracle.xml.parser.v2.XMLElement; import java.io.OutputStream; import java.io.PrintWriter; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; import java.awt.image.BufferedImage; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import java.awt.geom.Line2D; import java.awt.Color; <ROWSET> <ROW> </ROW> <ROW> </ROW> </ROWSET> XSQL Datagram XSLT Processor XSLT Stylesheet XML doc in custom schema JPEG Client Custom Serializer AWT 540 Chapter 19 import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; public class JpegGraphSerializer implements XSQLDocumentSerializer { float scale=1; /** * Takes an XML document that behaves the previously defined * schema and outputs a JPEG image created using AWT */ public void serialize(Document doc, XSQLPageRequest env) { String mimeType=”image/jpeg”; try { XMLElement rootElem=(XMLElement)doc.getDocumentElement(); // Get the top level information scale=getScale(rootElem); int width=getWidth(rootElem); int height=getHeight(rootElem); Node colorNode=rootElem.selectSingleNode(“./image/background”); Color bgcolor=getColor((XMLElement)colorNode); // Create an image and get the graphics used to draw with BufferedImage img=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g=img.createGraphics(); // Set up the Image g.setPaint(bgcolor); Rectangle2D allRect = new Rectangle2D.Float(0,0,width,height); g.fill(allRect); // Draw the bars based on the input XML doc NodeList elems=rootElem.getElementsByTagName(“bar”); drawBars(g,elems); // Write the data env.setContentType(mimeType); OutputStream out=env.getOutputStream(); JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); encoder.encode(img); out.close(); } catch (Exception e) { Serializers 541 env.setContentType(“text/html”); PrintWriter out=env.getWriter(); out.println(“An error has occurred”); out.println(e.getMessage()); e.printStackTrace(out); } } The preceding serialize() method sets up the initial image based on top-level parameters, calls the drawBars() method to do the actual drawing, and the uses the JPEGCodec class to write the JPEG data to the output stream. If any exception is thrown, an error message will be printed as an HTML file. The next method to exam- ine is the drawBars() method: private void drawBars(Graphics2D g, NodeList elems) throws Exception { int startX=20; int startY=20; for (int i=0;i<elems.getLength();i++) { XMLElement bar=(XMLElement)elems.item(i); startY=drawBar(g,bar,startX,startY); } } The main function of this method is to manage the position where the next bar should be drawn. The starting point is hard-coded in this example, but it could certainly be configurable. The real work of drawing is handled in the drawBar() method: private int drawBar(Graphics2D g, XMLElement bar, int x, int y) throws Exception{ // Get the data from the XML element String topMarginStr=bar.valueOf(“./@topmargin”); int topMargin=Integer.parseInt(topMarginStr); String bottomMarginStr=bar.valueOf(“./@bottommargin”); int bottomMargin=Integer.parseInt(bottomMarginStr); String heightStr=bar.valueOf(“./@height”); int height=Integer.parseInt(heightStr); String valueStr=bar.valueOf(“./@value”); int value=Integer.parseInt(valueStr); String title=bar.valueOf(“./title”); String titleWidthStr=bar.valueOf(“./title/@width”); int titleWidth=Integer.parseInt(titleWidthStr); Node colorNode=bar.selectSingleNode(“./barcolor”); Color barColor=getColor((XMLElement)colorNode); 542 Chapter 19 colorNode=bar.selectSingleNode(“./title/textcolor”); Color textColor=getColor((XMLElement)colorNode); title=title.trim(); // Do the drawing //Set the start position y=y+topMargin; // Write the title g.setPaint(textColor); g.drawString(title,x,y+height); // Draw the bar g.setPaint(barColor); Rectangle2D rect = new Rectangle2D.Float(x+titleWidth, y, value*scale,height); g.fill(rect); // Write the bar g.setPaint(textColor); g.drawString(“”+value,x+titleWidth+value*scale+10,y+height); // Return the next starting position return y+height+bottomMargin; } The code of this method is divided between two purposes: reading the data from the XML element for a particular bar and drawing the bar. A custom serializer will always need some strategy for reading its input from the XML document. What you do with that data varies widely. In this case, you are doing some simple drawing using AWT, but you are really unlimited in what you can do. The remainder of the class is displayed as follows. It consists of several helper functions. private int getWidth(XMLElement elem) throws Exception { String widthStr=elem.valueOf(“./image/@width”); return Integer.parseInt(widthStr); } private int getHeight(XMLElement elem) throws Exception { String heightStr=elem.valueOf(“./image/@height”); return Integer.parseInt(heightStr); } Serializers 543 private float getScale(XMLElement elem) throws Exception { String scaleStr=elem.valueOf(“./image/@scale”); if (scaleStr!=null) { return Float.parseFloat(scaleStr); } else { return 1; } } private Color getColor(XMLElement elem) throws Exception { String redStr=elem.valueOf(“@red”); String greenStr=elem.valueOf(“@green”); String blueStr=elem.valueOf(“@blue”); int red=Integer.parseInt(redStr); int green=Integer.parseInt(greenStr); int blue=Integer.parseInt(blueStr); return new Color(red,green,blue); } } Since this last snippet of code concludes the class, the last brace is the closing brace for the class. Your next steps are writing the XSQL and XSLT pages. It doesn’t really matter what your XSLT pages look like, as long as they produce an XML document that obeys the schema for this serializer. The XSQL and XSLT pages presented here produce the chart displayed earlier in this chapter. Here’s the XSQL page: <?xml version=”1.0”?> <?xml-stylesheet type=”text/xsl” href=”jpeg-graph-serializer.xsl” serializer=”java:JpegGraphSerializer”?> <page connection=”demo” xmlns:xsql=”urn:oracle-xsql”> <xsql:query> SELECT ename, sal FROM emp WHERE deptno={@deptno} </xsql:query> <xsql:set-stylesheet-param name=”max-value”> SELECT max(sal) FROM emp WHERE deptno={@deptno} </xsql:set-stylesheet-param> </page> 544 Chapter 19 This is pretty standard fare. The only thing that differs from what you’ve seen many times before is the serializer attribute in the stylesheet processing instruction. The stylesheet, displayed as follows, also requires the max-value parameter. This param- eter isn’t required of the serializer, but it is of the stylesheet. <?xml version=”1.0”?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0”> <xsl:param name=”max-value”/> <xsl:param name=”width”>1000</xsl:param> <xsl:param name=”title_width”>100</xsl:param> <xsl:param name=”scale-factor”><xsl:value-of select=”800 div $max- value”/></xsl:param> <xsl:param name=”height”><xsl:value-of select=”count(/page/ROWSET/ROW)*30+50”/></xsl:param> <xsl:template match=”/page”> <graph> <image> <xsl:attribute name=”height”><xsl:value-of select=”$height”/></xsl:attribute> <xsl:attribute name=”width”><xsl:value-of select=”$width”/></xsl:attribute> <xsl:attribute name=”scale”><xsl:value-of select=”$scale- factor”/></xsl:attribute> <background red=”200” green=”200” blue=”200”/> </image> <xsl:apply-templates select=”ROWSET/ROW”/> </graph> </xsl:template> <xsl:template match=”ROW”> <bar leftmargin=”10” topmargin=”10” bottommargin=”10” height=”10”> <xsl:attribute name=”value”><xsl:value-of select=”SAL”/></xsl:attribute> <title> <xsl:attribute name=”width”><xsl:value-of select=”$title_width”/></xsl:attribute> <text><xsl:value-of select=”ENAME”/></text> <textcolor red=”255” green=”0” blue=”0”/> </title> <barcolor red=”0” green=”0” blue=”255”/> </bar> </xsl:template> </xsl:stylesheet> Serializers 545 The majority of this stylesheet works to create the bar elements needed by the seri- alizer. The first few lines, though, are occupied in specifying the size correctly based on the number of rows of data and the correct scaling factor. By leaving this work to the stylesheet, you can have a simpler serializer that can handle a lot of different types of data. Moving On In this chapter you have learned how to use the final tool in the XSQL arsenal— serializers. They are especially apt for the creation of binary data. By using serializers, you can create any kind of data presentation that you wish. Often, this will mean lever- aging existing tools such as FOP, but you are also free to write the binary formats directly from the ground up. You now know all you need to know to easily create robust applications by using XSQL. You probably learned a number of new paradigms along the way, but hopefully you can now see how XSQL can make applications easily. Have fun! 546 Chapter 19 547 This appendix covers some resources that you should find useful as you develop XSQL applications. Code Examples from This Book All of the code examples from this book are available on the book’s Web site. You can visit the Web site at www.wiley.com/compbooks/thomas. The code examples are indexed by the page numbers on which they appear. Oracle Technet The Oracle Technology Network’s Web site resides at http://technet .oracle.com. You’ll need to register. Once you’ve registered, you’ll have access to a great variety of resources about Oracle, including white papers, product documenta- tion, and discussion forums. The most relevant discussion forum for XSQL is the XML forum. Resources CHAPTER APPENDIX A Oracle Database Resources You’ll find a lot of the best resources at http://technet.oracle.com, where you can find complete reference manuals for SQL, PL/SQL, Text, and XML functionality. Here are some especially useful links. SQL Manual for Oracle 9i This manual covers all the specifics of Oracle SQL. You’ll find the manual at http://otn.oracle.com/docs/products/oracle9i/doc_library /release2/server.920/a96540/toc.htm. PL/SQL Reference This manual covers all you need to know about PL/SQL. You’ll find the manual at http://otn.oracle.com/docs/products/oracle9i/doc_library/ release2/appdev.920/a96624/toc.htm. In this book, you used just a couple of supplied PL/SQL packages. Oracle offers a ton that you didn’t use. They are described in a document at http://otn.oracle.com/ docs/products/oracle9i/doc_library/release2/appdev.920/a96612/ toc.htm. Oracle XML DB The Oracle XML DB offers information about the XML storage functionality of the Oracle database. There is a lot of information at this site about the Oracle XMLType that you learned about in Chapter 11, which discussed using Oracle Text with XML documents. You’ll find the information at http://otn.oracle.com/docs/prod- ucts/oracle9i/doc_library/release2/appdev.920/a96620/toc.htm. Oracle XML Developer’s Kit You can find information about the Oracle XML Developer’s Kit (XDK) at http://otn.oracle.com/tech/xml/xdkhome.html. The XDK includes XSQL as well as the XMLAPIs that you used in the last three chap- ters of the book. If you are using XSQL in conjunction with Java—either by embedding XSQL in your code or writing custom action handlers or serializers—you can find the API documentation available in javadoc format at http://otn.oracle.com/docs /tech/xml/xdk_java/doc_library/Production9i/index.html. The Oracle XDK and the Oracle XML DB are considered separate by Oracle. If you want information about storing XML in the database (e.g., XMLType), you should look at the foregoing XMLType link. The Oracle XDK covers XML that is derived from the database and focuses on how to create XML from data stored in the traditional rela- tional format. It also includes the Oracle XML parser, XSLT processor, and other pieces that can be used with or without the database. 548 Appendix A XSLT Resources XSLT is extremely important in XSQL development. As a popular standard, there is a lot of information available on the Web. The definitive source of information is the XSLT Recommendation from W3C. You’ll find the information at http://www .w3.org/TR/xslt. However, this document covers a lot of information only needed by developers implementing an XSLT processor. A better starting point for the beginning XSLT devel- oper is http://www.xslt.com. Another great resource is available at zvon.org. This site provides an interactive guide to all of the XSLT elements and XPath. You’ll find the guide at http://www .zvon.org/xxl/XSLTreference/Output/index.html. Java Resources The definitive resource for Java is http://www.javasoft.com. You can also find information that is specific to Oracle and Java at the XDK links listed earlier. Resources 549 [...]... /batik/ SQLJ and Java-Stored Procedures This book showed you how to create stored procedures using PL/SQL You can also create stored procedures using Java This is known as SQLJ You find the Oracle guide to SQLJ at http://otn .oracle. com/tech/java/sqlj_jdbc/pdf/a96655.pdf SQLJ is implemented with a precompiler It translates statements in your Java source code to calls to the SQLJ APIs These calls sit on top... on dynamic data derived from your database You simply write an XSLT stylesheet that converts the XSQL datagram to an SVG document An example of how to do this is included in the XSQL distribution from Oracle SVG is a W3C recommendation and is available at http://www.w3.org /TR/SVG/ Adobe has some of the best information about SVG on its Web site at http://www.adobe.com/svg/main.html There are two ways... See also tags elements clause, 143–147 ELSEIF keyword, 237 empty_blob function, 223 empty_clob function, 223 ENAME element, 299, 301 encoding attribute, 292 END statement, 234–235 Enterprise Manager (Oracle) , 38 entities, creating, 314–319 error handling Web application development and, 375, 435, 438–442 Web services and, 453 errors compilation, 226 field names and, 71 installation and, 26 parameters... 495, 502 HASPATH operator, 258 hello_pkg.hello_plsql function, 248 hello_plsql function, 226–229 “Hello PL/SQL!” string, 225–227 hextoraw function, 220, 221 high-level architecture Java parts of, 54–58 Oracle XML modules and, 63–65 overview of, 53–54 XSQL command-line utility and, 58–59 See also architecture h1 h6 elements, 281 tags, 14, 283 href attribute, 85, 314 href=”string” attribute, 90 . http://otn .oracle. com/ docs/products /oracle9 i/doc_library/release2/appdev.920/a96612/ toc.htm. Oracle XML DB The Oracle XML DB offers information about the XML storage functionality of the Oracle. about the Oracle XMLType that you learned about in Chapter 11, which discussed using Oracle Text with XML documents. You’ll find the information at http://otn .oracle. com/docs/prod- ucts /oracle9 i/doc_library/release2/appdev.920/a96620/toc.htm. Oracle. javadoc format at http://otn .oracle. com/docs /tech/xml/xdk_java/doc_library/Production9i/index.html. The Oracle XDK and the Oracle XML DB are considered separate by Oracle. If you want information

Ngày đăng: 03/07/2014, 08:20

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN