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