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