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 
24 package complex.writer;
25 
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.text.XTextDocument;
28 import com.sun.star.uno.UnoRuntime;
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.openoffice.test.OfficeConnection;
35 import static org.junit.Assert.*;
36 
37 /**
38  *
39  * @author od138299
40  */
41 public class CheckCrossReferences {
42 
43     private com.sun.star.container.XEnumeration xParaEnum;
44     private com.sun.star.container.XEnumeration xPortionEnum;
45     private com.sun.star.util.XRefreshable xFldsRefresh;
46 
getNextField()47     public com.sun.star.text.XTextField getNextField()
48         throws com.sun.star.uno.Exception
49     {
50         if ( xPortionEnum != null ) {
51             while ( xPortionEnum.hasMoreElements() ) {
52                 com.sun.star.beans.XPropertySet xPortionProps =
53                     (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
54                         com.sun.star.beans.XPropertySet.class , xPortionEnum.nextElement());
55                 final String sPortionType =
56                     xPortionProps.getPropertyValue( "TextPortionType" ).toString();
57                 if ( sPortionType.equals( "TextField") ) {
58                     com.sun.star.text.XTextField xField = (com.sun.star.text.XTextField)UnoRuntime.queryInterface(
59                         com.sun.star.text.XTextField.class,
60                         xPortionProps.getPropertyValue( "TextField" ) );
61                     assertNotNull("Cannot retrieve next field.", xField);
62                     return xField;
63                 }
64             }
65         }
66 
67         while ( xParaEnum.hasMoreElements() ) {
68             com.sun.star.container.XEnumerationAccess aPara =
69                 (com.sun.star.container.XEnumerationAccess)UnoRuntime.queryInterface(
70                     com.sun.star.container.XEnumerationAccess.class, xParaEnum.nextElement());
71             xPortionEnum = aPara.createEnumeration();
72             while ( xPortionEnum.hasMoreElements() ) {
73                 com.sun.star.beans.XPropertySet xPortionProps =
74                     (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
75                         com.sun.star.beans.XPropertySet.class , xPortionEnum.nextElement());
76                 final String sPortionType =
77                     xPortionProps.getPropertyValue( "TextPortionType" ).toString();
78                 if ( sPortionType.equals( "TextField") ) {
79                     com.sun.star.text.XTextField xField = (com.sun.star.text.XTextField)UnoRuntime.queryInterface(
80                         com.sun.star.text.XTextField.class,
81                         xPortionProps.getPropertyValue( "TextField" ) );
82                     assertNotNull("Cannot retrieve next field.", xField);
83                     return xField;
84                 }
85             }
86         }
87 
88         fail("Cannot retrieve next field.");
89         return null; // unreachable
90     }
91 
getFieldProps( com.sun.star.text.XTextField xField )92     public com.sun.star.beans.XPropertySet getFieldProps(
93             com.sun.star.text.XTextField xField ) {
94         com.sun.star.beans.XPropertySet xProps =
95                 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
96                 com.sun.star.beans.XPropertySet.class, xField );
97 
98         assertNotNull("Cannot retrieve field properties.", xProps);
99 
100         return xProps;
101     }
102 
checkField( com.sun.star.text.XTextField xField, com.sun.star.beans.XPropertySet xProps, short nFormat, String aExpectedFldResult )103     public void checkField( com.sun.star.text.XTextField xField,
104             com.sun.star.beans.XPropertySet xProps,
105             short nFormat,
106             String aExpectedFldResult )
107         throws com.sun.star.uno.Exception
108     {
109         // set requested format
110         xProps.setPropertyValue("ReferenceFieldPart", new Short(nFormat));
111 
112         // refresh fields in order to get new format applied
113         xFldsRefresh.refresh();
114 
115         String aFldResult = xField.getPresentation( false );
116         assertEquals( "set reference field format doesn't result in correct field result",
117                       aExpectedFldResult, aFldResult);
118     }
119 
checkCrossReferences()120     @Test public void checkCrossReferences() throws com.sun.star.uno.Exception {
121         // setup paragraph enumeration
122         {
123             com.sun.star.container.XEnumerationAccess xParaEnumAccess =
124                     (com.sun.star.container.XEnumerationAccess)UnoRuntime.queryInterface(
125                     com.sun.star.container.XEnumerationAccess.class, document.getText());
126             xParaEnum = xParaEnumAccess.createEnumeration();
127         }
128 
129         // get field refresher
130         {
131             com.sun.star.text.XTextFieldsSupplier xFieldSupp =
132                     (com.sun.star.text.XTextFieldsSupplier)UnoRuntime.queryInterface(
133                     com.sun.star.text.XTextFieldsSupplier.class, document);
134             xFldsRefresh = (com.sun.star.util.XRefreshable)UnoRuntime.queryInterface(
135                     com.sun.star.util.XRefreshable.class, xFieldSupp.getTextFields());
136         }
137 
138         // check first reference field
139         {
140             // strings for checking
141             final String FldResult1 = "*i*";
142             final String FldResult2 = "+b+*i*";
143             final String FldResult3 = "-1-+b+*i*";
144             final String FldResult4 = "1.";
145             final String FldResult5 = " 1.";
146             final String FldResult6 = "A. 1.";
147 
148             // variables for current field
149             com.sun.star.text.XTextField xField = null;
150             com.sun.star.beans.XPropertySet xProps = null;
151 
152             xField = getNextField();
153             xProps = getFieldProps( xField );
154             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult2 );
155             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1 );
156             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3 );
157 
158             xField = getNextField();
159             xProps = getFieldProps( xField );
160             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult1 );
161             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1 );
162             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3 );
163 
164             xField = getNextField();
165             xProps = getFieldProps( xField );
166             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult3 );
167             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1 );
168             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3 );
169 
170             xField = getNextField();
171             xProps = getFieldProps( xField );
172             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult5 );
173             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4 );
174             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6 );
175 
176             xField = getNextField();
177             xProps = getFieldProps( xField );
178             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult4 );
179             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4 );
180             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6 );
181 
182             xField = getNextField();
183             xProps = getFieldProps( xField );
184             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult6 );
185             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4 );
186             checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6 );
187         }
188 
189         // insert a certain cross-reference bookmark and a reference field to this bookmark
190         {
191             // restart paragraph enumeration
192             com.sun.star.container.XEnumerationAccess xParaEnumAccess =
193                     (com.sun.star.container.XEnumerationAccess)UnoRuntime.queryInterface(
194                     com.sun.star.container.XEnumerationAccess.class, document.getText());
195             xParaEnum = xParaEnumAccess.createEnumeration();
196 
197             // iterate on the paragraphs to find certain paragraph to insert the bookmark
198             com.sun.star.text.XTextRange xParaTextRange = null;
199             while ( xParaEnum.hasMoreElements() ) {
200                 xParaTextRange = (com.sun.star.text.XTextRange)UnoRuntime.queryInterface(
201                     com.sun.star.text.XTextRange.class, xParaEnum.nextElement());
202                 if ( xParaTextRange.getString().equals( "J" ) ) {
203                     break;
204                 }
205                 else {
206                     xParaTextRange = null;
207                 }
208             }
209             assertNotNull(
210                 "Cannot find paragraph to insert cross-reference bookmark.",
211                 xParaTextRange);
212 
213             // insert bookmark
214             XMultiServiceFactory xFac = (XMultiServiceFactory)UnoRuntime.queryInterface(
215                     XMultiServiceFactory.class, document);
216             final String cBookmarkName = "__RefNumPara__47114711";
217             com.sun.star.text.XTextContent xBookmark =
218                 (com.sun.star.text.XTextContent)UnoRuntime.queryInterface(
219                     com.sun.star.text.XTextContent.class,
220                     xFac.createInstance( "com.sun.star.text.Bookmark" ) );
221             if ( xBookmark != null ) {
222                 com.sun.star.container.XNamed xName =
223                         (com.sun.star.container.XNamed)UnoRuntime.queryInterface(
224                         com.sun.star.container.XNamed.class, xBookmark );
225                 xName.setName( cBookmarkName );
226                 xBookmark.attach(xParaTextRange.getStart());
227             }
228 
229             // insert reference field, which references the inserted bookmark
230             com.sun.star.text.XTextContent xNewField =
231                 (com.sun.star.text.XTextContent)UnoRuntime.queryInterface(
232                     com.sun.star.text.XTextContent.class,
233                     xFac.createInstance( "com.sun.star.text.TextField.GetReference" ) );
234             if ( xNewField != null ) {
235                 com.sun.star.beans.XPropertySet xFieldProps =
236                         (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
237                         com.sun.star.beans.XPropertySet.class, xNewField );
238                 xFieldProps.setPropertyValue( "ReferenceFieldPart", new Short(com.sun.star.text.ReferenceFieldPart.TEXT) );
239                 xFieldProps.setPropertyValue( "ReferenceFieldSource", new Short(com.sun.star.text.ReferenceFieldSource.BOOKMARK) );
240                 xFieldProps.setPropertyValue( "SourceName", cBookmarkName );
241                 com.sun.star.text.XTextRange xFieldTextRange =
242                         (com.sun.star.text.XTextRange)UnoRuntime.queryInterface(
243                         com.sun.star.text.XTextRange.class, xParaEnum.nextElement());
244                 xNewField.attach(xFieldTextRange.getEnd());
245                 xFldsRefresh.refresh();
246             }
247 
248             // check inserted reference field
249             com.sun.star.text.XTextField xField =
250                     (com.sun.star.text.XTextField)UnoRuntime.queryInterface(
251                     com.sun.star.text.XTextField.class, xNewField );
252             assertEquals( "inserted reference field doesn't has correct field result",
253                           "J", xField.getPresentation( false ) );
254 
255             xParaTextRange.getStart().setString( "Hallo new bookmark: " );
256             xFldsRefresh.refresh();
257             assertEquals( "inserted reference field doesn't has correct field result",
258                           "Hallo new bookmark: J", xField.getPresentation( false ) );
259         }
260     }
261 
setUpDocument()262     @Before public void setUpDocument() throws com.sun.star.uno.Exception {
263         document = util.WriterTools.loadTextDoc(
264             UnoRuntime.queryInterface(
265                 XMultiServiceFactory.class,
266                 connection.getComponentContext().getServiceManager()),
267             TestDocument.getUrl("CheckCrossReferences.odt"));
268     }
269 
tearDownDocument()270     @After public void tearDownDocument() {
271         util.DesktopTools.closeDoc(document);
272     }
273 
274     private XTextDocument document = null;
275 
setUpConnection()276     @BeforeClass public static void setUpConnection() throws Exception {
277         connection.setUp();
278     }
279 
tearDownConnection()280     @AfterClass public static void tearDownConnection()
281         throws InterruptedException, com.sun.star.uno.Exception
282     {
283         connection.tearDown();
284     }
285 
286     private static final OfficeConnection connection = new OfficeConnection();
287 }
288