Package net.sourceforge.rtf.document

Examples of net.sourceforge.rtf.document.RTFBookmark


            String endBookmarkName = (String) iter.next();
            int index = getTransformerConfig()
                    .getBookmarkIndex(endBookmarkName);
            String startBookmarkName = getTransformerConfig()
                    .getBookmarkStartLoopName(index);
            RTFBookmark startBookmark = (RTFBookmark) bookmarkStartMap
                    .get(startBookmarkName);
            if (startBookmark != null) {
                // Start bookmark exist
                RTFBookmark endBookmark = (RTFBookmark) bookmarkEndMap
                        .get(endBookmarkName);
                String newContent = getMacroEndForEach(); // "#end";
                RTFPage page = (RTFPage) pageBeforeEndBookmarkMap
                        .get(endBookmarkName);
                if (page != null) {
                    String content = null;
                    if (groupByPerPageBreak > -1) {
                        // Content must be group by
                        // There is a page break defined into RTF and group by
                        // per page is defined
                        content = " "
                                + getMacroPageBreak(startBookmark
                                        .getItemNameList(), page
                                        .getRTFFirstContentOfElement(),
                                        groupByPerPageBreak) + " ";
                    } else {
                        // There is a page break defined into RTF
                        // and group by per page is not defined
                        content = " "
                                + getMacroPageBreak(startBookmark
                                        .getItemNameList(), page
                                        .getRTFFirstContentOfElement()) + " ";
                    }
                    page.replaceElement(content);
                } else {
                    if (groupByPerPageBreak > -1) {
                        if (lastEndLoopRTFBookmark != null
                                && lastEndLoopRTFBookmark.equals(endBookmark)) {
                            newContent = getMacroPageBreak(startBookmark
                                    .getItemNameList(), "\\page",
                                    groupByPerPageBreak)
                                    + newContent;
                        }
                    }
                }
                endBookmark.replaceElement(newContent);

                // Replace BKMEND by ""
                RTFBookmark startBookmarkBKMEND = (RTFBookmark) bookmarkBkmEndMap
                        .get(startBookmarkName);
                startBookmarkBKMEND.replaceElement("");
                RTFBookmark endBookmarkBKMEND = (RTFBookmark) bookmarkBkmEndMap
                        .get(endBookmarkName);
                endBookmarkBKMEND.replaceElement("");
            }
        }
        // Replace element ENDIF Bookmark end by #end
        for (Iterator iter = bookmarkEndIfMap.keySet().iterator(); iter
                .hasNext();) {
            String endIfBookmarkName = (String) iter.next();
            int index = getTransformerConfig().getBookmarkIndex(
                    endIfBookmarkName);
            String ifBookmarkName = getTransformerConfig().getBookmarkIfName(
                    index);
            RTFBookmark ifBookmark = (RTFBookmark) bookmarkIfMap
                    .get(ifBookmarkName);
            if (ifBookmark != null) {
                RTFBookmark endIfBookmark = (RTFBookmark) bookmarkEndIfMap
                        .get(endIfBookmarkName);
                endIfBookmark.replaceElement(getMacroEndIf());
                RTFBookmark endIfBookmarkBKMEND = (RTFBookmark) bookmarkBkmIfMap
                        .get(endIfBookmarkName);
                endIfBookmarkBKMEND.replaceElement("");
            }
        }
        // Replace element ELSE Bookmark end by #else
        for (Iterator iter = bookmarkElseMap.keySet().iterator(); iter
                .hasNext();) {
            String elseBookmarkName = (String) iter.next();
            int index = getTransformerConfig().getBookmarkIndex(
                    elseBookmarkName);
            String ifBookmarkName = getTransformerConfig().getBookmarkIfName(
                    index);
            RTFBookmark ifBookmark = (RTFBookmark) bookmarkIfMap
                    .get(ifBookmarkName);
            if (ifBookmark != null) {
                RTFBookmark elseBookmark = (RTFBookmark) bookmarkElseMap
                        .get(elseBookmarkName);
                elseBookmark.replaceElement(getMacroElse());
            }
        }
        return documentTransformed;

    }
View Full Code Here


                break;

            case RTF_ELEMENT_BOOKMARK:
                // RTF element Bookmark is found.
                // ONLY BKMSTART Bookmark are used.
                RTFBookmark currentBookmark = (RTFBookmark) element;
                String bookMarkName = currentBookmark.getName().toUpperCase();
                if (isBookmarkStartLoop(bookMarkName)) {
                    // Bookmark is start loop
                    if (currentBookmark instanceof RTFStartBookmark) {
                        startBookmarksList.addElement(currentBookmark);
                        lastPage = null;
                    } else {
                        // Bookmark BMKMEND => must be replace by "" at the end
                        bookmarkBkmEndMap.put(bookMarkName, currentBookmark);
                    }
                } else {
                    if (isBookmarkEndLoop(bookMarkName)) {
                        // Bookmark is end loop
                        if (currentBookmark instanceof RTFStartBookmark) {
                            bookmarkEndMap.put(bookMarkName, currentBookmark);
                            pageBeforeEndBookmarkMap
                                    .put(bookMarkName, lastPage);
                            // Store last RTF Bookmark End Loop
                            lastEndLoopRTFBookmark = currentBookmark;
                        } else {
                            // Bookmark BMKMEND => must be replace by "" at the
                            // end
                            bookmarkBkmEndMap
                                    .put(bookMarkName, currentBookmark);
                        }
                        // Current bookmark is END_LOOP_i Remove START_LOOP_i of
                        // the
                        // startBookmarksNotEnded Map
                        Collection bookmarksNotEnded = startBookmarksNotEnded
                                .values();
                        for (Iterator iterator = bookmarksNotEnded.iterator(); iterator
                                .hasNext();) {
                            RTFStartBookmark startBookmarkNoEnded = (RTFStartBookmark) iterator
                                    .next();
                            String startBookmarkNameNoEnded = startBookmarkNoEnded
                                    .getName();
                            int index = getTransformerConfig()
                                    .getBookmarkIndex(startBookmarkNameNoEnded);
                            String endBookmarkName = getTransformerConfig()
                                    .getBookmarkEndLoopName(index);
                            if (endBookmarkName.equals(currentBookmark
                                    .getName())) {
                                bookmarksNotEnded.remove(startBookmarkNoEnded);
                                break;
                            }
                        }

                    } else {
                        if (isBookmarkIf(bookMarkName)) {
                            // Bookmark is if condition
                            if (currentBookmark instanceof RTFStartBookmark)
                                bookmarkIfMap
                                        .put(bookMarkName, currentBookmark);
                            else
                                // Bookmark BMKMEND => must be replace by "" at
                                // the end
                                bookmarkBkmIfMap.put(bookMarkName,
                                        currentBookmark);
                        } else {
                            if (isBookmarkEndIf(bookMarkName)) {
                                // Bookmark is endif condition
                                if (currentBookmark instanceof RTFStartBookmark)
                                    bookmarkEndIfMap.put(bookMarkName,
                                            currentBookmark);
                                else
                                    // Bookmark BMKMEND => must be replace by ""
                                    // at
                                    // the end
                                    bookmarkBkmIfMap.put(bookMarkName,
                                            currentBookmark);
                            }
                            else {
                                if (isBookmarkElse(bookMarkName)) {
                                    // Bookmark is else condition
                                    if (currentBookmark instanceof RTFStartBookmark)
                                        bookmarkElseMap.put(bookMarkName,
                                                currentBookmark);
                                    else
                                        // Bookmark BMKMEND => must be replace by ""
                                        // at
                                        // the end
                                        bookmarkBkmIfMap.put(bookMarkName,
                                                currentBookmark);
                                }
                            }
                        }
                    }
                }

                break;

            case RTF_ELEMENT_FIELD:
                RTFField field = (RTFField) element;
                String fieldName = field.getName();
                if (fieldName != null) {
                    int fieldType = field.getType();
                    RTFContextField rtfField = (RTFContextField) contextFieldsMap
                            .get(fieldName);
                    if (rtfField != null) {
                        // RTF field is into the context
                        String newFieldName = "";
                        String fieldNameWithListInfo = rtfField.getListInfo();
                        boolean fieldIsList = isList(rtfField);
                        boolean fieldIsImage = isImage(rtfField);
                        switch (fieldType) {
                        case RTFField.FIELDTYPE_HYPERLINK:
                            /*
                             * HYPERLINK field code RTF example : /* {\field
                             * {\*\fldinst {\insrsid9655237 HYPERLINK
                             * "mailto:$developers.Email" } {\insrsid9655237
                             * {\*\datafield
                             * 00d0c9ea79f9bace118c8200aa004ba90b0200000003000000e0c9ea79f9bace118c8200aa004ba90b320000006d00610069006c0074006f003a00240064006500760065006c006f0070006500720073002e0045006d00610069006c000000 } } }
                             * {\fldrslt {\cs16\
                             * ul\cf2\insrsid9655237\charrsid9655237
                             * mailto:$developers.Email } } }
                             */
                            IRTFFieldTransform hyperlinkTransform = getRTFFieldTransform(
                                    fieldName, fieldType, fieldIsImage);
                            hyperlinkTransform.transform(field, fieldIsList,
                                    this);
                            break;
                        case RTFField.FIELDTYPE_MERGEFIELD:
                            /*
                             * MERGEFIELD code RTF example : {\field\flddirty
                             * {\*\fldinst
                             * {\b\cf17\lang1036\langfe2057\langnp1036\insrsid331776\charrsid5667574
                             * MERGEFIELD $personne.Name \\* MERGEFORMAT } }
                             * {\fldrslt
                             * {\b\cf17\lang1024\langfe1024\noproof\langnp1036\insrsid331776\charrsid5667574
                             * \'ab$personne.Name\'bb } } }
                             */
                            IRTFFieldTransform fieldTransform = getRTFFieldTransform(
                                    fieldName, fieldType, fieldIsImage);
                            fieldTransform.transform(field, fieldIsList, this);
                            break;
                        }

                        // Test if field is list
                        if (fieldIsList) {
                            boolean loopIsManagedWithBookmark = false;
                            // Mergefield is list, it must be placed :
                            // * between 2 Bookmark (START_LOOP and END_LOOP) or
                            // * inside Row
                            int bookmarkSize = startBookmarksList.size();
                            if (bookmarkSize > 0) {
                                // MergeField is found and Bookmark was started
                                // construct #foreach with name of field
                                RTFBookmark startBookmark = (RTFBookmark) startBookmarksList
                                        .elementAt(bookmarkSize - 1);
                                startBookmark.replaceElement(getForeach(
                                        fieldName, fieldNameWithListInfo));
                                startBookmark.setItemNameList(getItemListName(
                                        fieldName, fieldNameWithListInfo));
                                startBookmarksList.removeElement(startBookmark);
                                bookmarkStartMap.put(startBookmark.getName(),
                                        startBookmark);
                                startBookmarksNotEnded.put(startBookmark
                                        .getItemNameList(), startBookmark);
                                loopIsManagedWithBookmark = true;
                            } else {
                                // Tet si if exist A START_LOOP_i which is not
                                // ended and which have the name list
                                // waited
                                String itemListName = getItemListName(
                                        fieldName, fieldNameWithListInfo);
                                loopIsManagedWithBookmark = (startBookmarksNotEnded
                                        .get(itemListName) != null);
                            }

                            if (loopIsManagedWithBookmark) {

                                // Test if rtfParent is row => to indicate that
                                // row must not generate #foreach
                                if (getType(parentElement) == RTF_ELEMENT_ROW) {
                                    RTFRow row = (RTFRow) parentElement;
                                    if (rowMustNotGenerateForeachList == null) {
                                        rowMustNotGenerateForeachList = new ArrayList();
                                    }
                                    rowMustNotGenerateForeachList.add(row);
                                }
                            } else {
                                // Test if rtfParent is row
                                if (getType(parentElement) == RTF_ELEMENT_ROW) {
                                    RTFRow row = (RTFRow) parentElement;
                                    if (row.getFirstRTFString() == null) {
                                        if (rowMustNotGenerateForeachList == null
                                                || !rowMustNotGenerateForeachList
                                                        .contains(row)) {
                                            row.addFirstRTFString(getForeach(
                                                    fieldName,
                                                    fieldNameWithListInfo));
                                            row
                                                    .addLastRTFString(getMacroEndForEach()); // #end
                                        }
                                    }
                                }
                            }

                            // Add item_ after $
                            String fieldContent = field
                                    .getRTFContentOfSimpleElement();
                            if (fieldName.startsWith("$"))
                                fieldName = fieldName.substring(1);
                            newFieldName = getObjectValueList(fieldName,
                                    fieldNameWithListInfo, true);
                            if (newFieldName.startsWith("$"))
                                newFieldName = newFieldName.substring(1);

                            fieldContent = StringUtils.sub(fieldContent,
                                    fieldName, newFieldName);
                            field.replaceElement(fieldContent);
                        }
                    } else {
                        if (fieldType == RTFField.FIELDTYPE_REF) {
                            String condition = field.getCondition();
                            if (condition != null) {
                                // Condition is defined
                                // Field is REF (IF condition)
                                // Get Bookmark with name field
                                RTFBookmark ifBookmark = (RTFBookmark) bookmarkIfMap
                                        .get(fieldName);
                                if (ifBookmark != null) {
                                    // Bookmark IF exists. Replace this RTF code
                                    // with the condition
                                    // comming from field
                                    condition = getMacroIf(condition);
                                    ifBookmark.replaceElement(condition);
                                    // Remove content of the field
                                    field.replaceElement("");
                                    // Replace BKMEND by ""
                                    RTFBookmark ifBookmarkBKMEND = (RTFBookmark) bookmarkBkmIfMap
                                            .get(fieldName);
                                    ifBookmarkBKMEND.replaceElement("");                                   
                                }
                            }
                        }
                    }
                }
View Full Code Here

TOP

Related Classes of net.sourceforge.rtf.document.RTFBookmark

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.