Oracle9i Application Developer's Guide - XML Release 1 (9.0.1) Part Number A88894-01 |
|
This chapter contains the following sections:
The Oracle XML Transviewer beans are provided with Oracle9i Enterprise and Standard Editions from Release 2 (8.1.6) and higher, as part of XDK for Java Beans. If you do not have these editions you can download the beans from the site: http://otn.oracle.com/tech/xml
XML Transviewer Beans facilitate the addition of graphical or visual interfaces to your XML applications.
Bean encapsulation includes documentation and descriptors that can be accessed directly from Java Integrated Development Environments like JDeveloper.
A sample application that demonstrates all of the beans to create a simple XML editor and XSL transformer is included with your software.
The included sample application with XML SQL Utility (XSU) cause the following:
Database Connectivity is included with the XML Transviewer Beans. The beans can now connect directly to a JDBC-enabled database to retrieve and store XML and XSL files.
XML Transviewer Beans are comprised of the following beans:
The DOMBuilder bean is a non-visual bean. It builds a DOMTree from an XML document.
DOM Builder bean encapsulates the XML Parser for Java's DOMParser class with a bean interface, and extends its functionality to permit asynchronous parsing. By registering a listener, Java applications can parse large or successive documents having control return immediately to the caller. See "Using DOMBuilder Bean" .
The XSLTransformer bean is a non-visual bean. It accepts an XML file, applies the transformation specified by an input XSL stylesheet, and creates the resulting output file.
XSLTransformer bean enables you to transform an XML document to almost any text-based format including XML, HTML and DDL, by applying the appropriate XSL stylesheet.
See "Using XSLTransformer Bean".
The Treeviewer bean displays XML formatted files graphically as a tree. The branches and leaves of this tree can be manipulated with a mouse. See "Using Treeviewer Bean" .
The XMLSourceView bean is a visual Java bean. It allows visualization of XML documents and editing. It enables the display of XML and XSL formatted files with color syntax highlighting when modifying an XML document with an editing application. This helps view and edit the files. It can be easily integrated with DOM Builder bean, and allows for pre or post parsing visualization, and validation against a specified DTD. See "Using XMLSourceView Bean" .
A visual Java bean that applies XSL transformations on XML documents and shows the results. It allows editing of XML and XSL input files. See "Using XMLTransformPanel Bean" .
DBViewer bean is Java bean that can be used to display database queries or any XML by applying XSL stylesheets and visualizing the resulting HTML in a scrollable swing panel. DBViewer bean has XML and XSL tree buffers as well as a result buffer. DBViewer bean allows the calling program to do the following:
The result can be stored in the result buffer. The XML and XSL buffer content can be shown as a source or tree structure. The result buffer content can be rendered as HTML and also shown as source or tree structure. The XML buffer can be loaded from a database query.
DBAccess bean maintains CLOB tables that hold multiple XML and text documents.
Guidelines for using the XML Transviewer Beans are described in the following sections:
DOMBuilder() class implements an eXtensible Markup Language (XML) 1.0 parser according to the World Wide Web Consortium (W3C) recommendation, to parse an XML document and build a DOM tree.
The parsing is done in a separate thread and DOMBuilderListener interface must be used for notification when the tree is built.
DOM Builder bean encapsulates XML Parser for Java with a bean interface. It extends its functionality to permit asynchronous parsing. By registering a listener, a Java application can parse documents and return control return to the caller.
Asynchronous parsing in a background thread can be used interactively in visual applications. For example, when parsing a large file with the normal parser, the user interface can freeze till the parsing has completed. This can be avoided with the DOMBuilder bean. After calling the DOMBuilder bean parse method, the application can receive control back immediately and display "Parsing, please wait". If a "Cancel" button is included you can also cancel the operation. The application can continue when domBuilderOver()
method is called by DOMBuilder bean when background parsing task has completed.
When parsing a large number of files, DOMBuilder bean can save you much time. Up to 40% faster times have been recorded when compared to parsing the files one by one.
Figure 23-1 illustrates DOMBuilder Bean usage.
DOMBuilder.addDOMBuilderListener(DOMBuilderListener)
method. This adds DOMBuilderListener.
DOMBuilderOver()
method. This is called when it received an async call from an application. This interface must be implemented to receive notifications about events during asynchronous parsing. The class implementing this interface must be added to the DOMBuilder using addDOMBuilderListener
method.
Available DOMBuilderListener methods are:
DOMBuilder.getDocument()
fetches the resulting DOM document and outputs the DOM document.
The XSLTransformer bean accepts an XML file and applies the transformation specified by an input XSL stylesheet, to create and output file. It enables you to transform an XML document to almost any text-based format including XML, HTML and DDL, by applying an XSL stylesheet.
When integrated with other beans, XSLTransformer bean enables an application or user to view the results of transformations immediately.
This bean can also be used as the basis of a server-side application or servlet to render an XML document, such as an XML representation of a query result, into HTML for display in a browser.
The XSLTransformer bean encapsulates the Java XML Parser XSLT processing engine with a bean interface and extends its functionality to permit asynchronous transformation.
By registering a listener, your Java application can transform large and successive documents by having the control returned immediately to the caller.
XSL transformations can be time consuming. Use XSL Transformer bean in applications that transform large number of files. It can transform multiple files concurrently.
XSLTransformer bean can be used for visual applications for a responsive user interface. There are similar issues here as with DOMBuilder bean.
By implementing XSLTransformerListener()
method, the caller application can be notified when the transformation is complete. The application is free to perform other tasks in between requesting and receiving the transformation.
This scenario illustrates one way of applying XSLTransformer bean.
The HTML stored in the CLOB table always mirrors the last data stored in the tables being queried. A JSP (Java Server Page) can display the HTML.
In this scenario, multiple end users do not produce multiple data queries that contribute to bigger loads to the database. The HTML is regenerated only when the underlying data changes and only then.
Figure 23-2 illustrates the XSLTransformer bean usage. For examples of implementing this bean, see "Transviewer Bean Example 1: AsyncTransformSample.java".
XSLTransfomer.addXSLTransformerListener(XSLTransformerListener).
This adds a listener.
XSLTransformerListener.xslTransformerOver()
method is called. This interface must be implemented in order to receive notifications about events during the asynchronous transformation. The class implementing this interface must be added to the XSLTransformer event queue using addXSLTransformerListener
method.
Table 23-2 XSLTransformer Bean: Methods
The Treeviewer bean displays an XML document as a tree. It recognizes the following XML DOM nodes:
It takes as input an org.w3c.dom.Document object.
Figure 23-3, "Treeviewer Bean in Action: Displaying an XML Document as a Tree" shows how the Treeviewer bean displays the XML document and the editing options.
Figure 23-4 illustrates XML Treeviewer bean usage. A DOM XML document is input to XMLTreeView.setXMLDocument(doc)
method. This associates the XML Treeviewer with the XML document. Available Treeviewer bean methods are:
XMLSourceView bean is a visual Java bean that displays an XML document. It improves the viewing of XML and XSL files by color-highlighting the XML/XSL syntax. It also offers an Edit mode. XMLSourceView bean easily integrates with DOM Builder bean. It allows for pre or post parsing visualization and validation against a specified DTD.
XMLSourceView bean recognizes the following XML token types:
Each token type has a foreground color and font. The default color/font settings can be changed by the user. This takes as input, an org.w3c.dom.Document object.
Figure 23-6 shows the XMLSourceView bean usage. This is part of the oracle.xml.srcviewer
API. A DOM document inputs XMLSourceView.SetXMLDocument(Doc). The resulting DOM document is displayed. See "Transviewer Bean Example 2: ViewSample.java".
Figure 23-5, "XMLSourceView Bean in Action: Displaying an XML Document with Color Highlighting" displays an XML document with tags shown in blue, tag content in black and attributes in red.
Table 23-3 lists the XMLSourceView methods.
XMLTransformPanel visual bean applies XSL transformations to XML documents. It visualizes the result and allows editing of input XML and XSL documents and files. XMLTransformPanel bean requires no programmatic input. It is a component that interacts directly with you and is not customizable.
XMLTransformPanel bean has the following features:
CLOB tables created by the XSL Transformer bean can be used by trigger-based stored procedures to mirror tables or views in the database into HTML data held in these CLOB tables. See "XSL Transviewer Bean Scenario 1: Regenerating HTML Only When Underlying Data Changes".
Note:
The Transviewer bean is one application that illustrates the use of XMLTransform Panel bean. It can be used from a command line to perform the following actions:
"Transviewer Bean Example 3: XMLTransformPanelSample.java" for an example of how to use XMLTransformPanel.
See:
DBViewer bean can be used to display database queries on any XML document by applying XSL stylesheets and visualizing the resulting HTML in a scrollable swing panel. See:
DBViewer bean has the following three buffers:
DBViewer bean API allows the calling program to load or save buffers from various sources and apply stylesheet transformation to the XML buffer using the stylesheet in the XSL buffer. Results can be stored in the result buffer.
Content in the XML and XSL buffers can be shown as a source or tree structure. Content in the result buffer content can be rendered as HTML and also shown as a source or tree structure.
The XML buffer can be loaded using a database query. All the buffers can be loaded from and files saved from the following:
Therefore, control can also be used to move files between the file system and the user schema in the database.
Figure 23-10 illustrates DBViewer bean's usage.
Table 23-4 lists the DBViewer bean methods.
DBAccess bean maintains CLOB tables that can hold multiple XML and text documents. Each table is created using the following statement:
CREATE TABLE tablename FILENAME CHAR( 16) UNIQUE, FILEDATA CLOB) LOB(FILEDATA) STORE AS (DISABLE STORAGE IN ROW)
Each XML (or text) document is stored as a row in the table. The FILENAME field holds a unique string used as a key to retrieve, update, or delete the row. Document text is stored in the FILEDATA field. This is a CLOB object. CLOB tables are automatically maintained by the Transviewer bean. The CLOB tables maintained by DBAccess bean can be later used by the Transviewer bean. DBAccess bean does the following tasks:
Figure 23-11 illustrates the DBAccess bean usage. It shows how DBAccess bean maintains, and manipulates XML documents stored in CLOBs.
Table 23-5 lists the DBAccess bean methods.
The XDK for Java Transviewer bean sample/ directory contains sample Transviewer bean applications that illustrate how to use Oracle Transviewer beans. Oracle Transviewer beans toolset contains DOMBuilder, XMLSourceView, XMLTreeView, XSLTransformer, XMLTransformPanel, DBViewer, and DBAccess beans.
Table 23-6 lists the sample files in sample/.
File Name | Description |
---|---|
booklist.xml |
Sample XML file used by Example 1, 2, or 3 |
doc.xml |
Sample XML file used by Example 1, 2, or 3 |
doc.html |
Sample HTML file used by Examples 1, 2, or 3 |
doc.xsl |
Sample input XSL file used by Examples 1, 2, or 3. doc.xsl is used by XSLTransformer. |
emptable.xsl |
Sample input XSL file used by Examples 1, 2, or 3 |
tohtml.xsl |
Sample input XSL file used by Examples 1, 2, or 3. Transforms booklist.xml. |
See "Transviewer Bean Example 1: AsyncTransformSample.java". |
Sample nonvisual application using XSLTransformer bean and DOMBuilder bean. It applies the XSLT stylesheet specified in doc.xsl on all *.xml files from the current directory. The results are in the files with extension.log. |
Sample visual application that uses XMLSourceView and XMLTreeView beans.It visualizes XML document files. |
|
See "Transviewer Bean Example 3: XMLTransformPanelSample.java". |
A visual application that uses XMLTransformPanel bean. This bean uses all four beans from above. It applies XSL transformations on XML documents and shows the result Visualizes and allows editing of XML and XSL input files. |
See: |
A sample visual application that uses DBViewer bean to implement simple insurance claim handling application. |
The Transviewer beans require as a minimum JDK 1.1.6, and can be used with any version of JDK 1.2.
After installing this components, include classes111.zip and oraclexmlsql.jar in your classpath.
Makefile
to point to your JDK path. In addition, on Windows NT, change the file separator as stated in the Makefile
.
A few .xml files are provided as test cases. An XSL stylesheet 'doc.xsl' is used by XSLTransformer.
To use the database connectivity feature in this program, you must know the following:
You also need an account with CREATE TABLE privilege.
You can try the default account scott with password tiger if it still enabled on your Oracle9i system.
The following is the makefile
script:
# Makefile for sample java files .SUFFIXES : .java .class CLASSES = ViewSample.class AsyncTransformSample.class XMLTransformPanelSample.class # Change it to the appropriate separator based on the OS PATHSEP= : # Change this path to your JDK location. If you use JDK 1.1, you will need # to download also Swing 1.1 and add swingall.jar to your classpath. # You do not need to do this for JDK 1.2 since Swing is part of JDK 1.2 JDKPATH = /usr/local/packages/jdk1.2 # Make sure that the following product jar/zip files are in the classpath: # - Oracle JDBC driver for thin client (file classes111.zip) # - Oracle XML SQL Utility (file oraclexmlsql.jar) # You can download this products from technet.us.oracle.com # CLASSPATH :=$(CLASSPATH)$(PATHSEP)../lib/xmlparserv2.jar$(PATHSEP)../lib/xmlcomp.jar$(PATH SEP)../lib/jdev-rt.zip$(PATHSEP).$(PATHSEP) %.class: %.java $(JDKPATH)/bin/javac -classpath "$(CLASSPATH)" $< # make all class files all: $(CLASSES) sample1: XMLTransformPanelSample.class $(JDKPATH)/bin/java -classpath "$(CLASSPATH)" XMLTransformPanelSample sample2: ViewSample.class $(JDKPATH)/bin/java -classpath "$(CLASSPATH)" ViewSample sample3: AsyncTransformSample.class $(JDKPATH)/bin/java -classpath "$(CLASSPATH)" AsyncTransformSample
This example shows you how to use DOMBuilder and the XSLTransformer beans to asynchronously transform multiple XML files.
import java.net.URL; import java.net.MalformedURLException; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.OutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Vector; import org.w3c.dom.DocumentFragment; import org.w3c.dom.DOMException; import oracle.xml.async.DOMBuilder; import oracle.xml.async.DOMBuilderEvent; import oracle.xml.async.DOMBuilderListener; import oracle.xml.async.DOMBuilderErrorEvent; import oracle.xml.async.DOMBuilderErrorListener; import oracle.xml.async.XSLTransformer; import oracle.xml.async.XSLTransformerEvent; import oracle.xml.async.XSLTransformerListener; import oracle.xml.async.XSLTransformerErrorEvent; import oracle.xml.async.XSLTransformerErrorListener; import oracle.xml.async.ResourceManager; import oracle.xml.parser.v2.DOMParser; import oracle.xml.parser.v2.XMLDocument; import oracle.xml.parser.v2.XSLStylesheet; import oracle.xml.parser.v2.*; public class AsyncTransformSample { /** * uses DOMBuilder bean */ void runDOMBuilders () { rm = new ResourceManager (numXMLDocs); for (int i = 0; i < numXMLDocs; i++) { rm.getResource(); try { DOMBuilder builder = new DOMBuilder(i); URL xmlURL = createURL(basedir + "/" + (String)xmlfiles.elementAt(i)); if (xmlURL == null) exitWithError("File " + (String)xmlfiles.elementAt(i) + " not found"); builder.setPreserveWhitespace(true); builder.setBaseURL (createURL(basedir + "/")); builder.addDOMBuilderListener (new DOMBuilderListener() { public void domBuilderStarted(DOMBuilderEvent p0) {} public void domBuilderError(DOMBuilderEvent p0) {} public synchronized void domBuilderOver(DOMBuilderEvent p0) { DOMBuilder bld = (DOMBuilder)p0.getSource(); runXSLTransformer (bld.getDocument(), bld.getId()); } }); builder.addDOMBuilderErrorListener (new DOMBuilderErrorListener() { public void domBuilderErrorCalled(DOMBuilderErrorEvent p0) { int id = ((DOMBuilder)p0.getSource()).getId(); exitWithError("Error occurred while parsing " + xmlfiles.elementAt(id) + ": " + p0.getException().getMessage()); } }); builder.parse (xmlURL); System.err.println("Parsing file " + xmlfiles.elementAt(i)); } catch (Exception e) { exitWithError("Error occurred while parsing " + (String)xmlfiles.elementAt(i) + ": " + e.getMessage()); } } } /** * uses XSLTransformer bean */ void runXSLTransformer (XMLDocument xml, int id) { try { XSLTransformer processor = new XSLTransformer (id); XSLStylesheet xsl = new XSLStylesheet (xsldoc, xslURL); processor.showWarnings (true); processor.setErrorStream (errors); processor.addXSLTransformerListener (new XSLTransformerListener() { public void xslTransformerStarted (XSLTransformerEvent p0) {} public void xslTransformerError(XSLTransformerEvent p0) {} public void xslTransformerOver (XSLTransformerEvent p0) { XSLTransformer trans = (XSLTransformer)p0.getSource(); saveResult (trans.getResult(), trans.getId()); } }); processor.addXSLTransformerErrorListener (new XSLTransformerErrorListener() { public void xslTransformerErrorCalled(XSLTransformerErrorEvent p0) { int i = ((XSLTransformer)p0.getSource()).getId(); exitWithError("Error occurred while processing " + xmlfiles.elementAt(i) + ": " + p0.getException().getMessage()); } }); processor.processXSL (xsl, xml); // transform xml document } catch (Exception e) { exitWithError("Error occurred while processing " + xslFile + ": " + e.getMessage()); } } void saveResult (DocumentFragment result, int id) { System.err.println("Transforming '" + xmlfiles.elementAt(id) + "' to '" + xmlfiles.elementAt(id) + ".log'" + " applying '" + xslFile); try { File resultFile = new File((String)xmlfiles.elementAt(id) + ".log"); ((XMLNode)result).print(new FileOutputStream(resultFile)); } catch (Exception e) { exitWithError("Error occurred while generating output : " + e.getMessage()); } rm.releaseResource(); } void makeXSLDocument () { System.err.println ("Parsing file " + xslFile); try { DOMParser parser = new DOMParser(); parser.setPreserveWhitespace (true); xslURL = createURL (xslFile); parser.parse (xslURL); xsldoc = parser.getDocument(); } catch (Exception e) { exitWithError("Error occurred while parsing " + xslFile + ": " + e.getMessage()); } } private URL createURL(String fileName) throws Exception { URL url = null; try { url = new URL(fileName); } catch (MalformedURLException ex) { File f = new File(fileName); try { String path = f.getAbsolutePath(); // This is a bunch of weird code that is required to // make a valid URL on the Windows platform, due // to inconsistencies in what getAbsolutePath returns. String fs = System.getProperty("file.separator"); if (fs.length() == 1) { char sep = fs.charAt(0); if (sep != '/') path = path.replace(sep, '/'); if (path.charAt(0) != '/') path = '/' + path; } path = "file://" + path; url = new URL(path); } catch (MalformedURLException e) { exitWithError("Cannot create url for: " + fileName); } } return url; } boolean init () throws Exception { File directory = new File (basedir); String[] dirfiles = directory.list(); for (int j = 0; j < dirfiles.length; j++) { String dirfile = dirfiles[j]; if (!dirfile.endsWith(".xml")) continue; xmlfiles.addElement(dirfile); } if (xmlfiles.isEmpty()) { System.out.println("No files in directory were selected for processing"); return false; } numXMLDocs = xmlfiles.size(); return true; } private void exitWithError(String msg) { PrintWriter errs = new PrintWriter(errors); errs.println(msg); errs.flush(); System.exit(1); } void asyncTransform () throws Exception { System.err.println (numXMLDocs + " XML documents will be transformed" + " using XSLT stylesheet specified in " + xslFile + " with " + numXMLDocs + " threads"); makeXSLDocument (); runDOMBuilders (); // wait for the last request to complete while (rm.activeFound()) Thread.sleep(100); } String basedir = new String ("."); OutputStream errors = System.err; Vector xmlfiles = new Vector(); int numXMLDocs = 1; String xslFile = new String ("doc.xsl"); URL xslURL; XMLDocument xsldoc; private ResourceManager rm; /** * main */ public static void main (String args[]) { AsyncTransformSample inst = new AsyncTransformSample(); try { if (!inst.init()) System.exit(0); inst.asyncTransform (); } catch (Exception e) { e.printStackTrace(); } System.exit(0); } }
This example shows you how to use XMLSourceView and XMLTreeView beans to visually represent XML files.
import java.awt.*; import oracle.xml.srcviewer.*; import oracle.xml.treeviewer.*; import oracle.xml.parser.v2.XMLDocument; import oracle.xml.parser.v2.*; import org.w3c.dom.*; import java.net.*; import java.io.*; import java.util.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class ViewSample { public static void main(String[] args) { String fileName = new String ("booklist.xml"); if (args.length > 0) { fileName = args[0]; } JFrame frame = setFrame ("XMLViewer"); XMLDocument xmlDocument = getXMLDocumentFromFile (fileName); XMLSourceView xmlSourceView = setXMLSourceView (xmlDocument); XMLTreeView xmlTreeView = setXMLTreeView (xmlDocument); JTabbedPane jtbPane = new JTabbedPane (); jtbPane.addTab ("Source", null, xmlSourceView, "XML document sorce view"); jtbPane.addTab ("Tree", null, xmlTreeView, "XML document tree view"); jtbPane.setPreferredSize (new Dimension(400,300)); frame.getContentPane().add (jtbPane); frame.setTitle (fileName); frame.setJMenuBar (setMenuBar()); frame.setVisible (true); } static JFrame setFrame (String title) { JFrame frame = new JFrame (title); //Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation ((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.getContentPane().setLayout (new BorderLayout()); frame.setSize(new Dimension(400, 300)); frame.setVisible (false); frame.setTitle (title); return frame; } static JMenuBar setMenuBar () { JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu ("Exit"); menu.addMenuListener ( new MenuListener () { public void menuSelected (MenuEvent ev) { System.exit(0); } public void menuDeselected (MenuEvent ev) {} public void menuCanceled (MenuEvent ev) {} }); menuBar.add (menu); return menuBar; } /** * creates XMLSourceView object */ static XMLSourceView setXMLSourceView(XMLDocument xmlDocument) { XMLSourceView xmlView = new XMLSourceView(); xmlView.setXMLDocument(xmlDocument); xmlView.setBackground(Color.yellow); xmlView.setEditable(true); return xmlView; } /** * creates XMLTreeView object */ static XMLTreeView setXMLTreeView(XMLDocument xmlDocument) { XMLTreeView xmlView = new XMLTreeView(); xmlView.setXMLDocument(xmlDocument); xmlView.setBackground(Color.yellow); return xmlView; } static XMLDocument getXMLDocumentFromFile (String fileName) { XMLDocument doc = null; try { DOMParser parser = new DOMParser(); try { String dir= "" ; FileInputStream in = new FileInputStream(fileName); parser.setPreserveWhitespace(false); parser.setBaseURL(createURL(dir)); parser.parse(in); in.close(); } catch (Exception ex) { ex.printStackTrace(); System.exit(0); } doc = (XMLDocument)parser.getDocument(); try { doc.print(System.out); } catch (Exception ie) { ie.printStackTrace(); System.exit(0); } } catch (Exception e) { e.printStackTrace(); } return doc; } static URL createURL(String fileName) { URL url = null; try { url = new URL(fileName); } catch (MalformedURLException ex) { File f = new File(fileName); try { String path = f.getAbsolutePath(); String fs = System.getProperty("file.separator"); if (fs.length() == 1) { char sep = fs.charAt(0); if (sep != '/') path = path.replace(sep, '/'); if (path.charAt(0) != '/') path = '/' + path; } path = "file://" + path; url = new URL(path); } catch (MalformedURLException e) { System.out.println("Cannot create url for: " + fileName); System.exit(0); } } return url; } }
This example is an interactive application that uses XMLTransformPanel bean to do the following:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import oracle.xml.transviewer.XMLTransformPanel; public class XMLTransformPanelSample { XMLTransformPanel transformPanel = new XMLTransformPanel(); /** * Adjust frame size and add transformPanel to it. */ public XMLTransformPanelSample () { Frame frame = new JFrame(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setSize(510,550); transformPanel.setPreferredSize(new Dimension(510,550)); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation ((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setVisible(true); ((JFrame)frame).getContentPane().add (transformPanel); frame.pack(); } /** * main(). Only creates XMLTransformPanelSample object. */ public static void main (String[] args) { new XMLTransformPanelSample (); } }
This is an interactive example which lets you input the name or policy of an insurance claim. The appropriate claim is loaded as an XML buffer from the result set of an XML query. An XSL stylesheet is loaded from the file system. The DBViewer bean transforms the XML buffer using the XSL stylesheet to HTML. This HTML output can then be viewed.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import oracle.jdeveloper.layout.*; import oracle.xml.dbviewer.*; public class DBViewClaims extends JPanel { DBViewer dbPanel= new DBViewer(); JButton searchButton = new JButton(); XYLayout xYLayout1 = new XYLayout(); JLabel titleLabel = new JLabel(); JLabel nameLabel = new JLabel(); JLabel policyLabel = new JLabel(); JTextField nameTF = new JTextField(); JTextField policyTF = new JTextField(); JButton viewXMLButton = new JButton(); JButton viewXSLButton = new JButton(); JButton viewHTMLButton = new JButton(); public DBViewClaims() { super(); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { setBackground(SystemColor.controlLtHighlight); this.setLayout(xYLayout1); searchButton.setText("searchButton"); searchButton.setLabel("Search"); xYLayout1.setHeight(464); xYLayout1.setWidth(586); titleLabel.setText("List of Claims"); titleLabel.setHorizontalAlignment(SwingConstants.CENTER); titleLabel.setBackground(new Color(192, 192, 255)); titleLabel.setFont(new Font("Dialog", 1, 16)); nameLabel.setText("Last Name"); policyLabel.setText("Policy:"); viewXMLButton.setText("viewXMLButton"); viewXMLButton.setLabel("view XML"); viewXMLButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { viewXMLButton_actionPerformed(e); } }); viewXSLButton.setText("viewXSLButton"); viewXSLButton.setLabel("view XSL"); viewXSLButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { viewXSLButton_actionPerformed(e); } }); viewHTMLButton.setText("viewHTMLButton"); viewHTMLButton.setLabel("view HTML"); viewHTMLButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { viewHTMLButton_actionPerformed(e); } }); searchButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { searchButton_actionPerformed(e); } }); this.add(dbPanel, new XYConstraints(16, 55, 552, 302)); this.add(searchButton, new XYConstraints(413, 415, 154, 29)); this.add(titleLabel, new XYConstraints(79, 10, 413, 31)); this.add(nameLabel, new XYConstraints(333, 373, 72, -1)); this.add(policyLabel, new XYConstraints(334, 395, 59, -1)); this.add(nameTF, new XYConstraints(413, 368, 155, -1)); this.add(policyTF, new XYConstraints(413, 391, 156, -1)); this.add(viewXMLButton, new XYConstraints(19, 359, 94, 29)); this.add(viewXSLButton, new XYConstraints(19, 390, 94, 29)); this.add(viewHTMLButton, new XYConstraints(19, 421, 94, 29)); updateUI(); } void searchButton_actionPerformed(ActionEvent e) { String sqlText="select * from s_claim c "; try { if (!nameTF.getText().equals("")) { sqlText=sqlText+" where c.claimpolicy.primaryinsured.lastname="+ "'"+nameTF.getText()+"'"; } else if (!policyTF.getText().equals("")) { sqlText=sqlText+" where c.claimpolicy.policyid="+ policyTF.getText(); } dbPanel.setUsername("scott"); dbPanel.setPassword("tiger"); dbPanel.setInstancename("orcl"); dbPanel.setHostname("localhost"); dbPanel.setPort("1521"); dbPanel.loadXMLBufferFromSQL(sqlText); dbPanel.loadXslBuffer("xslfiles","CLAIM.XSL"); dbPanel.transformToRes(); dbPanel.setResHtmlView(true); } catch (Exception e1) { System.out.println(e1); } } void viewXMLButton_actionPerformed(ActionEvent e) { dbPanel.setXmlSourceEditView(true); } void viewXSLButton_actionPerformed(ActionEvent e) { dbPanel.setXslSourceEditView(true); } void viewHTMLButton_actionPerformed(ActionEvent e) { dbPanel.setResHtmlView(true); } }
This example provides a frame with a menu bar to access the DBView Claims functionality. Claims can then be loaded and displayed in HTML.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import oracle.jdeveloper.layout.*; public class DBViewFrame extends JFrame { JMenuBar menuBar1 = new JMenuBar(); JMenu menuFile = new JMenu(); JMenuItem menuFileExit = new JMenuItem(); JMenuItem menuListCustomerClaims = new JMenuItem(); public DBViewFrame() { super(); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.getContentPane().setLayout(new GridLayout(1,1)); this.setSize(new Dimension(600, 550)); menuFile.setText("File"); menuFileExit.setText("Exit"); menuListCustomerClaims.setText("List Claims"); menuFileExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fileExit_ActionPerformed(e); } }); menuListCustomerClaims.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ListCustomerClaims_ActionPerformed(e); } }); menuFile.add(menuFileExit); menuFile.add(menuListCustomerClaims); menuBar1.add(menuFile); this.setJMenuBar(menuBar1); this.setBackground(SystemColor.controlLtHighlight); } void fileExit_ActionPerformed(ActionEvent e) { System.exit(0); } void ListCustomerClaims_ActionPerformed(ActionEvent e) { this.getContentPane().removeAll(); this.getContentPane().add(new DBViewClaims()); this.getContentPane().paintAll(this.getGraphics()); } }
This example simply provides a main function which instantiates DBViewFrame, giving it a specific look and feel.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DBViewSample { public DBViewSample() { DBViewFrame frame = new DBViewFrame(); frame.setVisible(true); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } new DBViewSample(); } }
|
Copyright © 1996-2001, Oracle Corporation. All Rights Reserved. |
|