1*e6e6073dSLiu Zhe /************************************************************** 2*e6e6073dSLiu Zhe * 3*e6e6073dSLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 4*e6e6073dSLiu Zhe * or more contributor license agreements. See the NOTICE file 5*e6e6073dSLiu Zhe * distributed with this work for additional information 6*e6e6073dSLiu Zhe * regarding copyright ownership. The ASF licenses this file 7*e6e6073dSLiu Zhe * to you under the Apache License, Version 2.0 (the 8*e6e6073dSLiu Zhe * "License"); you may not use this file except in compliance 9*e6e6073dSLiu Zhe * with the License. You may obtain a copy of the License at 10*e6e6073dSLiu Zhe * 11*e6e6073dSLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 12*e6e6073dSLiu Zhe * 13*e6e6073dSLiu Zhe * Unless required by applicable law or agreed to in writing, 14*e6e6073dSLiu Zhe * software distributed under the License is distributed on an 15*e6e6073dSLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*e6e6073dSLiu Zhe * KIND, either express or implied. See the License for the 17*e6e6073dSLiu Zhe * specific language governing permissions and limitations 18*e6e6073dSLiu Zhe * under the License. 19*e6e6073dSLiu Zhe * 20*e6e6073dSLiu Zhe *************************************************************/ 21*e6e6073dSLiu Zhe package testlib.uno; 22*e6e6073dSLiu Zhe 23*e6e6073dSLiu Zhe 24*e6e6073dSLiu Zhe import org.openoffice.test.uno.UnoApp; 25*e6e6073dSLiu Zhe 26*e6e6073dSLiu Zhe import com.sun.star.beans.PropertyValue; 27*e6e6073dSLiu Zhe import com.sun.star.beans.XPropertySet; 28*e6e6073dSLiu Zhe import com.sun.star.container.XEnumeration; 29*e6e6073dSLiu Zhe import com.sun.star.container.XEnumerationAccess; 30*e6e6073dSLiu Zhe import com.sun.star.container.XNamed; 31*e6e6073dSLiu Zhe import com.sun.star.document.XDocumentInfo; 32*e6e6073dSLiu Zhe import com.sun.star.document.XDocumentInfoSupplier; 33*e6e6073dSLiu Zhe import com.sun.star.frame.XStorable; 34*e6e6073dSLiu Zhe import com.sun.star.io.IOException; 35*e6e6073dSLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 36*e6e6073dSLiu Zhe import com.sun.star.style.BreakType; 37*e6e6073dSLiu Zhe import com.sun.star.text.ControlCharacter; 38*e6e6073dSLiu Zhe import com.sun.star.text.XText; 39*e6e6073dSLiu Zhe import com.sun.star.text.XTextContent; 40*e6e6073dSLiu Zhe import com.sun.star.text.XTextCursor; 41*e6e6073dSLiu Zhe import com.sun.star.text.XTextDocument; 42*e6e6073dSLiu Zhe import com.sun.star.frame.XModel; 43*e6e6073dSLiu Zhe import com.sun.star.frame.XController; 44*e6e6073dSLiu Zhe import com.sun.star.uno.UnoRuntime; 45*e6e6073dSLiu Zhe 46*e6e6073dSLiu Zhe public class SWUtil { 47*e6e6073dSLiu Zhe 48*e6e6073dSLiu Zhe 49*e6e6073dSLiu Zhe 50*e6e6073dSLiu Zhe 51*e6e6073dSLiu Zhe public static void saveAsDoc(XTextDocument document, String url) throws IOException { 52*e6e6073dSLiu Zhe saveAs(document, "MS Word 97", url); 53*e6e6073dSLiu Zhe 54*e6e6073dSLiu Zhe } 55*e6e6073dSLiu Zhe 56*e6e6073dSLiu Zhe 57*e6e6073dSLiu Zhe public static void saveAsODT(XTextDocument document, String url) throws IOException { 58*e6e6073dSLiu Zhe saveAs(document, "writer8", url); 59*e6e6073dSLiu Zhe } 60*e6e6073dSLiu Zhe 61*e6e6073dSLiu Zhe public static void saveAs(XTextDocument document, String filterValue, String url) throws IOException { 62*e6e6073dSLiu Zhe XStorable store = UnoRuntime.queryInterface(XStorable.class, document); 63*e6e6073dSLiu Zhe PropertyValue[] propsValue = new PropertyValue[1]; 64*e6e6073dSLiu Zhe propsValue[0] = new PropertyValue(); 65*e6e6073dSLiu Zhe propsValue[0].Name = "FilterName"; 66*e6e6073dSLiu Zhe propsValue[0].Value = filterValue; 67*e6e6073dSLiu Zhe store.storeAsURL(url, propsValue); 68*e6e6073dSLiu Zhe 69*e6e6073dSLiu Zhe } 70*e6e6073dSLiu Zhe 71*e6e6073dSLiu Zhe public static void save(XTextDocument document) throws IOException { 72*e6e6073dSLiu Zhe XStorable store = UnoRuntime.queryInterface(XStorable.class, document); 73*e6e6073dSLiu Zhe store.store(); 74*e6e6073dSLiu Zhe } 75*e6e6073dSLiu Zhe 76*e6e6073dSLiu Zhe public static XTextDocument saveAndReload(XTextDocument document, UnoApp app) throws Exception { 77*e6e6073dSLiu Zhe XStorable store = UnoRuntime.queryInterface(XStorable.class, document); 78*e6e6073dSLiu Zhe store.store(); 79*e6e6073dSLiu Zhe String url = document.getURL(); 80*e6e6073dSLiu Zhe app.closeDocument(document); 81*e6e6073dSLiu Zhe return openDocumentFromURL(url, app); 82*e6e6073dSLiu Zhe 83*e6e6073dSLiu Zhe } 84*e6e6073dSLiu Zhe 85*e6e6073dSLiu Zhe public static XTextDocument newDocument(UnoApp app) throws Exception { 86*e6e6073dSLiu Zhe return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter")); 87*e6e6073dSLiu Zhe 88*e6e6073dSLiu Zhe } 89*e6e6073dSLiu Zhe 90*e6e6073dSLiu Zhe public static XTextDocument openDocumentFromURL(String url, UnoApp app) throws Exception { 91*e6e6073dSLiu Zhe return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url)); 92*e6e6073dSLiu Zhe 93*e6e6073dSLiu Zhe } 94*e6e6073dSLiu Zhe public static XTextDocument openDocument(String filePath, UnoApp app) throws Exception { 95*e6e6073dSLiu Zhe 96*e6e6073dSLiu Zhe return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(filePath)); 97*e6e6073dSLiu Zhe 98*e6e6073dSLiu Zhe } 99*e6e6073dSLiu Zhe 100*e6e6073dSLiu Zhe public static void moveCuror2End(XTextDocument document) { 101*e6e6073dSLiu Zhe XText xText = document.getText(); 102*e6e6073dSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 103*e6e6073dSLiu Zhe xTextCursor.gotoEnd(false); 104*e6e6073dSLiu Zhe } 105*e6e6073dSLiu Zhe 106*e6e6073dSLiu Zhe public static void moveCuror2Start(XTextDocument document) { 107*e6e6073dSLiu Zhe XText xText = document.getText(); 108*e6e6073dSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 109*e6e6073dSLiu Zhe xTextCursor.gotoStart(false); 110*e6e6073dSLiu Zhe } 111*e6e6073dSLiu Zhe 112*e6e6073dSLiu Zhe /** 113*e6e6073dSLiu Zhe * Set document properties. such as subject, title etc 114*e6e6073dSLiu Zhe * @param document - set document information on this document 115*e6e6073dSLiu Zhe * @param prop - document information, including "Subject" ,"Title", "Author", "Title", "KeyWords" 116*e6e6073dSLiu Zhe * @param propValue - value you want to set for prop 117*e6e6073dSLiu Zhe * @throws Exception 118*e6e6073dSLiu Zhe */ 119*e6e6073dSLiu Zhe public static void setDocumentProperty(XTextDocument document, String prop, String propValue) throws Exception { 120*e6e6073dSLiu Zhe XDocumentInfoSupplier docInfoSupplier = UnoRuntime.queryInterface(XDocumentInfoSupplier.class, document); 121*e6e6073dSLiu Zhe XDocumentInfo docInfo = docInfoSupplier.getDocumentInfo(); 122*e6e6073dSLiu Zhe XPropertySet propsDocInfo = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, docInfo); 123*e6e6073dSLiu Zhe propsDocInfo.setPropertyValue(prop, propValue); 124*e6e6073dSLiu Zhe } 125*e6e6073dSLiu Zhe 126*e6e6073dSLiu Zhe 127*e6e6073dSLiu Zhe /** 128*e6e6073dSLiu Zhe * Insert a bookmark into text document 129*e6e6073dSLiu Zhe * @param document text document 130*e6e6073dSLiu Zhe * @param textCursor which part will be bookmarked 131*e6e6073dSLiu Zhe * @param bookmarkName bookmark name 132*e6e6073dSLiu Zhe * @throws Exception 133*e6e6073dSLiu Zhe */ 134*e6e6073dSLiu Zhe public static void insertBookmark(XTextDocument document, XTextCursor textCursor, String bookmarkName) throws Exception { 135*e6e6073dSLiu Zhe XMultiServiceFactory xDocFactory = UnoRuntime.queryInterface(XMultiServiceFactory.class, document); 136*e6e6073dSLiu Zhe Object xBookmark = xDocFactory.createInstance("com.sun.star.text.Bookmark"); 137*e6e6073dSLiu Zhe XTextContent xBookmarkAsTextContent = UnoRuntime.queryInterface(XTextContent.class, xBookmark); 138*e6e6073dSLiu Zhe XNamed xBookmarkAsNamed = UnoRuntime.queryInterface(XNamed.class, xBookmark); 139*e6e6073dSLiu Zhe xBookmarkAsNamed.setName(bookmarkName); 140*e6e6073dSLiu Zhe document.getText().insertTextContent(textCursor, xBookmarkAsTextContent, true); 141*e6e6073dSLiu Zhe } 142*e6e6073dSLiu Zhe 143*e6e6073dSLiu Zhe /** 144*e6e6073dSLiu Zhe * insert column break in current cursor 145*e6e6073dSLiu Zhe * @param xText 146*e6e6073dSLiu Zhe * @param currentCursor 147*e6e6073dSLiu Zhe * @throws Exception 148*e6e6073dSLiu Zhe */ 149*e6e6073dSLiu Zhe public static void insertColumnBreak(XText xText, XTextCursor currentCursor) throws Exception 150*e6e6073dSLiu Zhe { 151*e6e6073dSLiu Zhe XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface( 152*e6e6073dSLiu Zhe XPropertySet.class, currentCursor); 153*e6e6073dSLiu Zhe xCursorProps.setPropertyValue("BreakType", BreakType.COLUMN_AFTER); 154*e6e6073dSLiu Zhe xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false); 155*e6e6073dSLiu Zhe } 156*e6e6073dSLiu Zhe 157*e6e6073dSLiu Zhe /** 158*e6e6073dSLiu Zhe * insert page break in current cursor 159*e6e6073dSLiu Zhe * @param xText 160*e6e6073dSLiu Zhe * @param currentCursor 161*e6e6073dSLiu Zhe * @throws Exception 162*e6e6073dSLiu Zhe */ 163*e6e6073dSLiu Zhe public static void insertPageBreak(XText xText, XTextCursor currentCursor) throws Exception 164*e6e6073dSLiu Zhe { 165*e6e6073dSLiu Zhe XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface( 166*e6e6073dSLiu Zhe XPropertySet.class, currentCursor); 167*e6e6073dSLiu Zhe xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER); 168*e6e6073dSLiu Zhe xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false); 169*e6e6073dSLiu Zhe } 170*e6e6073dSLiu Zhe 171*e6e6073dSLiu Zhe 172*e6e6073dSLiu Zhe /** 173*e6e6073dSLiu Zhe * get page count 174*e6e6073dSLiu Zhe * @param document 175*e6e6073dSLiu Zhe * @return 176*e6e6073dSLiu Zhe * @throws Exception 177*e6e6073dSLiu Zhe */ 178*e6e6073dSLiu Zhe public static int getPageCount(XTextDocument document) throws Exception 179*e6e6073dSLiu Zhe { 180*e6e6073dSLiu Zhe XModel xmodel = (XModel)UnoRuntime.queryInterface(XModel.class, document); 181*e6e6073dSLiu Zhe XController xcont = xmodel.getCurrentController(); 182*e6e6073dSLiu Zhe 183*e6e6073dSLiu Zhe XPropertySet xps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xcont); 184*e6e6073dSLiu Zhe Integer pageCount = (Integer) xps.getPropertyValue("PageCount"); 185*e6e6073dSLiu Zhe return pageCount.intValue(); 186*e6e6073dSLiu Zhe } 187*e6e6073dSLiu Zhe 188*e6e6073dSLiu Zhe } 189