diff options
author | Yegor Kozlov <yegor@apache.org> | 2008-09-16 12:25:54 +0000 |
---|---|---|
committer | Yegor Kozlov <yegor@apache.org> | 2008-09-16 12:25:54 +0000 |
commit | 9ef957f768f37731e4aed2aa6ceffa6b862951db (patch) | |
tree | 1348475c8f34393cf8bf894fefba42ea114bca61 /src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java | |
parent | 99008de47e47386bf16568ff36be5e0b483b163f (diff) | |
download | poi-9ef957f768f37731e4aed2aa6ceffa6b862951db.tar.gz poi-9ef957f768f37731e4aed2aa6ceffa6b862951db.zip |
Preserve rich text across read-write of SharedStringsTable, also improved performance and removed obsolete ole2-specific stuff
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@695832 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java')
-rw-r--r-- | src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java | 220 |
1 files changed, 197 insertions, 23 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index 8396004d52..83f44da842 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -19,76 +19,250 @@ package org.apache.poi.xssf.usermodel; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.xssf.model.StylesTable; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt; /** - * TODO - the rich part + * Rich text unicode string. These strings can have fonts applied to arbitary parts of the string. + * + * <p> + * Most strings in a workbook have formatting applied at the cell level, that is, the entire string in the cell has the + * same formatting applied. In these cases, the formatting for the cell is stored in the styles part, + * and the string for the cell can be shared across the workbook. The following xml and code snippet illustrate the example. + * </p> + * + * <blockquote> + * <pre> + * <sst xmlns=http://schemas.openxmlformats.org/spreadsheetml/2006/5/main + * count="1" uniqueCount="1"> + * <si> + * <t>Apache POI</t> + * </si> + * </sst> + * </pre> + * </blockquote> + * + * The code to produce xml above: + * <blockquote> + * <pre> + * cell1.setCellValue(new XSSFRichTextString("Apache POI")); + * cell2.setCellValue(new XSSFRichTextString("Apache POI")); + * cell3.setCellValue(new XSSFRichTextString("Apache POI")); + * </pre> + * </blockquote> + * In the above example all three cells will use the same string cached on workbook level. + * + * <p> + * Some strings in the workbook may have formatting applied at a level that is more granular than the cell level. + * For instance, specific characters within the string may be bolded, have coloring, italicizing, etc. + * In these cases, the formatting is stored along with the text in the string table, and is treated as + * a unique entry in the workbook. The following xml and code snippet illustrate this. + * </p> + * + * <blockquote> + * <pre> + * XSSFRichTextString s1 = new XSSFRichTextString("Apache POI"); + * s1.applyFont(boldArial); + * cell1.setCellValue(s1); + * + * XSSFRichTextString s2 = new XSSFRichTextString("Apache POI"); + * s2.applyFont(italicCourier); + * cell2.setCellValue(s2); + * </pre> + * </blockquote> + * + * The code above will produce the following xml: + * <blockquote> + * <pre> + * <sst xmlns=http://schemas.openxmlformats.org/spreadsheetml/2006/5/main count="2" uniqueCount="2"> + * <si> + * <r> + * <rPr> + * <b/> + * <sz val="11"/> + * <color theme="1"/> + * <rFont val="Arial"/> + * <family val="2"/> + * <scheme val="minor"/> + * </rPr> + * <t>Apache POI</t> + * </r> + * </si> + * <si> + * <r> + * <rPr> + * <i/> + * <sz val="11"/> + * <color theme="1"/> + * <rFont val="Courier"/> + * <family val="1"/> + * <scheme val="minor"/> + * </rPr> + * <t>Apache POI</t> + * </r> + * </si> + *</sst> + * + * </pre> + * </blockquote> + * + * @author Yegor Kozlov */ public class XSSFRichTextString implements RichTextString { - private String string; - + private CTRst st; + private StylesTable styles; + public XSSFRichTextString(String str) { - this.string = str; + st = CTRst.Factory.newInstance(); + st.setT(str); + } + + public XSSFRichTextString() { + st = CTRst.Factory.newInstance(); } - + + public XSSFRichTextString(CTRst st) { + this.st = st; + } + + /** + * Applies a font to the specified characters of a string. + * + * @param startIndex The start index to apply the font to (inclusive) + * @param endIndex The end index to apply the font to (exclusive) + * @param fontIndex The font to use. + */ public void applyFont(int startIndex, int endIndex, short fontIndex) { // TODO Auto-generated method stub } + /** + * Applies a font to the specified characters of a string. + * + * @param startIndex The start index to apply the font to (inclusive) + * @param endIndex The end index to apply to font to (exclusive) + * @param font The index of the font to use. + */ public void applyFont(int startIndex, int endIndex, Font font) { - // TODO Auto-generated method stub - + applyFont(0, length(), font.getIndex()); } + /** + * Sets the font of the entire string. + * @param font The font to use. + */ public void applyFont(Font font) { - // TODO Auto-generated method stub - + applyFont(0, length(), font); } + /** + * Applies the specified font to the entire string. + * + * @param fontIndex the font to apply. + */ public void applyFont(short fontIndex) { - // TODO Auto-generated method stub - + applyFont(0, length(), fontIndex); } + /** + * Removes any formatting that may have been applied to the string. + */ public void clearFormatting() { - // TODO Auto-generated method stub - - } - - public int compareTo(Object o) { - // TODO Auto-generated method stub - return 0; + for (int i = 0; i < st.sizeOfRArray(); i++) { + st.removeR(i); + } } + /** + * Returns the font in use at a particular index. + * + * @param index The index. + * @return The font that's currently being applied at that + * index or null if no font is being applied or the + * index is out of range. + */ public short getFontAtIndex(int index) { // TODO Auto-generated method stub return 0; } + /** + * Gets the font used in a particular formatting run. + * + * @param index the index of the formatting run + * @return the font number used. + */ public short getFontOfFormattingRun(int index) { // TODO Auto-generated method stub return 0; } + /** + * The index within the string to which the specified formatting run applies. + * @param index the index of the formatting run + * @return the index within the string. + */ public int getIndexOfFormattingRun(int index) { // TODO Auto-generated method stub return 0; } + /** + * Returns the plain string representation. + */ public String getString() { - return string; + if(st.sizeOfRArray() == 0) return st.getT(); + else { + StringBuffer buf = new StringBuffer(); + for(CTRElt r : st.getRArray()){ + buf.append(r.getT()); + } + return buf.toString(); + } } + + /** + * Removes any formatting and sets new string value + * + * @param s new string value + */ + public void setString(String s){ + clearFormatting(); + st.setT(s); + } + + /** + * Returns the plain string representation. + */ public String toString() { - return string; + return getString(); } + /** + * Returns the number of characters in this string. + */ public int length() { - return string.length(); + return getString().length(); } + /** + * @return The number of formatting runs used. + */ public int numFormattingRuns() { - // TODO Auto-generated method stub - return 0; + return st.sizeOfRArray(); + } + + /** + * Return the underlying xml bean + */ + public CTRst getCTRst() { + return st; + } + + protected void setStylesTableReference(StylesTable tbl){ + styles = tbl; } } |