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