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