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.XDocumentProperties;
33 import com.sun.star.document.XDocumentPropertiesSupplier;
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 
saveAsDoc(XTextDocument document, String url)56 	public static void saveAsDoc(XTextDocument document, String url) throws IOException {
57  		saveAs(document, "MS Word 97", url);
58 
59  	}
60 
saveAsDoc(XComponent component, String url)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 
saveAsODT(XTextDocument document, String url)66 	public static void saveAsODT(XTextDocument document, String url) throws IOException {
67  		saveAs(document, "writer8", url);
68  	}
69 
saveAs(XTextDocument document, String filterValue, String url)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 
save(XTextDocument document)80 	public static void save(XTextDocument document) throws IOException {
81  		XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document);
82 		store.store();
83 	}
84 
saveAndReload(XTextDocument document, UnoApp app)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 
newDocument(UnoApp app)94 	public static XTextDocument newDocument(UnoApp app) throws Exception {
95 		return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
96 
97  	}
98 
openDocumentFromURL(String url, UnoApp app)99 	public static XTextDocument openDocumentFromURL(String url, UnoApp app) throws Exception {
100 		return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url));
101 
102 	}
openDocument(String filePath, UnoApp app)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 
moveCuror2End(XTextDocument document)109 	public static void moveCuror2End(XTextDocument document) {
110 		XText xText = document.getText();
111 		XTextCursor xTextCursor = xText.createTextCursor();
112 		xTextCursor.gotoEnd(false);
113 	}
114 
moveCuror2Start(XTextDocument document)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. Only supported: subject, title, author
123 	 * @param document - set document information on this document
124 	 * @param prop - document information, including "Subject" ,"Title", "Author"
125 	 * @param propValue - value you want to set for prop
126 	 * @throws Exception
127 	 */
setDocumentProperty(XTextDocument document, String prop, String propValue)128 	public static void setDocumentProperty(XTextDocument document, String prop, String propValue) throws Exception {
129        XDocumentPropertiesSupplier docPropsSupplier = UnoRuntime.queryInterface(
130             XDocumentPropertiesSupplier.class, document);
131        XDocumentProperties docProps = docPropsSupplier.getDocumentProperties();
132         if ( prop.equals("Title"))
133             docProps.setTitle(propValue);
134         else if ( prop.equals("Author"))
135             docProps.setAuthor(propValue);
136         else if ( prop.equals("Subject"))
137             docProps.setSubject(propValue);
138     }
139 
140 
141 	/**
142 	 * Insert a bookmark into text document
143 	 * @param document text document
144 	 * @param textCursor which part will be bookmarked
145 	 * @param bookmarkName bookmark name
146 	 * @throws Exception
147 	 */
insertBookmark(XTextDocument document, XTextCursor textCursor, String bookmarkName)148 	public static void insertBookmark(XTextDocument document, XTextCursor textCursor, String bookmarkName) throws Exception {
149 		XMultiServiceFactory xDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
150 		Object xBookmark = xDocFactory.createInstance("com.sun.star.text.Bookmark");
151 		XTextContent xBookmarkAsTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xBookmark);
152 		XNamed xBookmarkAsNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, xBookmark);
153 		xBookmarkAsNamed.setName(bookmarkName);
154 		document.getText().insertTextContent(textCursor, xBookmarkAsTextContent, true);
155 	}
156 
157 	/**
158 	 * insert column break in current cursor
159 	 * @param xText
160 	 * @param currentCursor
161 	 * @throws Exception
162 	 */
insertColumnBreak(XText xText, XTextCursor currentCursor)163 	public static void insertColumnBreak(XText xText, XTextCursor currentCursor) throws Exception
164 	{
165 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
166 		        XPropertySet.class, currentCursor);
167 		xCursorProps.setPropertyValue("BreakType", BreakType.COLUMN_AFTER);
168 	    xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false);
169 	}
170 
171 	/**
172 	 * insert page break in current cursor
173 	 * @param xText
174 	 * @param currentCursor
175 	 * @throws Exception
176 	 */
insertPageBreak(XText xText, XTextCursor currentCursor)177 	public static void insertPageBreak(XText xText, XTextCursor currentCursor) throws Exception
178 	{
179 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
180 		        XPropertySet.class, currentCursor);
181 		xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER);
182 	    xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false);
183 	}
184 
185 
186 	/**
187 	 * get page count
188 	 * @param document
189 	 * @return
190 	 * @throws Exception
191 	 */
getPageCount(XTextDocument document)192 	public static int getPageCount(XTextDocument document) throws Exception
193 	{
194 		XModel xmodel = (XModel)UnoRuntime.queryInterface(XModel.class, document);
195 		XController xcont = xmodel.getCurrentController();
196 
197 		XPropertySet xps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xcont);
198 		Integer pageCount = (Integer) xps.getPropertyValue("PageCount");
199 		return pageCount.intValue();
200 	}
201 
202 
203 	/**
204 	 * get specific property value of the default page style
205 	 * @param xComponent
206 	 * @param propertyName
207 	 * @return
208 	 * @throws Exception
209 	 */
getDefaultPageStyleProperty(XComponent xComponent, String propertyName)210 	public static Object getDefaultPageStyleProperty(XComponent xComponent, String propertyName) throws Exception
211 	{
212 		XTextDocument textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);
213 		XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, textDocument);
214         XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface (XNameAccess.class, xSupplier.getStyleFamilies());
215         XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xFamilies.getByName("PageStyles"));
216         XStyle xStyle = (XStyle)UnoRuntime.queryInterface(XStyle.class, xFamily.getByName("Default"));
217         XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStyle);
218         Object propertyValue = xStyleProps.getPropertyValue(propertyName.toString());
219         return propertyValue;
220 	}
221 
222 	/**
223 	 * set value for specific property of default page style.
224 	 * @param xComponent
225 	 * @param propertyName
226 	 * @param propertyValue
227 	 * @throws Exception
228 	 */
setDefaultPageStyleProperty(XComponent xComponent, String propertyName, Object propertyValue)229 	public static void setDefaultPageStyleProperty(XComponent xComponent, String propertyName, Object propertyValue) throws Exception
230 	{
231 		XTextDocument textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);
232         XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, textDocument);
233         XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface (XNameAccess.class, xSupplier.getStyleFamilies());
234         XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xFamilies.getByName("PageStyles"));
235         XStyle xStyle = (XStyle)UnoRuntime.queryInterface(XStyle.class, xFamily.getByName("Default"));
236         XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStyle);
237         xStyleProps.setPropertyValue (propertyName.toString(), propertyValue);
238 	}
239 
saveTo_Override_reload(XTextDocument xTextDocument,String filtervalue, String url,UnoApp app)240 	public static XTextDocument saveTo_Override_reload(XTextDocument xTextDocument,String filtervalue, String url,UnoApp app) throws Exception {
241 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
242 		PropertyValue[] aStoreProperties = new PropertyValue[2];
243 		aStoreProperties[0] = new PropertyValue();
244 		aStoreProperties[1] = new PropertyValue();
245 		aStoreProperties[0].Name = "Override";
246 		aStoreProperties[0].Value = true;
247 		aStoreProperties[1].Name = "FilterName";
248 		aStoreProperties[1].Value = filtervalue;
249 		xStorable_odt.storeToURL(FileUtil.getUrl(url), aStoreProperties);
250 		//reopen the document
251 		return (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(url));
252  	}
253 	/**
254 	 * create document from template
255 	 */
newDocumentFromTemplate(String templatePath,UnoApp unoApp)256 	public static XComponent newDocumentFromTemplate(String templatePath,UnoApp unoApp) throws Exception
257 	{
258 		XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());
259 		PropertyValue[] pros = new PropertyValue[1];
260 		pros[0] = new PropertyValue();
261 		pros[0].Name = "AsTemplate";
262 		pros[0].Value = new Boolean(true);
263 		XComponent component = componentLoader.loadComponentFromURL(FileUtil.getUrl(templatePath), "_blank", 0,pros);
264 		return component;
265 	}
266 }
267