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