Example usage for org.apache.poi.hwpf.usermodel Paragraph getList

List of usage examples for org.apache.poi.hwpf.usermodel Paragraph getList

Introduction

In this page you can find the example usage for org.apache.poi.hwpf.usermodel Paragraph getList.

Prototype

public HWPFList getList() 

Source Link

Usage

From source file:org.apache.tika.parser.microsoft.ListManager.java

License:Apache License

/**
 * Get the formatted number for a given paragraph
 * <p/>/*from  ww  w  . j  av  a 2 s. com*/
 * <p><em>Note:</em> This only works correctly if called subsequently for <em>all</em> paragraphs in a valid selection (main document, text field, ...) which are part of a list.</p>
 *
 * @param paragraph list paragraph to process
 * @return String which represents the numbering of this list paragraph; never {@code null}, can be empty string, though, 
 *        if something goes wrong in getList()
 * @throws IllegalArgumentException If the given paragraph is {@code null} or is not part of a list
 */
public String getFormattedNumber(final Paragraph paragraph) {
    if (paragraph == null)
        throw new IllegalArgumentException("Given paragraph cannot be null.");
    if (!paragraph.isInList())
        throw new IllegalArgumentException("Can only process list paragraphs.");
    //lsid is equivalent to docx's abnum
    //ilfo is equivalent to docx's num
    int currAbNumId = -1;
    try {
        currAbNumId = paragraph.getList().getLsid();
    } catch (NoSuchElementException e) {
        //somewhat frequent exception when initializing HWPFList
        return "";
    } catch (IllegalArgumentException e) {
        return "";
    } catch (NullPointerException e) {
        return "";
    }

    int currNumId = paragraph.getIlfo();
    ParagraphLevelCounter lc = listLevelMap.get(currAbNumId);
    LevelTuple[] overrideTuples = overrideTupleMap.get(currNumId);

    if (lc == null) {
        ListData listData = listTables.getListData(paragraph.getList().getLsid());
        LevelTuple[] levelTuples = new LevelTuple[listData.getLevels().length];
        for (int i = 0; i < listData.getLevels().length; i++) {
            levelTuples[i] = buildTuple(i, listData.getLevels()[i]);
        }
        lc = new ParagraphLevelCounter(levelTuples);
    }
    if (overrideTuples == null) {
        overrideTuples = buildOverrideTuples(paragraph, lc.getNumberOfLevels());
    }
    String formattedString = lc.incrementLevel(paragraph.getIlvl(), overrideTuples);

    listLevelMap.put(currAbNumId, lc);
    overrideTupleMap.put(currNumId, overrideTuples);
    return formattedString;
}