Search in sources :

Example 11 with CTRst

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

the class TestXSSFRichTextString method testUtfDecode.

/**
     * test that unicode representation_ xHHHH_ is properly processed
     */
public void testUtfDecode() throws IOException {
    CTRst st = CTRst.Factory.newInstance();
    st.setT("abc_x000D_2ef_x000D_");
    XSSFRichTextString rt = new XSSFRichTextString(st);
    //_x000D_ is converted into carriage return
    assertEquals("abc\r2ef\r", rt.getString());
    // Test Lowercase case
    CTRst st2 = CTRst.Factory.newInstance();
    st2.setT("abc_x000d_2ef_x000d_");
    XSSFRichTextString rt2 = new XSSFRichTextString(st2);
    assertEquals("abc\r2ef\r", rt2.getString());
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst)

Example 12 with CTRst

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

the class SharedStringsTable method readFrom.

/**
     * Read this shared strings table from an XML file.
     * 
     * @param is The input stream containing the XML document.
     * @throws IOException if an error occurs while reading.
     */
public void readFrom(InputStream is) throws IOException {
    try {
        int cnt = 0;
        _sstDoc = SstDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
        CTSst sst = _sstDoc.getSst();
        count = (int) sst.getCount();
        uniqueCount = (int) sst.getUniqueCount();
        for (CTRst st : sst.getSiArray()) {
            stmap.put(getKey(st), cnt);
            strings.add(st);
            cnt++;
        }
    } catch (XmlException e) {
        throw new IOException("unable to parse shared strings table", e);
    }
}
Also used : CTSst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSst) CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) XmlException(org.apache.xmlbeans.XmlException) IOException(java.io.IOException)

Example 13 with CTRst

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

the class XSSFRichTextString method applyFont.

/**
     * 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) {
    if (startIndex > endIndex)
        throw new IllegalArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex);
    if (startIndex < 0 || endIndex > length())
        throw new IllegalArgumentException("Start and end index not in range, but had " + startIndex + " and " + endIndex);
    if (startIndex == endIndex)
        return;
    if (st.sizeOfRArray() == 0 && st.isSetT()) {
        //convert <t>string</t> into a text run: <r><t>string</t></r>
        st.addNewR().setT(st.getT());
        st.unsetT();
    }
    String text = getString();
    XSSFFont xssfFont = (XSSFFont) font;
    TreeMap<Integer, CTRPrElt> formats = getFormatMap(st);
    CTRPrElt fmt = CTRPrElt.Factory.newInstance();
    setRunAttributes(xssfFont.getCTFont(), fmt);
    applyFont(formats, startIndex, endIndex, fmt);
    CTRst newSt = buildCTRst(text, formats);
    st.set(newSt);
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Example 14 with CTRst

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

the class XSSFRichTextString method buildCTRst.

CTRst buildCTRst(String text, TreeMap<Integer, CTRPrElt> formats) {
    if (text.length() != formats.lastKey()) {
        throw new IllegalArgumentException("Text length was " + text.length() + " but the last format index was " + formats.lastKey());
    }
    CTRst stf = CTRst.Factory.newInstance();
    int runStartIdx = 0;
    for (Map.Entry<Integer, CTRPrElt> me : formats.entrySet()) {
        int runEndIdx = me.getKey();
        CTRElt run = stf.addNewR();
        String fragment = text.substring(runStartIdx, runEndIdx);
        run.setT(fragment);
        preserveSpaces(run.xgetT());
        CTRPrElt fmt = me.getValue();
        if (fmt != null) {
            run.setRPr(fmt);
        }
        runStartIdx = runEndIdx;
    }
    return stf;
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) CTRElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Example 15 with CTRst

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

the class TestReadOnlySharedStringsTable method testParse.

public void testParse() throws Exception {
    OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("SampleSS.xlsx"));
    List<PackagePart> parts = pkg.getPartsByName(Pattern.compile("/xl/sharedStrings.xml"));
    assertEquals(1, parts.size());
    SharedStringsTable stbl = new SharedStringsTable(parts.get(0));
    ReadOnlySharedStringsTable rtbl = new ReadOnlySharedStringsTable(parts.get(0));
    assertEquals(stbl.getCount(), rtbl.getCount());
    assertEquals(stbl.getUniqueCount(), rtbl.getUniqueCount());
    assertEquals(stbl.getItems().size(), stbl.getUniqueCount());
    assertEquals(rtbl.getItems().size(), rtbl.getUniqueCount());
    for (int i = 0; i < stbl.getUniqueCount(); i++) {
        CTRst i1 = stbl.getEntryAt(i);
        String i2 = rtbl.getEntryAt(i);
        assertEquals(i1.getT(), i2);
    }
}
Also used : SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Aggregations

CTRst (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst)14 CTRPrElt (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt)5 RichTextString (org.apache.poi.ss.usermodel.RichTextString)3 CTRElt (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt)3 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 QName (javax.xml.namespace.QName)1 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)1 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)1 BaseTestXCell (org.apache.poi.ss.usermodel.BaseTestXCell)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 CellAddress (org.apache.poi.ss.util.CellAddress)1 SharedStringsTable (org.apache.poi.xssf.model.SharedStringsTable)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlException (org.apache.xmlbeans.XmlException)1 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)1