Example usage for org.apache.poi.xwpf.usermodel PositionInParagraph getRun

List of usage examples for org.apache.poi.xwpf.usermodel PositionInParagraph getRun

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel PositionInParagraph getRun.

Prototype

public int getRun() 

Source Link

Usage

From source file:apachepoitest.XWPFParagraphClone.java

License:Apache License

/**
 * this methods parse the paragraph and search for the string searched.
 * If it finds the string, it will return true and the position of the String
 * will be saved in the parameter startPos.
 * @param searched//from   ww w .j a v a2  s.  c  o m
 * @param startPos
 */
public TextSegement searchText(String searched, PositionInParagraph startPos) {
    int startRun = startPos.getRun(), startText = startPos.getText(), startChar = startPos.getChar();
    int beginRunPos = 0, candCharPos = 0;
    boolean newList = false;
    CTR[] rArray = paragraph.getRArray();
    for (int runPos = startRun; runPos < rArray.length; runPos++) {
        int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos = 0;
        CTR ctRun = rArray[runPos];
        XmlCursor c = ctRun.newCursor();
        c.selectPath("./*");
        while (c.toNextSelection()) {
            XmlObject o = c.getObject();
            if (o instanceof CTText) {
                if (textPos >= startText) {
                    String candidate = ((CTText) o).getStringValue();
                    if (runPos == startRun)
                        charPos = startChar;
                    else
                        charPos = 0;

                    for (; charPos < candidate.length(); charPos++) {
                        if ((candidate.charAt(charPos) == searched.charAt(0)) && (candCharPos == 0)) {
                            beginTextPos = textPos;
                            beginCharPos = charPos;
                            beginRunPos = runPos;
                            newList = true;
                        }
                        if (candidate.charAt(charPos) == searched.charAt(candCharPos)) {
                            if (candCharPos + 1 < searched.length())
                                candCharPos++;
                            else if (newList) {
                                TextSegement segement = new TextSegement();
                                segement.setBeginRun(beginRunPos);
                                segement.setBeginText(beginTextPos);
                                segement.setBeginChar(beginCharPos);
                                segement.setEndRun(runPos);
                                segement.setEndText(textPos);
                                segement.setEndChar(charPos);
                                return segement;
                            }
                        } else {
                            candCharPos = 0;
                        }
                    }
                }
                textPos++;
            } else if (o instanceof CTProofErr) {
                c.removeXml();
            } else if (o instanceof CTRPr)
                ;
            //do nothing
            else
                candCharPos = 0;
        }

        c.dispose();
    }
    return null;
}