use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst in project poi by apache.
the class SharedStringsTable method addEntry.
/**
* Add an entry to this Shared String table (a new value is appened to the end).
*
* <p>
* If the Shared String table already contains this <code>CTRst</code> bean, its index is returned.
* Otherwise a new entry is aded.
* </p>
*
* @param st the entry to add
* @return index the index of added entry
*/
public int addEntry(CTRst st) {
String s = getKey(st);
count++;
if (stmap.containsKey(s)) {
return stmap.get(s);
}
uniqueCount++;
//create a CTRst bean attached to this SstDocument and copy the argument CTRst into it
CTRst newSt = _sstDoc.getSst().addNewSi();
newSt.set(st);
int idx = strings.size();
stmap.put(s, idx);
strings.add(newSt);
return idx;
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst 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());
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst in project poi by apache.
the class TestSharedStringsTable method testReadWrite.
public void testReadWrite() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
SharedStringsTable sst1 = wb1.getSharedStringSource();
//serialize, read back and compare with the original
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
SharedStringsTable sst2 = wb2.getSharedStringSource();
assertEquals(sst1.getCount(), sst2.getCount());
assertEquals(sst1.getUniqueCount(), sst2.getUniqueCount());
List<CTRst> items1 = sst1.getItems();
List<CTRst> items2 = sst2.getItems();
assertEquals(items1.size(), items2.size());
for (int i = 0; i < items1.size(); i++) {
CTRst st1 = items1.get(i);
CTRst st2 = items2.get(i);
assertEquals(st1.toString(), st2.toString());
}
XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
assertNotNull(wb3);
wb3.close();
wb2.close();
wb1.close();
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst in project poi by apache.
the class TestSXSSFCell method testPreserveSpaces.
@Test
public void testPreserveSpaces() throws IOException {
String[] samplesWithSpaces = { " POI", "POI ", " POI ", "\nPOI", "\n\nPOI \n" };
for (String str : samplesWithSpaces) {
Workbook swb = _testDataProvider.createWorkbook();
Cell sCell = swb.createSheet().createRow(0).createCell(0);
sCell.setCellValue(str);
assertEquals(sCell.getStringCellValue(), str);
// read back as XSSF and check that xml:spaces="preserve" is set
XSSFWorkbook xwb = (XSSFWorkbook) _testDataProvider.writeOutAndReadBack(swb);
XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
CTRst is = xCell.getCTCell().getIs();
XmlCursor c = is.newCursor();
c.toNextToken();
String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
c.dispose();
assertEquals("expected xml:spaces=\"preserve\" \"" + str + "\"", "preserve", t);
xwb.close();
swb.close();
}
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst 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;
}
Aggregations