xref: /AOO41X/test/testuno/source/testlib/uno/SWUtil.java (revision ca62e2c2083b5d0995f1245bad6c2edfb455fbec)
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 
241ff9903bSLi 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;
32*50864d1aSAriel Constenla-Haile import com.sun.star.document.XDocumentProperties;
33*50864d1aSAriel Constenla-Haile import com.sun.star.document.XDocumentPropertiesSupplier;
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;
461ff9903bSLi 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 
saveAsDoc(XTextDocument document, String url)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 
saveAsDoc(XComponent component, String url)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 
saveAsODT(XTextDocument document, String url)66e6e6073dSLiu Zhe 	public static void saveAsODT(XTextDocument document, String url) throws IOException {
67e6e6073dSLiu Zhe  		saveAs(document, "writer8", url);
68e6e6073dSLiu Zhe  	}
69e6e6073dSLiu Zhe 
saveAs(XTextDocument document, String filterValue, String url)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 
save(XTextDocument document)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 
saveAndReload(XTextDocument document, UnoApp app)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 
newDocument(UnoApp app)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 
openDocumentFromURL(String url, UnoApp app)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 	}
openDocument(String filePath, UnoApp app)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 
moveCuror2End(XTextDocument document)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 
moveCuror2Start(XTextDocument document)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 	/**
122*50864d1aSAriel Constenla-Haile 	 * Set document properties. Only supported: subject, title, author
123e6e6073dSLiu Zhe 	 * @param document - set document information on this document
124*50864d1aSAriel Constenla-Haile 	 * @param prop - document information, including "Subject" ,"Title", "Author"
125e6e6073dSLiu Zhe 	 * @param propValue - value you want to set for prop
126e6e6073dSLiu Zhe 	 * @throws Exception
127e6e6073dSLiu Zhe 	 */
setDocumentProperty(XTextDocument document, String prop, String propValue)128e6e6073dSLiu Zhe 	public static void setDocumentProperty(XTextDocument document, String prop, String propValue) throws Exception {
129*50864d1aSAriel Constenla-Haile        XDocumentPropertiesSupplier docPropsSupplier = UnoRuntime.queryInterface(
130*50864d1aSAriel Constenla-Haile             XDocumentPropertiesSupplier.class, document);
131*50864d1aSAriel Constenla-Haile        XDocumentProperties docProps = docPropsSupplier.getDocumentProperties();
132*50864d1aSAriel Constenla-Haile         if ( prop.equals("Title"))
133*50864d1aSAriel Constenla-Haile             docProps.setTitle(propValue);
134*50864d1aSAriel Constenla-Haile         else if ( prop.equals("Author"))
135*50864d1aSAriel Constenla-Haile             docProps.setAuthor(propValue);
136*50864d1aSAriel Constenla-Haile         else if ( prop.equals("Subject"))
137*50864d1aSAriel Constenla-Haile             docProps.setSubject(propValue);
138e6e6073dSLiu Zhe     }
139e6e6073dSLiu Zhe 
140e6e6073dSLiu Zhe 
141e6e6073dSLiu Zhe 	/**
142e6e6073dSLiu Zhe 	 * Insert a bookmark into text document
143e6e6073dSLiu Zhe 	 * @param document text document
144e6e6073dSLiu Zhe 	 * @param textCursor which part will be bookmarked
145e6e6073dSLiu Zhe 	 * @param bookmarkName bookmark name
146e6e6073dSLiu Zhe 	 * @throws Exception
147e6e6073dSLiu Zhe 	 */
insertBookmark(XTextDocument document, XTextCursor textCursor, String bookmarkName)148e6e6073dSLiu Zhe 	public static void insertBookmark(XTextDocument document, XTextCursor textCursor, String bookmarkName) throws Exception {
149cebb507aSLiu Zhe 		XMultiServiceFactory xDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
150e6e6073dSLiu Zhe 		Object xBookmark = xDocFactory.createInstance("com.sun.star.text.Bookmark");
151cebb507aSLiu Zhe 		XTextContent xBookmarkAsTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xBookmark);
152cebb507aSLiu Zhe 		XNamed xBookmarkAsNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, xBookmark);
153e6e6073dSLiu Zhe 		xBookmarkAsNamed.setName(bookmarkName);
154e6e6073dSLiu Zhe 		document.getText().insertTextContent(textCursor, xBookmarkAsTextContent, true);
155e6e6073dSLiu Zhe 	}
156e6e6073dSLiu Zhe 
157e6e6073dSLiu Zhe 	/**
158e6e6073dSLiu Zhe 	 * insert column break in current cursor
159e6e6073dSLiu Zhe 	 * @param xText
160e6e6073dSLiu Zhe 	 * @param currentCursor
161e6e6073dSLiu Zhe 	 * @throws Exception
162e6e6073dSLiu Zhe 	 */
insertColumnBreak(XText xText, XTextCursor currentCursor)163e6e6073dSLiu Zhe 	public static void insertColumnBreak(XText xText, XTextCursor currentCursor) throws Exception
164e6e6073dSLiu Zhe 	{
165e6e6073dSLiu Zhe 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
166e6e6073dSLiu Zhe 		        XPropertySet.class, currentCursor);
167e6e6073dSLiu Zhe 		xCursorProps.setPropertyValue("BreakType", BreakType.COLUMN_AFTER);
168e6e6073dSLiu Zhe 	    xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false);
169e6e6073dSLiu Zhe 	}
170e6e6073dSLiu Zhe 
171e6e6073dSLiu Zhe 	/**
172e6e6073dSLiu Zhe 	 * insert page break in current cursor
173e6e6073dSLiu Zhe 	 * @param xText
174e6e6073dSLiu Zhe 	 * @param currentCursor
175e6e6073dSLiu Zhe 	 * @throws Exception
176e6e6073dSLiu Zhe 	 */
insertPageBreak(XText xText, XTextCursor currentCursor)177e6e6073dSLiu Zhe 	public static void insertPageBreak(XText xText, XTextCursor currentCursor) throws Exception
178e6e6073dSLiu Zhe 	{
179e6e6073dSLiu Zhe 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
180e6e6073dSLiu Zhe 		        XPropertySet.class, currentCursor);
181e6e6073dSLiu Zhe 		xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER);
182e6e6073dSLiu Zhe 	    xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false);
183e6e6073dSLiu Zhe 	}
184e6e6073dSLiu Zhe 
185e6e6073dSLiu Zhe 
186e6e6073dSLiu Zhe 	/**
187e6e6073dSLiu Zhe 	 * get page count
188e6e6073dSLiu Zhe 	 * @param document
189e6e6073dSLiu Zhe 	 * @return
190e6e6073dSLiu Zhe 	 * @throws Exception
191e6e6073dSLiu Zhe 	 */
getPageCount(XTextDocument document)192e6e6073dSLiu Zhe 	public static int getPageCount(XTextDocument document) throws Exception
193e6e6073dSLiu Zhe 	{
194e6e6073dSLiu Zhe 		XModel xmodel = (XModel)UnoRuntime.queryInterface(XModel.class, document);
195e6e6073dSLiu Zhe 		XController xcont = xmodel.getCurrentController();
196e6e6073dSLiu Zhe 
197e6e6073dSLiu Zhe 		XPropertySet xps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xcont);
198e6e6073dSLiu Zhe 		Integer pageCount = (Integer) xps.getPropertyValue("PageCount");
199e6e6073dSLiu Zhe 		return pageCount.intValue();
200e6e6073dSLiu Zhe 	}
201e6e6073dSLiu Zhe 
202deb45f49SLiu Zhe 
203deb45f49SLiu Zhe 	/**
204deb45f49SLiu Zhe 	 * get specific property value of the default page style
205deb45f49SLiu Zhe 	 * @param xComponent
206deb45f49SLiu Zhe 	 * @param propertyName
207deb45f49SLiu Zhe 	 * @return
208deb45f49SLiu Zhe 	 * @throws Exception
209deb45f49SLiu Zhe 	 */
getDefaultPageStyleProperty(XComponent xComponent, String propertyName)210deb45f49SLiu Zhe 	public static Object getDefaultPageStyleProperty(XComponent xComponent, String propertyName) throws Exception
211deb45f49SLiu Zhe 	{
212deb45f49SLiu Zhe 		XTextDocument textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);
213deb45f49SLiu Zhe 		XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, textDocument);
214deb45f49SLiu Zhe         XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface (XNameAccess.class, xSupplier.getStyleFamilies());
215deb45f49SLiu Zhe         XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xFamilies.getByName("PageStyles"));
216deb45f49SLiu Zhe         XStyle xStyle = (XStyle)UnoRuntime.queryInterface(XStyle.class, xFamily.getByName("Default"));
217deb45f49SLiu Zhe         XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStyle);
218deb45f49SLiu Zhe         Object propertyValue = xStyleProps.getPropertyValue(propertyName.toString());
219deb45f49SLiu Zhe         return propertyValue;
220deb45f49SLiu Zhe 	}
221deb45f49SLiu Zhe 
222deb45f49SLiu Zhe 	/**
223deb45f49SLiu Zhe 	 * set value for specific property of default page style.
224deb45f49SLiu Zhe 	 * @param xComponent
225deb45f49SLiu Zhe 	 * @param propertyName
226deb45f49SLiu Zhe 	 * @param propertyValue
227deb45f49SLiu Zhe 	 * @throws Exception
228deb45f49SLiu Zhe 	 */
setDefaultPageStyleProperty(XComponent xComponent, String propertyName, Object propertyValue)229deb45f49SLiu Zhe 	public static void setDefaultPageStyleProperty(XComponent xComponent, String propertyName, Object propertyValue) throws Exception
230deb45f49SLiu Zhe 	{
231deb45f49SLiu Zhe 		XTextDocument textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);
232deb45f49SLiu Zhe         XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, textDocument);
233deb45f49SLiu Zhe         XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface (XNameAccess.class, xSupplier.getStyleFamilies());
234deb45f49SLiu Zhe         XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xFamilies.getByName("PageStyles"));
235deb45f49SLiu Zhe         XStyle xStyle = (XStyle)UnoRuntime.queryInterface(XStyle.class, xFamily.getByName("Default"));
236deb45f49SLiu Zhe         XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStyle);
237deb45f49SLiu Zhe         xStyleProps.setPropertyValue (propertyName.toString(), propertyValue);
238deb45f49SLiu Zhe 	}
2391ff9903bSLi Feng Wang 
saveTo_Override_reload(XTextDocument xTextDocument,String filtervalue, String url,UnoApp app)2401ff9903bSLi Feng Wang 	public static XTextDocument saveTo_Override_reload(XTextDocument xTextDocument,String filtervalue, String url,UnoApp app) throws Exception {
2411ff9903bSLi Feng Wang 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
2421ff9903bSLi Feng Wang 		PropertyValue[] aStoreProperties = new PropertyValue[2];
2431ff9903bSLi Feng Wang 		aStoreProperties[0] = new PropertyValue();
2441ff9903bSLi Feng Wang 		aStoreProperties[1] = new PropertyValue();
2451ff9903bSLi Feng Wang 		aStoreProperties[0].Name = "Override";
2461ff9903bSLi Feng Wang 		aStoreProperties[0].Value = true;
2471ff9903bSLi Feng Wang 		aStoreProperties[1].Name = "FilterName";
2481ff9903bSLi Feng Wang 		aStoreProperties[1].Value = filtervalue;
2491ff9903bSLi Feng Wang 		xStorable_odt.storeToURL(FileUtil.getUrl(url), aStoreProperties);
2501ff9903bSLi Feng Wang 		//reopen the document
2511ff9903bSLi Feng Wang 		return (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(url));
2521ff9903bSLi Feng Wang  	}
2531ff9903bSLi Feng Wang 	/**
2541ff9903bSLi Feng Wang 	 * create document from template
2551ff9903bSLi Feng Wang 	 */
newDocumentFromTemplate(String templatePath,UnoApp unoApp)2561ff9903bSLi Feng Wang 	public static XComponent newDocumentFromTemplate(String templatePath,UnoApp unoApp) throws Exception
2571ff9903bSLi Feng Wang 	{
2581ff9903bSLi Feng Wang 		XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());
2591ff9903bSLi Feng Wang 		PropertyValue[] pros = new PropertyValue[1];
2601ff9903bSLi Feng Wang 		pros[0] = new PropertyValue();
2611ff9903bSLi Feng Wang 		pros[0].Name = "AsTemplate";
2621ff9903bSLi Feng Wang 		pros[0].Value = new Boolean(true);
2631ff9903bSLi Feng Wang 		XComponent component = componentLoader.loadComponentFromURL(FileUtil.getUrl(templatePath), "_blank", 0,pros);
2641ff9903bSLi Feng Wang 		return component;
2651ff9903bSLi Feng Wang 	}
266e6e6073dSLiu Zhe }
267