Search in sources :

Example 1 with CTRPrElt

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt in project poi by apache.

the class TestSharedStringsTable method testCreateNew.

public void testCreateNew() {
    SharedStringsTable sst = new SharedStringsTable();
    CTRst st;
    int idx;
    // Check defaults
    assertNotNull(sst.getItems());
    assertEquals(0, sst.getItems().size());
    assertEquals(0, sst.getCount());
    assertEquals(0, sst.getUniqueCount());
    st = CTRst.Factory.newInstance();
    st.setT("Hello, World!");
    idx = sst.addEntry(st);
    assertEquals(0, idx);
    assertEquals(1, sst.getCount());
    assertEquals(1, sst.getUniqueCount());
    //add the same entry again
    idx = sst.addEntry(st);
    assertEquals(0, idx);
    assertEquals(2, sst.getCount());
    assertEquals(1, sst.getUniqueCount());
    //and again
    idx = sst.addEntry(st);
    assertEquals(0, idx);
    assertEquals(3, sst.getCount());
    assertEquals(1, sst.getUniqueCount());
    st = CTRst.Factory.newInstance();
    st.setT("Second string");
    idx = sst.addEntry(st);
    assertEquals(1, idx);
    assertEquals(4, sst.getCount());
    assertEquals(2, sst.getUniqueCount());
    //add the same entry again
    idx = sst.addEntry(st);
    assertEquals(1, idx);
    assertEquals(5, sst.getCount());
    assertEquals(2, sst.getUniqueCount());
    st = CTRst.Factory.newInstance();
    CTRElt r = st.addNewR();
    CTRPrElt pr = r.addNewRPr();
    //red
    pr.addNewColor().setRgb(new byte[] { (byte) 0xFF, 0, 0 });
    //bold
    pr.addNewI().setVal(true);
    //italic
    pr.addNewB().setVal(true);
    r.setT("Second string");
    idx = sst.addEntry(st);
    assertEquals(2, idx);
    assertEquals(6, sst.getCount());
    assertEquals(3, sst.getUniqueCount());
    idx = sst.addEntry(st);
    assertEquals(2, idx);
    assertEquals(7, sst.getCount());
    assertEquals(3, sst.getUniqueCount());
    //OK. the sst table is filled, check the contents
    assertEquals(3, sst.getItems().size());
    assertEquals("Hello, World!", new XSSFRichTextString(sst.getEntryAt(0)).toString());
    assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(1)).toString());
    assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(2)).toString());
}
Also used : XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) CTRElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt)

Example 2 with CTRPrElt

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt in project poi by apache.

the class XSSFSimpleShape method addNewTextParagraph.

/**
     * Add a new paragraph run to this shape, set to the provided rich text string 
     *
     * @return created paragraph run
     */
public XSSFTextParagraph addNewTextParagraph(XSSFRichTextString str) {
    CTTextBody txBody = ctShape.getTxBody();
    CTTextParagraph p = txBody.addNewP();
    if (str.numFormattingRuns() == 0) {
        CTRegularTextRun r = p.addNewR();
        CTTextCharacterProperties rPr = r.addNewRPr();
        rPr.setLang("en-US");
        rPr.setSz(1100);
        r.setT(str.getString());
    } else {
        for (int i = 0; i < str.getCTRst().sizeOfRArray(); i++) {
            CTRElt lt = str.getCTRst().getRArray(i);
            CTRPrElt ltPr = lt.getRPr();
            if (ltPr == null)
                ltPr = lt.addNewRPr();
            CTRegularTextRun r = p.addNewR();
            CTTextCharacterProperties rPr = r.addNewRPr();
            rPr.setLang("en-US");
            applyAttributes(ltPr, rPr);
            r.setT(lt.getT());
        }
    }
    // Note: the XSSFTextParagraph constructor will create its required XSSFTextRuns from the provided CTTextParagraph
    XSSFTextParagraph paragraph = new XSSFTextParagraph(p, ctShape);
    _paragraphs.add(paragraph);
    return paragraph;
}
Also used : CTRElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt)

Example 3 with CTRPrElt

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt in project poi by apache.

the class XSSFRichTextString method getFormatMap.

TreeMap<Integer, CTRPrElt> getFormatMap(CTRst entry) {
    int length = 0;
    TreeMap<Integer, CTRPrElt> formats = new TreeMap<Integer, CTRPrElt>();
    //noinspection deprecation - for performance reasons!
    for (CTRElt r : entry.getRArray()) {
        String txt = r.getT();
        CTRPrElt fmt = r.getRPr();
        length += txt.length();
        formats.put(length, fmt);
    }
    return formats;
}
Also used : CTRElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Example 4 with CTRPrElt

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt in project poi by apache.

the class XSSFRichTextString method append.

/**
     * Append new text to this text run and apply the specify font to it
     *
     * @param text  the text to append
     * @param font  the font to apply to the appended text or <code>null</code> if no formatting is required
     */
public void append(String text, XSSFFont font) {
    if (st.sizeOfRArray() == 0 && st.isSetT()) {
        //convert <t>string</t> into a text run: <r><t>string</t></r>
        CTRElt lt = st.addNewR();
        lt.setT(st.getT());
        preserveSpaces(lt.xgetT());
        st.unsetT();
    }
    CTRElt lt = st.addNewR();
    lt.setT(text);
    preserveSpaces(lt.xgetT());
    if (font != null) {
        CTRPrElt pr = lt.addNewRPr();
        setRunAttributes(font.getCTFont(), pr);
    }
}
Also used : CTRElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt)

Example 5 with CTRPrElt

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt in project poi by apache.

the class TestXSSFRichTextString method testApplyFont_lowlevel.

public void testApplyFont_lowlevel() {
    CTRst st = CTRst.Factory.newInstance();
    String text = "Apache Software Foundation";
    XSSFRichTextString str = new XSSFRichTextString(text);
    assertEquals(26, text.length());
    st.addNewR().setT(text);
    TreeMap<Integer, CTRPrElt> formats = str.getFormatMap(st);
    assertEquals(1, formats.size());
    assertEquals(26, (int) formats.firstKey());
    assertNull(formats.get(formats.firstKey()));
    CTRPrElt fmt1 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 0, 6, fmt1);
    assertEquals(2, formats.size());
    assertEquals("[6, 26]", formats.keySet().toString());
    Object[] runs1 = formats.values().toArray();
    assertSame(fmt1, runs1[0]);
    assertSame(null, runs1[1]);
    CTRPrElt fmt2 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 7, 15, fmt2);
    assertEquals(4, formats.size());
    assertEquals("[6, 7, 15, 26]", formats.keySet().toString());
    Object[] runs2 = formats.values().toArray();
    assertSame(fmt1, runs2[0]);
    assertSame(null, runs2[1]);
    assertSame(fmt2, runs2[2]);
    assertSame(null, runs2[3]);
    CTRPrElt fmt3 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 6, 7, fmt3);
    assertEquals(4, formats.size());
    assertEquals("[6, 7, 15, 26]", formats.keySet().toString());
    Object[] runs3 = formats.values().toArray();
    assertSame(fmt1, runs3[0]);
    assertSame(fmt3, runs3[1]);
    assertSame(fmt2, runs3[2]);
    assertSame(null, runs3[3]);
    CTRPrElt fmt4 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 0, 7, fmt4);
    assertEquals(3, formats.size());
    assertEquals("[7, 15, 26]", formats.keySet().toString());
    Object[] runs4 = formats.values().toArray();
    assertSame(fmt4, runs4[0]);
    assertSame(fmt2, runs4[1]);
    assertSame(null, runs4[2]);
    CTRPrElt fmt5 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 0, 26, fmt5);
    assertEquals(1, formats.size());
    assertEquals("[26]", formats.keySet().toString());
    Object[] runs5 = formats.values().toArray();
    assertSame(fmt5, runs5[0]);
    CTRPrElt fmt6 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 15, 26, fmt6);
    assertEquals(2, formats.size());
    assertEquals("[15, 26]", formats.keySet().toString());
    Object[] runs6 = formats.values().toArray();
    assertSame(fmt5, runs6[0]);
    assertSame(fmt6, runs6[1]);
    str.applyFont(formats, 0, 26, null);
    assertEquals(1, formats.size());
    assertEquals("[26]", formats.keySet().toString());
    Object[] runs7 = formats.values().toArray();
    assertSame(null, runs7[0]);
    str.applyFont(formats, 15, 26, fmt6);
    assertEquals(2, formats.size());
    assertEquals("[15, 26]", formats.keySet().toString());
    Object[] runs8 = formats.values().toArray();
    assertSame(null, runs8[0]);
    assertSame(fmt6, runs8[1]);
    str.applyFont(formats, 15, 26, fmt5);
    assertEquals(2, formats.size());
    assertEquals("[15, 26]", formats.keySet().toString());
    Object[] runs9 = formats.values().toArray();
    assertSame(null, runs9[0]);
    assertSame(fmt5, runs9[1]);
    str.applyFont(formats, 2, 20, fmt6);
    assertEquals(3, formats.size());
    assertEquals("[2, 20, 26]", formats.keySet().toString());
    Object[] runs10 = formats.values().toArray();
    assertSame(null, runs10[0]);
    assertSame(fmt6, runs10[1]);
    assertSame(fmt5, runs10[2]);
    str.applyFont(formats, 22, 24, fmt4);
    assertEquals(5, formats.size());
    assertEquals("[2, 20, 22, 24, 26]", formats.keySet().toString());
    Object[] runs11 = formats.values().toArray();
    assertSame(null, runs11[0]);
    assertSame(fmt6, runs11[1]);
    assertSame(fmt5, runs11[2]);
    assertSame(fmt4, runs11[3]);
    assertSame(fmt5, runs11[4]);
    str.applyFont(formats, 0, 10, fmt1);
    assertEquals(5, formats.size());
    assertEquals("[10, 20, 22, 24, 26]", formats.keySet().toString());
    Object[] runs12 = formats.values().toArray();
    assertSame(fmt1, runs12[0]);
    assertSame(fmt6, runs12[1]);
    assertSame(fmt5, runs12[2]);
    assertSame(fmt4, runs12[3]);
    assertSame(fmt5, runs12[4]);
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt)

Aggregations

CTRPrElt (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt)10 CTRElt (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt)7 RichTextString (org.apache.poi.ss.usermodel.RichTextString)4 CTRst (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst)4 CTColor (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor)2 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)1 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)1 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)1 XmlObject (org.apache.xmlbeans.XmlObject)1 Test (org.junit.Test)1 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)1 CTFont (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont)1