1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 package org.apache.openoffice.ooxml.viewer;
23 
24 import java.awt.BorderLayout;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.io.File;
28 import java.util.Vector;
29 
30 import javax.swing.JComponent;
31 import javax.swing.JFileChooser;
32 import javax.swing.JFrame;
33 import javax.swing.JMenu;
34 import javax.swing.JMenuBar;
35 import javax.swing.JMenuItem;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.JScrollPane;
39 import javax.swing.JSplitPane;
40 import javax.swing.JTabbedPane;
41 import javax.swing.UIManager;
42 
43 import org.apache.openoffice.ooxml.framework.part.OOXMLPackage;
44 import org.apache.openoffice.ooxml.framework.part.parser.ParserFactory;
45 import org.apache.openoffice.ooxml.parser.Log;
46 import org.apache.openoffice.ooxml.viewer.content.ContentView;
47 
48 /** A simple viewer for the streams inside an OOXML file.
49  */
50 public class OOXMLViewer
51 {
main(final String[] aArguments)52     public static void main (final String[] aArguments)
53     {
54         try
55         {
56             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
57         }
58         catch (Exception e)
59         {
60             e.printStackTrace();
61         }
62 
63         Log.Dbg = null;
64 
65         // Process options.
66         final Vector<String> aFilenames = new Vector<>();
67         for (int nIndex=0; nIndex<aArguments.length; ++nIndex)
68         {
69             if (aArguments[nIndex].startsWith("-"))
70             {
71                 switch (aArguments[nIndex])
72                 {
73                     case "-t":
74                         ++nIndex;
75                         if (nIndex >= aArguments.length)
76                         {
77                             System.err.printf("expecting argument after option '-t'");
78                             System.exit(1);
79                         }
80                         ParserFactory.SetParserTableFilename(aArguments[nIndex]);
81                         break;
82 
83                     default:
84                         System.out.printf("unknown option '%s'\n", aArguments[nIndex]);
85                         System.exit(1);;
86                         break;
87 
88                 }
89             }
90             else
91                 aFilenames.add(aArguments[nIndex]);
92         }
93 
94         for (final String sFilename : aFilenames)
95         {
96             final OOXMLViewer aViewer = new OOXMLViewer();
97             aViewer.SetFile(new File(sFilename));
98             aViewer.maFrame.setVisible(true);
99         }
100     }
101 
102 
103 
104 
OOXMLViewer()105     private OOXMLViewer ()
106     {
107         maFrame = new JFrame("OOXML Viewer");
108         maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
109         maFrame.setSize(1024,768);
110         maContainer = new JPanel();
111         maFrame.add(maContainer, BorderLayout.CENTER);
112 
113         maFrame.setJMenuBar(CreateMenuBar());
114         maFrame.addKeyListener(new KeyListener());
115     }
116 
117 
118 
119 
SetFile(final File aFile)120     private void SetFile (final File aFile)
121     {
122         if ( ! aFile.exists())
123         {
124             JOptionPane.showMessageDialog(
125                     maFrame,
126                     "File '"+aFile.toString()+"' does not exist",
127                     "File Error",
128                     JOptionPane.ERROR_MESSAGE);
129         }
130         else if ( ! aFile.canRead())
131         {
132             JOptionPane.showMessageDialog(
133                     maFrame,
134                     "Can not open '"+aFile.toString()+"' for reading",
135                     "File Error",
136                     JOptionPane.ERROR_MESSAGE);
137         }
138         else
139         {
140             maContainer.removeAll();
141             maContainer.setLayout(new BorderLayout());
142 
143             final OOXMLPackage aPackage = OOXMLPackage.Create(aFile);
144             final JScrollPane aDetailViewContainer = new JScrollPane(
145                 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
146                 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
147             aDetailViewContainer.getVerticalScrollBar().setUnitIncrement(10);
148             final DetailViewManager aDetailViewManager = new DetailViewManager(aDetailViewContainer, aPackage);
149 
150             final JTabbedPane aLeftSidebar = new JTabbedPane();
151 
152             final ContentView aContentView = new ContentView(aDetailViewManager, aPackage);
153             aLeftSidebar.addTab("Content", new JScrollPane(aContentView));
154 
155             final StreamView aFragmentView = new StreamView(aDetailViewManager, aPackage);
156             aLeftSidebar.addTab("Streams", new JScrollPane(aFragmentView));
157 
158             final PartsView aPartsView = new PartsView(aDetailViewManager, aPackage);
159             aLeftSidebar.addTab("Parts", new JScrollPane(aPartsView));
160 
161             final JSplitPane aPane = new JSplitPane(
162                     JSplitPane.HORIZONTAL_SPLIT,
163                     aLeftSidebar,
164                     aDetailViewContainer
165                     );
166             aPane.setDividerLocation(200);
167 
168             maContainer.add(aPane, BorderLayout.CENTER);
169 
170             aFragmentView.ShowInitialPart();
171         }
172     }
173 
174 
175 
176 
CreateMenuBar()177     private JMenuBar CreateMenuBar ()
178     {
179         final JMenuBar aMenuBar = new JMenuBar();
180 
181         final JMenu aFileMenu = new JMenu("File");
182         aMenuBar.add(aFileMenu);
183 
184         final JMenuItem aOpenItem = new JMenuItem("Open");
185         aOpenItem.addActionListener(new ActionListener()
186         {
187             @Override public void actionPerformed (final ActionEvent aEvent)
188             {
189                 final JFileChooser aFileChooser = new JFileChooser();
190                 final int nResult = aFileChooser.showOpenDialog(null);
191                 if (nResult == JFileChooser.APPROVE_OPTION)
192                 {
193                     final OOXMLViewer aViewer = new OOXMLViewer();
194                     aViewer.SetFile(aFileChooser.getSelectedFile());
195                 }
196             }
197         });
198         aFileMenu.add(aOpenItem);
199 
200         return aMenuBar;
201     }
202 
203 
204 
205 
206     private final JFrame maFrame;
207     private final JComponent maContainer;
208 }
209