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