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 javax.swing.JLabel;
26 
27 public class FileUpdater {
28 
updateProtocolHandler( String installPath, JLabel statusLabel )29 	public static boolean updateProtocolHandler( String installPath, JLabel statusLabel ) {
30             File in_file = null;
31             FileInputStream in = null;
32             File out_file = null;
33             FileWriter out = null;
34             int count = 0;
35 
36             try {
37                 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" );
38 
39 		String[] xmlArray = new String[50];
40 		try {
41 			BufferedReader reader = new BufferedReader(new FileReader(in_file));
42 			count = -1;
43 	    		for (String s = reader.readLine(); s != null; s = reader.readLine()) { //</oor:node>
44 				count = count + 1;
45 				if(s != null) {
46 					s.trim();
47 					xmlArray[count] = s;
48 				}
49 				else
50 					break;
51 			}
52 		}
53 		catch( IOException ioe ) {
54 			String message = "\nError reading ProtocolHandler.xcu, please view SFrameworkInstall.log.";
55 			System.out.println(message);
56 			ioe.printStackTrace();
57 			statusLabel.setText(message);
58 			return false;
59 		}
60 
61 		in_file.delete();
62 
63                 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" );
64                 out_file.createNewFile();
65                 out = new FileWriter( out_file );
66 
67 		for(int i=0; i<count + 1; i++) {
68                     out.write(xmlArray[i]+"\n");
69                     if( ( xmlArray[i].indexOf( "<node oor:name=\"HandlerSet\">" ) != -1 ) && ( xmlArray[i+1].indexOf( "ScriptProtocolHandler" ) == -1 ) ) {
70                         out.write( "		<node oor:name=\"com.sun.star.comp.ScriptProtocolHandler\" oor:op=\"replace\">\n" );
71                         out.write( "			<prop oor:name=\"Protocols\">\n" );
72                         out.write( "				<value>script:*</value>\n" );
73                         out.write( "			</prop>\n" );
74                         out.write( "		</node>\n" );
75                      }
76                 }
77             }
78             catch( Exception e ) {
79 		String message = "\nError updating ProtocolHandler.xcu, please view SFrameworkInstall.log.";
80                 System.out.println(message);
81 		e.printStackTrace();
82 		statusLabel.setText(message);
83 		return false;
84             }
85             finally {
86                 try {
87                     out.close();
88                     System.out.println("File closed");
89                 }
90                 catch(Exception e) {
91                     System.out.println("Update ProtocolHandler Failed, please view SFrameworkInstall.log.");
92 		    System.err.println(e);
93 		    e.printStackTrace();
94                 }
95             }
96 	    return true;
97 
98 	}// updateProtocolHandler
99 
100 
updateScriptXLC( String installPath, JLabel statusLabel )101         public static boolean updateScriptXLC( String installPath, JLabel statusLabel ) {
102 
103             File in_file = null;
104             FileInputStream in = null;
105             File out_file = null;
106             FileWriter out = null;
107             int count = 0;
108 
109 	    //System.out.println("updateScriptXLC");
110             try {
111                 in_file = new File( installPath+File.separator+"user"+File.separator+"basic"+File.separator+"script.xlc" );
112 
113 		String[] xmlArray = new String[50];
114 		try {
115 			BufferedReader reader = new BufferedReader(new FileReader(in_file));
116 			count = -1;
117 	    		for (String s = reader.readLine(); s != null; s = reader.readLine()) { //</oor:node>
118 				count = count + 1;
119 				if(s != null) {
120 					s.trim();
121 					xmlArray[count] = s;
122 				}
123 				else
124 					break;
125 			}
126 		}
127 		catch( IOException ioe ) {
128 			String message = "Error reading script.xlc, please view SFrameworkInstall.log.";
129 			System.out.println(message);
130 			ioe.printStackTrace();
131 			statusLabel.setText(message);
132 			return false;
133 		}
134 
135 		in_file.delete();
136 
137                 out_file = new File( installPath+File.separator+"user"+File.separator+"basic"+File.separator+"script.xlc" );
138                 out_file.createNewFile();
139                 out = new FileWriter( out_file );
140 
141                 //split the string into a string array with one line of xml in each element
142                 //String[] xmlArray = xmlLine.split("\n");
143 		for(int i=0; i<count + 1; i++) {
144                     out.write(xmlArray[i]+"\n");
145                     if( ( xmlArray[i].indexOf( "<library:libraries xmlns:library" ) != -1 ) && ( xmlArray[i+1].indexOf( "ScriptBindingLibrary" ) == -1 ) ) {
146 			String opSys = System.getProperty("os.name");
147 			if (opSys.indexOf("Windows") != -1) {
148 				out.write(" <library:library library:name=\"ScriptBindingLibrary\" library:link=\"true\"/>\n" );
149 			}
150 			else {
151 				out.write(" <library:library library:name=\"ScriptBindingLibrary\" xlink:href=\"file://"+installPath+"/share/basic/ScriptBindingLibrary/script.xlb/\" xlink:type=\"simple\" library:link=\"true\"/>\n" );
152 			}
153                      }
154                 }
155             }
156             catch( Exception e ) {
157 			String message = "\nError updating script.xlc, please view SFrameworkInstall.log.";
158 			System.out.println(message);
159 			e.printStackTrace();
160 			statusLabel.setText(message);
161 			return false;
162             }
163             finally {
164                 try {
165                     out.close();
166                 }
167                 catch(Exception e) {
168                     System.out.println("Update Script.xlc Failed, please view SFrameworkInstall.log.");
169 		    e.printStackTrace();
170                     System.err.println(e);
171                 }
172             }
173 	    return true;
174         }// updateScriptXLC
175 
176 
updateDialogXLC( String installPath, JLabel statusLabel )177         public static boolean updateDialogXLC( String installPath, JLabel statusLabel ) {
178             File in_file = null;
179             FileInputStream in = null;
180             File out_file = null;
181             FileWriter out = null;
182             int count = 0;
183 
184             //System.out.println( "updateDialogXLC" );
185             try {
186                 in_file = new File( installPath+File.separator+"user"+File.separator+"basic"+File.separator+"dialog.xlc" );
187                 String xmlLine = "";
188 
189 		String[] xmlArray = new String[50];
190 		try {
191 			BufferedReader reader = new BufferedReader(new FileReader(in_file));
192 			count = -1;
193 	    		for (String s = reader.readLine(); s != null; s = reader.readLine()) {
194 				count = count + 1;
195 				if(s != null) {
196 					s.trim();
197 					xmlArray[count] = s;
198 				}
199 				else
200 					break;
201 			}
202 		}
203 		catch( IOException ioe ) {
204 
205 			String message = "\nError reading dialog.xlc, please view SFrameworkInstall.log.";
206 			System.out.println(message);
207 			statusLabel.setText(message);
208 			return false;
209 		}
210                 in_file.delete();
211 
212                 out_file = new File( installPath+File.separator+"user"+File.separator+"basic"+File.separator+"dialog.xlc" );
213                 out_file.createNewFile();
214 
215                 out = new FileWriter( out_file );
216 
217                 //split the string into a string array with one line of xml in each element
218                 // String[] xmlArray = xmlLine.split("\n");
219 		for(int i=0; i<count + 1; i++) {
220                     out.write(xmlArray[i]+"\n");
221                     if( ( xmlArray[i].indexOf( "<library:libraries xmlns:library" ) != -1 ) && ( xmlArray[i+1].indexOf( "ScriptBindingLibrary" ) == -1 ) ) {
222 			String opSys = System.getProperty("os.name");
223 			if (opSys.indexOf("Windows") != -1) {
224 				out.write(" <library:library library:name=\"ScriptBindingLibrary\" library:link=\"true\"/>\n" );
225 			}
226 			else {
227 				out.write(" <library:library library:name=\"ScriptBindingLibrary\" xlink:href=\"file://"+installPath+"/share/basic/ScriptBindingLibrary/dialog.xlb/\" xlink:type=\"simple\" library:link=\"true\"/>\n" );
228 			}
229                      }
230                 }
231             }
232             catch( Exception e ) {
233 			String message = "\nError updating dialog.xlc, please view SFrameworkInstall.log.";
234 			System.out.println(message);
235 			e.printStackTrace();
236 			statusLabel.setText(message);
237 			return false;
238             }
239             finally {
240                 try {
241                     out.close();
242                 }
243                 catch(Exception e) {
244                     System.out.println("Update dialog.xlc Failed, please view SFrameworkInstall.log.");
245 		    e.printStackTrace();
246                     System.err.println(e);
247                 }
248             }
249 	    return true;
250         }// updateScriptXLC
251 
252 
253 }
254