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 installer;
23 
24 import java.io.*;
25 import java.util.*;
26 import java.util.jar.*;
27 import java.net.URL;
28 import java.net.JarURLConnection;
29 import javax.swing.*;
30 
31 /**
32  *
33  *
34  *  @author  Aidan Butler
35  */
36 public class XmlUpdater extends Thread {
37 
38     private String classesPath = null;
39     private String jarfilename;
40     private String installPath;
41     private boolean netInstall;
42     private boolean bindingsInstall;
43 
44     private JLabel statusLabel;
45 
46     private Vector listeners;
47     private Thread internalThread;
48     private boolean threadSuspended;
49     private JProgressBar progressBar;
50 
51     private final String[] bakFiles =
52     {
53         "writermenubar.xml",
54         "writerkeybinding.xml",
55         "calcmenubar.xml",
56         "calckeybinding.xml",
57         "impressmenubar.xml",
58         "impresskeybinding.xml",
59         "drawmenubar.xml",
60         "drawkeybinding.xml",
61         "eventbindings.xml",
62         "META-INF" + File.separator + "manifest.xml"
63     };
64 
65     private final String[] dirs =
66     {
67         "java" + File.separator + "Highlight",
68         "java" + File.separator + "MemoryUsage",
69         "java" + File.separator + "ScriptFrmwrkHelper",
70         "java" + File.separator + "debugger",
71         "java" + File.separator + "debugger" + File.separator + "rhino",
72         "beanshell" + File.separator + "InteractiveBeanShell",
73         "beanshell" + File.separator + "Highlight",
74         "beanshell" + File.separator + "MemoryUsage",
75         "javascript" + File.separator + "ExportSheetsToHTML"
76     };
77 
78     private final String[] names =
79     {
80         "java/Highlight/HighlightUtil.java",
81         "java/Highlight/HighlightText.java",
82         "java/Highlight/Highlight.jar",
83         "java/Highlight/parcel-descriptor.xml",
84         "java/MemoryUsage/MemoryUsage.java",
85         "java/MemoryUsage/MemoryUsage.class",
86         "java/MemoryUsage/parcel-descriptor.xml",
87         "java/MemoryUsage/ExampleSpreadSheet.sxc",
88         "java/ScriptFrmwrkHelper/parcel-descriptor.xml",
89         "java/ScriptFrmwrkHelper/ScriptFrmwrkHelper.java",
90         "java/ScriptFrmwrkHelper/ScriptFrmwrkHelper.class",
91         "java/ScriptFrmwrkHelper/ScriptFrmwrkHelper.jar",
92         "java/debugger/debugger.jar",
93         "java/debugger/OOBeanShellDebugger.java",
94         "java/debugger/OOScriptDebugger.java",
95         "java/debugger/DebugRunner.java",
96         "java/debugger/OORhinoDebugger.java",
97         "java/debugger/parcel-descriptor.xml",
98         "java/debugger/rhino/Main.java",
99         "beanshell/InteractiveBeanShell/parcel-descriptor.xml",
100         "beanshell/InteractiveBeanShell/interactive.bsh",
101         "beanshell/Highlight/parcel-descriptor.xml",
102         "beanshell/Highlight/highlighter.bsh",
103         "beanshell/MemoryUsage/parcel-descriptor.xml",
104         "beanshell/MemoryUsage/memusage.bsh",
105         "javascript/ExportSheetsToHTML/parcel-descriptor.xml",
106         "javascript/ExportSheetsToHTML/exportsheetstohtml.js"
107     };
108 
109 
XmlUpdater(String installPath, JLabel statusLabel,JProgressBar pBar, boolean netInstall, boolean bindingsInstall)110     public XmlUpdater(String installPath, JLabel statusLabel,JProgressBar pBar, boolean netInstall, boolean bindingsInstall) {
111         this.installPath = installPath;
112         this.statusLabel = statusLabel;
113         this.netInstall = netInstall;
114         this.bindingsInstall = bindingsInstall;
115         listeners = new Vector();
116         threadSuspended = false;
117         progressBar=pBar;
118         progressBar.setStringPainted(true);
119     }// XmlUpdater
120 
121 
checkStop()122 	public boolean checkStop()
123 	{
124         if (internalThread == Thread.currentThread())
125             return false;
126         return true;
127 	}// checkStop
128 
129 
checkSuspend()130 	public void checkSuspend()
131 	{
132         if (threadSuspended) {
133             synchronized(this) {
134                 while (threadSuspended) {
135                     try	{
136                         wait();
137                     } catch (InterruptedException eInt) {
138                         //...
139                     }
140                 }
141             }
142         }
143 	}// checkSuspend
144 
145 
setSuspend()146 	public void setSuspend()
147 	{
148         threadSuspended = true;
149 	}// setSuspend
150 
151 
setResume()152 	public void setResume()
153 	{
154         threadSuspended = false;
155         notify();
156 	}// setResume
157 
158 
setStop()159 	public void setStop()
160 	{
161         internalThread = null;
162 	}// setStop
163 
164 
run()165     public void run() {
166 
167         InputStream            istream;
168         //InputSource            isource;
169         //DocumentBuilderFactory builderFactory;
170         //DocumentBuilder        builder = null;
171         URL                    url;
172         String                 fileName = null;
173 
174         internalThread = Thread.currentThread();
175 
176         //System.out.println("\n\n\n\nFileName: "+installPath);
177         classesPath= installPath.concat(File.separator+"program"+File.separator+"classes"+File.separator);
178         String opSys =System.getProperty("os.name");
179         //System.out.println("\n System "+opSys);
180 
181         String progpath=installPath;
182         progpath= progpath.concat(File.separator+"program"+File.separator);
183         //System.out.println("Office progpath" + progpath );
184         //System.out.println("\nModifying Installation "+installPath);
185 
186         String starBasicPath=installPath;
187         starBasicPath= starBasicPath.concat(File.separator+"share"+File.separator+"basic"+File.separator+"ScriptBindingLibrary"+File.separator);
188         //System.out.println( "Office StarBasic path: " + starBasicPath );
189 
190         String regSchemaOfficePath=installPath;
191         regSchemaOfficePath= regSchemaOfficePath.concat(File.separator+"share"+File.separator+"registry"+File.separator+"schema"+File.separator+"org"+File.separator+"openoffice"+File.separator+"Office"+File.separator);
192         //System.out.println( "Office schema path: " + regSchemaOfficePath );
193 
194         // Get the NetBeans installation
195         //String netbeansPath=
196 
197         progressBar.setString("Unzipping Required Files");
198         ZipData zd = new ZipData("SFrameworkInstall.jar");
199 
200 
201         if( (!netInstall) || bindingsInstall) {
202             String configPath=installPath;
203             configPath= configPath.concat(File.separator+"user"+File.separator+"config"+File.separator+"soffice.cfg"+File.separator);
204             //System.out.println( "Office configuration path: " + configPath );
205             String manifestPath=configPath + "META-INF" + File.separator;
206 
207             //Adding <Office>/user/config/soffice.cfg/
208             File configDir = new File( configPath );
209             if( !configDir.isDirectory() ) {
210                 if( !configDir.mkdir() ) {
211                     System.out.println( "creating  " + configDir + "directory failed");
212                 }
213                 else {
214                     System.out.println( configDir + "directory created");
215                 }
216             }
217             else
218                 System.out.println( "soffice.cfg exists" );
219 
220             File manifestDir = new File( manifestPath );
221             if( !manifestDir.isDirectory() ) {
222                 if( !manifestDir.mkdir() ) {
223                     System.out.println( "creating " + manifestPath + "directory failed");
224                 }
225                 else {
226                     System.out.println( manifestPath + " directory created");
227                 }
228             }
229             else
230                 System.out.println( manifestPath + " exists" );
231 
232             // Backup the confguration files in
233             // <office>/user/config/soffice.cfg/
234             // If they already exist.
235 
236             for( int i=0; i < bakFiles.length; i++ )
237             {
238                 String pathNameBak = configPath + bakFiles[i];
239                 File origFile = new File( pathNameBak );
240                 if( origFile.exists() )
241                 {
242                     System.out.println( "Attempting to backup " + pathNameBak + " to " + pathNameBak + ".bak" );
243                     if(! origFile.renameTo( new File( pathNameBak + ".bak" ) ) )
244                     {
245                         System.out.println( "Failed to backup " + pathNameBak + " to " + pathNameBak + ".bak" );
246                     }
247                 }
248             }
249 
250             // Adding Office configuration files
251             if (!zd.extractEntry("bindingdialog/writermenubar.xml",configPath, statusLabel))
252             {
253                 onInstallComplete();
254                 return;
255             }
256             if (!zd.extractEntry("bindingdialog/writerkeybinding.xml",configPath, statusLabel))
257             {
258                 onInstallComplete();
259                 return;
260             }
261             if (!zd.extractEntry("bindingdialog/calcmenubar.xml",configPath, statusLabel))
262             {
263                 onInstallComplete();
264                 return;
265             }
266             if (!zd.extractEntry("bindingdialog/calckeybinding.xml",configPath, statusLabel))
267             {
268                 onInstallComplete();
269                 return;
270             }
271             if (!zd.extractEntry("bindingdialog/impressmenubar.xml",configPath, statusLabel))
272             {
273                 onInstallComplete();
274                 return;
275             }
276             if (!zd.extractEntry("bindingdialog/impresskeybinding.xml",configPath, statusLabel))
277             {
278                 onInstallComplete();
279                 return;
280             }
281             if (!zd.extractEntry("bindingdialog/drawmenubar.xml",configPath, statusLabel))
282             {
283                 onInstallComplete();
284                 return;
285             }
286             if (!zd.extractEntry("bindingdialog/drawkeybinding.xml",configPath, statusLabel))
287             {
288                 onInstallComplete();
289                 return;
290             }
291             if (!zd.extractEntry("bindingdialog/eventbindings.xml",configPath, statusLabel))
292             {
293                 onInstallComplete();
294                 return;
295             }
296             if (!zd.extractEntry("bindingdialog/manifest.xml",manifestPath, statusLabel))
297             {
298                 onInstallComplete();
299                 return;
300             }
301         }
302 
303         if(!bindingsInstall) {
304             // Adding new directories to Office
305             // Adding <Office>/user/basic/ScriptBindingLibrary/
306             File scriptBindingLib = new File( starBasicPath );
307             if( !scriptBindingLib.isDirectory() ) {
308                 if( !scriptBindingLib.mkdir() ) {
309                     System.out.println( "ScriptBindingLibrary failed");
310                 }
311                 else {
312                     System.out.println( "ScriptBindingLibrary directory created");
313                 }
314             }
315             else
316                 System.out.println( "ScriptBindingLibrary exists" );
317 
318             // Adding Scripting Framework and tools
319             if (!zd.extractEntry("sframework/ooscriptframe.zip",progpath, statusLabel))
320             {
321                 onInstallComplete();
322                 return;
323             }
324 
325             if (!zd.extractEntry("sframework/bshruntime.zip",progpath, statusLabel))
326             {
327                 onInstallComplete();
328                 return;
329             }
330 
331             if (!zd.extractEntry("sframework/jsruntime.zip",progpath, statusLabel))
332             {
333                 onInstallComplete();
334                 return;
335             }
336 
337             if (!zd.extractEntry("schema/Scripting.xcs",regSchemaOfficePath, statusLabel))
338             {
339                 onInstallComplete();
340                 return;
341             }
342 
343     //--------------------------------
344 
345             progressBar.setString("Registering Scripting Framework");
346             progressBar.setValue(3);
347             if(!Register.register(installPath+File.separator, statusLabel) ) {
348                onInstallComplete();
349                return;
350             }
351             progressBar.setValue(5);
352 
353             String path = installPath + File.separator +
354                 "share" + File.separator + "Scripts" + File.separator;
355 
356             for (int i = 0; i < dirs.length; i++) {
357                 File dir = new File(path + dirs[i]);
358 
359                 if (!dir.exists()) {
360                     if (!dir.mkdirs()) {
361                         System.err.println("Error making dir: " +
362                             dir.getAbsolutePath());
363                         onInstallComplete();
364                         return;
365                     }
366                 }
367             }
368 
369             for (int i = 0; i < names.length; i++) {
370                 String source = "/examples/" + names[i];
371                 String dest = path + names[i].replace('/', File.separatorChar);
372 
373                 if (!zd.extractEntry(source, dest, statusLabel)) {
374                     onInstallComplete();
375                     return;
376                 }
377             }
378 
379 
380             // Adding binding dialog
381             if (!zd.extractEntry("bindingdialog/ScriptBinding.xba",starBasicPath, statusLabel))
382             {
383                 onInstallComplete();
384                 return;
385             }
386             if (!zd.extractEntry("bindingdialog/MenuBinding.xdl",starBasicPath, statusLabel))
387             {
388                 onInstallComplete();
389                 return;
390             }
391             if (!zd.extractEntry("bindingdialog/KeyBinding.xdl",starBasicPath, statusLabel))
392             {
393                 onInstallComplete();
394                 return;
395             }
396             if (!zd.extractEntry("bindingdialog/EventsBinding.xdl",starBasicPath, statusLabel))
397             {
398                 onInstallComplete();
399                 return;
400             }
401             if (!zd.extractEntry("bindingdialog/HelpBinding.xdl",starBasicPath, statusLabel))
402             {
403                 onInstallComplete();
404                 return;
405             }
406             if (!zd.extractEntry("bindingdialog/EditDebug.xdl",starBasicPath, statusLabel))
407             {
408                 onInstallComplete();
409                 return;
410             }
411             if (!zd.extractEntry("bindingdialog/dialog.xlb",starBasicPath, statusLabel))
412             {
413                 onInstallComplete();
414                 return;
415             }
416             if (!zd.extractEntry("bindingdialog/script.xlb",starBasicPath, statusLabel))
417             {
418                 onInstallComplete();
419                 return;
420             }
421         }
422 
423 
424         statusLabel.setText("Installation Complete");
425         progressBar.setString("Installation Complete");
426         progressBar.setValue(10);
427         onInstallComplete();
428 
429     }// run
430 
431 
addInstallListener(InstallListener listener)432     public void addInstallListener(InstallListener listener)
433     {
434         listeners.addElement(listener);
435     }// addInstallListener
436 
437 
onInstallComplete()438     private void onInstallComplete()
439     {
440         Enumeration e = listeners.elements();
441         while (e.hasMoreElements())
442         {
443             InstallListener listener = (InstallListener)e.nextElement();
444             listener.installationComplete(null);
445         }
446     }// onInstallComplete
447 
448 }// XmlUpdater class
449