0

DOCX4J を使用してコンテンツを解析し、テンプレートに挿入しようとしています。このテンプレートの一部として、2 つのマーカーの間のすべてをコピーし、そのすべてのコンテンツを X 回繰り返す必要があるループがあります。

関連するコードは次のとおりです。

public List<Object> getBetweenLoop(String name){
        String startTag = this.tag_start + name + "_LOOP" + this.tag_end;
        String endTag = this.tag_start + name + this.tag_end;
        P begin_loop = this.getTagParagraph(startTag);
        P end_loop = this.getTagParagraph(endTag);

        ContentAccessor parent = (ContentAccessor) this.getCommonParent(begin_loop, end_loop);

        List<Object> loop = new ArrayList<Object>();

        boolean save = false;
        //Cycle through the content for the parent and copy all the objects that
        //are between and including the start and end-tags
        for(Object item : parent.getContent()){
            if(item.equals(begin_loop) || item.equals(end_loop))
                save = (save) ? false : true;
            if(save || item.equals(end_loop)){
                loop.add(XmlUtils.deepCopy(item));
            }
            if(item.equals(end_loop)){
                //Here I want to insert everything copied X times after the current item and then exit the for loop.
                //This is the part I'm not sure how to do since I don't see any methods "Insert Child", etc.
            }
        }
        return loop;
    }

getTagParagraph は、送信されたタグの段落を表すオブジェクトを正常に返します。これは美しく機能します。

getCommonParent は、提供された 2 つのタグ間の共有の親を返します。これは美しく機能します。

私の問題は、コメントされているように、新しくコピーされたアイテムを適切な場所に挿入する方法です。

4

2 に答える 2

0

@ベン、ありがとう!

以下が機能しない例を知っている場合は、お知らせください。

私は実際に非常によく似たものを見つけたばかりでしたが、より多くのコードを変更することになりました。以下は私がまとめたものです。

   public void repeatLoop(String startTag, String endTag, Integer iterations){
        P begin_loop = this.getTagParagraph(startTag);
        P end_loop = this.getTagParagraph(endTag);
        ContentAccessor parent = (ContentAccessor) this.getCommonParent(begin_loop, end_loop);
        List<Object> content = parent.getContent();

        Integer begin_pointer = content.indexOf(begin_loop);
        Integer end_pointer = content.indexOf(end_loop);

        List<Object> loop = new ArrayList<Object>();
        for(int x=begin_pointer; x <= end_pointer; x = x + 1){
            loop.add(XmlUtils.deepCopy(content.get(x)));
        }

        Integer insert = end_pointer + 1;
        for(int z = 1; z < iterations; z = z + 1){
            content.addAll(insert, loop);
            insert = insert + loop.size();
        }
    }
于 2013-06-13T14:08:45.857 に答える