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 24e6e6073dSLiu Zhe import org.openoffice.test.uno.UnoApp; 25e6e6073dSLiu Zhe 26e6e6073dSLiu Zhe import com.sun.star.beans.PropertyValue; 27e6e6073dSLiu Zhe import com.sun.star.beans.XPropertySet; 28*deb45f49SLiu Zhe import com.sun.star.container.XNameAccess; 29*deb45f49SLiu Zhe import com.sun.star.container.XNameContainer; 30e6e6073dSLiu Zhe import com.sun.star.container.XNamed; 31e6e6073dSLiu Zhe import com.sun.star.document.XDocumentInfo; 32e6e6073dSLiu Zhe import com.sun.star.document.XDocumentInfoSupplier; 33e6e6073dSLiu Zhe import com.sun.star.frame.XStorable; 34e6e6073dSLiu Zhe import com.sun.star.io.IOException; 35*deb45f49SLiu Zhe import com.sun.star.lang.XComponent; 36e6e6073dSLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 37e6e6073dSLiu Zhe import com.sun.star.style.BreakType; 38*deb45f49SLiu Zhe import com.sun.star.style.XStyle; 39*deb45f49SLiu Zhe import com.sun.star.style.XStyleFamiliesSupplier; 40e6e6073dSLiu Zhe import com.sun.star.text.ControlCharacter; 41e6e6073dSLiu Zhe import com.sun.star.text.XText; 42e6e6073dSLiu Zhe import com.sun.star.text.XTextContent; 43e6e6073dSLiu Zhe import com.sun.star.text.XTextCursor; 44e6e6073dSLiu Zhe import com.sun.star.text.XTextDocument; 45e6e6073dSLiu Zhe import com.sun.star.frame.XModel; 46e6e6073dSLiu Zhe import com.sun.star.frame.XController; 47e6e6073dSLiu Zhe import com.sun.star.uno.UnoRuntime; 48e6e6073dSLiu Zhe 49e6e6073dSLiu Zhe public class SWUtil { 50e6e6073dSLiu Zhe 51e6e6073dSLiu Zhe 52e6e6073dSLiu Zhe 53e6e6073dSLiu Zhe 54e6e6073dSLiu Zhe public static void saveAsDoc(XTextDocument document, String url) throws IOException { 55e6e6073dSLiu Zhe saveAs(document, "MS Word 97", url); 56e6e6073dSLiu Zhe 57e6e6073dSLiu Zhe } 58e6e6073dSLiu Zhe 59*deb45f49SLiu Zhe public static void saveAsDoc(XComponent component, String url) throws IOException{ 60*deb45f49SLiu Zhe XTextDocument document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 61*deb45f49SLiu Zhe saveAs(document, "MS Word 97", url); 62*deb45f49SLiu Zhe } 63e6e6073dSLiu Zhe 64e6e6073dSLiu Zhe public static void saveAsODT(XTextDocument document, String url) throws IOException { 65e6e6073dSLiu Zhe saveAs(document, "writer8", url); 66e6e6073dSLiu Zhe } 67e6e6073dSLiu Zhe 68e6e6073dSLiu Zhe public static void saveAs(XTextDocument document, String filterValue, String url) throws IOException { 69cebb507aSLiu Zhe XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 70e6e6073dSLiu Zhe PropertyValue[] propsValue = new PropertyValue[1]; 71e6e6073dSLiu Zhe propsValue[0] = new PropertyValue(); 72e6e6073dSLiu Zhe propsValue[0].Name = "FilterName"; 73e6e6073dSLiu Zhe propsValue[0].Value = filterValue; 74e6e6073dSLiu Zhe store.storeAsURL(url, propsValue); 75e6e6073dSLiu Zhe 76e6e6073dSLiu Zhe } 77e6e6073dSLiu Zhe 78e6e6073dSLiu Zhe public static void save(XTextDocument document) throws IOException { 79cebb507aSLiu Zhe XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 80e6e6073dSLiu Zhe store.store(); 81e6e6073dSLiu Zhe } 82e6e6073dSLiu Zhe 83e6e6073dSLiu Zhe public static XTextDocument saveAndReload(XTextDocument document, UnoApp app) throws Exception { 84cebb507aSLiu Zhe XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 85e6e6073dSLiu Zhe store.store(); 86e6e6073dSLiu Zhe String url = document.getURL(); 87e6e6073dSLiu Zhe app.closeDocument(document); 88e6e6073dSLiu Zhe return openDocumentFromURL(url, app); 89e6e6073dSLiu Zhe 90e6e6073dSLiu Zhe } 91e6e6073dSLiu Zhe 92e6e6073dSLiu Zhe public static XTextDocument newDocument(UnoApp app) throws Exception { 93e6e6073dSLiu Zhe return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter")); 94e6e6073dSLiu Zhe 95e6e6073dSLiu Zhe } 96e6e6073dSLiu Zhe 97e6e6073dSLiu Zhe public static XTextDocument openDocumentFromURL(String url, UnoApp app) throws Exception { 98e6e6073dSLiu Zhe return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url)); 99e6e6073dSLiu Zhe 100e6e6073dSLiu Zhe } 101e6e6073dSLiu Zhe public static XTextDocument openDocument(String filePath, UnoApp app) throws Exception { 102e6e6073dSLiu Zhe 103e6e6073dSLiu Zhe return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(filePath)); 104e6e6073dSLiu Zhe 105e6e6073dSLiu Zhe } 106e6e6073dSLiu Zhe 107e6e6073dSLiu Zhe public static void moveCuror2End(XTextDocument document) { 108e6e6073dSLiu Zhe XText xText = document.getText(); 109e6e6073dSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 110e6e6073dSLiu Zhe xTextCursor.gotoEnd(false); 111e6e6073dSLiu Zhe } 112e6e6073dSLiu Zhe 113e6e6073dSLiu Zhe public static void moveCuror2Start(XTextDocument document) { 114e6e6073dSLiu Zhe XText xText = document.getText(); 115e6e6073dSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 116e6e6073dSLiu Zhe xTextCursor.gotoStart(false); 117e6e6073dSLiu Zhe } 118e6e6073dSLiu Zhe 119e6e6073dSLiu Zhe /** 120e6e6073dSLiu Zhe * Set document properties. such as subject, title etc 121e6e6073dSLiu Zhe * @param document - set document information on this document 122e6e6073dSLiu Zhe * @param prop - document information, including "Subject" ,"Title", "Author", "Title", "KeyWords" 123e6e6073dSLiu Zhe * @param propValue - value you want to set for prop 124e6e6073dSLiu Zhe * @throws Exception 125e6e6073dSLiu Zhe */ 126e6e6073dSLiu Zhe public static void setDocumentProperty(XTextDocument document, String prop, String propValue) throws Exception { 127cebb507aSLiu Zhe XDocumentInfoSupplier docInfoSupplier = (XDocumentInfoSupplier) UnoRuntime.queryInterface(XDocumentInfoSupplier.class, document); 128e6e6073dSLiu Zhe XDocumentInfo docInfo = docInfoSupplier.getDocumentInfo(); 129e6e6073dSLiu Zhe XPropertySet propsDocInfo = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, docInfo); 130e6e6073dSLiu Zhe propsDocInfo.setPropertyValue(prop, propValue); 131e6e6073dSLiu Zhe } 132e6e6073dSLiu Zhe 133e6e6073dSLiu Zhe 134e6e6073dSLiu Zhe /** 135e6e6073dSLiu Zhe * Insert a bookmark into text document 136e6e6073dSLiu Zhe * @param document text document 137e6e6073dSLiu Zhe * @param textCursor which part will be bookmarked 138e6e6073dSLiu Zhe * @param bookmarkName bookmark name 139e6e6073dSLiu Zhe * @throws Exception 140e6e6073dSLiu Zhe */ 141e6e6073dSLiu Zhe public static void insertBookmark(XTextDocument document, XTextCursor textCursor, String bookmarkName) throws Exception { 142cebb507aSLiu Zhe XMultiServiceFactory xDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document); 143e6e6073dSLiu Zhe Object xBookmark = xDocFactory.createInstance("com.sun.star.text.Bookmark"); 144cebb507aSLiu Zhe XTextContent xBookmarkAsTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xBookmark); 145cebb507aSLiu Zhe XNamed xBookmarkAsNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, xBookmark); 146e6e6073dSLiu Zhe xBookmarkAsNamed.setName(bookmarkName); 147e6e6073dSLiu Zhe document.getText().insertTextContent(textCursor, xBookmarkAsTextContent, true); 148e6e6073dSLiu Zhe } 149e6e6073dSLiu Zhe 150e6e6073dSLiu Zhe /** 151e6e6073dSLiu Zhe * insert column break in current cursor 152e6e6073dSLiu Zhe * @param xText 153e6e6073dSLiu Zhe * @param currentCursor 154e6e6073dSLiu Zhe * @throws Exception 155e6e6073dSLiu Zhe */ 156e6e6073dSLiu Zhe public static void insertColumnBreak(XText xText, XTextCursor currentCursor) throws Exception 157e6e6073dSLiu Zhe { 158e6e6073dSLiu Zhe XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface( 159e6e6073dSLiu Zhe XPropertySet.class, currentCursor); 160e6e6073dSLiu Zhe xCursorProps.setPropertyValue("BreakType", BreakType.COLUMN_AFTER); 161e6e6073dSLiu Zhe xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false); 162e6e6073dSLiu Zhe } 163e6e6073dSLiu Zhe 164e6e6073dSLiu Zhe /** 165e6e6073dSLiu Zhe * insert page break in current cursor 166e6e6073dSLiu Zhe * @param xText 167e6e6073dSLiu Zhe * @param currentCursor 168e6e6073dSLiu Zhe * @throws Exception 169e6e6073dSLiu Zhe */ 170e6e6073dSLiu Zhe public static void insertPageBreak(XText xText, XTextCursor currentCursor) throws Exception 171e6e6073dSLiu Zhe { 172e6e6073dSLiu Zhe XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface( 173e6e6073dSLiu Zhe XPropertySet.class, currentCursor); 174e6e6073dSLiu Zhe xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER); 175e6e6073dSLiu Zhe xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false); 176e6e6073dSLiu Zhe } 177e6e6073dSLiu Zhe 178e6e6073dSLiu Zhe 179e6e6073dSLiu Zhe /** 180e6e6073dSLiu Zhe * get page count 181e6e6073dSLiu Zhe * @param document 182e6e6073dSLiu Zhe * @return 183e6e6073dSLiu Zhe * @throws Exception 184e6e6073dSLiu Zhe */ 185e6e6073dSLiu Zhe public static int getPageCount(XTextDocument document) throws Exception 186e6e6073dSLiu Zhe { 187e6e6073dSLiu Zhe XModel xmodel = (XModel)UnoRuntime.queryInterface(XModel.class, document); 188e6e6073dSLiu Zhe XController xcont = xmodel.getCurrentController(); 189e6e6073dSLiu Zhe 190e6e6073dSLiu Zhe XPropertySet xps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xcont); 191e6e6073dSLiu Zhe Integer pageCount = (Integer) xps.getPropertyValue("PageCount"); 192e6e6073dSLiu Zhe return pageCount.intValue(); 193e6e6073dSLiu Zhe } 194e6e6073dSLiu Zhe 195*deb45f49SLiu Zhe 196*deb45f49SLiu Zhe /** 197*deb45f49SLiu Zhe * get specific property value of the default page style 198*deb45f49SLiu Zhe * @param xComponent 199*deb45f49SLiu Zhe * @param propertyName 200*deb45f49SLiu Zhe * @return 201*deb45f49SLiu Zhe * @throws Exception 202*deb45f49SLiu Zhe */ 203*deb45f49SLiu Zhe public static Object getDefaultPageStyleProperty(XComponent xComponent, String propertyName) throws Exception 204*deb45f49SLiu Zhe { 205*deb45f49SLiu Zhe XTextDocument textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent); 206*deb45f49SLiu Zhe XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, textDocument); 207*deb45f49SLiu Zhe XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface (XNameAccess.class, xSupplier.getStyleFamilies()); 208*deb45f49SLiu Zhe XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xFamilies.getByName("PageStyles")); 209*deb45f49SLiu Zhe XStyle xStyle = (XStyle)UnoRuntime.queryInterface(XStyle.class, xFamily.getByName("Default")); 210*deb45f49SLiu Zhe XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStyle); 211*deb45f49SLiu Zhe Object propertyValue = xStyleProps.getPropertyValue(propertyName.toString()); 212*deb45f49SLiu Zhe return propertyValue; 213*deb45f49SLiu Zhe } 214*deb45f49SLiu Zhe 215*deb45f49SLiu Zhe /** 216*deb45f49SLiu Zhe * set value for specific property of default page style. 217*deb45f49SLiu Zhe * @param xComponent 218*deb45f49SLiu Zhe * @param propertyName 219*deb45f49SLiu Zhe * @param propertyValue 220*deb45f49SLiu Zhe * @throws Exception 221*deb45f49SLiu Zhe */ 222*deb45f49SLiu Zhe public static void setDefaultPageStyleProperty(XComponent xComponent, String propertyName, Object propertyValue) throws Exception 223*deb45f49SLiu Zhe { 224*deb45f49SLiu Zhe XTextDocument textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent); 225*deb45f49SLiu Zhe XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, textDocument); 226*deb45f49SLiu Zhe XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface (XNameAccess.class, xSupplier.getStyleFamilies()); 227*deb45f49SLiu Zhe XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xFamilies.getByName("PageStyles")); 228*deb45f49SLiu Zhe XStyle xStyle = (XStyle)UnoRuntime.queryInterface(XStyle.class, xFamily.getByName("Default")); 229*deb45f49SLiu Zhe XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStyle); 230*deb45f49SLiu Zhe xStyleProps.setPropertyValue (propertyName.toString(), propertyValue); 231*deb45f49SLiu Zhe } 232e6e6073dSLiu Zhe } 233