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

The following examples show how to use org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText. 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
/**
 * 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 #2
Source File: BookmarkManager.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Marks dangling references.
 * 
 * @param result
 *            the {@link GenerationResult}
 * @return <code>true</code> if any dangling reference was found, <code>false</code> otherwise
 */
public boolean markDanglingReferences(GenerationResult result) {
    final boolean res = !pendingReferences.isEmpty();

    if (res) {
        for (Entry<String, Set<CTText>> entry : pendingReferences.entrySet()) {
            for (CTText ref : entry.getValue()) {
                final XWPFRun refRun = messagePositions.remove(ref);
                result.addMessage(M2DocUtils.insertMessageAfter(refRun, ValidationMessageLevel.ERROR,
                        "dangling reference for bookmark " + entry.getKey()));
            }
        }
    }

    return res;
}
 
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
/** 
 * Iterates through all and any children of the Node whose reference will be 
 * passed as an argument to the node parameter, and recover the contents of 
 * any text nodes. Testing revealed that a node can be called a text node 
 * and yet report it's type as being something different, an element node 
 * for example. Calling the getNodeValue() method on a text node will return 
 * the text the node encapsulates but doing the same on an element node will 
 * not. In fact, the call will simply return a null value. As a result, this 
 * method will test the nodes name to catch all text nodes - those whose 
 * name is to 'w:t' and then it's type. If the type is reported to be a text 
 * node, it is a trivial task to get at it's contents. However, if the type 
 * is not reported as a text type, then it is necessary to parse the raw XML 
 * markup for the node to recover it's value. 
 * 
 * @param node An instance of the Node class that encapsulates a reference 
 * to a node recovered from the document being processed. It should be 
 * passed a reference to a character run - 'w:r' - node. 
 * @return An instance of the String class that encapsulates the text 
 * recovered from the nodes children, if they are text nodes. 
 * @throws XmlException Thrown if a problem is encountered parsing the XML 
 * markup recovered from the document in order to construct the CTText 
 * instance which may be required to obtain the bookmarks text. 
 */
private String getTextFromChildNodes(Node node) throws XmlException {
	NodeList childNodes = null;
	Node childNode = null;
	CTText text = null;
	StringBuilder builder = new StringBuilder();
	int numChildNodes = 0;

	// Get a list of chid nodes from the node passed to the method and 
	// find out how many children there are in the list. 
	childNodes = node.getChildNodes();
	numChildNodes = childNodes.getLength();

	// Iterate through the children one at a time - it is possible for a 
	// run to ciontain zero, one or more text nodes - and recover the text 
	// from an text type child nodes. 
	for (int i = 0; i < numChildNodes; i++) {

		// Get a node and check it's name. If this is 'w:t' then process as 
		// text type node. 
		childNode = childNodes.item(i);

		if (childNode.getNodeName().equals(BookMark.TEXT_NODE_NAME)) {

			// If the node reports it's type as txet, then simply call the 
			// getNodeValue() method to get at it's text. 
			if (childNode.getNodeType() == Node.TEXT_NODE) {
				builder.append(childNode.getNodeValue());
			} else {
				// Correct the type by parsing the node's XML markup and 
				// creating a CTText object. Call the getStringValue() 
				// method on that to get the text. 
				text = CTText.Factory.parse(childNode);
				builder.append(text.getStringValue());
			}
		}
	}
	return (builder.toString());
}
 
Example #6
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 #7
Source File: BookmarkManager.java    From M2Doc with Eclipse Public License 1.0 5 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 name
 *            the bookmark name
 * @param text
 *            the text
 * @return the {@link CTText} corresponding to the reference.
 */
private CTText insertPendingReference(XWPFParagraph paragraph, String name, String text) {
    final byte[] id = getReferenceID(name);
    final XWPFRun beginRun = paragraph.createRun();
    beginRun.getCTR().setRsidR(id);
    beginRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.BEGIN);

    final XWPFRun preservedRun = paragraph.createRun();
    preservedRun.getCTR().setRsidR(id);
    final CTText pgcttext = preservedRun.getCTR().addNewInstrText();
    pgcttext.setSpace(Space.PRESERVE);

    final XWPFRun separateRun = paragraph.createRun();
    separateRun.getCTR().setRsidR(id);
    separateRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.SEPARATE);

    final XWPFRun textRun = paragraph.createRun();
    textRun.getCTR().setRsidR(id);
    textRun.getCTR().addNewRPr().addNewNoProof();
    textRun.setText(text);
    textRun.setBold(true);

    final XWPFRun endRun = paragraph.createRun();
    endRun.getCTR().setRsidR(id);
    endRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.END);

    return pgcttext;
}
 
Example #8
Source File: FieldUtils.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * reads up the instruction of a field's run.
 * 
 * @param run
 *            the run to read.
 * @return the aggregated instruction text of the run
 */
public static StringBuilder readUpInstrText(XWPFRun run) {
    List<CTText> texts = run.getCTR().getInstrTextList();
    StringBuilder runBuilder = new StringBuilder();
    for (CTText text : texts) {
        runBuilder.append(text.getStringValue());
    }
    return runBuilder;
}
 
Example #9
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));
}