}
public void checkCrossReferences() throws com.sun.star.uno.Exception {
// load test document
try {
XMultiServiceFactory xMSF = (XMultiServiceFactory)param.getMSF();
xDoc = util.WriterTools.loadTextDoc( xMSF, util.utils.getFullTestURL("CheckCrossReferences.odt"));
} catch(com.sun.star.uno.RuntimeException e) {
System.out.println("Cannot load test document.");
e.printStackTrace();
failed(e.getMessage());
return;
}
// setup paragraph enumeration
{
com.sun.star.container.XEnumerationAccess xParaEnumAccess =
(com.sun.star.container.XEnumerationAccess)UnoRuntime.queryInterface(
com.sun.star.container.XEnumerationAccess.class, xDoc.getText());
xParaEnum = xParaEnumAccess.createEnumeration();
}
// get field refresher
{
com.sun.star.text.XTextFieldsSupplier xFieldSupp =
(com.sun.star.text.XTextFieldsSupplier)UnoRuntime.queryInterface(
com.sun.star.text.XTextFieldsSupplier.class, xDoc);
xFldsRefresh = (com.sun.star.util.XRefreshable)UnoRuntime.queryInterface(
com.sun.star.util.XRefreshable.class, xFieldSupp.getTextFields());
}
// check first reference field
{
// strings for checking
final String FldResult1 = "*i*";
final String FldResult2 = "+b+*i*";
final String FldResult3 = "-1-+b+*i*";
final String FldResult4 = "1.";
final String FldResult5 = " 1.";
final String FldResult6 = "A. 1.";
// variables for current field
com.sun.star.text.XTextField xField = null;
com.sun.star.beans.XPropertySet xProps = null;
log.println( "Checking field reference formats NUMBER, NUMBER_NO_CONTEXT and NUMBER_FULL_CONTEXT for existing fields" );
xField = getNextField();
xProps = getFieldProps( xField );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult2 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3 );
xField = getNextField();
xProps = getFieldProps( xField );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult1 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3 );
xField = getNextField();
xProps = getFieldProps( xField );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult3 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3 );
xField = getNextField();
xProps = getFieldProps( xField );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult5 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6 );
xField = getNextField();
xProps = getFieldProps( xField );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult4 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6 );
xField = getNextField();
xProps = getFieldProps( xField );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult6 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4 );
checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6 );
}
// insert a certain cross-reference bookmark and a reference field to this bookmark
{
log.println("Checking insert of cross-reference bookmark and corresponding reference field");
// restart paragraph enumeration
com.sun.star.container.XEnumerationAccess xParaEnumAccess =
(com.sun.star.container.XEnumerationAccess)UnoRuntime.queryInterface(
com.sun.star.container.XEnumerationAccess.class, xDoc.getText());
xParaEnum = xParaEnumAccess.createEnumeration();
// iterate on the paragraphs to find certain paragraph to insert the bookmark
com.sun.star.text.XTextRange xParaTextRange = null;
while ( xParaEnum.hasMoreElements() ) {
try {
xParaTextRange = (com.sun.star.text.XTextRange)UnoRuntime.queryInterface(
com.sun.star.text.XTextRange.class, xParaEnum.nextElement());
if ( xParaTextRange.getString().equals( "*i*J" ) ) {
break;
}
} catch (com.sun.star.container.NoSuchElementException e) {
System.out.println("Cannot find paragraph to insert cross-reference bookmark.");
e.printStackTrace();
failed(e.getMessage());
return;
} catch (com.sun.star.lang.WrappedTargetException e) {
System.out.println("Cannot find paragraph to insert cross-reference bookmark.");
e.printStackTrace();
failed(e.getMessage());
return;
}
}
if ( xParaTextRange == null ) {
System.out.println("Cannot find paragraph to insert cross-reference bookmark.");
failed("Cannot find paragraph to insert cross-reference bookmark.");
return;
}
// insert bookmark
XMultiServiceFactory xFac = (XMultiServiceFactory)UnoRuntime.queryInterface(
XMultiServiceFactory.class, xDoc);
final String cBookmarkName = "__RefNumPara__47114711";
com.sun.star.text.XTextContent xBookmark = null;
try {
xBookmark = (com.sun.star.text.XTextContent)UnoRuntime.queryInterface(
com.sun.star.text.XTextContent.class,
xFac.createInstance( "com.sun.star.text.Bookmark" ) );
} catch (com.sun.star.lang.IllegalArgumentException e) {
System.out.println("Cannot create bookmark.");
e.printStackTrace();
failed(e.getMessage());
return;
} catch (com.sun.star.uno.Exception e) {
System.out.println("Cannot create bookmark.");
e.printStackTrace();
failed(e.getMessage());
return;
}
if ( xBookmark != null ) {
com.sun.star.container.XNamed xName =
(com.sun.star.container.XNamed)UnoRuntime.queryInterface(
com.sun.star.container.XNamed.class, xBookmark );
xName.setName( cBookmarkName );
xBookmark.attach(xParaTextRange.getStart());
}
// insert reference field, which references the inserted bookmark
com.sun.star.text.XTextContent xNewField = null;
try {
xNewField = (com.sun.star.text.XTextContent)UnoRuntime.queryInterface(
com.sun.star.text.XTextContent.class,
xFac.createInstance( "com.sun.star.text.TextField.GetReference" ) );
} catch (com.sun.star.lang.IllegalArgumentException e) {
System.out.println("Cannot create new field.");
e.printStackTrace();
failed(e.getMessage());
return;