1e6e6073dSLiu Zhe /************************************************************** 2e6e6073dSLiu Zhe * 3e6e6073dSLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 4e6e6073dSLiu Zhe * or more contributor license agreements. See the NOTICE file 5e6e6073dSLiu Zhe * distributed with this work for additional information 6e6e6073dSLiu Zhe * regarding copyright ownership. The ASF licenses this file 7e6e6073dSLiu Zhe * to you under the Apache License, Version 2.0 (the 8e6e6073dSLiu Zhe * "License"); you may not use this file except in compliance 9e6e6073dSLiu Zhe * with the License. You may obtain a copy of the License at 10e6e6073dSLiu Zhe * 11e6e6073dSLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 12e6e6073dSLiu Zhe * 13e6e6073dSLiu Zhe * Unless required by applicable law or agreed to in writing, 14e6e6073dSLiu Zhe * software distributed under the License is distributed on an 15e6e6073dSLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16e6e6073dSLiu Zhe * KIND, either express or implied. See the License for the 17e6e6073dSLiu Zhe * specific language governing permissions and limitations 18e6e6073dSLiu Zhe * under the License. 19e6e6073dSLiu Zhe * 20e6e6073dSLiu Zhe *************************************************************/ 21e6e6073dSLiu Zhe package testlib.uno; 22e6e6073dSLiu Zhe 23e6e6073dSLiu Zhe 24*1ff9903bSLi Feng Wang import org.openoffice.test.common.FileUtil; 25e6e6073dSLiu Zhe import org.openoffice.test.uno.UnoApp; 26e6e6073dSLiu Zhe 27e6e6073dSLiu Zhe import com.sun.star.beans.PropertyValue; 28e6e6073dSLiu Zhe import com.sun.star.beans.XPropertySet; 29deb45f49SLiu Zhe import com.sun.star.container.XNameAccess; 30deb45f49SLiu Zhe import com.sun.star.container.XNameContainer; 31e6e6073dSLiu Zhe import com.sun.star.container.XNamed; 32e6e6073dSLiu Zhe import com.sun.star.document.XDocumentInfo; 33e6e6073dSLiu Zhe import com.sun.star.document.XDocumentInfoSupplier; 34e6e6073dSLiu Zhe import com.sun.star.frame.XStorable; 35e6e6073dSLiu Zhe import com.sun.star.io.IOException; 36deb45f49SLiu Zhe import com.sun.star.lang.XComponent; 37e6e6073dSLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 38e6e6073dSLiu Zhe import com.sun.star.style.BreakType; 39deb45f49SLiu Zhe import com.sun.star.style.XStyle; 40deb45f49SLiu Zhe import com.sun.star.style.XStyleFamiliesSupplier; 41e6e6073dSLiu Zhe import com.sun.star.text.ControlCharacter; 42e6e6073dSLiu Zhe import com.sun.star.text.XText; 43e6e6073dSLiu Zhe import com.sun.star.text.XTextContent; 44e6e6073dSLiu Zhe import com.sun.star.text.XTextCursor; 45e6e6073dSLiu Zhe import com.sun.star.text.XTextDocument; 46*1ff9903bSLi Feng Wang import com.sun.star.frame.XComponentLoader; 47e6e6073dSLiu Zhe import com.sun.star.frame.XModel; 48e6e6073dSLiu Zhe import com.sun.star.frame.XController; 49e6e6073dSLiu Zhe import com.sun.star.uno.UnoRuntime; 50e6e6073dSLiu Zhe 51e6e6073dSLiu Zhe public class SWUtil { 52e6e6073dSLiu Zhe 53e6e6073dSLiu Zhe 54e6e6073dSLiu Zhe 55e6e6073dSLiu Zhe 56e6e6073dSLiu Zhe public static void saveAsDoc(XTextDocument document, String url) throws IOException { 57e6e6073dSLiu Zhe saveAs(document, "MS Word 97", url); 58e6e6073dSLiu Zhe 59e6e6073dSLiu Zhe } 60e6e6073dSLiu Zhe 61deb45f49SLiu Zhe public static void saveAsDoc(XComponent component, String url) throws IOException{ 62deb45f49SLiu Zhe XTextDocument document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 63deb45f49SLiu Zhe saveAs(document, "MS Word 97", url); 64deb45f49SLiu Zhe } 65e6e6073dSLiu Zhe 66e6e6073dSLiu Zhe public static void saveAsODT(XTextDocument document, String url) throws IOException { 67e6e6073dSLiu Zhe saveAs(document, "writer8", url); 68e6e6073dSLiu Zhe } 69e6e6073dSLiu Zhe 70e6e6073dSLiu Zhe public static void saveAs(XTextDocument document, String filterValue, String url) throws IOException { 71cebb507aSLiu Zhe XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 72e6e6073dSLiu Zhe PropertyValue[] propsValue = new PropertyValue[1]; 73e6e6073dSLiu Zhe propsValue[0] = new PropertyValue(); 74e6e6073dSLiu Zhe propsValue[0].Name = "FilterName"; 75e6e6073dSLiu Zhe propsValue[0].Value = filterValue; 76e6e6073dSLiu Zhe store.storeAsURL(url, propsValue); 77e6e6073dSLiu Zhe 78e6e6073dSLiu Zhe } 79e6e6073dSLiu Zhe 80e6e6073dSLiu Zhe public static void save(XTextDocument document) throws IOException { 81cebb507aSLiu Zhe XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 82e6e6073dSLiu Zhe store.store(); 83e6e6073dSLiu Zhe } 84e6e6073dSLiu Zhe 85e6e6073dSLiu Zhe public static XTextDocument saveAndReload(XTextDocument document, UnoApp app) throws Exception { 86cebb507aSLiu Zhe XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 87e6e6073dSLiu Zhe store.store(); 88e6e6073dSLiu Zhe String url = document.getURL(); 89e6e6073dSLiu Zhe app.closeDocument(document); 90e6e6073dSLiu Zhe return openDocumentFromURL(url, app); 91e6e6073dSLiu Zhe 92e6e6073dSLiu Zhe } 93e6e6073dSLiu Zhe 94e6e6073dSLiu Zhe public static XTextDocument newDocument(UnoApp app) throws Exception { 95e6e6073dSLiu Zhe return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter")); 96e6e6073dSLiu Zhe 97e6e6073dSLiu Zhe } 98e6e6073dSLiu Zhe 99e6e6073dSLiu Zhe public static XTextDocument openDocumentFromURL(String url, UnoApp app) throws Exception { 100e6e6073dSLiu Zhe return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url)); 101e6e6073dSLiu Zhe 102e6e6073dSLiu Zhe } 103e6e6073dSLiu Zhe public static XTextDocument openDocument(String filePath, UnoApp app) throws Exception { 104e6e6073dSLiu Zhe 105e6e6073dSLiu Zhe return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(filePath)); 106e6e6073dSLiu Zhe 107e6e6073dSLiu Zhe } 108e6e6073dSLiu Zhe 109e6e6073dSLiu Zhe public static void moveCuror2End(XTextDocument document) { 110e6e6073dSLiu Zhe XText xText = document.getText(); 111e6e6073dSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 112e6e6073dSLiu Zhe xTextCursor.gotoEnd(false); 113e6e6073dSLiu Zhe } 114e6e6073dSLiu Zhe 115e6e6073dSLiu Zhe public static void moveCuror2Start(XTextDocument document) { 116e6e6073dSLiu Zhe XText xText = document.getText(); 117e6e6073dSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 118e6e6073dSLiu Zhe xTextCursor.gotoStart(false); 119e6e6073dSLiu Zhe } 120e6e6073dSLiu Zhe 121e6e6073dSLiu Zhe /** 122e6e6073dSLiu Zhe * Set document properties. such as subject, title etc 123e6e6073dSLiu Zhe * @param document - set document information on this document 124e6e6073dSLiu Zhe * @param prop - document information, including "Subject" ,"Title", "Author", "Title", "KeyWords" 125e6e6073dSLiu Zhe * @param propValue - value you want to set for prop 126e6e6073dSLiu Zhe * @throws Exception 127e6e6073dSLiu Zhe */ 128e6e6073dSLiu Zhe public static void setDocumentProperty(XTextDocument document, String prop, String propValue) throws Exception { 129cebb507aSLiu Zhe XDocumentInfoSupplier docInfoSupplier = (XDocumentInfoSupplier) UnoRuntime.queryInterface(XDocumentInfoSupplier.class, document); 130e6e6073dSLiu Zhe XDocumentInfo docInfo = docInfoSupplier.getDocumentInfo(); 131e6e6073dSLiu Zhe XPropertySet propsDocInfo = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, docInfo); 132e6e6073dSLiu Zhe propsDocInfo.setPropertyValue(prop, propValue); 133e6e6073dSLiu Zhe } 134e6e6073dSLiu Zhe 135e6e6073dSLiu Zhe 136e6e6073dSLiu Zhe /** 137e6e6073dSLiu Zhe * Insert a bookmark into text document 138e6e6073dSLiu Zhe * @param document text document 139e6e6073dSLiu Zhe * @param textCursor which part will be bookmarked 140e6e6073dSLiu Zhe * @param bookmarkName bookmark name 141e6e6073dSLiu Zhe * @throws Exception 142e6e6073dSLiu Zhe */ 143e6e6073dSLiu Zhe public static void insertBookmark(XTextDocument document, XTextCursor textCursor, String bookmarkName) throws Exception { 144cebb507aSLiu Zhe XMultiServiceFactory xDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document); 145e6e6073dSLiu Zhe Object xBookmark = xDocFactory.createInstance("com.sun.star.text.Bookmark"); 146cebb507aSLiu Zhe XTextContent xBookmarkAsTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xBookmark); 147cebb507aSLiu Zhe XNamed xBookmarkAsNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, xBookmark); 148e6e6073dSLiu Zhe xBookmarkAsNamed.setName(bookmarkName); 149e6e6073dSLiu Zhe document.getText().insertTextContent(textCursor, xBookmarkAsTextContent, true); 150e6e6073dSLiu Zhe } 151e6e6073dSLiu Zhe 152e6e6073dSLiu Zhe /** 153e6e6073dSLiu Zhe * insert column break in current cursor 154e6e6073dSLiu Zhe * @param xText 155e6e6073dSLiu Zhe * @param currentCursor 156e6e6073dSLiu Zhe * @throws Exception 157e6e6073dSLiu Zhe */ 158e6e6073dSLiu Zhe public static void insertColumnBreak(XText xText, XTextCursor currentCursor) throws Exception 159e6e6073dSLiu Zhe { 160e6e6073dSLiu Zhe XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface( 161e6e6073dSLiu Zhe XPropertySet.class, currentCursor); 162e6e6073dSLiu Zhe xCursorProps.setPropertyValue("BreakType", BreakType.COLUMN_AFTER); 163e6e6073dSLiu Zhe xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false); 164e6e6073dSLiu Zhe } 165e6e6073dSLiu Zhe 166e6e6073dSLiu Zhe /** 167e6e6073dSLiu Zhe * insert page break in current cursor 168e6e6073dSLiu Zhe * @param xText 169e6e6073dSLiu Zhe * @param currentCursor 170e6e6073dSLiu Zhe * @throws Exception 171e6e6073dSLiu Zhe */ 172e6e6073dSLiu Zhe public static void insertPageBreak(XText xText, XTextCursor currentCursor) throws Exception 173e6e6073dSLiu Zhe { 174e6e6073dSLiu Zhe XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface( 175e6e6073dSLiu Zhe XPropertySet.class, currentCursor); 176e6e6073dSLiu Zhe xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER); 177e6e6073dSLiu Zhe xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false); 178e6e6073dSLiu Zhe } 179e6e6073dSLiu Zhe 180e6e6073dSLiu Zhe 181e6e6073dSLiu Zhe /** 182e6e6073dSLiu Zhe * get page count 183e6e6073dSLiu Zhe * @param document 184e6e6073dSLiu Zhe * @return 185e6e6073dSLiu Zhe * @throws Exception 186e6e6073dSLiu Zhe */ 187e6e6073dSLiu Zhe public static int getPageCount(XTextDocument document) throws Exception 188e6e6073dSLiu Zhe { 189e6e6073dSLiu Zhe XModel xmodel = (XModel)UnoRuntime.queryInterface(XModel.class, document); 190e6e6073dSLiu Zhe XController xcont = xmodel.getCurrentController(); 191e6e6073dSLiu Zhe 192e6e6073dSLiu Zhe XPropertySet xps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xcont); 193e6e6073dSLiu Zhe Integer pageCount = (Integer) xps.getPropertyValue("PageCount"); 194e6e6073dSLiu Zhe return pageCount.intValue(); 195e6e6073dSLiu Zhe } 196e6e6073dSLiu Zhe 197deb45f49SLiu Zhe 198deb45f49SLiu Zhe /** 199deb45f49SLiu Zhe * get specific property value of the default page style 200deb45f49SLiu Zhe * @param xComponent 201deb45f49SLiu Zhe * @param propertyName 202deb45f49SLiu Zhe * @return 203deb45f49SLiu Zhe * @throws Exception 204deb45f49SLiu Zhe */ 205deb45f49SLiu Zhe public static Object getDefaultPageStyleProperty(XComponent xComponent, String propertyName) throws Exception 206deb45f49SLiu Zhe { 207deb45f49SLiu Zhe XTextDocument textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent); 208deb45f49SLiu Zhe XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, textDocument); 209deb45f49SLiu Zhe XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface (XNameAccess.class, xSupplier.getStyleFamilies()); 210deb45f49SLiu Zhe XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xFamilies.getByName("PageStyles")); 211deb45f49SLiu Zhe XStyle xStyle = (XStyle)UnoRuntime.queryInterface(XStyle.class, xFamily.getByName("Default")); 212deb45f49SLiu Zhe XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStyle); 213deb45f49SLiu Zhe Object propertyValue = xStyleProps.getPropertyValue(propertyName.toString()); 214deb45f49SLiu Zhe return propertyValue; 215deb45f49SLiu Zhe } 216deb45f49SLiu Zhe 217deb45f49SLiu Zhe /** 218deb45f49SLiu Zhe * set value for specific property of default page style. 219deb45f49SLiu Zhe * @param xComponent 220deb45f49SLiu Zhe * @param propertyName 221deb45f49SLiu Zhe * @param propertyValue 222deb45f49SLiu Zhe * @throws Exception 223deb45f49SLiu Zhe */ 224deb45f49SLiu Zhe public static void setDefaultPageStyleProperty(XComponent xComponent, String propertyName, Object propertyValue) throws Exception 225deb45f49SLiu Zhe { 226deb45f49SLiu Zhe XTextDocument textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent); 227deb45f49SLiu Zhe XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, textDocument); 228deb45f49SLiu Zhe XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface (XNameAccess.class, xSupplier.getStyleFamilies()); 229deb45f49SLiu Zhe XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xFamilies.getByName("PageStyles")); 230deb45f49SLiu Zhe XStyle xStyle = (XStyle)UnoRuntime.queryInterface(XStyle.class, xFamily.getByName("Default")); 231deb45f49SLiu Zhe XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStyle); 232deb45f49SLiu Zhe xStyleProps.setPropertyValue (propertyName.toString(), propertyValue); 233deb45f49SLiu Zhe } 234*1ff9903bSLi Feng Wang 235*1ff9903bSLi Feng Wang public static XTextDocument saveTo_Override_reload(XTextDocument xTextDocument,String filtervalue, String url,UnoApp app) throws Exception { 236*1ff9903bSLi Feng Wang XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument); 237*1ff9903bSLi Feng Wang PropertyValue[] aStoreProperties = new PropertyValue[2]; 238*1ff9903bSLi Feng Wang aStoreProperties[0] = new PropertyValue(); 239*1ff9903bSLi Feng Wang aStoreProperties[1] = new PropertyValue(); 240*1ff9903bSLi Feng Wang aStoreProperties[0].Name = "Override"; 241*1ff9903bSLi Feng Wang aStoreProperties[0].Value = true; 242*1ff9903bSLi Feng Wang aStoreProperties[1].Name = "FilterName"; 243*1ff9903bSLi Feng Wang aStoreProperties[1].Value = filtervalue; 244*1ff9903bSLi Feng Wang xStorable_odt.storeToURL(FileUtil.getUrl(url), aStoreProperties); 245*1ff9903bSLi Feng Wang //reopen the document 246*1ff9903bSLi Feng Wang return (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(url)); 247*1ff9903bSLi Feng Wang } 248*1ff9903bSLi Feng Wang /** 249*1ff9903bSLi Feng Wang * create document from template 250*1ff9903bSLi Feng Wang */ 251*1ff9903bSLi Feng Wang public static XComponent newDocumentFromTemplate(String templatePath,UnoApp unoApp) throws Exception 252*1ff9903bSLi Feng Wang { 253*1ff9903bSLi Feng Wang XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop()); 254*1ff9903bSLi Feng Wang PropertyValue[] pros = new PropertyValue[1]; 255*1ff9903bSLi Feng Wang pros[0] = new PropertyValue(); 256*1ff9903bSLi Feng Wang pros[0].Name = "AsTemplate"; 257*1ff9903bSLi Feng Wang pros[0].Value = new Boolean(true); 258*1ff9903bSLi Feng Wang XComponent component = componentLoader.loadComponentFromURL(FileUtil.getUrl(templatePath), "_blank", 0,pros); 259*1ff9903bSLi Feng Wang return component; 260*1ff9903bSLi Feng Wang } 261e6e6073dSLiu Zhe } 262