1*cd519653SAndrew Rist /**************************************************************
2*cd519653SAndrew Rist  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cd519653SAndrew Rist  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cd519653SAndrew Rist  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19*cd519653SAndrew Rist  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22cdf0e10cSrcweir package installer;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import java.io.*;
25cdf0e10cSrcweir import javax.swing.JLabel;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir public class FileUpdater {
28cdf0e10cSrcweir 
updateProtocolHandler( String installPath, JLabel statusLabel )29cdf0e10cSrcweir 	public static boolean updateProtocolHandler( String installPath, JLabel statusLabel ) {
30cdf0e10cSrcweir             File in_file = null;
31cdf0e10cSrcweir             FileInputStream in = null;
32cdf0e10cSrcweir             File out_file = null;
33cdf0e10cSrcweir             FileWriter out = null;
34cdf0e10cSrcweir             int count = 0;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir             try {
37cdf0e10cSrcweir                 in_file = new File( installPath+File.separator+"share"+File.separator+"registry"+File.separator+"data"+File.separator+"org"+File.separator+"openoffice"+File.separator+"Office"+File.separator+"ProtocolHandler.xcu" );
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 		String[] xmlArray = new String[50];
40cdf0e10cSrcweir 		try {
41cdf0e10cSrcweir 			BufferedReader reader = new BufferedReader(new FileReader(in_file));
42cdf0e10cSrcweir 			count = -1;
43cdf0e10cSrcweir 	    		for (String s = reader.readLine(); s != null; s = reader.readLine()) { //</oor:node>
44cdf0e10cSrcweir 				count = count + 1;
45cdf0e10cSrcweir 				if(s != null) {
46cdf0e10cSrcweir 					s.trim();
47cdf0e10cSrcweir 					xmlArray[count] = s;
48cdf0e10cSrcweir 				}
49cdf0e10cSrcweir 				else
50cdf0e10cSrcweir 					break;
51cdf0e10cSrcweir 			}
52cdf0e10cSrcweir 		}
53cdf0e10cSrcweir 		catch( IOException ioe ) {
54cdf0e10cSrcweir 			String message = "\nError reading ProtocolHandler.xcu, please view SFrameworkInstall.log.";
55cdf0e10cSrcweir 			System.out.println(message);
56cdf0e10cSrcweir 			ioe.printStackTrace();
57cdf0e10cSrcweir 			statusLabel.setText(message);
58cdf0e10cSrcweir 			return false;
59cdf0e10cSrcweir 		}
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 		in_file.delete();
62cdf0e10cSrcweir 
63cdf0e10cSrcweir                 out_file = new File( installPath+File.separator+"share"+File.separator+"registry"+File.separator+"data"+File.separator+"org"+File.separator+"openoffice"+File.separator+"Office"+File.separator+"ProtocolHandler.xcu" );
64cdf0e10cSrcweir                 out_file.createNewFile();
65cdf0e10cSrcweir                 out = new FileWriter( out_file );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 		for(int i=0; i<count + 1; i++) {
68cdf0e10cSrcweir                     out.write(xmlArray[i]+"\n");
69cdf0e10cSrcweir                     if( ( xmlArray[i].indexOf( "<node oor:name=\"HandlerSet\">" ) != -1 ) && ( xmlArray[i+1].indexOf( "ScriptProtocolHandler" ) == -1 ) ) {
70cdf0e10cSrcweir                         out.write( "		<node oor:name=\"com.sun.star.comp.ScriptProtocolHandler\" oor:op=\"replace\">\n" );
71cdf0e10cSrcweir                         out.write( "			<prop oor:name=\"Protocols\">\n" );
72cdf0e10cSrcweir                         out.write( "				<value>script:*</value>\n" );
73cdf0e10cSrcweir                         out.write( "			</prop>\n" );
74cdf0e10cSrcweir                         out.write( "		</node>\n" );
75cdf0e10cSrcweir                      }
76cdf0e10cSrcweir                 }
77cdf0e10cSrcweir             }
78cdf0e10cSrcweir             catch( Exception e ) {
79cdf0e10cSrcweir 		String message = "\nError updating ProtocolHandler.xcu, please view SFrameworkInstall.log.";
80cdf0e10cSrcweir                 System.out.println(message);
81cdf0e10cSrcweir 		e.printStackTrace();
82cdf0e10cSrcweir 		statusLabel.setText(message);
83cdf0e10cSrcweir 		return false;
84cdf0e10cSrcweir             }
85cdf0e10cSrcweir             finally {
86cdf0e10cSrcweir                 try {
87cdf0e10cSrcweir                     out.close();
88cdf0e10cSrcweir                     System.out.println("File closed");
89cdf0e10cSrcweir                 }
90cdf0e10cSrcweir                 catch(Exception e) {
91cdf0e10cSrcweir                     System.out.println("Update ProtocolHandler Failed, please view SFrameworkInstall.log.");
92cdf0e10cSrcweir 		    System.err.println(e);
93cdf0e10cSrcweir 		    e.printStackTrace();
94cdf0e10cSrcweir                 }
95cdf0e10cSrcweir             }
96cdf0e10cSrcweir 	    return true;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	}// updateProtocolHandler
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 
updateScriptXLC( String installPath, JLabel statusLabel )101cdf0e10cSrcweir         public static boolean updateScriptXLC( String installPath, JLabel statusLabel ) {
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             File in_file = null;
104cdf0e10cSrcweir             FileInputStream in = null;
105cdf0e10cSrcweir             File out_file = null;
106cdf0e10cSrcweir             FileWriter out = null;
107cdf0e10cSrcweir             int count = 0;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 	    //System.out.println("updateScriptXLC");
110cdf0e10cSrcweir             try {
111cdf0e10cSrcweir                 in_file = new File( installPath+File.separator+"user"+File.separator+"basic"+File.separator+"script.xlc" );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 		String[] xmlArray = new String[50];
114cdf0e10cSrcweir 		try {
115cdf0e10cSrcweir 			BufferedReader reader = new BufferedReader(new FileReader(in_file));
116cdf0e10cSrcweir 			count = -1;
117cdf0e10cSrcweir 	    		for (String s = reader.readLine(); s != null; s = reader.readLine()) { //</oor:node>
118cdf0e10cSrcweir 				count = count + 1;
119cdf0e10cSrcweir 				if(s != null) {
120cdf0e10cSrcweir 					s.trim();
121cdf0e10cSrcweir 					xmlArray[count] = s;
122cdf0e10cSrcweir 				}
123cdf0e10cSrcweir 				else
124cdf0e10cSrcweir 					break;
125cdf0e10cSrcweir 			}
126cdf0e10cSrcweir 		}
127cdf0e10cSrcweir 		catch( IOException ioe ) {
128cdf0e10cSrcweir 			String message = "Error reading script.xlc, please view SFrameworkInstall.log.";
129cdf0e10cSrcweir 			System.out.println(message);
130cdf0e10cSrcweir 			ioe.printStackTrace();
131cdf0e10cSrcweir 			statusLabel.setText(message);
132cdf0e10cSrcweir 			return false;
133cdf0e10cSrcweir 		}
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 		in_file.delete();
136cdf0e10cSrcweir 
137cdf0e10cSrcweir                 out_file = new File( installPath+File.separator+"user"+File.separator+"basic"+File.separator+"script.xlc" );
138cdf0e10cSrcweir                 out_file.createNewFile();
139cdf0e10cSrcweir                 out = new FileWriter( out_file );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir                 //split the string into a string array with one line of xml in each element
142cdf0e10cSrcweir                 //String[] xmlArray = xmlLine.split("\n");
143cdf0e10cSrcweir 		for(int i=0; i<count + 1; i++) {
144cdf0e10cSrcweir                     out.write(xmlArray[i]+"\n");
145cdf0e10cSrcweir                     if( ( xmlArray[i].indexOf( "<library:libraries xmlns:library" ) != -1 ) && ( xmlArray[i+1].indexOf( "ScriptBindingLibrary" ) == -1 ) ) {
146cdf0e10cSrcweir 			String opSys = System.getProperty("os.name");
147cdf0e10cSrcweir 			if (opSys.indexOf("Windows") != -1) {
148cdf0e10cSrcweir 				out.write(" <library:library library:name=\"ScriptBindingLibrary\" library:link=\"true\"/>\n" );
149cdf0e10cSrcweir 			}
150cdf0e10cSrcweir 			else {
151cdf0e10cSrcweir 				out.write(" <library:library library:name=\"ScriptBindingLibrary\" xlink:href=\"file://"+installPath+"/share/basic/ScriptBindingLibrary/script.xlb/\" xlink:type=\"simple\" library:link=\"true\"/>\n" );
152cdf0e10cSrcweir 			}
153cdf0e10cSrcweir                      }
154cdf0e10cSrcweir                 }
155cdf0e10cSrcweir             }
156cdf0e10cSrcweir             catch( Exception e ) {
157cdf0e10cSrcweir 			String message = "\nError updating script.xlc, please view SFrameworkInstall.log.";
158cdf0e10cSrcweir 			System.out.println(message);
159cdf0e10cSrcweir 			e.printStackTrace();
160cdf0e10cSrcweir 			statusLabel.setText(message);
161cdf0e10cSrcweir 			return false;
162cdf0e10cSrcweir             }
163cdf0e10cSrcweir             finally {
164cdf0e10cSrcweir                 try {
165cdf0e10cSrcweir                     out.close();
166cdf0e10cSrcweir                 }
167cdf0e10cSrcweir                 catch(Exception e) {
168cdf0e10cSrcweir                     System.out.println("Update Script.xlc Failed, please view SFrameworkInstall.log.");
169cdf0e10cSrcweir 		    e.printStackTrace();
170cdf0e10cSrcweir                     System.err.println(e);
171cdf0e10cSrcweir                 }
172cdf0e10cSrcweir             }
173cdf0e10cSrcweir 	    return true;
174cdf0e10cSrcweir         }// updateScriptXLC
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 
updateDialogXLC( String installPath, JLabel statusLabel )177cdf0e10cSrcweir         public static boolean updateDialogXLC( String installPath, JLabel statusLabel ) {
178cdf0e10cSrcweir             File in_file = null;
179cdf0e10cSrcweir             FileInputStream in = null;
180cdf0e10cSrcweir             File out_file = null;
181cdf0e10cSrcweir             FileWriter out = null;
182cdf0e10cSrcweir             int count = 0;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir             //System.out.println( "updateDialogXLC" );
185cdf0e10cSrcweir             try {
186cdf0e10cSrcweir                 in_file = new File( installPath+File.separator+"user"+File.separator+"basic"+File.separator+"dialog.xlc" );
187cdf0e10cSrcweir                 String xmlLine = "";
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 		String[] xmlArray = new String[50];
190cdf0e10cSrcweir 		try {
191cdf0e10cSrcweir 			BufferedReader reader = new BufferedReader(new FileReader(in_file));
192cdf0e10cSrcweir 			count = -1;
193cdf0e10cSrcweir 	    		for (String s = reader.readLine(); s != null; s = reader.readLine()) {
194cdf0e10cSrcweir 				count = count + 1;
195cdf0e10cSrcweir 				if(s != null) {
196cdf0e10cSrcweir 					s.trim();
197cdf0e10cSrcweir 					xmlArray[count] = s;
198cdf0e10cSrcweir 				}
199cdf0e10cSrcweir 				else
200cdf0e10cSrcweir 					break;
201cdf0e10cSrcweir 			}
202cdf0e10cSrcweir 		}
203cdf0e10cSrcweir 		catch( IOException ioe ) {
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 			String message = "\nError reading dialog.xlc, please view SFrameworkInstall.log.";
206cdf0e10cSrcweir 			System.out.println(message);
207cdf0e10cSrcweir 			statusLabel.setText(message);
208cdf0e10cSrcweir 			return false;
209cdf0e10cSrcweir 		}
210cdf0e10cSrcweir                 in_file.delete();
211cdf0e10cSrcweir 
212cdf0e10cSrcweir                 out_file = new File( installPath+File.separator+"user"+File.separator+"basic"+File.separator+"dialog.xlc" );
213cdf0e10cSrcweir                 out_file.createNewFile();
214cdf0e10cSrcweir 
215cdf0e10cSrcweir                 out = new FileWriter( out_file );
216cdf0e10cSrcweir 
217cdf0e10cSrcweir                 //split the string into a string array with one line of xml in each element
218cdf0e10cSrcweir                 // String[] xmlArray = xmlLine.split("\n");
219cdf0e10cSrcweir 		for(int i=0; i<count + 1; i++) {
220cdf0e10cSrcweir                     out.write(xmlArray[i]+"\n");
221cdf0e10cSrcweir                     if( ( xmlArray[i].indexOf( "<library:libraries xmlns:library" ) != -1 ) && ( xmlArray[i+1].indexOf( "ScriptBindingLibrary" ) == -1 ) ) {
222cdf0e10cSrcweir 			String opSys = System.getProperty("os.name");
223cdf0e10cSrcweir 			if (opSys.indexOf("Windows") != -1) {
224cdf0e10cSrcweir 				out.write(" <library:library library:name=\"ScriptBindingLibrary\" library:link=\"true\"/>\n" );
225cdf0e10cSrcweir 			}
226cdf0e10cSrcweir 			else {
227cdf0e10cSrcweir 				out.write(" <library:library library:name=\"ScriptBindingLibrary\" xlink:href=\"file://"+installPath+"/share/basic/ScriptBindingLibrary/dialog.xlb/\" xlink:type=\"simple\" library:link=\"true\"/>\n" );
228cdf0e10cSrcweir 			}
229cdf0e10cSrcweir                      }
230cdf0e10cSrcweir                 }
231cdf0e10cSrcweir             }
232cdf0e10cSrcweir             catch( Exception e ) {
233cdf0e10cSrcweir 			String message = "\nError updating dialog.xlc, please view SFrameworkInstall.log.";
234cdf0e10cSrcweir 			System.out.println(message);
235cdf0e10cSrcweir 			e.printStackTrace();
236cdf0e10cSrcweir 			statusLabel.setText(message);
237cdf0e10cSrcweir 			return false;
238cdf0e10cSrcweir             }
239cdf0e10cSrcweir             finally {
240cdf0e10cSrcweir                 try {
241cdf0e10cSrcweir                     out.close();
242cdf0e10cSrcweir                 }
243cdf0e10cSrcweir                 catch(Exception e) {
244cdf0e10cSrcweir                     System.out.println("Update dialog.xlc Failed, please view SFrameworkInstall.log.");
245cdf0e10cSrcweir 		    e.printStackTrace();
246cdf0e10cSrcweir                     System.err.println(e);
247cdf0e10cSrcweir                 }
248cdf0e10cSrcweir             }
249cdf0e10cSrcweir 	    return true;
250cdf0e10cSrcweir         }// updateScriptXLC
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 
253cdf0e10cSrcweir }
254