You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Range.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /* ====================================================================
  2. Copyright 2002-2004 Apache Software Foundation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ==================================================================== */
  13. package org.apache.poi.hwpf.usermodel;
  14. import org.apache.poi.util.LittleEndian;
  15. import org.apache.poi.hwpf.HWPFDocument;
  16. import org.apache.poi.hwpf.usermodel.CharacterRun;
  17. import org.apache.poi.hwpf.usermodel.Paragraph;
  18. import org.apache.poi.hwpf.usermodel.ParagraphProperties;
  19. import org.apache.poi.hwpf.usermodel.Section;
  20. import org.apache.poi.hwpf.model.PropertyNode;
  21. import org.apache.poi.hwpf.model.StyleSheet;
  22. import org.apache.poi.hwpf.model.StyleDescription;
  23. import org.apache.poi.hwpf.model.CHPBinTable;
  24. import org.apache.poi.hwpf.model.CHPX;
  25. import org.apache.poi.hwpf.model.PAPX;
  26. import org.apache.poi.hwpf.model.SEPX;
  27. import org.apache.poi.hwpf.model.PAPBinTable;
  28. import org.apache.poi.hwpf.model.SectionTable;
  29. import org.apache.poi.hwpf.model.TextPieceTable;
  30. import org.apache.poi.hwpf.model.TextPiece;
  31. import org.apache.poi.hwpf.model.ListTables;
  32. import org.apache.poi.hwpf.sprm.CharacterSprmUncompressor;
  33. import org.apache.poi.hwpf.sprm.CharacterSprmCompressor;
  34. import org.apache.poi.hwpf.sprm.SectionSprmUncompressor;
  35. import org.apache.poi.hwpf.sprm.ParagraphSprmUncompressor;
  36. import org.apache.poi.hwpf.sprm.ParagraphSprmCompressor;
  37. import org.apache.poi.hwpf.sprm.SprmBuffer;
  38. import java.util.List;
  39. import java.util.ArrayList;
  40. import java.util.Iterator;
  41. import java.util.NoSuchElementException;
  42. import java.io.UnsupportedEncodingException;
  43. import java.lang.ref.WeakReference;
  44. /**
  45. * This class is the central class of the HWPF object model. All properties
  46. * that apply to a range of characters in a Word document extend this class.
  47. *
  48. * It is possible to insert text and/or properties at the beginning or end of a
  49. * range.
  50. *
  51. * Ranges are only valid if there hasn't been an insert in a prior Range since
  52. * the Range's creation. Once an element (text, paragraph, etc.) has been
  53. * inserted into a Range, subsequent Ranges become unstable.
  54. *
  55. * @author Ryan Ackley
  56. */
  57. public class Range
  58. {
  59. public static final int TYPE_PARAGRAPH = 0;
  60. public static final int TYPE_CHARACTER= 1;
  61. public static final int TYPE_SECTION = 2;
  62. public static final int TYPE_TEXT = 3;
  63. public static final int TYPE_LISTENTRY = 4;
  64. public static final int TYPE_TABLE = 5;
  65. public static final int TYPE_UNDEFINED = 6;
  66. /** Needed so inserts and deletes will ripple up through containing Ranges */
  67. private WeakReference _parent;
  68. /** The starting character offset of this range.*/
  69. protected int _start;
  70. /** The ending character offset of this range.*/
  71. protected int _end;
  72. /** The document this range blongs to.*/
  73. protected HWPFDocument _doc;
  74. /** Have we loaded the section indexes yet*/
  75. boolean _sectionRangeFound;
  76. /** All sections that belong to the document this Range belongs to.*/
  77. protected List _sections;
  78. /** The start index in the sections list for this Range*/
  79. protected int _sectionStart;
  80. /** The end index in the sections list for this Range.*/
  81. protected int _sectionEnd;
  82. /** Have we loaded the paragraph indexes yet.*/
  83. protected boolean _parRangeFound;
  84. /** All paragraphs that belong to the document this Range belongs to.*/
  85. protected List _paragraphs;
  86. /** The start index in the paragraphs list for this Range*/
  87. protected int _parStart;
  88. /** The end index in the paragraphs list for this Range.*/
  89. protected int _parEnd;
  90. /** Have we loaded the characterRun indexes yet.*/
  91. protected boolean _charRangeFound;
  92. /** All CharacterRuns that belong to the document this Range belongs to.*/
  93. protected List _characters;
  94. /** The start index in the characterRuns list for this Range*/
  95. protected int _charStart;
  96. /** The end index in the characterRuns list for this Range. */
  97. protected int _charEnd;
  98. /** Have we loaded the Text indexes yet*/
  99. protected boolean _textRangeFound;
  100. /** All text pieces that belong to the document this Range belongs to.*/
  101. protected List _text;
  102. /** The start index in the text list for this Range.*/
  103. protected int _textStart;
  104. /** The end index in the text list for this Range.*/
  105. protected int _textEnd;
  106. // protected Range()
  107. // {
  108. //
  109. // }
  110. /**
  111. * Used to construct a Range from a document. This is generally used to
  112. * create a Range that spans the whole document.
  113. *
  114. * @param start Starting character offset of the range.
  115. * @param end Ending character offset of the range.
  116. * @param doc The HWPFDocument the range is based on.
  117. */
  118. public Range(int start, int end, HWPFDocument doc)
  119. {
  120. _start = start;
  121. _end = end;
  122. _doc = doc;
  123. _sections = _doc.getSectionTable().getSections();
  124. _paragraphs = _doc.getParagraphTable().getParagraphs();
  125. _characters = _doc.getCharacterTable().getTextRuns();
  126. _text = _doc.getTextTable().getTextPieces();
  127. _parent = new WeakReference(null);
  128. }
  129. /**
  130. * Used to create Ranges that are children of other Ranges.
  131. *
  132. * @param start Starting character offset of the range.
  133. * @param end Ending character offset of the range.
  134. * @param parent The parent this range belongs to.
  135. */
  136. protected Range(int start, int end, Range parent)
  137. {
  138. _start = start;
  139. _end = end;
  140. _doc = parent._doc;
  141. _sections = parent._sections;
  142. _paragraphs = parent._paragraphs;
  143. _characters = parent._characters;
  144. _text = parent._text;
  145. _parent = new WeakReference(parent);
  146. }
  147. /**
  148. * Constructor used to build a Range from indexes in one of its internal
  149. * lists.
  150. *
  151. * @param startIdx The starting index in the list.
  152. * @param endIdx The ending index in the list.
  153. * @param idxType The list type.
  154. * @param parent The parent Range this range belongs to.
  155. */
  156. protected Range(int startIdx, int endIdx, int idxType, Range parent)
  157. {
  158. _doc = parent._doc;
  159. _sections = parent._sections;
  160. _paragraphs = parent._paragraphs;
  161. _characters = parent._characters;
  162. _text = parent._text;
  163. _parent = new WeakReference(parent);
  164. switch (idxType)
  165. {
  166. case TYPE_PARAGRAPH:
  167. _parStart = parent._parStart + startIdx;
  168. _parEnd = parent._parStart + endIdx;
  169. _start = ((PropertyNode)_paragraphs.get(_parStart)).getStart();
  170. _end = ((PropertyNode)_paragraphs.get(_parEnd)).getEnd();
  171. _parRangeFound = true;
  172. break;
  173. case TYPE_CHARACTER:
  174. _charStart = parent._charStart + startIdx;
  175. _charEnd = parent._charStart + endIdx;
  176. _start = ((PropertyNode)_characters.get(_charStart)).getStart();
  177. _end = ((PropertyNode)_characters.get(_charEnd)).getEnd();
  178. _charRangeFound = true;
  179. break;
  180. case TYPE_SECTION:
  181. _sectionStart = parent._sectionStart + startIdx;
  182. _sectionEnd = parent._sectionStart + endIdx;
  183. _start = ((PropertyNode)_sections.get(_sectionStart)).getStart();
  184. _end = ((PropertyNode)_sections.get(_sectionEnd)).getEnd();
  185. _sectionRangeFound = true;
  186. break;
  187. case TYPE_TEXT:
  188. _textStart = parent._textStart + startIdx;
  189. _textEnd = parent._textStart + endIdx;
  190. _start = ((PropertyNode)_text.get(_textStart)).getStart();
  191. _end = ((PropertyNode)_text.get(_textEnd)).getEnd();
  192. _textRangeFound = true;
  193. break;
  194. }
  195. }
  196. /**
  197. * Gets the text that this Range contains.
  198. *
  199. * @return The text for this range.
  200. */
  201. public String text()
  202. {
  203. initText();
  204. StringBuffer sb = new StringBuffer();
  205. for (int x = _textStart; x < _textEnd; x++)
  206. {
  207. TextPiece piece = (TextPiece)_text.get(x);
  208. int start = _start > piece.getStart() ? _start - piece.getStart() : 0;
  209. int end = _end <= piece.getEnd() ? _end - piece.getStart() : piece.getEnd() - piece.getStart();
  210. if(piece.usesUnicode()) // convert the byte pointers to char pointers
  211. {
  212. start/=2;
  213. end/=2;
  214. }
  215. sb.append(piece.getStringBuffer().substring(start, end));
  216. }
  217. return sb.toString();
  218. }
  219. /**
  220. * Used to get the number of sections in a range. If this range is smaller
  221. * than a section, it will return 1 for its containing section.
  222. *
  223. * @return The number of sections in this range.
  224. */
  225. public int numSections()
  226. {
  227. initSections();
  228. return _sectionEnd - _sectionStart;
  229. }
  230. /**
  231. * Used to get the number of paragraphs in a range. If this range is smaller
  232. * than a paragraph, it will return 1 for its containing paragraph.
  233. *
  234. * @return The number of paragraphs in this range.
  235. */
  236. public int numParagraphs()
  237. {
  238. initParagraphs();
  239. return _parEnd - _parStart;
  240. }
  241. /**
  242. *
  243. * @return The number of characterRuns in this range.
  244. */
  245. public int numCharacterRuns()
  246. {
  247. initCharacterRuns();
  248. return _charEnd - _charStart;
  249. }
  250. /**
  251. * Inserts text into the front of this range.
  252. *
  253. * @param text The text to insert
  254. * @return The character run that text was inserted into.
  255. */
  256. public CharacterRun insertBefore(String text)
  257. //throws UnsupportedEncodingException
  258. {
  259. initAll();
  260. TextPiece tp = (TextPiece)_text.get(_textStart);
  261. StringBuffer sb = (StringBuffer)tp.getStringBuffer();
  262. // Since this is the first item in our list, it is safe to assume that
  263. // _start >= tp.getStart()
  264. int insertIndex = _start - tp.getStart();
  265. sb.insert(insertIndex, text);
  266. int adjustedLength = _doc.getTextTable().adjustForInsert(_textStart, text.length());
  267. _doc.getCharacterTable().adjustForInsert(_charStart, adjustedLength);
  268. _doc.getParagraphTable().adjustForInsert(_parStart, adjustedLength);
  269. _doc.getSectionTable().adjustForInsert(_sectionStart, adjustedLength);
  270. adjustForInsert(text.length());
  271. return getCharacterRun(0);
  272. }
  273. /**
  274. * Inserts text onto the end of this range
  275. *
  276. * @param text The text to insert
  277. * @return The character run the text was inserted into.
  278. */
  279. public CharacterRun insertAfter(String text)
  280. {
  281. initAll();
  282. int listIndex = _textEnd - 1;
  283. TextPiece tp = (TextPiece)_text.get(listIndex);
  284. StringBuffer sb = (StringBuffer)tp.getStringBuffer();
  285. int insertIndex = _end - tp.getStart();
  286. if (tp.getStringBuffer().charAt(_end - 1) == '\r' && text.charAt(0) != '\u0007')
  287. {
  288. insertIndex--;
  289. }
  290. sb.insert(insertIndex, text);
  291. int adjustedLength = _doc.getTextTable().adjustForInsert(listIndex, text.length());
  292. _doc.getCharacterTable().adjustForInsert(_charEnd - 1, adjustedLength);
  293. _doc.getParagraphTable().adjustForInsert(_parEnd - 1, adjustedLength);
  294. _doc.getSectionTable().adjustForInsert(_sectionEnd - 1, adjustedLength);
  295. adjustForInsert(text.length());
  296. return getCharacterRun(numCharacterRuns() - 1);
  297. }
  298. /**
  299. * Inserts text into the front of this range and it gives that text the
  300. * CharacterProperties specified in props.
  301. *
  302. * @param text The text to insert.
  303. * @param props The CharacterProperties to give the text.
  304. * @return A new CharacterRun that has the given text and properties and is n
  305. * ow a part of the document.
  306. */
  307. public CharacterRun insertBefore(String text, CharacterProperties props)
  308. //throws UnsupportedEncodingException
  309. {
  310. initAll();
  311. PAPX papx = (PAPX)_paragraphs.get(_parStart);
  312. short istd = papx.getIstd();
  313. StyleSheet ss = _doc.getStyleSheet();
  314. CharacterProperties baseStyle = ss.getCharacterStyle(istd);
  315. byte[] grpprl = CharacterSprmCompressor.compressCharacterProperty(props, baseStyle);
  316. SprmBuffer buf = new SprmBuffer(grpprl);
  317. _doc.getCharacterTable().insert(_charStart, _start, buf);
  318. return insertBefore(text);
  319. }
  320. /**
  321. * Inserts text onto the end of this range and gives that text the
  322. * CharacterProperties specified in props.
  323. *
  324. * @param text The text to insert.
  325. * @param props The CharacterProperties to give the text.
  326. * @return A new CharacterRun that has the given text and properties and is n
  327. * ow a part of the document.
  328. */
  329. public CharacterRun insertAfter(String text, CharacterProperties props)
  330. //throws UnsupportedEncodingException
  331. {
  332. initAll();
  333. PAPX papx = (PAPX)_paragraphs.get(_parEnd - 1);
  334. short istd = papx.getIstd();
  335. StyleSheet ss = _doc.getStyleSheet();
  336. CharacterProperties baseStyle = ss.getCharacterStyle(istd);
  337. byte[] grpprl = CharacterSprmCompressor.compressCharacterProperty(props, baseStyle);
  338. SprmBuffer buf = new SprmBuffer(grpprl);
  339. _doc.getCharacterTable().insert(_charEnd, _end, buf);
  340. _charEnd++;
  341. return insertAfter(text);
  342. }
  343. /**
  344. * Inserts and empty paragraph into the front of this range.
  345. *
  346. * @param props The properties that the new paragraph will have.
  347. * @param styleIndex The index into the stylesheet for the new paragraph.
  348. * @return The newly inserted paragraph.
  349. */
  350. public Paragraph insertBefore(ParagraphProperties props, int styleIndex)
  351. //throws UnsupportedEncodingException
  352. {
  353. return this.insertBefore(props, styleIndex, "\r");
  354. }
  355. /**
  356. * Inserts a paragraph into the front of this range. The paragraph will
  357. * contain one character run that has the default properties for the
  358. * paragraph's style.
  359. *
  360. * It is necessary for the text to end with the character '\r'
  361. *
  362. * @param props The paragraph's properties.
  363. * @param styleIndex The index of the paragraph's style in the style sheet.
  364. * @param text The text to insert.
  365. * @return A newly inserted paragraph.
  366. */
  367. protected Paragraph insertBefore(ParagraphProperties props, int styleIndex, String text)
  368. //throws UnsupportedEncodingException
  369. {
  370. initAll();
  371. StyleSheet ss = _doc.getStyleSheet();
  372. ParagraphProperties baseStyle = ss.getParagraphStyle(styleIndex);
  373. CharacterProperties baseChp = ss.getCharacterStyle(styleIndex);
  374. byte[] grpprl = ParagraphSprmCompressor.compressParagraphProperty(props, baseStyle);
  375. byte[] withIndex = new byte[grpprl.length + LittleEndian.SHORT_SIZE];
  376. LittleEndian.putShort(withIndex, (short)styleIndex);
  377. System.arraycopy(grpprl, 0, withIndex, LittleEndian.SHORT_SIZE, grpprl.length);
  378. SprmBuffer buf = new SprmBuffer(withIndex);
  379. _doc.getParagraphTable().insert(_parStart, _start, buf);
  380. insertBefore(text, baseChp);
  381. return getParagraph(0);
  382. }
  383. /**
  384. * Inserts and empty paragraph into the end of this range.
  385. *
  386. * @param props The properties that the new paragraph will have.
  387. * @param styleIndex The index into the stylesheet for the new paragraph.
  388. * @return The newly inserted paragraph.
  389. */
  390. public Paragraph insertAfter(ParagraphProperties props, int styleIndex)
  391. //throws UnsupportedEncodingException
  392. {
  393. return this.insertAfter(props, styleIndex, "\r");
  394. }
  395. /**
  396. * Inserts a paragraph into the end of this range. The paragraph will
  397. * contain one character run that has the default properties for the
  398. * paragraph's style.
  399. *
  400. * It is necessary for the text to end with the character '\r'
  401. *
  402. * @param props The paragraph's properties.
  403. * @param styleIndex The index of the paragraph's style in the style sheet.
  404. * @param text The text to insert.
  405. * @return A newly inserted paragraph.
  406. */
  407. protected Paragraph insertAfter(ParagraphProperties props, int styleIndex, String text)
  408. //throws UnsupportedEncodingException
  409. {
  410. initAll();
  411. StyleSheet ss = _doc.getStyleSheet();
  412. ParagraphProperties baseStyle = ss.getParagraphStyle(styleIndex);
  413. CharacterProperties baseChp = ss.getCharacterStyle(styleIndex);
  414. byte[] grpprl = ParagraphSprmCompressor.compressParagraphProperty(props, baseStyle);
  415. byte[] withIndex = new byte[grpprl.length + LittleEndian.SHORT_SIZE];
  416. LittleEndian.putShort(withIndex, (short)styleIndex);
  417. System.arraycopy(grpprl, 0, withIndex, LittleEndian.SHORT_SIZE, grpprl.length);
  418. SprmBuffer buf = new SprmBuffer(withIndex);
  419. _doc.getParagraphTable().insert(_parEnd, _end, buf);
  420. _parEnd++;
  421. insertAfter(text, baseChp);
  422. return getParagraph(numParagraphs() - 1);
  423. }
  424. public void delete()
  425. {
  426. initAll();
  427. int numSections = _sections.size();
  428. int numRuns = _characters.size();
  429. int numParagraphs = _paragraphs.size();
  430. for (int x = _charStart; x < numRuns; x++)
  431. {
  432. CHPX chpx = (CHPX)_characters.get(x);
  433. chpx.adjustForDelete(_start, _end - _start);
  434. }
  435. for (int x = _parStart; x < numParagraphs; x++)
  436. {
  437. PAPX papx = (PAPX)_paragraphs.get(x);
  438. papx.adjustForDelete(_start, _end - _start);
  439. }
  440. for (int x = _sectionStart; x < numSections; x++)
  441. {
  442. SEPX sepx = (SEPX)_sections.get(x);
  443. sepx.adjustForDelete(_start, _end - _start);
  444. }
  445. }
  446. /**
  447. * Inserts a simple table into the beginning of this range. The number of
  448. * columns is determined by the TableProperties passed into this function.
  449. *
  450. * @param props The table properties for the table.
  451. * @param rows The number of rows.
  452. * @return The empty Table that is now part of the document.
  453. */
  454. public Table insertBefore(TableProperties props, int rows)
  455. {
  456. ParagraphProperties parProps = new ParagraphProperties();
  457. parProps.setFInTable((byte)1);
  458. parProps.setTableLevel((byte)1);
  459. int columns = props.getItcMac();
  460. for (int x = 0; x < rows; x++)
  461. {
  462. Paragraph cell = this.insertBefore(parProps, StyleSheet.NIL_STYLE);
  463. cell.insertAfter(String.valueOf('\u0007'));
  464. for(int y = 1; y < columns; y++)
  465. {
  466. cell = cell.insertAfter(parProps, StyleSheet.NIL_STYLE);
  467. cell.insertAfter(String.valueOf('\u0007'));
  468. }
  469. cell = cell.insertAfter(parProps, StyleSheet.NIL_STYLE, String.valueOf('\u0007'));
  470. cell.setTableRowEnd(props);
  471. }
  472. return new Table(_start, _start + (rows * (columns + 1)), this, 1);
  473. }
  474. /**
  475. * Inserts a list into the beginning of this range.
  476. *
  477. * @param props The properties of the list entry. All list entries are
  478. * paragraphs.
  479. * @param listID The id of the list that contains the properties.
  480. * @param level The indentation level of the list.
  481. * @param styleIndex The base style's index in the stylesheet.
  482. * @return The empty ListEntry that is now part of the document.
  483. */
  484. public ListEntry insertBefore(ParagraphProperties props, int listID, int level, int styleIndex)
  485. {
  486. ListTables lt = _doc.getListTables();
  487. if (lt.getLevel(listID, level) == null)
  488. {
  489. throw new NoSuchElementException("The specified list and level do not exist");
  490. }
  491. int ilfo = lt.getOverrideIndexFromListID(listID);
  492. props.setIlfo(ilfo);
  493. props.setIlvl((byte)level);
  494. return (ListEntry)insertBefore(props, styleIndex);
  495. }
  496. /**
  497. * Inserts a list into the beginning of this range.
  498. *
  499. * @param props The properties of the list entry. All list entries are
  500. * paragraphs.
  501. * @param listID The id of the list that contains the properties.
  502. * @param level The indentation level of the list.
  503. * @param styleIndex The base style's index in the stylesheet.
  504. * @return The empty ListEntry that is now part of the document.
  505. */
  506. public ListEntry insertAfter(ParagraphProperties props, int listID, int level, int styleIndex)
  507. {
  508. ListTables lt = _doc.getListTables();
  509. if (lt.getLevel(listID, level) == null)
  510. {
  511. throw new NoSuchElementException("The specified list and level do not exist");
  512. }
  513. int ilfo = lt.getOverrideIndexFromListID(listID);
  514. props.setIlfo(ilfo);
  515. props.setIlvl((byte)level);
  516. return (ListEntry)insertAfter(props, styleIndex);
  517. }
  518. /**
  519. * Gets the character run at index. The index is relative to this range.
  520. *
  521. * @param index The index of the character run to get.
  522. * @return The character run at the specified index in this range.
  523. */
  524. public CharacterRun getCharacterRun(int index)
  525. {
  526. initCharacterRuns();
  527. CHPX chpx = (CHPX)_characters.get(index + _charStart);
  528. int[] point = findRange(_paragraphs, _parStart, Math.max(chpx.getStart(), _start),
  529. chpx.getEnd());
  530. PAPX papx = (PAPX)_paragraphs.get(point[0]);
  531. short istd = papx.getIstd();
  532. CharacterRun chp = new CharacterRun(chpx, _doc.getStyleSheet(), istd, this);
  533. return chp;
  534. }
  535. /**
  536. * Gets the section at index. The index is relative to this range.
  537. *
  538. * @param index The index of the section to get.
  539. * @return The section at the specified index in this range.
  540. */
  541. public Section getSection(int index)
  542. {
  543. initSections();
  544. SEPX sepx = (SEPX)_sections.get(index + _sectionStart);
  545. Section sep = new Section(sepx, this);
  546. return sep;
  547. }
  548. /**
  549. * Gets the paragraph at index. The index is relative to this range.
  550. *
  551. * @param index The index of the paragraph to get.
  552. * @return The paragraph at the specified index in this range.
  553. */
  554. public Paragraph getParagraph(int index)
  555. {
  556. initParagraphs();
  557. PAPX papx = (PAPX)_paragraphs.get(index + _parStart);
  558. ParagraphProperties props = papx.getParagraphProperties(_doc.getStyleSheet());
  559. Paragraph pap = null;
  560. if (props.getIlfo() > 0)
  561. {
  562. pap = new ListEntry(papx, this, _doc.getListTables());
  563. }
  564. else
  565. {
  566. pap = new Paragraph(papx, this);
  567. }
  568. return pap;
  569. }
  570. /**
  571. * This method is used to determine the type. Handy for switch statements
  572. * compared to the instanceof operator.
  573. *
  574. * @return A TYPE constant.
  575. */
  576. public int type()
  577. {
  578. return TYPE_UNDEFINED;
  579. }
  580. /**
  581. * Gets the table that starts with paragraph. In a Word file, a table consists
  582. * of a group of paragraphs with certain flags set.
  583. *
  584. * @param paragraph The paragraph that is the first paragraph in the table.
  585. * @return The table that starts with paragraph
  586. */
  587. public Table getTable(Paragraph paragraph)
  588. {
  589. if (!paragraph.isInTable())
  590. {
  591. throw new IllegalArgumentException("This paragraph doesn't belong to a table");
  592. }
  593. Range r = (Range)paragraph;
  594. if (r._parent.get() != this)
  595. {
  596. throw new IllegalArgumentException("This paragraph is not a child of this range");
  597. }
  598. r.initAll();
  599. int tableEnd = r._parEnd;
  600. if (r._parStart != 0 && getParagraph(r._parStart - 1).isInTable()
  601. && getParagraph(r._parStart - 1)._sectionEnd >= r._sectionStart)
  602. {
  603. throw new IllegalArgumentException("This paragraph is not the first one in the table");
  604. }
  605. int limit = _paragraphs.size();
  606. for (; tableEnd < limit; tableEnd++)
  607. {
  608. if (!getParagraph(tableEnd).isInTable())
  609. {
  610. break;
  611. }
  612. }
  613. initAll();
  614. if (tableEnd > _parEnd)
  615. {
  616. throw new ArrayIndexOutOfBoundsException("The table's bounds fall outside of this Range");
  617. }
  618. return new Table(r._parStart, tableEnd, r._doc.getRange(), paragraph.getTableLevel());
  619. }
  620. /**
  621. * loads all of the list indexes.
  622. */
  623. protected void initAll()
  624. {
  625. initText();
  626. initCharacterRuns();
  627. initParagraphs();
  628. initSections();
  629. }
  630. /**
  631. * inits the paragraph list indexes.
  632. */
  633. private void initParagraphs()
  634. {
  635. if (!_parRangeFound)
  636. {
  637. int[] point = findRange(_paragraphs, _parStart, _start, _end);
  638. _parStart = point[0];
  639. _parEnd = point[1];
  640. _parRangeFound = true;
  641. }
  642. }
  643. /**
  644. * inits the character run list indexes.
  645. */
  646. private void initCharacterRuns()
  647. {
  648. if (!_charRangeFound)
  649. {
  650. int[] point = findRange(_characters, _charStart, _start, _end);
  651. _charStart = point[0];
  652. _charEnd = point[1];
  653. _charRangeFound = true;
  654. }
  655. }
  656. /**
  657. * inits the text piece list indexes.
  658. */
  659. private void initText()
  660. {
  661. if (!_textRangeFound)
  662. {
  663. int[] point = findRange(_text, _textStart, _start, _end);
  664. _textStart = point[0];
  665. _textEnd = point[1];
  666. _textRangeFound = true;
  667. }
  668. }
  669. /**
  670. * inits the section list indexes.
  671. */
  672. private void initSections()
  673. {
  674. if (!_sectionRangeFound)
  675. {
  676. int[] point = findRange(_sections, _sectionStart, _start, _end);
  677. _sectionStart = point[0];
  678. _sectionEnd = point[1];
  679. _sectionRangeFound = true;
  680. }
  681. }
  682. /**
  683. * Used to find the list indexes of a particular property.
  684. *
  685. * @param rpl A list of property nodes.
  686. * @param min A hint on where to start looking.
  687. * @param start The starting character offset.
  688. * @param end The ending character offset.
  689. * @return An int array of length 2. The first int is the start index and the
  690. * second int is the end index.
  691. */
  692. private int[] findRange(List rpl, int min, int start, int end)
  693. {
  694. int x = min;
  695. PropertyNode node = (PropertyNode)rpl.get(x);
  696. while(node.getEnd() <= start && x < rpl.size()-1)
  697. {
  698. x++;
  699. node = (PropertyNode)rpl.get(x);
  700. }
  701. int y = x;
  702. node = (PropertyNode)rpl.get(y);
  703. while(node.getEnd() < end && y < rpl.size()-1)
  704. {
  705. y++;
  706. node = (PropertyNode)rpl.get(y);
  707. }
  708. return new int[]{x, y + 1};
  709. }
  710. /**
  711. * resets the list indexes.
  712. */
  713. private void reset()
  714. {
  715. _textRangeFound = false;
  716. _charRangeFound = false;
  717. _parRangeFound = false;
  718. _sectionRangeFound = false;
  719. }
  720. /**
  721. * adjust this range after an insert happens.
  722. * @param length the length to adjust for
  723. */
  724. private void adjustForInsert(int length)
  725. {
  726. _end += length;
  727. reset();
  728. Range parent = (Range)_parent.get();
  729. if (parent != null)
  730. {
  731. parent.adjustForInsert(length);
  732. }
  733. }
  734. }