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 
22 
23 package complex.sfx2;
24 
25 import com.sun.star.beans.PropertyAttribute;
26 import com.sun.star.beans.Property;
27 import com.sun.star.beans.PropertyValue;
28 import com.sun.star.beans.XPropertyContainer;
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.beans.XPropertySetInfo;
31 import com.sun.star.document.XDocumentInfo;
32 import com.sun.star.document.XDocumentInfoSupplier;
33 import com.sun.star.frame.XComponentLoader;
34 import com.sun.star.frame.XStorable;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.text.XTextDocument;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.util.Date;
40 
41 
42 
43 import util.DesktopTools;
44 import util.WriterTools;
45 
46 import org.junit.After;
47 import org.junit.AfterClass;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.openoffice.test.OfficeConnection;
51 import static org.junit.Assert.*;
52 
53 public class DocumentInfo
54 {
55     XMultiServiceFactory m_xMSF = null;
56     XTextDocument xTextDoc = null;
57     XTextDocument xTextDocSecond = null;
58 
checkDocInfo()59     @Test public void checkDocInfo()
60     {
61         m_xMSF = getMSF();
62 
63         System.out.println("check wether there is a valid MultiServiceFactory");
64 
65         assertNotNull("## Couldn't get MultiServiceFactory make sure your Office is started", m_xMSF);
66 
67         // TODO: need other temp directory!
68         String tempdir = System.getProperty("java.io.tmpdir");
69         String fs = System.getProperty("file.separator");
70 
71         if (!tempdir.endsWith(fs))
72         {
73             tempdir += fs;
74         }
75         tempdir = util.utils.getFullURL(tempdir);
76         final String sTempDocument = tempdir + "DocInfo.oot";
77 
78         if (true)
79         {
80             System.out.println("... done");
81 
82 
83             System.out.println("Opening a Writer document");
84             xTextDoc = WriterTools.createTextDoc(m_xMSF);
85             System.out.println("... done");
86 
87             XDocumentInfoSupplier xDocInfoSup = UnoRuntime.queryInterface(XDocumentInfoSupplier.class, xTextDoc);
88             XDocumentInfo xDocInfo = xDocInfoSup.getDocumentInfo();
89             XPropertyContainer xPropContainer = UnoRuntime.queryInterface(XPropertyContainer.class, xDocInfo);
90 
91             System.out.println("Trying to add a existing property");
92 
93             boolean worked = addProperty(xPropContainer, "Author", (short) 0, "");
94             assertTrue("Could set an existing property", !worked);
95             System.out.println("...done");
96 
97             System.out.println("Trying to add a integer property");
98             worked = addProperty(xPropContainer, "intValue", com.sun.star.beans.PropertyAttribute.READONLY, new Integer(17));
99             assertTrue("Couldn't set an integer property", worked);
100             System.out.println("...done");
101 
102             System.out.println("Trying to add a double property");
103             worked = addProperty(xPropContainer, "doubleValue", com.sun.star.beans.PropertyAttribute.REMOVEABLE, new Double(17.7));
104             assertTrue("Couldn't set an double property", worked);
105             System.out.println("...done");
106 
107             System.out.println("Trying to add a boolean property");
108             worked = addProperty(xPropContainer, "booleanValue", com.sun.star.beans.PropertyAttribute.REMOVEABLE, Boolean.TRUE);
109             assertTrue("Couldn't set an boolean property", worked);
110             System.out.println("...done");
111 
112             System.out.println("Trying to add a date property");
113             worked = addProperty(xPropContainer, "dateValue", com.sun.star.beans.PropertyAttribute.REMOVEABLE, new Date());
114             assertTrue("Couldn't set an date property", worked);
115             System.out.println("...done");
116 
117             System.out.println("trying to remove a read only Property");
118             try
119             {
120                 xPropContainer.removeProperty("intValue");
121                 fail("Could remove read only property");
122             }
123             catch (Exception e)
124             {
125                 System.out.println("\tException was thrown " + e);
126                 System.out.println("\t...OK");
127             }
128             System.out.println("...done");
129 
130             XPropertySet xProps2 = UnoRuntime.queryInterface(XPropertySet.class, xPropContainer);
131             showPropertySet(xProps2);
132 
133 
134             System.out.println("Storing the document");
135             try
136             {
137                 XStorable store = UnoRuntime.queryInterface(XStorable.class, xTextDoc);
138                 store.storeToURL(sTempDocument, new PropertyValue[] {});
139                 DesktopTools.closeDoc(xTextDoc);
140             }
141             catch (Exception e)
142             {
143                 fail("Couldn't store document");
144             }
145 
146             System.out.println("...done");
147         }
148 
149 
150         if (true)
151         {
152             System.out.println("loading the document");
153 
154             try
155             {
156                 XComponentLoader xCL = UnoRuntime.queryInterface(XComponentLoader.class, m_xMSF.createInstance("com.sun.star.frame.Desktop"));
157                 XComponent xComp = xCL.loadComponentFromURL(sTempDocument, "_blank", 0, new PropertyValue[] {});
158                 xTextDocSecond = UnoRuntime.queryInterface(XTextDocument.class, xComp);
159             }
160             catch (Exception e)
161             {
162                 fail("Couldn't load document");
163             }
164 
165             System.out.println("...done");
166 
167             XDocumentInfoSupplier xDocInfoSup = UnoRuntime.queryInterface(XDocumentInfoSupplier.class, xTextDocSecond);
168             XDocumentInfo xDocInfo = xDocInfoSup.getDocumentInfo();
169             XPropertyContainer xPropContainer = UnoRuntime.queryInterface(XPropertyContainer.class, xDocInfo);
170 
171             XPropertySet xProps = UnoRuntime.queryInterface(XPropertySet.class, xDocInfo);
172             showPropertySet(xProps);
173 
174             assertTrue("Double doesn't work", checkType(xProps, "doubleValue", "java.lang.Double"));
175             assertTrue("Boolean doesn't work", checkType(xProps, "booleanValue", "java.lang.Boolean"));
176 
177             // TODO: dateValue does not exist.
178             // assertTrue("Date doesn't work",     checkType(xProps, "dateValue", "com.sun.star.util.DateTime"));
179 
180             // TODO: is java.lang.Double
181             // assertTrue("Integer doesn't work",  checkType(xProps, "intValue", "java.lang.Integer"));
182 
183             xPropContainer = UnoRuntime.queryInterface(XPropertyContainer.class, xDocInfo);
184 
185             System.out.println("trying to remove a not user defined Property");
186             try
187             {
188                 xPropContainer.removeProperty("Author");
189                 fail("Could remove non user defined property");
190             }
191             catch (Exception e)
192             {
193                 System.out.println("\tException was thrown " + e);
194                 System.out.println("\t...OK");
195             }
196             System.out.println("...done");
197 
198 
199             System.out.println("Trying to remove a user defined property");
200             try
201             {
202                 xPropContainer.removeProperty("booleanValue");
203                 System.out.println("\t...OK");
204             }
205             catch (Exception e)
206             {
207                 System.out.println("\tException was thrown " + e);
208                 System.out.println("\t...FAILED");
209                 fail("Could not remove user defined property");
210             }
211             showPropertySet(xProps);
212             System.out.println("...done");
213         }
214     }
215 
cleanup()216     @After public void cleanup()
217     {
218         DesktopTools.closeDoc(xTextDocSecond);
219         DesktopTools.closeDoc(xTextDoc);
220     }
221 
showPropertySet(XPropertySet xProps)222     private void showPropertySet(XPropertySet xProps)
223         {
224             try
225             {
226                 // get an XPropertySet, here the one of a text cursor
227                 // XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, mxDocCursor);
228 
229                 // get the property info interface of this XPropertySet
230                 XPropertySetInfo xPropsInfo = xProps.getPropertySetInfo();
231 
232                 // get all properties (NOT the values) from XPropertySetInfo
233                 Property[] aProps = xPropsInfo.getProperties();
234                 int i;
235                 for (i = 0; i < aProps.length; ++i) {
236                     // number of property within this info object
237                     System.out.print("Property #" + i);
238 
239                     // name of property
240                     System.out.print(": Name<" + aProps[i].Name);
241 
242                     // handle of property (only for XFastPropertySet)
243                     System.out.print("> Handle<" + aProps[i].Handle);
244 
245                     // type of property
246                     System.out.print("> " + aProps[i].Type.toString());
247 
248                     // attributes (flags)
249                     System.out.print(" Attributes<");
250                     short nAttribs = aProps[i].Attributes;
251                     if ((nAttribs & PropertyAttribute.MAYBEVOID) != 0)
252                         System.out.print("MAYBEVOID|");
253                     if ((nAttribs & PropertyAttribute.BOUND) != 0)
254                         System.out.print("BOUND|");
255                     if ((nAttribs & PropertyAttribute.CONSTRAINED) != 0)
256                         System.out.print("CONSTRAINED|");
257                     if ((nAttribs & PropertyAttribute.READONLY) != 0)
258                         System.out.print("READONLY|");
259                     if ((nAttribs & PropertyAttribute.TRANSIENT) != 0)
260                         System.out.print("TRANSIENT|");
261                     if ((nAttribs & PropertyAttribute.MAYBEAMBIGUOUS ) != 0)
262                         System.out.print("MAYBEAMBIGUOUS|");
263                     if ((nAttribs & PropertyAttribute.MAYBEDEFAULT) != 0)
264                         System.out.print("MAYBEDEFAULT|");
265                     if ((nAttribs & PropertyAttribute.REMOVEABLE) != 0)
266                         System.out.print("REMOVEABLE|");
267                     System.out.println("0>");
268                 }
269             } catch (Exception e) {
270                 // If anything goes wrong, give the user a stack trace
271                 e.printStackTrace(System.out);
272             }
273         }
274 
checkType(XPropertySet xProps, String aName, String expected)275     private boolean checkType(XPropertySet xProps, String aName,
276             String expected)
277     {
278         boolean ret = true;
279         System.out.println("Checking " + expected);
280 
281         String getting =
282                 getPropertyByName(xProps, aName).getClass().getName();
283 
284         if (!getting.equals(expected))
285         {
286             System.out.println("\t Expected: " + expected);
287             System.out.println("\t Getting:  " + getting);
288             ret = false;
289         }
290 
291         if (ret)
292         {
293             System.out.println("...OK");
294         }
295         return ret;
296     }
297 
getPropertyByName(XPropertySet xProps, String aName)298     private Object getPropertyByName(XPropertySet xProps, String aName)
299     {
300         Object ret = null;
301 
302         try
303         {
304             ret = xProps.getPropertyValue(aName);
305         }
306         catch (Exception e)
307         {
308             System.out.println("\tCouldn't get Property " + aName);
309             System.out.println("\tMessage " + e);
310         }
311 
312         return ret;
313     }
314 
addProperty(XPropertyContainer xPropContainer, String aName, short attr, Object defaults)315     private boolean addProperty(XPropertyContainer xPropContainer,
316             String aName, short attr, Object defaults)
317     {
318         boolean ret = true;
319 
320         try
321         {
322             xPropContainer.addProperty(aName, attr, defaults);
323         }
324         catch (Exception e)
325         {
326             ret = false;
327             System.out.println("\tCouldn't get Property " + aName);
328             System.out.println("\tMessage " + e);
329         }
330 
331         return ret;
332     }
333 
getMSF()334     private XMultiServiceFactory getMSF()
335     {
336         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
337         return xMSF1;
338     }
339 
340     // setup and close connections
setUpConnection()341     @BeforeClass public static void setUpConnection() throws Exception
342     {
343         System.out.println( "------------------------------------------------------------" );
344         System.out.println( "starting class: " + DocumentInfo.class.getName() );
345         System.out.println( "------------------------------------------------------------" );
346         connection.setUp();
347     }
348 
tearDownConnection()349     @AfterClass public static void tearDownConnection()
350             throws InterruptedException, com.sun.star.uno.Exception
351     {
352         System.out.println( "------------------------------------------------------------" );
353         System.out.println( "finishing class: " + DocumentInfo.class.getName() );
354         System.out.println( "------------------------------------------------------------" );
355         connection.tearDown();
356     }
357     private static final OfficeConnection connection = new OfficeConnection();
358 }
359