Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.26 MB, 774 trang )
Installing the Transviewer Bean Samples
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
new DBViewSample();
}
}
XMLDiffSample.java
import oracle.xml.parser.v2.*;
import oracle.xml.async.*;
import oracle.xml.differ.*;
import
import
import
import
import
import
java.io.*;
java.awt.*;
javax.swing.*;
javax.swing.tree.*;
java.net.URL;
java.net.MalformedURLException;
public class XMLDiffSample
{
/**
* Constructor
*/
public XMLDiffSample() {
}
/**
* main
* @param args
*/
public static void main(String[] args)
{
dfxApp = new XMLDiffSample();
diffFrame = new XMLDiffFrame(dfxApp);
diffFrame.addTransformMenu();
xmlDiff = new XMLDiff();
if (args.length == 3)
XDK JavaBeans 10-55
Installing the Transviewer Bean Samples
outFile = args[2];
/* Use the default outFile name = XMLDiffSample.xsl */
if(args.length >= 2)
dfxApp.showDiffs(new File(args[0]), new File(args[1]));
diffFrame.setVisible(true);
}
public void showDiffs(File file1, File file2)
{
try
{
xmlDiff.setFiles(file1, file2);
/* Check if files are equal */
if(!xmlDiff.diff())
{
JOptionPane.showMessageDialog(diffFrame,
"Files are equivalent in XML representation",
"XMLDiffSample Message",
JOptionPane.PLAIN_MESSAGE);
}
/* generate xsl file */
xmlDiff.generateXSLFile(outFile);
/* parse the xsl file created, alternately you can use
generateXSLDoc to get the xsl as a document tree instead of a file */
parseXSL();
/* Display the document trees created by the xmlDiff object */
diffFrame.makeSrcPane(xmlDiff.getDocument1(), xmlDiff.getDocument2());
diffFrame.makeDiffSrcPane(new XMLDiffSrcView(xmlDiff.getDiffPane1()),
new XMLDiffSrcView(xmlDiff.getDiffPane2()));
diffFrame.makeXslPane(xslDoc, "Diff XSL Script");
diffFrame.makeXslTabbedPane();
}catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(diffFrame,
"File Not Found: "+e.getMessage(),
"XMLDiffSample Error Message",
JOptionPane.ERROR_MESSAGE);
}
catch (Exception e)
{
e.printStackTrace();
10-56 Oracle9i XML Developer’s Kits Guide - XDK
Installing the Transviewer Bean Samples
JOptionPane.showMessageDialog(diffFrame,
"Error: "+e.getMessage(),
"XMLDiffSample Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
public void doXSLTransform()
{
try
{
doc1 = xmlDiff.getDocument1();
doc2 = xmlDiff.getDocument2();
XSLProcessor xslProc = new XSLProcessor();
/* Using the xsl stylesheet generated (xslDoc), transform the first file
(doc1) into the second file (resultDocFrag) */
XMLDocumentFragment resultDocFrag = xslProc.processXSL(new XSLStylesheet
(xslDoc, createURL(outFile)), doc1);
XMLDocument resultDoc = new XMLDocument();
/* The XML declaration has to be copied over to the transformed XML doc,
the xsl will not generate it automatically */
if (doc1.getFirstChild() instanceof XMLDeclPI)
if (doc1.getFirstChild() instanceof XMLDeclPI)
{
XMLNode xmldecl = (XMLNode) resultDoc.importNode(doc1.getFirstChild(),
false);
resultDoc.appendChild(xmldecl);
}
/* Create the DTD node in the transformed XML document */
if(doc1.getDoctype() != null)
{
DTD dtd = (DTD)doc1.getDoctype();
resultDoc.setDoctype(dtd.getName(), dtd.getSystemId(),
dtd.getPublicId());
}
/* Create the result document tree from the document fragment */
resultDoc.appendChild(resultDocFrag);
diffFrame.makeResultFilePane(resultDoc);
} catch (XSLException e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(diffFrame,
"Error: "+e.getMessage(),
XDK JavaBeans 10-57
Installing the Transviewer Bean Samples
"XMLDiffSample Error Message",
JOptionPane.ERROR_MESSAGE);
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(diffFrame,
"Error:"+e.getMessage(),
"XMLDiffSample Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
/* Parse the XSL file generated into a DOM tree */
protected void parseXSL()
{
try
{
BufferedReader xslFile = new BufferedReader(new FileReader(outFile));
DOMParser domParser = new DOMParser();
domParser.parse(xslFile);
xslDoc = domParser.getDocument();
}catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(diffFrame,
"File Not Found: "+e.getMessage(),
"XMLDiffSample Message",
JOptionPane.PLAIN_MESSAGE);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(diffFrame,
"Error:"+e.getMessage(),
"XMLDiffSample Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
// create a URL from a file name
protected URL createURL(String fileName)
{
URL url = null;
try
{
10-58 Oracle9i XML Developer’s Kits Guide - XDK
Installing the Transviewer Bean Samples
url = new URL(fileName);
}
catch (MalformedURLException ex)
{
File f = new File(fileName);
try
{
String path = f.getAbsolutePath();
// to handle Windows platform
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)
{
JOptionPane.showMessageDialog(diffFrame,
"Cannot create url for: " + fileName,
"XMLDiffSample Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
return url;
}
protected
protected
protected
protected
protected
protected
protected
XMLDocument doc1; /* DOM tree for first file */
XMLDocument doc2; /* DOME tree for second file */
static XMLDiffFrame diffFrame; /* GUI frame */
static XMLDiffSample dfxApp; /* XMLDiff sample application */
static XMLDiff xmlDiff;
/* XML diff object */
static XMLDocument xslDoc;
/* parsed xsl file */
static String outFile = new String("XMLDiffSample.xsl"); /* output
xsl file name */
}
XDK JavaBeans 10-59
Installing the Transviewer Bean Samples
XMLDiffFrame.java
import
import
import
import
java.awt.*;
java.awt.event.*;
java.io.*;
javax.swing.*;
import
import
import
import
oracle.xml.parser.v2.*;
oracle.xml.srcviewer.*;
oracle.xml.differ.*;
org.w3c.dom.*;
public class XMLDiffFrame extends JFrame implements ActionListener {
public XMLDiffFrame(XMLDiffSample dfApp)
{
super();
mydfApp = dfApp;
init();
}
public void makeSrcPane(XMLDocument doc1, XMLDocument doc2)
{
//undo srcviewer highlighting here
XMLSourceView XmlSrcView1 = new XMLSourceView();
XmlSrcView1.setXMLDocument(doc1);
XmlSrcView1.setTagForeground(Color.black);
XmlSrcView1.setAttributeValueForeground(Color.black);
XmlSrcView1.setPIDataForeground(Color.black);
XmlSrcView1.setCommentDataForeground(Color.black);
XmlSrcView1.setCDATAForeground(Color.black);
XmlSrcView1.setBackground(Color.lightGray);
XmlSrcView1.getJTextPane().setBackground(Color.white);
XmlSrcView1.add(new JLabel(filename1,SwingConstants.CENTER),
BorderLayout.NORTH);
XMLSourceView XmlSrcView2 = new XMLSourceView();
XmlSrcView2.setXMLDocument(doc2);
XmlSrcView2.setTagForeground(Color.black);
XmlSrcView2.setAttributeValueForeground(Color.black);
XmlSrcView2.setPIDataForeground(Color.black);
XmlSrcView2.setCommentDataForeground(Color.black);
XmlSrcView2.setCDATAForeground(Color.black);
10-60 Oracle9i XML Developer’s Kits Guide - XDK
Installing the Transviewer Bean Samples
XmlSrcView2.setBackground(Color.lightGray);
XmlSrcView2.getJTextPane().setBackground(Color.white);
XmlSrcView2.add(new JLabel(filename2,SwingConstants.CENTER),
BorderLayout.NORTH);
XmlSrcView2.updateUI();
XmlSrcView1.updateUI();
srcPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
XmlSrcView1, XmlSrcView2);
srcPane.setSize(FRAMEWIDTH,FRAMEHEIGHT);
srcPane.setDividerLocation(0.5);
srcPane.validate();
}
public void makeDiffSrcPane(XMLDiffSrcView srcView1, XMLDiffSrcView srcView2)
{
srcView1.setBackground(Color.lightGray);
srcView2.setBackground(Color.lightGray);
srcView1.add(new
JLabel(filename1,SwingConstants.CENTER),BorderLayout.NORTH);
srcView2.add(new
JLabel(filename2,SwingConstants.CENTER),BorderLayout.NORTH);
JScrollBar vscrollBar = srcView2.getScrollPane().getVerticalScrollBar();
// make the diffSrcView divider fixed.
srcView1.getScrollPane().setVerticalScrollBar(vscrollBar);
srcView1.getScrollPane().setMinimumSize(
new
Dimension(FRAMEWIDTH/2,srcView1.getScrollPane().getPreferredSize().height));
srcView2.getScrollPane().setMinimumSize(
new
Dimension(FRAMEWIDTH/2,srcView2.getScrollPane().getPreferredSize().height));
srcView2.getScrollPane().updateUI();
srcView1.getScrollPane().updateUI();
srcView2.getTextPane().updateUI();
srcView1.getTextPane().updateUI();
XDK JavaBeans 10-61
Installing the Transviewer Bean Samples
srcView2.updateUI();
srcView1.updateUI();
diffSrcPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
srcView1, srcView2);
diffSrcPane.setSize(FRAMEWIDTH,FRAMEHEIGHT);
diffSrcPane.setDividerLocation(0.5);
diffSrcPane.validate();
}
public void makeTabbedPane()
{
tabbedPane = new JTabbedPane();
tabbedPane.addTab("SourceView", null , srcPane, "Source View of Files being
Diffed");
tabbedPane.addTab("SourceDiffView", null , diffSrcPane, "Source View of File
Diffs");
tabbedPane.addTab("TreeDiffView", null , diffTreePane, "DOM Tree View of
File Diffs");
tabbedPane.setSelectedIndex(1);
tabbedPane.setSize(FRAMEWIDTH,FRAMEHEIGHT);
this.getContentPane().add(tabbedPane);
this.setVisible(true);
}
public void makeXslPane(XMLDocument doc, String title)
{
xslSrcView = new XMLSourceView();
xslSrcView.setXMLDocument(doc);
xslSrcView.setTagForeground(Color.black);
xslSrcView.setAttributeValueForeground(Color.black);
xslSrcView.setPIDataForeground(Color.black);
xslSrcView.setCommentDataForeground(Color.black);
xslSrcView.setCDATAForeground(Color.black);
xslSrcView.setBackground(Color.lightGray);
xslSrcView.getJTextPane().setBackground(Color.white);
xslSrcView.add(new JLabel(title,SwingConstants.CENTER),
BorderLayout.NORTH);
this.enableTransformItem(true);
}
10-62 Oracle9i XML Developer’s Kits Guide - XDK
Installing the Transviewer Bean Samples
public void makeResultFilePane(XMLDocument doc)
{
resultDoc = doc;
XMLSourceView resultSrcView = new XMLSourceView();
resultSrcView.setXMLDocument(doc);
resultSrcView.setTagForeground(Color.black);
resultSrcView.setAttributeValueForeground(Color.black);
resultSrcView.setPIDataForeground(Color.black);
resultSrcView.setCommentDataForeground(Color.black);
resultSrcView.setCDATAForeground(Color.black);
resultSrcView.setBackground(Color.lightGray);
resultSrcView.getJTextPane().setBackground(Color.white);
resultSrcView.add(new JLabel("XSLT Result File",SwingConstants.CENTER),
BorderLayout.NORTH);
tabbedPane.addTab("ResultSourceView", null , resultSrcView,
"Source View of XSLT on File1");
tabbedPane.setSelectedIndex(3);
this.enableSaveAsItem(true);
}
public void makeXslTabbedPane()
{
tabbedPane = new JTabbedPane();
tabbedPane.addTab("SourceView", null , srcPane, "Source View of XML Files
being Diffed");
tabbedPane.addTab("SourceDiffView", null , diffSrcPane, "Source View of File
Diffs");
tabbedPane.addTab("XSL Script",null,xslSrcView, "Source View of Diff XSL
script");
tabbedPane.setSelectedIndex(2);
tabbedPane.setSize(FRAMEWIDTH,FRAMEHEIGHT);
this.getContentPane().add(tabbedPane);
this.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
XDK JavaBeans 10-63
Installing the Transviewer Bean Samples
File selectedFile1, selectedFile2;
BufferedReader file1, file2;
String arg, temp;
if(evt.getSource() instanceof JMenuItem)
{
arg = evt.getActionCommand();
if(arg.equals("Compare XML Files"))
{
JFileChooser jFC = new JFileChooser();
jFC.setCurrentDirectory(new File("."));
int retval = jFC.showOpenDialog(this);
switch (retval)
{
case JFileChooser.APPROVE_OPTION:
selectedFile1 = jFC.getSelectedFile();
temp = selectedFile1.getName();
jFC.cancelSelection();
jFC.updateUI();
switch(jFC.showOpenDialog(this))
{
case JFileChooser.APPROVE_OPTION:
selectedFile2 = jFC.getSelectedFile();
filename2 = selectedFile2.getName();
filename1 = temp;
this.getContentPane().removeAll();
this.enableSaveAsItem(false);
mydfApp.showDiffs(selectedFile1, selectedFile2);
break;
case JFileChooser.CANCEL_OPTION:
break; //filename1 = null; // filename1 also null
}// switch (jFC.showOpenDialog(this))
break;
case JFileChooser.CANCEL_OPTION:
break;
}
}// if(arg.equals("Compare XML Files"))
10-64 Oracle9i XML Developer’s Kits Guide - XDK
Installing the Transviewer Bean Samples
else if(arg.equals("Apply XSL to 1st Input File"))
{
mydfApp.doXSLTransform();
}
else if(arg.equals("Save As"))
{
JFileChooser jFC = new JFileChooser();
jFC.setCurrentDirectory(new File("."));
int retval = jFC.showOpenDialog(this);
if (retval == JFileChooser.APPROVE_OPTION)
{
File file = jFC.getSelectedFile();
try
{
resultDoc.print(new FileOutputStream(file));
}catch (IOException e)
{
JOptionPane.showMessageDialog(this,
"Error:"+e.getMessage(),
"XMLDiffer Message",
JOptionPane.PLAIN_MESSAGE);
}
}
}
else if(arg.equals("Exit"))
{
System.exit(0);
}
}
}
public void addTransformMenu()
{
JMenuItem item;
JMenu jmenu = new JMenu("Transform");
item = new JMenuItem("Apply XSL to 1st Input File");
item.addActionListener(this);
XDK JavaBeans 10-65