range.java

来自「EXCEL read and write」· Java 代码 · 共 1,078 行 · 第 1/3 页

JAVA
1,078
字号
   *   * @param pPlaceHolder    The text to be replaced (e.g., "${organization}")   * @param pValue          The replacement text (e.g., "Apache Software Foundation")   * @param pOffset         The offset or index where the text to be replaced begins   *                        (relative to/within this <code>Range</code>)   */  public void replaceText(String pPlaceHolder, String pValue, int pOffset)  {	int absPlaceHolderIndex = getStartOffset() + pOffset;    Range subRange = new Range(                absPlaceHolderIndex, 				(absPlaceHolderIndex + pPlaceHolder.length()), getDocument()    );	// this Range isn't a proper parent of the subRange() so we'll have to keep	// track of an updated endOffset on our own	int previousEndOffset = subRange.getEndOffset();    subRange.insertBefore(pValue);	if (subRange.getEndOffset() != previousEndOffset)		_end += (subRange.getEndOffset() - previousEndOffset);    // re-create the sub-range so we can delete it    subRange = new Range(            (absPlaceHolderIndex + pValue.length()),            (absPlaceHolderIndex + pPlaceHolder.length() + pValue.length()), 			getDocument()    );	// deletes are automagically propagated    subRange.delete();  }  /**   * Replace (all instances of) a piece of text with another...   *   * @param pPlaceHolder    The text to be replaced (e.g., "${organization}")   * @param pValue          The replacement text (e.g., "Apache Software Foundation")   */  public void replaceText(String pPlaceHolder, String pValue)  {		boolean keepLooking = true;		while (keepLooking) {			String text = text();			int offset = text.indexOf(pPlaceHolder);			if (offset >= 0)				replaceText(pPlaceHolder, pValue, offset);			else				keepLooking = false;		}  }  /**   * Gets the character run at index. The index is relative to this range.   *   * @param index The index of the character run to get.   * @return The character run at the specified index in this range.   */  public CharacterRun getCharacterRun(int index)  {    initCharacterRuns();    CHPX chpx = (CHPX)_characters.get(index + _charStart);    int[] point = findRange(_paragraphs, _parStart, Math.max(chpx.getStart(), _start),                              chpx.getEnd());    PAPX papx = (PAPX)_paragraphs.get(point[0]);    short istd = papx.getIstd();    CharacterRun chp = new CharacterRun(chpx, _doc.getStyleSheet(), istd, this);    return chp;  }  /**   * Gets the section at index. The index is relative to this range.   *   * @param index The index of the section to get.   * @return The section at the specified index in this range.   */  public Section getSection(int index)  {    initSections();    SEPX sepx = (SEPX)_sections.get(index + _sectionStart);    Section sep = new Section(sepx, this);    return sep;  }  /**   * Gets the paragraph at index. The index is relative to this range.   *   * @param index The index of the paragraph to get.   * @return The paragraph at the specified index in this range.   */  public Paragraph getParagraph(int index)  {    initParagraphs();    PAPX papx = (PAPX)_paragraphs.get(index + _parStart);    ParagraphProperties props = papx.getParagraphProperties(_doc.getStyleSheet());    Paragraph pap = null;    if (props.getIlfo() > 0)    {      pap = new ListEntry(papx, this, _doc.getListTables());    }    else    {      pap = new Paragraph(papx, this);    }    return pap;  }  /**   * This method is used to determine the type. Handy for switch statements   * compared to the instanceof operator.   *   * @return A TYPE constant.   */  public int type()  {    return TYPE_UNDEFINED;  }  /**   * Gets the table that starts with paragraph. In a Word file, a table consists   * of a group of paragraphs with certain flags set.   *   * @param paragraph The paragraph that is the first paragraph in the table.   * @return The table that starts with paragraph   */  public Table getTable(Paragraph paragraph)  {    if (!paragraph.isInTable())    {      throw new IllegalArgumentException("This paragraph doesn't belong to a table");    }    Range r = (Range)paragraph;    if (r._parent.get() != this)    {      throw new IllegalArgumentException("This paragraph is not a child of this range");    }    r.initAll();    int tableEnd = r._parEnd;    if (r._parStart != 0 && getParagraph(r._parStart - 1).isInTable()        && getParagraph(r._parStart - 1)._sectionEnd >= r._sectionStart)    {      throw new IllegalArgumentException("This paragraph is not the first one in the table");    }    int limit = _paragraphs.size();    for (; tableEnd < limit; tableEnd++)    {       if (!getParagraph(tableEnd).isInTable())      {        break;      }    }    initAll();    if (tableEnd > _parEnd)    {      throw new ArrayIndexOutOfBoundsException("The table's bounds fall outside of this Range");    }    if (tableEnd < 0)    {      throw new ArrayIndexOutOfBoundsException("The table's end is negative, which isn't allowed!");    }    return new Table(r._parStart, tableEnd, r._doc.getRange(), paragraph.getTableLevel());  }  /**   * loads all of the list indexes.   */  protected void initAll()  {    initText();    initCharacterRuns();    initParagraphs();    initSections();  }  /**   * inits the paragraph list indexes.   */  private void initParagraphs()  {    if (!_parRangeFound)    {      int[] point = findRange(_paragraphs, _parStart, _start, _end);      _parStart = point[0];      _parEnd = point[1];      _parRangeFound = true;    }  }  /**   * inits the character run list indexes.   */  private void initCharacterRuns()  {    if (!_charRangeFound)    {      int[] point = findRange(_characters, _charStart, _start, _end);      _charStart = point[0];      _charEnd = point[1];      _charRangeFound = true;    }  }  /**   * inits the text piece list indexes.   */  private void initText()  {    if (!_textRangeFound)    {      int[] point = findRange(_text, _textStart, _start, _end);      _textStart = point[0];      _textEnd = point[1];      _textRangeFound = true;    }  }  /**   * inits the section list indexes.   */  private void initSections()  {    if (!_sectionRangeFound)    {      int[] point = findRange(_sections, _sectionStart, _start, _end);      _sectionStart = point[0];      _sectionEnd = point[1];      _sectionRangeFound = true;    }  }  /**   * Used to find the list indexes of a particular property.   *   * @param rpl A list of property nodes.   * @param min A hint on where to start looking.   * @param start The starting character offset.   * @param end The ending character offset.   * @return An int array of length 2. The first int is the start index and the   *         second int is the end index.   */  private int[] findRange(List rpl, int min, int start, int end)  {    int x = min;    PropertyNode node = (PropertyNode)rpl.get(x);    while(node.getEnd() <= start && x < rpl.size()-1)    {      x++;      node = (PropertyNode)rpl.get(x);    }    int y = x;    node = (PropertyNode)rpl.get(y);    while(node.getEnd() < end && y < rpl.size()-1)    {      y++;      node = (PropertyNode)rpl.get(y);    }    return new int[]{x, y + 1};  }  /**   * resets the list indexes.   */  private void reset()  {    _textRangeFound = false;    _charRangeFound = false;    _parRangeFound = false;    _sectionRangeFound = false;  }  /**   * Adjust the value of the various FIB character count fields,   *  eg <code>FIB.CCPText</code> after an insert or a delete...   *     * Works on all CCP fields from this range onwards   *   * @param	adjustment	The (signed) value that should be added to the FIB CCP fields   */  protected void adjustFIB(int adjustment) {	// update the FIB.CCPText field (this should happen once per adjustment, so we don't want it in	// adjustForInsert() or it would get updated multiple times if the range has a parent)	// without this, OpenOffice.org (v. 2.2.x) does not see all the text in the document	CPSplitCalculator cpS = _doc.getCPSplitCalculator();	FileInformationBlock fib = _doc.getFileInformationBlock();		// Do for each affected part	if(_start < cpS.getMainDocumentEnd()) {		fib.setCcpText(fib.getCcpText() + adjustment);	}		if(_start < cpS.getCommentsEnd()) {		fib.setCcpAtn(fib.getCcpAtn() + adjustment);	}	if(_start < cpS.getEndNoteEnd()) {		fib.setCcpEdn(fib.getCcpEdn() + adjustment);	}	if(_start < cpS.getFootnoteEnd()) {		fib.setCcpFtn(fib.getCcpFtn() + adjustment);	}	if(_start < cpS.getHeaderStoryEnd()) {		fib.setCcpHdd(fib.getCcpHdd() + adjustment);	}	if(_start < cpS.getHeaderTextboxEnd()) {		fib.setCcpHdrTxtBx(fib.getCcpHdrTxtBx() + adjustment);	}	if(_start < cpS.getMainTextboxEnd()) {		fib.setCcpTxtBx(fib.getCcpTxtBx() + adjustment);	}  }  /**   * adjust this range after an insert happens.   * @param length the length to adjust for (expected to be a count of code-points, not necessarily chars)   */  private void adjustForInsert(int length)  {    _end += length;    reset();    Range parent = (Range)_parent.get();    if (parent != null)    {      parent.adjustForInsert(length);    }  }	public int getStartOffset() {		return _start;	}	public int getEndOffset() {		return _end;	}	protected HWPFDocument getDocument() {		return _doc;	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?