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