/************************************************************************* * * $RCSfile: TestTool.java,v $ * * $Revision: 1.1.1.1 $ * * last change: $Author: mt $ $Date: 2004-07-12 13:15:24 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses * * - GNU Lesser General Public License Version 2.1 * - Sun Industry Standards Source License Version 1.1 * * Sun Microsystems Inc., October, 2000 * * GNU Lesser General Public License Version 2.1 * ============================================= * Copyright 2000 by Sun Microsystems, Inc. * 901 San Antonio Road, Palo Alto, CA 94303, USA * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 2.1, as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * * Sun Industry Standards Source License Version 1.1 * ================================================= * The contents of this file are subject to the Sun Industry Standards * Source License Version 1.1 (the "License"); You may not use this file * except in compliance with the License. You may obtain a copy of the * License at http://www.openoffice.org/license.html. * * Software provided under this License is provided on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. * See the License for the specific provisions governing your rights and * obligations concerning the Software. * * The Initial Developer of the Original Code is: Sun Microsystems, Inc. * * Copyright: 2000 by Sun Microsystems, Inc. * * All Rights Reserved. * * Contributor(s): _______________________________________ * * ************************************************************************/ package com.sun.star.xml.security.uno; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.InputStream; import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /* Basic GUI components */ import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JFileChooser; import javax.swing.ToolTipManager; import javax.swing.JTable; import javax.swing.JLabel; import javax.swing.BorderFactory; /* GUI components for right-hand side */ import javax.swing.JSplitPane; import javax.swing.JOptionPane; import javax.swing.JTabbedPane; /* GUI support classes */ import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Container; import java.awt.Toolkit; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /* For creating borders */ import javax.swing.border.EmptyBorder; import javax.swing.border.BevelBorder; import javax.swing.border.CompoundBorder; /* For creating a TreeModel */ import javax.swing.tree.TreePath; import java.util.Vector; /* UNO classes */ import com.sun.star.uno.UnoRuntime; import com.sun.star.bridge.XUnoUrlResolver; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.beans.XPropertySet; import com.sun.star.uno.XComponentContext; import com.sun.star.xml.sax.XDocumentHandler; import com.sun.star.xml.crypto.*; import com.sun.star.xml.crypto.sax.*; public class TestTool extends JFrame implements ActionListener { /* * xml security framewrok component names */ public static String SIGNATURECREATOR_COMPONENT = "com.sun.star.xml.crypto.sax.SignatureCreator"; public static String SIGNATUREVERIFIER_COMPONENT = "com.sun.star.xml.crypto.sax.SignatureVerifier"; public static String ENCRYPTOR_COMPONENT = "com.sun.star.xml.crypto.sax.Encryptor"; public static String DECRYPTOR_COMPONENT = "com.sun.star.xml.crypto.sax.Decryptor"; public static String SAXEVENTKEEPER_COMPONENT = "com.sun.star.xml.crypto.sax.SAXEventKeeper"; /* * Java-based component names */ public static String SEINITIALIZER_COMPONENT_JAVA = "com.sun.star.xml.security.bridge.jxsec.SEInitializer_JxsecImpl"; public static String XMLSIGNATURE_COMPONENT_JAVA = "com.sun.star.xml.security.bridge.jxsec.XMLSignature_JxsecImpl"; public static String XMLENCRYPTION_COMPONENT_JAVA = "com.sun.star.xml.security.bridge.jxsec.XMLEncryption_JxsecImpl"; public static String XMLDOCUMENTWRAPPER_COMPONENT_JAVA = "com.sun.star.xml.security.bridge.jxsec.XMLDocumentWrapper_JxsecImpl"; /* * C-based component names */ public static String SEINITIALIZER_COMPONENT_C = "com.sun.star.xml.security.bridge.xmlsec.SEInitializer_NssImpl"; public static String XMLSIGNATURE_COMPONENT_C = "com.sun.star.xml.security.bridge.xmlsec.XMLSignature_NssImpl"; public static String XMLENCRYPTION_COMPONENT_C = "com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_NssImpl"; public static String XMLDOCUMENTWRAPPER_COMPONENT_C = "com.sun.star.xml.security.bridge.xmlsec.XMLDocumentWrapper_XmlSecImpl"; /* url resolver name */ public static String UNOURLRESOLVER = "com.sun.star.bridge.UnoUrlResolver"; /* * connection URL */ private String m_unoURL = "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"; /* key file */ private String m_javaTokenFile = null; private String m_nssTokenPath = null; /* User Interfaces */ private JButton m_goButton; private JButton m_stepButton; private JButton m_startButton; private JButton m_openButton; private JCheckBox m_isExportingButton; private JCheckBox m_isJavaComponentButton; private JButton m_saveButton; private JButton m_batchButton; private JTree m_leftTree; private JTextArea m_leftTextArea; private JTree m_middleTree; private JTree m_rightTree; private JTabbedPane m_leftTabPane; private JTextArea m_bufferNodeTextArea; private JLabel m_saxChainLabel; private JTextField m_saxEventText; private JTable m_unsolvedReferenceTable; /* * whether a batch file is running, * if so, no message box is popped up */ private boolean m_bIsBatchRunning = false; /* * whether the UI needs to be updated. * when user click the "go" button, the UI needs * not to be updated step by step for performance * reason */ private boolean m_bIsUIUpdateSuppressed = false; /* * three DOM tree adapter */ private DomToTreeModelAdapter m_leftTreeModelAdapter; private DomToTreeModelAdapter m_middleTreeModelAdapter; private DomToTreeModelAdapter m_rightTreeModelAdapter; /* * the current directory, which reserves the default * location when user open/save a file. */ private File m_currentDirectory = null; /* * the log file */ private FileOutputStream m_logFileOutputStream = null; /* * the thread which is parsing the current XML * file */ private ParsingThread m_parsingThread; /* * whether is exporting or importing */ private boolean m_bIsExporting; /* * whether java based component or c based component * is used now */ private boolean m_bIsJavaBased; /* * XML security component interface */ private XComponentContext m_xRemoteContext = null; private XMultiComponentFactory m_xRemoteServiceManager = null; private XXMLSecurityContext m_xXMLSecurityContext = null; private XXMLSignature m_xXMLSignature = null; private XXMLEncryption m_xXMLEncryption = null; private XSEInitializer m_xSEInitializer = null; /* * SAX event collector for the middle tree and the right tree */ private SAXEventCollector m_rightTreeEventCollector = null; private SAXEventCollector m_middleTreeEventCollector = null; /* * security framework controller */ private XMLSecurityFrameworkController m_xmlSecurityFrameworkController = null; /* org.w3c.dom.Document */ private Document m_document; /* represents whether "Go" or "Step" */ private boolean stepMode = true; /************************************************************************************** * private methods **************************************************************************************/ /****************************************************************************** * UI related methods ******************************************************************************/ /* * initalizes the UI. */ private void initUI() { m_leftTreeModelAdapter = new DomToTreeModelAdapter(m_document); m_middleTreeModelAdapter = new DomToTreeModelAdapter(m_document); m_rightTreeModelAdapter = new DomToTreeModelAdapter(m_document); m_parsingThread = null; m_leftTree.setModel(m_leftTreeModelAdapter); m_middleTree.setModel(m_middleTreeModelAdapter); m_rightTree.setModel(m_rightTreeModelAdapter); } /* * constructs the user interface. */ private Container buildUI(int width, int height) { JPanel mainPanel = new JPanel(); int frameHeight = height-40; int leftWindowWidth = (width-40)/3; int middleWindowWidth = leftWindowWidth; int rightWindowWidth = leftWindowWidth; int leftPaneWidth = leftWindowWidth+middleWindowWidth; int frameWidth = leftPaneWidth + rightWindowWidth; /* Make a nice border */ EmptyBorder emptyBorder = new EmptyBorder(5,5,5,5); BevelBorder bevelBorder = new BevelBorder(BevelBorder.LOWERED); CompoundBorder compoundBorder = new CompoundBorder(emptyBorder,bevelBorder); mainPanel.setBorder(new CompoundBorder(compoundBorder,emptyBorder)); /* Set up the tree */ m_leftTreeModelAdapter = new DomToTreeModelAdapter(m_document); m_middleTreeModelAdapter = new DomToTreeModelAdapter(m_document); m_rightTreeModelAdapter = new DomToTreeModelAdapter(m_document); m_leftTree = new JTree(m_leftTreeModelAdapter); m_leftTextArea = new JTextArea(); m_middleTree = new JTree(m_middleTreeModelAdapter); m_rightTree = new JTree(m_rightTreeModelAdapter); ToolTipManager.sharedInstance().registerComponent(m_leftTree); ToolTipManager.sharedInstance().registerComponent(m_middleTree); ToolTipManager.sharedInstance().registerComponent(m_rightTree); /* Builds left tab pane */ JScrollPane leftTreePane = new JScrollPane(m_leftTree); JScrollPane leftTextPane = new JScrollPane(m_leftTextArea); m_leftTabPane= new JTabbedPane(); m_leftTabPane.add("Tree View",leftTreePane); m_leftTabPane.add("Text View",leftTextPane); /* Builds middle tree pane */ JScrollPane middleTreePane = new JScrollPane(m_middleTree); /* Builds right tree pane */ JScrollPane rightTreePane = new JScrollPane(m_rightTree); rightTreePane.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Result"), BorderFactory.createEmptyBorder(8,8,8,8))); m_leftTabPane.setPreferredSize( new Dimension( leftWindowWidth, frameHeight )); middleTreePane.setPreferredSize( new Dimension( middleWindowWidth, frameHeight )); rightTreePane.setPreferredSize( new Dimension( rightWindowWidth, frameHeight )); /* Builds the SAX event text box */ m_saxEventText = new JTextField(); /* Builds the unsolved reference table */ m_unsolvedReferenceTable = new JTable( new UnsolvedReferenceTableModel(this)); /* Builds the BufferNode information text area */ m_bufferNodeTextArea = new JTextArea(); /* Builds the SAX chain information label */ m_saxChainLabel = new JLabel(); /* Builds the left pane */ JPanel tabPaneWithSaxEventPane = new JPanel(); tabPaneWithSaxEventPane.setLayout(new BorderLayout()); tabPaneWithSaxEventPane.add("Center",m_leftTabPane); tabPaneWithSaxEventPane.add("South",new JScrollPane(m_saxEventText)); JSplitPane leftPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT, tabPaneWithSaxEventPane, new JScrollPane(m_unsolvedReferenceTable)); leftPane.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Original"), BorderFactory.createEmptyBorder(8,8,8,8))); leftPane.setContinuousLayout( true ); leftPane.setDividerLocation( frameHeight*2/3 ); leftPane.setPreferredSize( new Dimension( leftWindowWidth, frameHeight )); /* Builds the middle pane */ JPanel bufferNodeWithSaxChainPane = new JPanel(); bufferNodeWithSaxChainPane.setLayout(new BorderLayout()); bufferNodeWithSaxChainPane.add("Center",m_bufferNodeTextArea); bufferNodeWithSaxChainPane.add("South",new JScrollPane(m_saxChainLabel)); JSplitPane middlePane = new JSplitPane( JSplitPane.VERTICAL_SPLIT, middleTreePane, new JScrollPane(bufferNodeWithSaxChainPane)); middlePane.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Insight SAXEventKeeper"), BorderFactory.createEmptyBorder(8,8,8,8))); middlePane.setContinuousLayout( true ); middlePane.setDividerLocation( frameHeight/2+5 ); middlePane.setPreferredSize( new Dimension( middleWindowWidth, frameHeight )); /* Builds the whole frame pane */ JSplitPane leftWithMiddlePane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, leftPane, middlePane ); leftWithMiddlePane.setContinuousLayout( true ); leftWithMiddlePane.setDividerLocation( leftWindowWidth ); leftWithMiddlePane.setPreferredSize( new Dimension( leftPaneWidth + 10, frameHeight+10 )); JSplitPane framePane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, leftWithMiddlePane, rightTreePane ); framePane.setContinuousLayout( true ); framePane.setDividerLocation(leftPaneWidth+10 ); framePane.setPreferredSize( new Dimension( frameWidth + 20, frameHeight+10 )); /* Adds all GUI components to the main panel */ mainPanel.setLayout(new BorderLayout()); mainPanel.add("Center", framePane ); m_openButton = new JButton("Open..."); m_openButton.addActionListener(this); m_goButton = new JButton("Go!"); m_goButton.addActionListener(this); m_stepButton = new JButton("Step"); m_stepButton.addActionListener(this); m_startButton = new JButton("Start"); m_startButton.addActionListener(this); m_startButton.setEnabled(false); m_isExportingButton = new JCheckBox("export, not import", true); m_isJavaComponentButton = new JCheckBox("java, not C++", false); m_saveButton = new JButton("Save..."); m_saveButton.addActionListener(this); m_batchButton = new JButton("Batch..."); m_batchButton.addActionListener(this); JPanel buttonPanel = new JPanel(); buttonPanel.add(m_batchButton); buttonPanel.add(m_openButton); buttonPanel.add(m_startButton); buttonPanel.add(m_goButton); buttonPanel.add(m_stepButton); buttonPanel.add(m_isExportingButton); buttonPanel.add(m_isJavaComponentButton); buttonPanel.add(m_saveButton); mainPanel.add("South", buttonPanel); enableGoButton(false); return mainPanel; } /* * enables/disables the Go(and Step) button. */ private void enableGoButton(boolean enabled) { m_goButton.setEnabled(enabled); m_stepButton.setEnabled(enabled); } /* * updates the unsolved reference information. */ private void updatesUnsolvedReferencesInformation() { m_unsolvedReferenceTable.setModel(new UnsolvedReferenceTableModel(this)); } /* * adjusts the view of the tree in order to make the * particular Node into the focus tree leaf. */ private void updatesTree(Node node, JTree tree) { int i=0; int currentLine = 0; while (i= 3) { if (argv[2].startsWith("-b")) { fileName = argv[2].substring(2); hasBatch = true; } else { fileName = argv[2]; hasFile = true; } } tt = new TestTool(); tt.m_javaTokenFile = new String(argv[0]); tt.m_nssTokenPath = new String(argv[1]); tt.connectSO(null); /* Set up a GUI framework */ JFrame myFrame = new JFrame("XML Security Components Tester"); myFrame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} } ); myFrame.setContentPane(tt.buildUI(screenSize.width, screenSize.height)); myFrame.pack(); int w = screenSize.width-30; int h = screenSize.height-30; myFrame.setLocation(screenSize.width/2 - w/2, screenSize.height/2 - h/2); myFrame.setSize(w, h); myFrame.setVisible(true); if (hasFile) { tt.parseFile(new File(fileName)); } else if (hasBatch) { tt.runBatch(new File(fileName)); } } }