org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark Java Examples

The following examples show how to use org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BookmarkManager.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Ends the bookmark with the given name.
 * 
 * @param result
 *            the {@link GenerationResult}
 * @param paragraph
 *            the current {@link XWPFParagraph}
 * @param name
 *            the bookmark name
 */
public void endBookmark(GenerationResult result, XWPFParagraph paragraph, String name) {
    final CTBookmark bookmark = startedBookmarks.remove(name);
    if (bookmark != null) {
        final CTMarkupRange range = paragraph.getCTP().addNewBookmarkEnd();
        range.setId(bookmarks.get(name).getId());
        // we remove the run created for error messages.
        final XWPFRun run = messagePositions.get(bookmark);
        final IRunBody parent = run.getParent();
        if (parent instanceof XWPFParagraph) {
            ((XWPFParagraph) parent).removeRun(((XWPFParagraph) parent).getRuns().indexOf(run));
        } else {
            throw new IllegalStateException("this should not happend");
        }
    } else if (bookmarks.containsKey(name)) {
        result.addMessage(M2DocUtils.appendMessageRun(paragraph, ValidationMessageLevel.ERROR,
                "Can't end already closed bookmark " + name));
    } else {
        result.addMessage(M2DocUtils.appendMessageRun(paragraph, ValidationMessageLevel.ERROR,
                "Can't end not existing bookmark " + name));
    }
}
 
Example #2
Source File: BookmarkManager.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Inserts a pending reference to the given name in the given {@link XWPFParagraph}.
 * 
 * @param paragraph
 *            the {@link XWPFParagraph}
 * @param name
 *            the bookmark name
 * @param text
 *            the text
 */
public void insertReference(XWPFParagraph paragraph, String name, String text) {
    final CTBookmark bookmark = bookmarks.get(name);
    if (bookmark != null) {
        insertReference(paragraph, bookmark, text);
    } else {
        final XWPFRun messageRun = paragraph.createRun();
        final CTText ref = insertPendingReference(paragraph, name, text);
        ref.setStringValue(String.format(REF_TAG, name));
        messagePositions.put(ref, messageRun);
        Set<CTText> pendingRefs = pendingReferences.get(name);
        if (pendingRefs == null) {
            pendingRefs = new LinkedHashSet<>();
            pendingReferences.put(name, pendingRefs);
        }
        pendingRefs.add(ref);
        xmlObjectToName.put(ref, name);
    }
}
 
Example #3
Source File: BookmarkManager.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Updates the old {@link XmlObject} with the new one.
 * 
 * @param newObject
 *            the new {@link XmlObject}
 * @param oldObject
 *            the old {@link XmlObject}
 * @param <T>
 *            the actual type of both {@link XmlObject}
 */
public <T extends XmlObject> void updateXmlObject(T newObject, T oldObject) {
    final String name = xmlObjectToName.remove(oldObject);
    if (name != null) {
        xmlObjectToName.put(newObject, name);
        if (bookmarks.get(name) == oldObject) {
            bookmarks.put(name, (CTBookmark) newObject);
            if (startedBookmarks.get(name) == oldObject) {
                startedBookmarks.put(name, (CTBookmark) newObject);
            }
            newObject.validate();
        } else {
            final Set<CTText> refs = pendingReferences.get(name);
            if (refs != null && refs.contains(oldObject)) {
                refs.remove(oldObject);
                refs.add((CTText) newObject);
                newObject.validate();
            }
        }
    }
    final XWPFRun run = messagePositions.remove(oldObject);
    if (run != null) {
        messagePositions.put(newObject, run); // TODO find the new run
    }
}
 
Example #4
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Updates bookmarks for the given output {@link CTP} according to its input {@link CTP}.
 * 
 * @param bookmarkManager
 *            the {@link BookmarkManager}
 * @param outputParagraph
 *            the output {@link CTP}
 * @param inputParagraph
 *            the input {@link CTP}
 */
private void updateBookmarks(BookmarkManager bookmarkManager, CTP outputParagraph, CTP inputParagraph) {
    final List<CTBookmark> oldBookmarks = inputParagraph.getBookmarkStartList();
    final List<CTBookmark> newBookmarks = outputParagraph.getBookmarkStartList();
    for (int bookmarkIndex = 0; bookmarkIndex < oldBookmarks.size(); bookmarkIndex++) {
        bookmarkManager.updateXmlObject(newBookmarks.get(bookmarkIndex), oldBookmarks.get(bookmarkIndex));
    }
    List<CTR> inputRuns = inputParagraph.getRList();
    final List<CTR> outputRuns = outputParagraph.getRList();
    for (int runIndex = 0; runIndex < inputRuns.size(); runIndex++) {
        final CTR inputRun = inputRuns.get(runIndex);
        final CTR outputRun = outputRuns.get(runIndex);
        final List<CTText> inputTexts = inputRun.getInstrTextList();
        final List<CTText> outputTexts = outputRun.getInstrTextList();
        for (int textIndex = 0; textIndex < inputTexts.size(); textIndex++) {
            bookmarkManager.updateXmlObject(outputTexts.get(textIndex), inputTexts.get(textIndex));
        }
    }
}
 
Example #5
Source File: BookMark.java    From frpMgr with MIT License 5 votes vote down vote up
/**
 * 构造函数
 * @param ctBookmark
 * @param para
 */
public BookMark(CTBookmark ctBookmark, XWPFParagraph para) {
	this._ctBookmark = ctBookmark;
	this._para = para;
	this._bookmarkName = ctBookmark.getName();
	this._tableCell = null;
	this._isCell = false;
}
 
Example #6
Source File: BookMarks.java    From frpMgr with MIT License 5 votes vote down vote up
private void procParaList(XWPFTableCell cell) {
	List<XWPFParagraph> paragraphList = cell.getParagraphs();

	for (XWPFParagraph paragraph : paragraphList) {
		//得到段落中的标签标记
		List<CTBookmark> bookmarkList = paragraph.getCTP().getBookmarkStartList();
		for (CTBookmark bookmark : bookmarkList) {
			this._bookmarks.put(bookmark.getName(), new BookMark(bookmark, paragraph, cell));
		}
	}
}
 
Example #7
Source File: BookMarks.java    From frpMgr with MIT License 5 votes vote down vote up
/**
 * 解析普通段落中的标签
 * @param paragraphList  传入的段落
 */
private void procParaList(List<XWPFParagraph> paragraphList) {
	for (XWPFParagraph paragraph : paragraphList) {
		List<CTBookmark> bookmarkList = paragraph.getCTP().getBookmarkStartList();
		//循环加入标签
		for (CTBookmark bookmark : bookmarkList) {
			this._bookmarks.put(bookmark.getName(), new BookMark(bookmark, paragraph));
		}
	}
}
 
Example #8
Source File: BookmarkManager.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Starts a bookmark in the given {@link XWPFParagraph} with the given name.
 * 
 * @param result
 *            the {@link GenerationResult}
 * @param paragraph
 *            the current {@link XWPFParagraph}
 * @param name
 *            the bookmark name
 */
public void startBookmark(GenerationResult result, XWPFParagraph paragraph, String name) {
    if (bookmarks.containsKey(name)) {
        result.addMessage(M2DocUtils.appendMessageRun(paragraph, ValidationMessageLevel.ERROR,
                "Can't start duplicated bookmark " + name));
    } else {
        final CTBookmark bookmark = paragraph.getCTP().addNewBookmarkStart();
        // we create a new run for future error messages.
        messagePositions.put(bookmark, paragraph.createRun());
        bookmark.setName(name);
        final BigInteger id = getRandomID();
        bookmark.setId(id);
        bookmarks.put(name, bookmark);
        xmlObjectToName.put(bookmark, name);
        startedBookmarks.put(name, bookmark);
        Set<CTText> pendingRefs = pendingReferences.remove(name);
        if (pendingRefs != null) {
            for (CTText pendingRef : pendingRefs) {
                xmlObjectToName.remove(pendingRef);
                // we remove the run created for error messages.
                final XWPFRun run = messagePositions.get(pendingRef);
                final IRunBody parent = run.getParent();
                if (parent instanceof XWPFParagraph) {
                    ((XWPFParagraph) parent).removeRun(((XWPFParagraph) parent).getRuns().indexOf(run));
                } else {
                    throw new IllegalStateException("this should not happend");
                }
                pendingRef.setStringValue(String.format(REF_TAG, bookmark.getName()));
            }
        }
    }
}
 
Example #9
Source File: BookmarkManager.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Marks the bookmarks that are still open.
 * 
 * @param result
 *            the {@link GenerationResult}
 * @return <code>true</code> if any open bookmarks was found, <code>false</code> otherwise
 */
public boolean markOpenBookmarks(GenerationResult result) {
    final boolean res = !startedBookmarks.isEmpty();

    if (res) {
        for (Entry<String, CTBookmark> entry : startedBookmarks.entrySet()) {
            final XWPFRun positionRun = messagePositions.remove(entry.getValue());
            result.addMessage(M2DocUtils.setRunMessage(positionRun, ValidationMessageLevel.ERROR,
                    "unclosed bookmark " + entry.getKey()));
        }
    }

    return res;
}
 
Example #10
Source File: BookmarkRenderPolicy.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public void doRender(RenderContext<Object> context) throws Exception {
    Helper.renderTextRun(context.getRun(), context.getData());

    XWPFRun run = context.getRun();
    XWPFParagraphWrapper wapper = new XWPFParagraphWrapper((XWPFParagraph) run.getParent());
    CTBookmark bookmarkStart = wapper.insertNewBookmark(run);

    Object renderData = context.getData();
    TextRenderData data = renderData instanceof TextRenderData ? (TextRenderData) renderData
            : new TextRenderData(renderData.toString());

    String text = null == data.getText() ? "" : data.getText();
    bookmarkStart.setName(text);
}
 
Example #11
Source File: XWPFParagraphWrapper.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public CTBookmark insertNewBookmark(XWPFRun run) {
    int pos = getPosOfRun(run);

    CTMarkupRange end = insertNewBookmarkEnd(pos + 1);
    CTBookmark start = insertNewBookmarkStart(pos);
    start.setId(BigInteger.valueOf(500 + new Random().nextInt(50000)));
    end.setId(start.getId());
    return start;
}
 
Example #12
Source File: BookMarks.java    From frpMgr with MIT License 4 votes vote down vote up
/**
 * 解析表格中的标签
 * @param paragraphList   传入的段落列表
 * @param tableRow   对应的表格行对象 
 */
@SuppressWarnings("unused")
private void procParaList(List<XWPFParagraph> paragraphList, XWPFTableRow tableRow) {

	NamedNodeMap attributes = null;
	Node colFirstNode = null;
	Node colLastNode = null;
	int firstColIndex = 0;
	int lastColIndex = 0;

	//循环判断,解析段落中的标签
	for (XWPFParagraph paragraph : paragraphList) {
		//得到段落中的标签标记
		List<CTBookmark> bookmarkList = paragraph.getCTP().getBookmarkStartList();

		for (CTBookmark bookmark : bookmarkList) {
			// With a bookmark in hand, test to see if the bookmarkStart tag 
			// has w:colFirst or w:colLast attributes. If it does, we are 
			// dealing with a bookmarked table cell. This will need to be 
			// handled differnetly - I think by an different concrete class 
			// that implements the Bookmark interface!! 
			attributes = bookmark.getDomNode().getAttributes();
			if (attributes != null) {

				// Get the colFirst and colLast attributes. If both - for 
				// now - are found, then we are dealing with a bookmarked 
				// cell. 
				colFirstNode = attributes.getNamedItem("w:colFirst");
				colLastNode = attributes.getNamedItem("w:colLast");
				if (colFirstNode != null && colLastNode != null) {

					// Get the index of the cell (or cells later) from them. 
					// First convefrt the String values both return to primitive 
					// int value. TO DO, what happens if there is a 
					// NumberFormatException. 
					firstColIndex = Integer.parseInt(colFirstNode.getNodeValue());
					lastColIndex = Integer.parseInt(colLastNode.getNodeValue());
					// if the indices are equal, then we are dealing with a# 
					// cell and can create the bookmark for it. 
					if (firstColIndex == lastColIndex) {
						this._bookmarks.put(bookmark.getName(), new BookMark(bookmark, paragraph, tableRow.getCell(firstColIndex)));
					} else {
						System.out.println("This bookmark " + bookmark.getName() + " identifies a number of cells in the "
								+ "table. That condition is not handled yet.");
					}
				} else {
					this._bookmarks.put(bookmark.getName(), new BookMark(bookmark, paragraph, tableRow.getCell(1)));
				}
			} else {
				this._bookmarks.put(bookmark.getName(), new BookMark(bookmark, paragraph, tableRow.getCell(1)));
			}
		}
	}
}
 
Example #13
Source File: BookMark.java    From frpMgr with MIT License 2 votes vote down vote up
/**
 * 构造函数,用于表格中的标签
 * @param ctBookmark
 * @param para
 * @param tableCell
 */
public BookMark(CTBookmark ctBookmark, XWPFParagraph para, XWPFTableCell tableCell) {
	this(ctBookmark, para);
	this._tableCell = tableCell;
	this._isCell = true;
}
 
Example #14
Source File: BookmarkManager.java    From M2Doc with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Inserts a reference to the given {@link CTBookmark} with the given text in the given {@link XWPFParagraph}.
 * 
 * @param paragraph
 *            the {@link XWPFParagraph}
 * @param bookmark
 *            the {@link CTBookmark}
 * @param text
 *            the text
 */
private void insertReference(XWPFParagraph paragraph, CTBookmark bookmark, String text) {
    final String name = bookmark.getName();
    final CTText pendingCTText = insertPendingReference(paragraph, name, text);
    pendingCTText.setStringValue(String.format(REF_TAG, name));
}