merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / uno / TestTool.java
blob6c87b34bbb433d5d61f0dd89cdadc38eb9936736
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 package com.sun.star.xml.security.uno;
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.parsers.ParserConfigurationException;
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.FileOutputStream;
37 import java.io.FileInputStream;
38 import java.io.InputStream;
39 import java.io.ByteArrayInputStream;
40 import java.io.UnsupportedEncodingException;
42 import org.w3c.dom.Document;
43 import org.w3c.dom.Node;
44 import org.w3c.dom.NodeList;
46 /* Basic GUI components */
47 import javax.swing.JFrame;
48 import javax.swing.JPanel;
49 import javax.swing.JScrollPane;
50 import javax.swing.JTree;
51 import javax.swing.JButton;
52 import javax.swing.JCheckBox;
53 import javax.swing.JTextArea;
54 import javax.swing.JTextField;
55 import javax.swing.JFileChooser;
56 import javax.swing.ToolTipManager;
57 import javax.swing.JTable;
58 import javax.swing.JLabel;
59 import javax.swing.BorderFactory;
61 /* GUI components for right-hand side */
62 import javax.swing.JSplitPane;
63 import javax.swing.JOptionPane;
64 import javax.swing.JTabbedPane;
67 /* GUI support classes */
68 import java.awt.BorderLayout;
69 import java.awt.Dimension;
70 import java.awt.Container;
71 import java.awt.Toolkit;
72 import java.awt.event.WindowEvent;
73 import java.awt.event.WindowAdapter;
74 import java.awt.event.ActionEvent;
75 import java.awt.event.ActionListener;
77 /* For creating borders */
78 import javax.swing.border.EmptyBorder;
79 import javax.swing.border.BevelBorder;
80 import javax.swing.border.CompoundBorder;
82 /* For creating a TreeModel */
83 import javax.swing.tree.TreePath;
84 import java.util.Vector;
86 /* UNO classes */
87 import com.sun.star.uno.UnoRuntime;
88 import com.sun.star.bridge.XUnoUrlResolver;
89 import com.sun.star.lang.XMultiComponentFactory;
90 import com.sun.star.beans.XPropertySet;
91 import com.sun.star.uno.XComponentContext;
92 import com.sun.star.xml.sax.XDocumentHandler;
94 import com.sun.star.xml.crypto.*;
95 import com.sun.star.xml.crypto.sax.*;
97 public class TestTool extends JFrame implements ActionListener
100 * xml security framewrok component names
102 public static String SIGNATURECREATOR_COMPONENT = "com.sun.star.xml.crypto.sax.SignatureCreator";
103 public static String SIGNATUREVERIFIER_COMPONENT = "com.sun.star.xml.crypto.sax.SignatureVerifier";
104 public static String ENCRYPTOR_COMPONENT = "com.sun.star.xml.crypto.sax.Encryptor";
105 public static String DECRYPTOR_COMPONENT = "com.sun.star.xml.crypto.sax.Decryptor";
106 public static String SAXEVENTKEEPER_COMPONENT = "com.sun.star.xml.crypto.sax.SAXEventKeeper";
109 * Java-based component names
111 public static String SEINITIALIZER_COMPONENT_JAVA = "com.sun.star.xml.security.bridge.jxsec.SEInitializer_JxsecImpl";
112 public static String XMLSIGNATURE_COMPONENT_JAVA = "com.sun.star.xml.security.bridge.jxsec.XMLSignature_JxsecImpl";
113 public static String XMLENCRYPTION_COMPONENT_JAVA = "com.sun.star.xml.security.bridge.jxsec.XMLEncryption_JxsecImpl";
114 public static String XMLDOCUMENTWRAPPER_COMPONENT_JAVA = "com.sun.star.xml.security.bridge.jxsec.XMLDocumentWrapper_JxsecImpl";
117 * C-based component names
119 public static String SEINITIALIZER_COMPONENT_C = "com.sun.star.xml.crypto.SEInitializer";
120 public static String XMLSIGNATURE_COMPONENT_C = "com.sun.star.xml.crypto.XMLSignature";
121 public static String XMLENCRYPTION_COMPONENT_C = "com.sun.star.xml.crypto.XMLEncryption";
122 public static String XMLDOCUMENTWRAPPER_COMPONENT_C = "com.sun.star.xml.wrapper.XMLDocumentWrapper";
124 /* url resolver name */
125 public static String UNOURLRESOLVER = "com.sun.star.bridge.UnoUrlResolver";
128 * connection URL
130 private String m_unoURL = "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager";
132 /* key file */
133 private String m_javaTokenFile = null;
134 private String m_nssTokenPath = null;
136 /* User Interfaces */
137 private JButton m_goButton;
138 private JButton m_stepButton;
139 private JButton m_startButton;
140 private JButton m_openButton;
141 private JCheckBox m_isExportingButton;
142 private JCheckBox m_isJavaComponentButton;
143 private JButton m_saveButton;
144 private JButton m_batchButton;
145 private JTree m_leftTree;
146 private JTextArea m_leftTextArea;
147 private JTree m_middleTree;
148 private JTree m_rightTree;
149 private JTabbedPane m_leftTabPane;
150 private JTextArea m_bufferNodeTextArea;
151 private JLabel m_saxChainLabel;
152 private JTextField m_saxEventText;
153 private JTable m_unsolvedReferenceTable;
156 * whether a batch file is running,
157 * if so, no message box is popped up
159 private boolean m_bIsBatchRunning = false;
162 * whether the UI needs to be updated.
163 * when user click the "go" button, the UI needs
164 * not to be updated step by step for performance
165 * reason
167 private boolean m_bIsUIUpdateSuppressed = false;
170 * three DOM tree adapter
172 private DomToTreeModelAdapter m_leftTreeModelAdapter;
173 private DomToTreeModelAdapter m_middleTreeModelAdapter;
174 private DomToTreeModelAdapter m_rightTreeModelAdapter;
177 * the current directory, which reserves the default
178 * location when user open/save a file.
180 private File m_currentDirectory = null;
183 * the log file
185 private FileOutputStream m_logFileOutputStream = null;
188 * the thread which is parsing the current XML
189 * file
191 private ParsingThread m_parsingThread;
194 * whether is exporting or importing
196 private boolean m_bIsExporting;
199 * whether java based component or c based component
200 * is used now
202 private boolean m_bIsJavaBased;
205 * XML security component interface
207 private XComponentContext m_xRemoteContext = null;
208 private XMultiComponentFactory m_xRemoteServiceManager = null;
209 private XXMLSecurityContext m_xXMLSecurityContext = null;
210 private XXMLSignature m_xXMLSignature = null;
211 private XXMLEncryption m_xXMLEncryption = null;
212 private XSEInitializer m_xSEInitializer = null;
215 * SAX event collector for the middle tree and the right tree
217 private SAXEventCollector m_rightTreeEventCollector = null;
218 private SAXEventCollector m_middleTreeEventCollector = null;
221 * security framework controller
223 private XMLSecurityFrameworkController m_xmlSecurityFrameworkController = null;
225 /* org.w3c.dom.Document */
226 private Document m_document;
228 /* represents whether "Go" or "Step" */
229 private boolean stepMode = true;
231 /**************************************************************************************
232 * private methods
233 **************************************************************************************/
236 /******************************************************************************
237 * UI related methods
238 ******************************************************************************/
241 * initalizes the UI.
243 private void initUI()
245 m_leftTreeModelAdapter = new DomToTreeModelAdapter(m_document);
246 m_middleTreeModelAdapter = new DomToTreeModelAdapter(m_document);
247 m_rightTreeModelAdapter = new DomToTreeModelAdapter(m_document);
249 m_parsingThread = null;
251 m_leftTree.setModel(m_leftTreeModelAdapter);
252 m_middleTree.setModel(m_middleTreeModelAdapter);
253 m_rightTree.setModel(m_rightTreeModelAdapter);
257 * constructs the user interface.
259 private Container buildUI(int width, int height)
261 JPanel mainPanel = new JPanel();
263 int frameHeight = height-40;
264 int leftWindowWidth = (width-40)/3;
265 int middleWindowWidth = leftWindowWidth;
266 int rightWindowWidth = leftWindowWidth;
267 int leftPaneWidth = leftWindowWidth+middleWindowWidth;
268 int frameWidth = leftPaneWidth + rightWindowWidth;
270 /* Make a nice border */
271 EmptyBorder emptyBorder = new EmptyBorder(5,5,5,5);
272 BevelBorder bevelBorder = new BevelBorder(BevelBorder.LOWERED);
273 CompoundBorder compoundBorder = new CompoundBorder(emptyBorder,bevelBorder);
274 mainPanel.setBorder(new CompoundBorder(compoundBorder,emptyBorder));
276 /* Set up the tree */
277 m_leftTreeModelAdapter = new DomToTreeModelAdapter(m_document);
278 m_middleTreeModelAdapter = new DomToTreeModelAdapter(m_document);
279 m_rightTreeModelAdapter = new DomToTreeModelAdapter(m_document);
281 m_leftTree = new JTree(m_leftTreeModelAdapter);
282 m_leftTextArea = new JTextArea();
283 m_middleTree = new JTree(m_middleTreeModelAdapter);
284 m_rightTree = new JTree(m_rightTreeModelAdapter);
286 ToolTipManager.sharedInstance().registerComponent(m_leftTree);
287 ToolTipManager.sharedInstance().registerComponent(m_middleTree);
288 ToolTipManager.sharedInstance().registerComponent(m_rightTree);
290 /* Builds left tab pane */
291 JScrollPane leftTreePane = new JScrollPane(m_leftTree);
292 JScrollPane leftTextPane = new JScrollPane(m_leftTextArea);
293 m_leftTabPane= new JTabbedPane();
294 m_leftTabPane.add("Tree View",leftTreePane);
295 m_leftTabPane.add("Text View",leftTextPane);
297 /* Builds middle tree pane */
298 JScrollPane middleTreePane = new JScrollPane(m_middleTree);
300 /* Builds right tree pane */
301 JScrollPane rightTreePane = new JScrollPane(m_rightTree);
302 rightTreePane.setBorder(BorderFactory.createCompoundBorder(
303 BorderFactory.createTitledBorder("Result"),
304 BorderFactory.createEmptyBorder(8,8,8,8)));
306 m_leftTabPane.setPreferredSize(
307 new Dimension( leftWindowWidth, frameHeight ));
308 middleTreePane.setPreferredSize(
309 new Dimension( middleWindowWidth, frameHeight ));
310 rightTreePane.setPreferredSize(
311 new Dimension( rightWindowWidth, frameHeight ));
313 /* Builds the SAX event text box */
314 m_saxEventText = new JTextField();
316 /* Builds the unsolved reference table */
317 m_unsolvedReferenceTable = new JTable(
318 new UnsolvedReferenceTableModel(this));
320 /* Builds the BufferNode information text area */
321 m_bufferNodeTextArea = new JTextArea();
323 /* Builds the SAX chain information label */
324 m_saxChainLabel = new JLabel();
326 /* Builds the left pane */
327 JPanel tabPaneWithSaxEventPane = new JPanel();
328 tabPaneWithSaxEventPane.setLayout(new BorderLayout());
329 tabPaneWithSaxEventPane.add("Center",m_leftTabPane);
330 tabPaneWithSaxEventPane.add("South",new JScrollPane(m_saxEventText));
332 JSplitPane leftPane =
333 new JSplitPane( JSplitPane.VERTICAL_SPLIT,
334 tabPaneWithSaxEventPane,
335 new JScrollPane(m_unsolvedReferenceTable));
336 leftPane.setBorder(BorderFactory.createCompoundBorder(
337 BorderFactory.createTitledBorder("Original"),
338 BorderFactory.createEmptyBorder(8,8,8,8)));
340 leftPane.setContinuousLayout( true );
341 leftPane.setDividerLocation( frameHeight*2/3 );
342 leftPane.setPreferredSize(
343 new Dimension( leftWindowWidth, frameHeight ));
345 /* Builds the middle pane */
346 JPanel bufferNodeWithSaxChainPane = new JPanel();
347 bufferNodeWithSaxChainPane.setLayout(new BorderLayout());
348 bufferNodeWithSaxChainPane.add("Center",m_bufferNodeTextArea);
349 bufferNodeWithSaxChainPane.add("South",new JScrollPane(m_saxChainLabel));
351 JSplitPane middlePane =
352 new JSplitPane( JSplitPane.VERTICAL_SPLIT,
353 middleTreePane,
354 new JScrollPane(bufferNodeWithSaxChainPane));
356 middlePane.setBorder(BorderFactory.createCompoundBorder(
357 BorderFactory.createTitledBorder("Insight SAXEventKeeper"),
358 BorderFactory.createEmptyBorder(8,8,8,8)));
360 middlePane.setContinuousLayout( true );
361 middlePane.setDividerLocation( frameHeight/2+5 );
362 middlePane.setPreferredSize(
363 new Dimension( middleWindowWidth, frameHeight ));
365 /* Builds the whole frame pane */
366 JSplitPane leftWithMiddlePane =
367 new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
368 leftPane,
369 middlePane );
370 leftWithMiddlePane.setContinuousLayout( true );
371 leftWithMiddlePane.setDividerLocation( leftWindowWidth );
372 leftWithMiddlePane.setPreferredSize(
373 new Dimension( leftPaneWidth + 10, frameHeight+10 ));
375 JSplitPane framePane =
376 new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
377 leftWithMiddlePane,
378 rightTreePane );
381 framePane.setContinuousLayout( true );
382 framePane.setDividerLocation(leftPaneWidth+10 );
383 framePane.setPreferredSize(
384 new Dimension( frameWidth + 20, frameHeight+10 ));
386 /* Adds all GUI components to the main panel */
387 mainPanel.setLayout(new BorderLayout());
388 mainPanel.add("Center", framePane );
390 m_openButton = new JButton("Open...");
391 m_openButton.addActionListener(this);
393 m_goButton = new JButton("Go!");
394 m_goButton.addActionListener(this);
396 m_stepButton = new JButton("Step");
397 m_stepButton.addActionListener(this);
399 m_startButton = new JButton("Start");
400 m_startButton.addActionListener(this);
401 m_startButton.setEnabled(false);
403 m_isExportingButton = new JCheckBox("export, not import", true);
404 m_isJavaComponentButton = new JCheckBox("use java component", false);
406 m_saveButton = new JButton("Save...");
407 m_saveButton.addActionListener(this);
409 m_batchButton = new JButton("Batch...");
410 m_batchButton.addActionListener(this);
412 JPanel buttonPanel = new JPanel();
413 buttonPanel.add(m_batchButton);
414 buttonPanel.add(m_openButton);
415 buttonPanel.add(m_startButton);
416 buttonPanel.add(m_goButton);
417 buttonPanel.add(m_stepButton);
418 buttonPanel.add(m_isExportingButton);
419 buttonPanel.add(m_isJavaComponentButton);
420 buttonPanel.add(m_saveButton);
422 mainPanel.add("South", buttonPanel);
424 enableGoButton(false);
426 return mainPanel;
430 * enables/disables the Go(and Step) button.
432 private void enableGoButton(boolean enabled)
434 m_goButton.setEnabled(enabled);
435 m_stepButton.setEnabled(enabled);
439 * updates the unsolved reference information.
441 private void updatesUnsolvedReferencesInformation()
443 m_unsolvedReferenceTable.setModel(new UnsolvedReferenceTableModel(this));
447 * adjusts the view of the tree in order to make the
448 * particular Node into the focus tree leaf.
450 private void updatesTree(Node node, JTree tree)
452 int i=0;
453 int currentLine = 0;
455 while (i<tree.getRowCount())
457 TreePath treePath = tree.getPathForRow(i);
458 tree.expandPath(treePath);
460 AdapterNode adapterNode = (AdapterNode)treePath.getLastPathComponent();
462 if (node == adapterNode.getNode())
464 tree.addSelectionPath(treePath);
465 currentLine = i;
468 ++i;
471 tree.setCellRenderer(new XMLTreeCellRanderer(node));
472 tree.scrollRowToVisible(currentLine);
475 /******************************************************************************
476 * action listener related methods.
477 ******************************************************************************/
480 * reads in a document, either the document is a file or
481 * is a text paragraph.
483 private void openDocument()
485 if (m_leftTabPane.getSelectedIndex() == 0)
487 File f = openFile();
488 if (f != null)
490 parseFile(f);
493 else
495 String text = m_leftTextArea.getText();
499 parseStream(new ByteArrayInputStream(text.getBytes("UTF-8")));
501 catch(UnsupportedEncodingException e)
503 e.printStackTrace();
506 m_leftTabPane.setSelectedIndex(0);
511 * save the result tree to a file.
513 private void saveResult()
515 saveFile();
519 * selects a batch file to excute.
521 private void openBatch()
523 File f = openFile();
524 if (f != null)
526 runBatch(f);
531 * makes the current operation to an end.
533 private void endMission()
535 enableGoButton(false);
536 m_parsingThread = null;
538 if (m_xmlSecurityFrameworkController != null)
540 m_xmlSecurityFrameworkController.endMission();
543 updatesUIs();
545 m_xmlSecurityFrameworkController = null;
546 freeComponents();
548 System.gc();
552 /******************************************************************************
553 * UNO component related methods
554 ******************************************************************************/
557 * connects the SO server.
559 private void connectSO(String unoUrlString)
561 if (unoUrlString != null)
563 m_unoURL = new String(unoUrlString);
568 m_xRemoteServiceManager = getRemoteServiceManager(m_unoURL);
570 catch(Exception e)
572 e.printStackTrace();
577 * creates UNO components.
579 private boolean createComponents()
583 String SEInitializer_comp;
584 String XMLSignature_comp;
585 String XMLEncryption_comp;
586 String tokenPath;
588 if (m_bIsJavaBased)
590 SEInitializer_comp = SEINITIALIZER_COMPONENT_JAVA;
591 XMLSignature_comp = XMLSIGNATURE_COMPONENT_JAVA;
592 XMLEncryption_comp = XMLENCRYPTION_COMPONENT_JAVA;
593 tokenPath = m_javaTokenFile;
595 else
597 SEInitializer_comp = SEINITIALIZER_COMPONENT_C;
598 XMLSignature_comp = XMLSIGNATURE_COMPONENT_C;
599 XMLEncryption_comp = XMLENCRYPTION_COMPONENT_C;
600 tokenPath = m_nssTokenPath;
603 Object seInitializerObj = m_xRemoteServiceManager.createInstanceWithContext(
604 SEInitializer_comp, m_xRemoteContext);
606 if (seInitializerObj == null)
608 freeComponents();
609 return false;
612 m_xSEInitializer = (XSEInitializer)UnoRuntime.queryInterface(
613 XSEInitializer.class, seInitializerObj);
615 m_xXMLSecurityContext = m_xSEInitializer.createSecurityContext(tokenPath);
617 Object xmlSignatureObj = m_xRemoteServiceManager.createInstanceWithContext(
618 XMLSignature_comp, m_xRemoteContext);
620 if (xmlSignatureObj == null)
622 freeComponents();
623 return false;
626 m_xXMLSignature = (XXMLSignature)UnoRuntime.queryInterface(
627 XXMLSignature.class, xmlSignatureObj);
629 Object xmlEncryptionObj = m_xRemoteServiceManager.createInstanceWithContext(
630 XMLEncryption_comp, m_xRemoteContext);
632 if (xmlEncryptionObj == null)
634 freeComponents();
635 return false;
638 m_xXMLEncryption = (XXMLEncryption)UnoRuntime.queryInterface(
639 XXMLEncryption.class, xmlEncryptionObj);
641 return true;
643 catch(Exception e)
645 freeComponents();
646 e.printStackTrace();
647 return false;
652 * frees UNO components.
654 private void freeComponents()
658 if (m_xXMLSecurityContext != null)
660 m_xSEInitializer.freeSecurityContext(m_xXMLSecurityContext);
661 m_xXMLSecurityContext = null;
664 m_xXMLSignature = null;
665 m_xXMLEncryption = null;
666 m_xSEInitializer = null;
668 catch(Exception e)
670 e.printStackTrace();
675 * getRemoteServiceManager
677 private XMultiComponentFactory getRemoteServiceManager(String unoUrl) throws java.lang.Exception
679 if (m_xRemoteContext == null)
682 * First step: create local component context, get local servicemanager and
683 * ask it to create a UnoUrlResolver object with an XUnoUrlResolver interface
685 XComponentContext xLocalContext =
686 com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
687 XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
688 Object urlResolver = xLocalServiceManager.createInstanceWithContext(
689 UNOURLRESOLVER, xLocalContext );
691 * query XUnoUrlResolver interface from urlResolver object
693 XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(
694 XUnoUrlResolver.class, urlResolver );
697 * Second step: use xUrlResolver interface to import the remote StarOffice.ServiceManager,
698 * retrieve its property DefaultContext and get the remote servicemanager
700 Object initialObject = xUnoUrlResolver.resolve( unoUrl );
701 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(
702 XPropertySet.class, initialObject);
703 Object context = xPropertySet.getPropertyValue("DefaultContext");
704 m_xRemoteContext = (XComponentContext)UnoRuntime.queryInterface(
705 XComponentContext.class, context);
707 return m_xRemoteContext.getServiceManager();
711 /******************************************************************************
712 * XML related methods
713 ******************************************************************************/
716 * removes all empty text node inside the particular element
718 private void removeEmptyText(Node node)
720 int type = node.getNodeType();
721 NodeList children;
722 int i;
724 switch (type)
726 case Node.DOCUMENT_NODE:
727 case Node.ELEMENT_NODE:
728 Node child = node.getFirstChild();
729 while (child!= null)
731 Node nextSibling = child.getNextSibling();
732 int childType = child.getNodeType();
734 if (childType==Node.TEXT_NODE)
736 String message = child.getNodeValue().trim();
737 if (message == null || message.length()<=0)
739 node.removeChild(child);
742 else if (childType == Node.ELEMENT_NODE)
744 removeEmptyText(child);
747 child = nextSibling;
749 break;
754 * reads a stream, and parses it into the original tree.
756 private void parseStream(InputStream is)
760 DocumentBuilderFactory factory =
761 DocumentBuilderFactory.newInstance();
762 m_document = null;
763 m_startButton.setEnabled(false);
764 initUI();
766 /* factory.setValidating(true); */
767 /* factory.setNamespaceAware(true); */
771 DocumentBuilder builder = factory.newDocumentBuilder();
772 m_document = builder.parse(is);
773 m_startButton.setEnabled(true);
774 initUI();
776 catch (ParserConfigurationException pce)
778 pce.printStackTrace();
780 catch (IOException ioe)
782 ioe.printStackTrace();
785 catch(Exception exce)
787 System.out.println("input stream Exception");
792 /******************************************************************************
793 * file operation related methods
794 ******************************************************************************/
797 * opens a file, and parses it into the original tree.
799 private void parseFile(File name)
803 FileInputStream fis = new FileInputStream(name);
804 parseStream(fis);
805 fis.close();
807 catch(Exception exce)
809 System.out.println("open file Exception");
815 * selects a file to open
817 private File openFile()
819 File rc = null;
821 JFileChooser fileChooser= new JFileChooser();
823 fileChooser.setDialogTitle("Select File To Open");
824 fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
826 fileChooser.setApproveButtonText("Ok");
828 if (m_currentDirectory == null)
830 fileChooser.rescanCurrentDirectory();
832 else
834 fileChooser.setCurrentDirectory(m_currentDirectory);
837 fileChooser.setFileFilter(new XMLFileFilter());
839 int result = fileChooser.showDialog(this,null);
840 if (result==fileChooser.APPROVE_OPTION)
842 m_currentDirectory = fileChooser.getCurrentDirectory();
843 rc = fileChooser.getSelectedFile();
846 return rc;
849 private void saveFile()
851 JFileChooser fileChooser= new JFileChooser();
853 fileChooser.setDialogTitle("Select File To Save");
854 fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
856 fileChooser.setApproveButtonText("Ok");
858 if (m_currentDirectory == null)
860 fileChooser.rescanCurrentDirectory();
862 else
864 fileChooser.setCurrentDirectory(m_currentDirectory);
867 fileChooser.setFileFilter(new XMLFileFilter());
869 int result = fileChooser.showDialog(this,null);
870 if (result==fileChooser.APPROVE_OPTION)
874 m_currentDirectory = fileChooser.getCurrentDirectory();
875 saveFile(fileChooser.getSelectedFile());
877 catch(Exception exce)
879 System.out.println("save file Exception");
880 exce.printStackTrace();
886 * excutes a batch file.
888 private void runBatch(File f)
890 FileInputStream fis = null;
894 fis = new FileInputStream(f);
895 StringBuffer commandBuffer = new StringBuffer();
897 m_logFileOutputStream = new FileOutputStream("TestTool-log.txt");
898 m_bIsBatchRunning = true;
899 int ch = 0;
901 while (ch != -1)
903 ch = fis.read();
905 if (ch != 0x0a && ch != -1)
907 if (ch != 0x0d)
909 commandBuffer.append((char)ch);
912 else
914 String command = new String(commandBuffer);
915 if (command.startsWith("Open "))
917 m_logFileOutputStream.write(("start \""+command+"\" ...\n").getBytes());
918 String fileName = command.substring(5);
919 parseFile(new File(fileName));
920 m_logFileOutputStream.write("command end \n\n".getBytes());
922 else if (command.startsWith("Use Java Component"))
924 m_logFileOutputStream.write(("start \""+command+"\" ...\n").getBytes());
925 m_isJavaComponentButton.setSelected(true);
926 m_logFileOutputStream.write("command end \n\n".getBytes());
928 else if (command.startsWith("Use C++ Component"))
930 m_logFileOutputStream.write(("start \""+command+"\" ...\n").getBytes());
931 m_isJavaComponentButton.setSelected(false);
932 m_logFileOutputStream.write("command end \n\n".getBytes());
934 else if (command.startsWith("Go "))
936 m_logFileOutputStream.write(("start \""+command+"\" ...\n").getBytes());
937 String opera = command.substring(3);
938 if (opera.equals("Sign") || opera.equals("Encrypt"))
940 m_isExportingButton.setSelected(true);
942 else
944 m_isExportingButton.setSelected(false);
947 startsUp();
948 if (m_parsingThread != null)
950 m_bIsUIUpdateSuppressed = true;
951 try{
952 while (m_parsingThread.nextStep());
953 endMission();
955 catch(Exception e)
957 System.out.println("exception happen during batch:"+e);
958 e.printStackTrace();
961 m_bIsUIUpdateSuppressed = false;
962 updatesUIs();
964 m_logFileOutputStream.write("command end \n\n".getBytes());
966 else if (command.startsWith("Save "))
968 m_logFileOutputStream.write(("start \""+command+"\" ...\n").getBytes());
969 String fileName = command.substring(5);
970 saveFile(new File(fileName));
971 m_logFileOutputStream.write("command end \n\n".getBytes());
974 commandBuffer = new StringBuffer();
978 m_bIsBatchRunning = false;
979 m_logFileOutputStream.close();
980 m_logFileOutputStream = null;
982 fis.close();
983 fis = null;
985 catch(java.io.IOException e)
987 e.printStackTrace();
992 * save the current result tree to a particular file.
994 private void saveFile(File f)
996 try{
997 FileOutputStream fos = new FileOutputStream(f);
998 SAXEventPrinter.display((Document)m_rightTreeEventCollector.getDocument(),
999 0,fos,false);
1000 fos.close();
1001 }catch(Exception e)
1003 e.printStackTrace();
1007 /******************************************************************************
1008 * others
1009 ******************************************************************************/
1012 * starts up the operation.
1014 private void startsUp()
1016 if (m_parsingThread != null)
1018 m_parsingThread = null;
1021 m_bIsExporting = m_isExportingButton.isSelected();
1022 m_bIsJavaBased = m_isJavaComponentButton.isSelected();
1024 if (createComponents())
1026 m_rightTreeEventCollector = new SAXEventCollector(this);
1028 m_parsingThread = new ParsingThread(
1029 m_document,
1030 null,
1031 this);
1033 m_xmlSecurityFrameworkController =
1034 new XMLSecurityFrameworkController(
1035 this,
1036 m_bIsExporting,
1037 m_bIsJavaBased,
1038 m_rightTreeEventCollector,
1039 m_parsingThread,
1040 m_xXMLSecurityContext,
1041 m_xXMLSignature,
1042 m_xXMLEncryption,
1043 m_xRemoteServiceManager,
1044 m_xRemoteContext);
1046 enableGoButton(true);
1048 else
1050 showMessage("Error in creating XML Security Components!");
1054 /**************************************************************************************
1055 * protected methods
1056 **************************************************************************************/
1058 /******************************************************************************
1059 * UI related methods
1060 ******************************************************************************/
1063 * updates the sax chain information.
1065 protected void updatesSAXChainInformation(String chainStr)
1067 m_saxChainLabel.setText(chainStr);
1071 * update the current SAX event information.
1073 protected void updatesCurrentSAXEventInformation(String event)
1075 m_saxEventText.setText(event);
1079 * updates all information in the UI.
1081 protected void updatesUIs()
1083 if (!m_bIsUIUpdateSuppressed)
1085 m_leftTree.clearSelection();
1086 updatesTree(null, m_leftTree);
1088 if (m_xmlSecurityFrameworkController != null)
1090 String bufferNodeTreeText = m_xmlSecurityFrameworkController.getBufferNodeTreeInformation();
1091 if (bufferNodeTreeText == null)
1093 m_middleTree.setVisible(false);
1094 m_bufferNodeTextArea.setText("No XML Security Related");
1096 else
1098 m_middleTreeEventCollector = new SAXEventCollector(null);
1099 m_xmlSecurityFrameworkController.getDocument(m_middleTreeEventCollector);
1101 m_middleTreeModelAdapter = new DomToTreeModelAdapter(m_middleTreeEventCollector.getDocument());
1102 m_middleTree.setModel(m_middleTreeModelAdapter);
1103 updatesTree(null, m_middleTree);
1104 m_middleTree.setVisible(true);
1105 m_bufferNodeTextArea.setText(bufferNodeTreeText);
1108 else
1110 m_middleTree.setVisible(false);
1111 m_bufferNodeTextArea.setText("No XMLImporter/XMLExporter");
1114 if (m_rightTreeEventCollector != null)
1116 m_rightTreeModelAdapter = new DomToTreeModelAdapter((Document)m_rightTreeEventCollector.getDocument());
1117 m_rightTree.setModel(m_rightTreeModelAdapter);
1118 updatesTree((Node)m_rightTreeEventCollector.getCurrentElement(), m_rightTree);
1121 updatesUnsolvedReferencesInformation();
1126 * shows a message.
1128 protected void showMessage(String msg)
1130 if (m_bIsBatchRunning)
1134 if (!msg.startsWith("Message from : SAXEventKeeper"))
1136 byte [] b = msg.getBytes();
1137 m_logFileOutputStream.write(" ".getBytes());
1139 for (int i=0; i<b.length; ++i)
1141 m_logFileOutputStream.write(b[i]);
1142 if (b[i] == '\n')
1144 m_logFileOutputStream.write(" ".getBytes());
1147 m_logFileOutputStream.write("\n ==============================\n".getBytes());
1150 catch(IOException e)
1152 e.printStackTrace();
1155 else
1157 if (stepMode)
1159 JOptionPane optionPane = new JOptionPane();
1160 optionPane.showMessageDialog(this, msg, "TestTool Notification", JOptionPane.INFORMATION_MESSAGE);
1162 else
1164 Object[] options = { "OK", "Go back to step mode" };
1165 if (1 == JOptionPane.showOptionDialog(this, msg, "TestTool Notification",
1166 JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
1167 null, options, options[0]))
1169 stepMode = true;
1176 /******************************************************************************
1177 * information retrieving
1178 ******************************************************************************/
1181 * gets all unsolved reference ids.
1182 * a reference id is the value of the id attribute of an
1183 * referenced element.
1185 protected Vector getUnsolvedReferenceIds()
1187 Vector rc;
1189 if (m_xmlSecurityFrameworkController == null)
1191 rc = new Vector();
1193 else
1195 rc = ((XMLSecurityFrameworkController)m_xmlSecurityFrameworkController).
1196 getUnsolvedReferenceIds();
1199 return rc;
1203 * gets all unsolved reference keeper ids.
1204 * a reference keeper id is the id which the SAXEventKeeper uses
1205 * to identify the corresponding BufferNode.
1207 protected Vector getUnsolvedReferenceKeeperIds()
1209 Vector rc;
1211 if (m_xmlSecurityFrameworkController == null)
1213 rc = new Vector();
1215 else
1217 rc = ((XMLSecurityFrameworkController)m_xmlSecurityFrameworkController).
1218 getUnsolvedReferenceKeeperIds();
1221 return rc;
1225 * gets all unsolved references' remaining numbers.
1226 * a remaining number is that how many claims have not been found for
1227 * a unsolved reference.
1229 protected Vector getUnsolvedReferenceRefNum()
1231 Vector rc;
1233 if (m_xmlSecurityFrameworkController == null)
1235 rc = new Vector();
1237 else
1239 rc = ((XMLSecurityFrameworkController)m_xmlSecurityFrameworkController).
1240 getUnsolvedReferenceRefNum();
1243 return rc;
1247 /**************************************************************************************
1248 * public methods
1249 **************************************************************************************/
1251 /******************************************************************************
1252 * action listener related methods.
1253 ******************************************************************************/
1256 * action listening method.
1258 public void actionPerformed(ActionEvent e)
1260 if (e.getSource().equals(m_startButton))
1262 endMission();
1263 startsUp();
1265 if (e.getSource().equals(m_goButton))
1267 if (m_parsingThread != null)
1269 stepMode = false;
1270 boolean notOver;
1271 while ( notOver = m_parsingThread.nextStep())
1273 if (stepMode) break;
1276 if (!notOver) endMission();
1279 if (e.getSource().equals(m_stepButton))
1281 if (m_parsingThread != null)
1283 if (!m_parsingThread.nextStep())
1285 endMission();
1289 if (e.getSource().equals(m_openButton))
1291 openDocument();
1293 if (e.getSource().equals(m_saveButton))
1295 saveResult();
1297 if (e.getSource().equals(m_batchButton))
1299 openBatch();
1304 * void-consturctor method
1306 public TestTool()
1308 getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
1312 m_currentDirectory = new File(System.getProperty("user.dir"));
1314 catch(Exception e)
1316 System.out.println("getProperty error :"+e);
1321 * consturctor method with a specific connection URL
1323 public TestTool(String connecturl)
1325 this();
1326 m_unoURL = new String(connecturl);
1329 public static void main(String argv[])
1331 Dimension screenSize =
1332 Toolkit.getDefaultToolkit().getScreenSize();
1334 TestTool tt;
1336 if (argv.length < 1)
1338 System.out.println("Usage: java TestTool [javaTokenFile] [nssTokenPath] [xml file]?");
1339 return;
1342 boolean hasFile = false;
1343 boolean hasBatch = false;
1344 String fileName = null;
1346 if (argv.length >= 3)
1348 if (argv[2].startsWith("-b"))
1350 fileName = argv[2].substring(2);
1351 hasBatch = true;
1353 else
1355 fileName = argv[2];
1356 hasFile = true;
1360 tt = new TestTool();
1361 tt.m_javaTokenFile = new String(argv[0]);
1362 tt.m_nssTokenPath = new String(argv[1]);
1363 tt.connectSO(null);
1365 /* Set up a GUI framework */
1366 JFrame myFrame = new JFrame("XML Security Components Tester");
1367 myFrame.addWindowListener(
1368 new WindowAdapter() {
1369 public void windowClosing(WindowEvent e) {System.exit(0);}
1373 myFrame.setContentPane(tt.buildUI(screenSize.width, screenSize.height));
1374 myFrame.pack();
1375 int w = screenSize.width-30;
1376 int h = screenSize.height-30;
1377 myFrame.setLocation(screenSize.width/2 - w/2,
1378 screenSize.height/2 - h/2);
1379 myFrame.setSize(w, h);
1380 myFrame.setVisible(true);
1382 if (hasFile)
1384 tt.parseFile(new File(fileName));
1386 else if (hasBatch)
1388 tt.runBatch(new File(fileName));