Hi
I found how to do it but maybe there's a faster/cleaner/... solution.
This is how I did it:
public static void preparseBulletList(TextDocument document, String
fieldName, int listLength) {
for (Iterator<List> listIterator = document.getListIterator();
listIterator.hasNext();) {
List list = listIterator.next();
java.util.List<ListItem> listItems = list.getItems();
int size = listItems.size();
if (size == 1) { // only bullet lists with 1 item can be dynamic
ListItem listItem = listItems.get(0);
NamedNodeMap variableNodeAttributes =
getVariableNodeAttributes(listItem);
if (variableNodeAttributes != null) {
Node nameNode =
variableNodeAttributes.getNamedItem("text:name");
if (fieldName.equals(nameNode.getNodeValue())) {
// found the bullet with correct field name
for (int i = 1; i < listLength; i++) {
// copy the existing bullet so styling etc is
taken over
ListItem newListItem = list.addItem(listItem);
NamedNodeMap newVariableNodeAttributes =
getVariableNodeAttributes(newListItem);
Node newNameNode =
newVariableNodeAttributes.getNamedItem("text:name");
// the new, indexed field name
newNameNode.setNodeValue(fieldName + i);
}
}
}
}
}
}
private static NamedNodeMap getVariableNodeAttributes(ListItem
listItem) {
// door de DOM tree stappen tot aan de text node
TextListItemElement listItemElement = listItem.getOdfElement();
Node stylingNode = listItemElement.getFirstChild();
Node textNode = stylingNode.getFirstChild();
// zoeken naar de 'variabele' node.
// vb igv
// * some text <Set variable a_field = %INVULLEN%>
while (!(textNode instanceof TextVariableSetElement)) {
textNode = textNode.getNextSibling();
}
NamedNodeMap textNodeAttributes = textNode.getAttributes();
return textNodeAttributes;
}
On Thu, Jul 26, 2012 at 11:24 AM, Nick De Graeve <[email protected]>wrote:
> Hi
>
>
> We have ODT documents in which there are a set of text fields with value
> %INVULLEN%. Our application parses the document and replaces %INVULLEN%
> with the appropriate value. Some of the templates have dynamic bullet lists
> and tables. This is handled by a pre-parsing step that will add to the
> list/table a new field with ascending number. Eg. in the document there's a
> bullet list with:
>
> * <Set variable my_field = %INVULLEN%>
>
> There will be 3 items in the bullet list, so the pre-parser will modify
> the document as follows:
>
> * <Set variable my_field = %INVULLEN%>
> * <Set variable my_field1 = %INVULLEN%>
> * <Set variable my_field2 = %INVULLEN%>
>
> The parser will then replace all %INVULLEN% in the document with the
> correct values.
>
> At this moment the pre-parser manipulates the content.xml directly by
> reading it in as a String and inserting String fragments at the appropriate
> place (field declaration and bullets).
>
> I'm investigating if we can't use ODF Toolkit to do this. Our parser works
> also by replacing String fragments, so it might be a candidate for
> refactoring too but initially I will only use it for the pre-parser.
> Is this possible with the current version of the Toolkit?
>
> What I've tried so far is to get the list of lists with
>
> // I have the name of the field and the # of bullet items
> Iterator<List> listIterator = document.getListIterator();
> while (listIterator.hasNext()) {
> List list = listIterator.next();
> // what to do next?
> }
>
> but how to proceed?
> How can I determine the list with the required field?
> How can I add a new item with a new field to the list?
>
>
> Thanks,
> Nick
>