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