Page 1 of 1

Existing template - Repeated Sections

PostPosted: Tue Oct 29, 2013 1:59 am
by gingwa123
I've been learning docx4j and have finally got simple headers, body and tables to be changed through a simple template however the last piece of the puzzle is there is many repeated sections within the document denoted by something similar to the below (so there would be multiple tests and multiple machines failing and possibly more that one nested repeated sections

[!-- START variable --]
Test: ${test-id}
Description ${test-description}
Failing Machines:
[!-- START test-variable2--]
${machine-name}
[!-- END test-variable2 --]
..... There may be more than one repeated section .......
[!-- END variable --]

So what I wanted to do was basically step through the object list find the start and the end copy everything between and for every input (I'll populate through a database) use the template and duplicate the section. The question I have is how do i copy and paste back in the list of nodes saying I only have the start text node as a pointer and there doesn't seem to be a method to do that like a table where you know the exact parent node etc

Or am I doing it totally wrong and is there any better API's / easier way to do that. Any help would be appreciated Im new to this and have been flung in the deep end with no time :( .. I did search the forum but didn't find anything but it might just be me being stupid.


Code: Select all
public void processRepeatedSection(List<Object> list, RepeatedSectionObjectList rso){
        String lookup = rso.getIdentifier();
        LinkedList objectList = new LinkedList();
       
        boolean foundStart = false;
        boolean foundEnd = false;
       
        // Start point
        Text textElementStart = new Text();
       
        // Build our repeated sectionList
        for (Object obj: list){
            if (obj instanceof Text){
                Text textElement = (Text) obj;
                if (textElement.getValue().indexOf("[!-- START "+lookup+" --]") > -1){
                    foundStart = true;
                }
               
                if (textElement.getValue().indexOf("[!-- END "+lookup+" --]") > -1){
                    foundEnd = true;
                }
               
                if (foundStart && !foundEnd){
                    objectList.add(obj);
                }
               
               
               
            }

            // At this point maybe i can copy the list and replace values and call myself for repeated nested statements

        }       
       
       
       
    }

Re: Existing template - Repeated Sections

PostPosted: Tue Oct 29, 2013 7:55 am
by jason
The "docx4j way" of doing repeats is via content control data binding.

Please see http://www.opendope.org/opendope_conventions_v2.3.html for an overview.

Basically, you insert a content control to denote the stuff you want to repeat. Within that content control, you'd use XML data bindings for your various variables.

So your XML data structure might be something like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting

<tests>
    <test id="t1">
        <machine id="m1">
              <result>pass</result>
        </machine>
        <machine id="m2">
              <result>fail</result>
        </machine>
    </test>
    <test id="t2">
        <machine id="m1">
              <result>pass</result>
        </machine>
        <machine id="m2">
              <result>fail</result>
        </machine>
    </test>
</tests>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Docx4j can do all the work for you, if you can provide your data in XML format, and your docx is set up with content controls to match.

Re: Existing template - Repeated Sections

PostPosted: Fri Nov 01, 2013 12:21 am
by gingwa123
Thanks this works and is a lot better than what I was doing :) Thanks again