1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package complex.writer; 29 30 import complexlib.ComplexTestCase; 31 import com.sun.star.uno.UnoRuntime; 32 import com.sun.star.uno.AnyConverter; 33 import com.sun.star.uno.XComponentContext; 34 import com.sun.star.lang.WrappedTargetException; 35 import com.sun.star.lang.WrappedTargetRuntimeException; 36 import com.sun.star.lang.EventObject; 37 import com.sun.star.lang.XMultiServiceFactory; 38 import com.sun.star.lang.XComponent; 39 import com.sun.star.lang.XServiceInfo; 40 import com.sun.star.beans.XPropertySet; 41 import com.sun.star.beans.PropertyValue; 42 import com.sun.star.beans.Pair; 43 import com.sun.star.util.XCloseable; 44 import com.sun.star.frame.XStorable; 45 import com.sun.star.document.DocumentEvent; 46 import com.sun.star.document.XDocumentEventBroadcaster; 47 import com.sun.star.document.XDocumentEventListener; 48 import com.sun.star.text.XTextDocument; 49 50 import java.util.List; 51 import java.util.ArrayList; 52 import java.io.File; 53 54 /** 55 * a small program to load documents from one directory (recursively) 56 * and store them in another, implemented as a complex test. 57 */ 58 public class LoadSaveTest extends ComplexTestCase 59 { 60 private XMultiServiceFactory m_xMSF = null; 61 private XComponentContext m_xContext = null; 62 private XDocumentEventBroadcaster m_xGEB = null; 63 private String m_TmpDir = null; 64 65 private String m_fileURL = "file://"; 66 // these should be parameters or something? 67 private String m_SourceDir = "FIXME"; 68 private String m_TargetDir = "/tmp/out"; 69 70 public String[] getTestMethodNames() { 71 return new String[] { "testLoadStore" }; 72 } 73 74 public void before() throws Exception 75 { 76 m_xMSF = (XMultiServiceFactory) param.getMSF(); 77 XPropertySet xPropertySet = (XPropertySet) 78 UnoRuntime.queryInterface(XPropertySet.class, m_xMSF); 79 Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext"); 80 m_xContext = (XComponentContext) 81 UnoRuntime.queryInterface(XComponentContext.class, defaultCtx); 82 assure("could not get component context.", m_xContext != null); 83 Object oGEB = m_xMSF.createInstance( 84 "com.sun.star.frame.GlobalEventBroadcaster"); 85 m_xGEB = (XDocumentEventBroadcaster) 86 UnoRuntime.queryInterface(XDocumentEventBroadcaster.class, oGEB); 87 assure("could not get global event broadcaster.", m_xGEB != null); 88 m_TmpDir = util.utils.getOfficeTemp/*Dir*/(m_xMSF); 89 log.println("tempdir: " + m_TmpDir); 90 log.println("sourcedir: " + m_SourceDir); 91 log.println("targetdir: " + m_TargetDir); 92 } 93 94 /* 95 public void after() 96 { 97 } 98 */ 99 100 public void testLoadStore() throws Exception 101 { 102 Pair<List<String>, List<String>> dirsFiles = 103 getDirAndFileNames(m_SourceDir); 104 makeDirs(m_TargetDir, dirsFiles.First); 105 for (String fileName : dirsFiles.Second) 106 { 107 try { 108 testDoc(fileName); 109 } catch (Exception e) { 110 report(e); 111 } 112 } 113 } 114 115 public void testDoc(String fileName) throws Exception 116 { 117 XComponent xDoc = null; 118 EventListener xListener = new EventListener(); 119 try { 120 m_xGEB.addDocumentEventListener(xListener); 121 122 log.println("Loading document: " + fileName + " ..."); 123 124 PropertyValue[] loadProps = new PropertyValue[1]; 125 loadProps[0] = new PropertyValue(); 126 loadProps[0].Name = "ReadOnly"; 127 loadProps[0].Value = new Boolean(true); 128 129 String sourceFile = m_fileURL + m_SourceDir + fileName; 130 131 xDoc = util.DesktopTools.loadDoc(m_xMSF, sourceFile, loadProps); 132 133 log.println("... done"); 134 135 { 136 // apparently OnLayoutFinished is not sent for every doc??? 137 // 10 seconds is evidently not enough for large documents 138 int time = 0; 139 while (!xListener.IsLayoutFinished() && (time < 30000)) { 140 Thread.sleep(100); 141 time += 100; 142 } 143 if (time >= 30000) { 144 log.println("timeout: no OnLayoutFinished received!"); 145 } 146 } 147 148 log.println("Storing document: " + fileName + " ..."); 149 150 XStorable xStor = (XStorable) UnoRuntime.queryInterface( 151 XStorable.class, xDoc); 152 153 String targetFile = m_fileURL + m_TargetDir + fileName; 154 155 xStor.storeToURL(targetFile, new PropertyValue[0]); 156 157 log.println("... done"); 158 159 } finally { 160 if (xDoc != null) { 161 util.DesktopTools.closeDoc(xDoc); 162 } 163 if (xListener != null) { 164 m_xGEB.removeDocumentEventListener(xListener); 165 } 166 } 167 } 168 169 class EventListener implements XDocumentEventListener 170 { 171 boolean m_isLayoutFinished = false; 172 boolean IsLayoutFinished() { return m_isLayoutFinished; } 173 public void documentEventOccured(DocumentEvent Event) 174 { 175 // log.println("event: " + Event.EventName); 176 if ("OnLayoutFinished".equals(Event.EventName)) 177 { 178 // we only have one doc at any time, so no need to check 179 m_isLayoutFinished = true; 180 // log.println("received OnLayoutFinished"); 181 } 182 } 183 public void disposing(EventObject Event) { } 184 } 185 186 void report2(Exception e) 187 { 188 if (e instanceof WrappedTargetException) 189 { 190 log.println("Cause:"); 191 Exception cause = (Exception) 192 (((WrappedTargetException)e).TargetException); 193 log.println(cause.toString()); 194 report2(cause); 195 } else if (e instanceof WrappedTargetRuntimeException) { 196 log.println("Cause:"); 197 Exception cause = (Exception) 198 (((WrappedTargetRuntimeException)e).TargetException); 199 log.println(cause.toString()); 200 report2(cause); 201 } 202 } 203 204 void report(Exception e) { 205 log.println("Exception occurred:"); 206 log.println(e.toString()); 207 e.printStackTrace((java.io.PrintWriter) log); 208 report2(e); 209 // failed(); 210 } 211 212 Pair<List<String>, List<String>> getDirAndFileNames(String dir) 213 { 214 List<String> dirs = new ArrayList<String>(); 215 List<String> files = new ArrayList<String>(); 216 File root = new File(dir); 217 getDirAndFileNames(root, "", dirs, files); 218 return new Pair<List<String>, List<String>>(dirs, files); 219 } 220 221 void getDirAndFileNames(File file, String relPath, 222 List<String> dirs, List<String> files) 223 { 224 assure("does not exist: " + relPath, file.exists()); 225 if (file.isDirectory()) { 226 dirs.add(relPath); 227 File[] subfiles = file.listFiles(); 228 for (File subfile : subfiles) 229 { 230 String subfileName = 231 relPath + File.separator + subfile.getName(); 232 getDirAndFileNames(subfile, subfileName, dirs, files); 233 } 234 } 235 else if (file.isFile()) { 236 if (file.getName().endsWith(".odt")) { 237 files.add(relPath); 238 } 239 } 240 } 241 242 void makeDirs(String target, List<String> dirs) throws Exception 243 { 244 for (String dir : dirs) { 245 File f = new File(target + dir); 246 if (!f.exists()) { 247 if (!f.mkdir()) { 248 throw new Exception("cannot mkdir: " + target + dir); 249 } 250 } 251 } 252 } 253 } 254 255