incubator-odf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steffen Börsig <Steffen.Boer...@abas.de>
Subject Problem with prebuild template (CellSelection.replaceWith(...))
Date Tue, 16 Jul 2013 12:13:44 GMT
Hi,

today I stumbled upon a weird problem. I use the latest build of the ODF 
Toolkit and Oracles Java 1.7 on Debian 6.
It's basically a problem with CellSelection.advancedReplaceWith(...) 
[2]. The replacetext is inserted more than once into the cell. 
Everything seems to be fine until mCell.setDisplayText(text, adapter). 
The problem emerges if I create a Writer-Template in OpenOffice.org3 and 
then replace it with the ODF-Toolkit. If the document is created through 
ODF-Toolkit and then the same procedure happens the problem does not 
exist. The problem also does only emerge if the placeholder has one or 
more linebreaks before it in the same cell!

Since it's not the most exact problem description I appended a JUnit 
Testcase [1] which should explain everything. One example 
Template-Document is appended to this email. I did test several 
different templates, the problem does persist.
I'm not experienced with the API yet so I don't blame it on a bug. Maybe 
it's me just not using the API in the right way. Would love to get some 
input from you guys.

Greetings,
Steffen


[1]
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.odftoolkit.simple.TextDocument;
import org.odftoolkit.simple.common.TextExtractor;
import org.odftoolkit.simple.common.navigation.InvalidNavigationException;
import org.odftoolkit.simple.common.navigation.TextNavigation;
import org.odftoolkit.simple.common.navigation.TextSelection;
import org.odftoolkit.simple.table.Table;

public class TemplateReplaceTest {

     @Test
     public void test() throws Exception {
         TextDocument document = TextDocument.newTextDocument();
         Table table = document.addTable(2, 1);
         table.getCellByPosition(0, 0).setStringValue("TestPlaceholder");
         table.getCellByPosition(0, 
1).setStringValue("\n\nTestPlaceholder");

         searchAndReplaceTestPlaceholder(document);
         document.save("TestGeneratedTemplate.odt");
         String textFromGenerate = 
TextExtractor.getText(document.getContentRoot());
         document.close();

         document = TextDocument.loadDocument("TestTemplate.ott");
         searchAndReplaceTestPlaceholder(document);
         document.save("TestPrebuildTemplate.odt");
         String textFromTemplate = 
TextExtractor.getText(document.getContentRoot());
         document.close();

         assertEquals(textFromGenerate, textFromTemplate);
     }

     private static void searchAndReplaceTestPlaceholder(TextDocument 
document) throws InvalidNavigationException {
         TextNavigation nav = new TextNavigation("TestPlaceholder", 
document);
         while (nav.hasNext())
         {
             TextSelection nextSelection = (TextSelection) 
nav.nextSelection();
             nextSelection.replaceWith("Replaced Text");
         }
     }

}

[2]
     public void advancedReplaceWith(String newText, CellValueAdapter 
adapter) throws InvalidNavigationException {
         super.replaceWith(newText);
         if (mCell != null) {
             String text = mCell.getDisplayText();
             // update mCell value and value type.
             mCell.setDisplayText(text, adapter);
         }
     }

Mime
View raw message