Example #1
0
        CT_Rst buildCTRst(String text, SortedDictionary <int, CT_RPrElt> formats)
        {
            if (text.Length != GetLastKey(formats.Keys))
            {
                throw new ArgumentException("Text length was " + text.Length +
                                            " but the last format index was " + GetLastKey(formats.Keys));
            }
            CT_Rst st          = new CT_Rst();
            int    runStartIdx = 0;

            for (SortedDictionary <int, CT_RPrElt> .KeyCollection.Enumerator it = formats.Keys.GetEnumerator(); it.MoveNext();)
            {
                int     runEndIdx = it.Current;
                CT_RElt run       = st.AddNewR();
                String  fragment  = text.Substring(runStartIdx, runEndIdx - runStartIdx);
                run.t = (fragment);
                PreserveSpaces(run.t);
                CT_RPrElt fmt = formats[runEndIdx];
                if (fmt != null)
                {
                    run.rPr = (fmt);
                }
                runStartIdx = runEndIdx;
            }
            return(st);
        }
Example #2
0
        CT_Rst BuildCTRst(String text, SortedDictionary <int, CT_RPrElt> formats)
        {
            if (text.Length != GetLastKey(formats.Keys))
            {
                throw new ArgumentException("Text length was " + text.Length +
                                            " but the last format index was " + GetLastKey(formats.Keys));
            }
            CT_Rst st          = new CT_Rst();
            int    runStartIdx = 0;

            foreach (KeyValuePair <int, CT_RPrElt> kv in formats)
            {
                int     runEndIdx = kv.Key;
                CT_RElt run       = st.AddNewR();
                String  fragment  = text.Substring(runStartIdx, runEndIdx - runStartIdx);
                run.t = (fragment);
                PreserveSpaces(run.t);
                CT_RPrElt fmt = kv.Value;
                if (fmt != null)
                {
                    run.rPr = fmt;
                }
                runStartIdx = runEndIdx;
            }
            return(st);
        }
Example #3
0
        public void TestClearFormatting()
        {
            XSSFRichTextString rt = new XSSFRichTextString("Apache POI");

            Assert.AreEqual("Apache POI", rt.String);
            Assert.AreEqual(false, rt.HasFormatting());

            rt.ClearFormatting();

            CT_Rst st = rt.GetCTRst();

            Assert.IsTrue(st.IsSetT());
            Assert.AreEqual("Apache POI", rt.String);
            Assert.AreEqual(0, rt.NumFormattingRuns);
            Assert.AreEqual(false, rt.HasFormatting());

            XSSFFont font = new XSSFFont();

            font.IsBold = true;

            rt.ApplyFont(7, 10, font);
            Assert.AreEqual(2, rt.NumFormattingRuns);
            Assert.AreEqual(true, rt.HasFormatting());

            rt.ClearFormatting();

            Assert.AreEqual("Apache POI", rt.String);
            Assert.AreEqual(0, rt.NumFormattingRuns);
            Assert.AreEqual(false, rt.HasFormatting());
        }
Example #4
0
        public void TestPreserveSpaces()
        {
            String[] samplesWithSpaces =
            {
                " POI",
                "POI ",
                " POI ",
                "\nPOI",
                "\n\nPOI \n",
            };
            foreach (String str in samplesWithSpaces)
            {
                IWorkbook swb   = _testDataProvider.CreateWorkbook();
                ICell     sCell = swb.CreateSheet().CreateRow(0).CreateCell(0);
                sCell.SetCellValue(str);
                Assert.AreEqual(sCell.StringCellValue, 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) as XSSFCell;

                CT_Rst is1 = xCell.GetCTCell().@is;
                //XmlCursor c = is1.NewCursor();
                //c.ToNextToken();
                //String t = c.GetAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
                //c.Dispose();


                //write is1 to xml stream writer ,get the xml text and parse it and get space attr.
                //Assert.AreEqual("preserve", t, "expected xml:spaces=\"preserve\" \"" + str + "\"");
                xwb.Close();
                swb.Close();
            }
        }
        private CT_Rst buildCTRst(string text, SortedDictionary <int, CT_RPrElt> formats)
        {
            if (text.Length != this.GetLastKey(formats.Keys))
            {
                throw new ArgumentException("Text length was " + (object)text.Length + " but the last format index was " + (object)this.GetLastKey(formats.Keys));
            }
            CT_Rst ctRst      = new CT_Rst();
            int    startIndex = 0;

            SortedDictionary <int, CT_RPrElt> .KeyCollection.Enumerator enumerator = formats.Keys.GetEnumerator();
            while (enumerator.MoveNext())
            {
                int     current = enumerator.Current;
                CT_RElt ctRelt  = ctRst.AddNewR();
                string  str     = text.Substring(startIndex, current - startIndex);
                ctRelt.t = str;
                XSSFRichTextString.PreserveSpaces(ctRelt.t);
                CT_RPrElt format = formats[current];
                if (format != null)
                {
                    ctRelt.rPr = format;
                }
                startIndex = current;
            }
            return(ctRst);
        }
Example #6
0
        public void GetCellComment()
        {
            CommentsTable sheetComments = new CommentsTable();

            CT_Comments    comments    = sheetComments.GetCTComments();
            CT_CommentList commentList = comments.commentList;

            // Create 2 comments for A1 and A" cells
            CT_Comment comment0 = commentList.InsertNewComment(0);

            comment0.@ref = "A1";
            CT_Rst ctrst0 = new CT_Rst();

            ctrst0.t      = (TEST_A1_TEXT);
            comment0.text = (ctrst0);
            CT_Comment comment1 = commentList.InsertNewComment(0);

            comment1.@ref = ("A2");
            CT_Rst ctrst1 = new CT_Rst();

            ctrst1.t      = (TEST_A2_TEXT);
            comment1.text = (ctrst1);

            // Test Finding the right comment for a cell
            Assert.AreSame(comment0, sheetComments.GetCTComment("A1"));
            Assert.AreSame(comment1, sheetComments.GetCTComment("A2"));
            Assert.IsNull(sheetComments.GetCTComment("A3"));
        }
Example #7
0
        /**
         * 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, IFont font)
        {
            if (startIndex > endIndex)
            {
                throw new ArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex);
            }
            if (startIndex < 0 || endIndex > Length)
            {
                throw new ArgumentException("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().t = (st.t);
                st.unsetT();
            }

            String   text     = this.String;
            XSSFFont xssfFont = (XSSFFont)font;

            SortedDictionary <int, CT_RPrElt> formats = GetFormatMap(st);
            CT_RPrElt fmt = new CT_RPrElt();

            SetRunAttributes(xssfFont.GetCTFont(), fmt);
            ApplyFont(formats, startIndex, endIndex, fmt);

            CT_Rst newSt = buildCTRst(text, formats);

            st.Set(newSt);
        }
Example #8
0
 private string GetKey(CT_Rst st)
 {
     if (st.t != null)
     {
         return(st.t);
     }
     return(string.Empty);
 }
Example #9
0
        public void TestUtfDecode()
        {
            CT_Rst st = new CT_Rst();

            st.t = ("abc_x000D_2ef_x000D_");
            XSSFRichTextString rt = new XSSFRichTextString(st);

            //_x000D_ is Converted into carriage return
            Assert.AreEqual("abc\r2ef\r", rt.String);
        }
Example #10
0
        public void TestRgbColor()
        {
            const string       testText = "Apache";
            XSSFRichTextString rt       = new XSSFRichTextString(testText);
            XSSFFont           font     = new XSSFFont {
                FontName = "Times New Roman", FontHeightInPoints = 11
            };

            font.SetColor(new XSSFColor(Color.Red));
            rt.ApplyFont(0, testText.Length, font);
            CT_Rst ct = rt.GetCTRst();

            Assert.AreEqual("<r><rPr><color rgb=\"FF0000\"/><rFont val=\"Times New Roman\"/><sz val=\"11\"/></rPr><t>Apache</t></r>", ct.XmlText);
        }
        internal SortedDictionary <int, CT_RPrElt> GetFormatMap(CT_Rst entry)
        {
            int index = 0;
            SortedDictionary <int, CT_RPrElt> sortedDictionary = new SortedDictionary <int, CT_RPrElt>();

            foreach (CT_RElt ctRelt in entry.r)
            {
                string    t   = ctRelt.t;
                CT_RPrElt rPr = ctRelt.rPr;
                index += t.Length;
                sortedDictionary[index] = rPr;
            }
            return(sortedDictionary);
        }
Example #12
0
        internal SortedDictionary <int, CT_RPrElt> GetFormatMap(CT_Rst entry)
        {
            int length = 0;
            SortedDictionary <int, CT_RPrElt> formats = new SortedDictionary <int, CT_RPrElt>();

            foreach (CT_RElt r in entry.r)
            {
                String    txt = r.t;
                CT_RPrElt fmt = r.rPr;

                length         += txt.Length;
                formats[length] = fmt;
            }
            return(formats);
        }
Example #13
0
        /**
         * make sure we insert xml:space="preserve" attribute
         * if a string has leading or trailing white spaces
         */
        public void TestPreserveSpaces()
        {
            XSSFRichTextString rt = new XSSFRichTextString("Apache");
            CT_Rst             ct = rt.GetCTRst();
            string             t  = ct.t;

            Assert.AreEqual("<t>Apache</t>", ct.XmlText);
            rt.String = "  Apache";
            Assert.AreEqual("<t xml:space=\"preserve\">  Apache</t>", ct.XmlText);
            rt.Append(" POI");
            rt.Append(" ");
            Assert.AreEqual("  Apache POI ", rt.String);
            //Assert.AreEqual("<xml-fragment xml:space=\"preserve\">  Apache</t>", rt.GetCTRst().GetRArray(0).xmlText());
            //Assert.AreEqual("<xml-fragment xml:space=\"preserve\"> POI</xml-fragment>", rt.getCTRst().getRArray(1).xgetT().xmlText());
            //Assert.AreEqual("<xml-fragment xml:space=\"preserve\"> </xml-fragment>", rt.getCTRst().getRArray(2).xgetT().xmlText());
        }
Example #14
0
        public void TestCreate()
        {
            XSSFRichTextString rt = new XSSFRichTextString("Apache POI");

            Assert.AreEqual("Apache POI", rt.String);

            CT_Rst st = rt.GetCTRst();

            Assert.IsTrue(st.IsSetT());
            Assert.AreEqual("Apache POI", st.t);

            rt.Append(" is cool stuff");
            Assert.AreEqual(2, st.sizeOfRArray());
            Assert.IsFalse(st.IsSetT());

            Assert.AreEqual("Apache POI is cool stuff", rt.String);
        }
Example #15
0
        public int AddEntry(CT_Rst st)
        {
            string key = this.GetKey(st);

            ++this.count;
            if (this.stmap.ContainsKey(key))
            {
                return(this.stmap[key]);
            }
            ++this.uniqueCount;
            CT_Rst ctRst = new CT_Rst();

            this._sstDoc.GetSst().si.Add(ctRst);
            ctRst.Set(st);
            int count = this.strings.Count;

            this.stmap[key] = count;
            this.strings.Add(ctRst);
            return(count);
        }
Example #16
0
        public void TestReadWrite()
        {
            XSSFWorkbook       wb   = XSSFTestDataSamples.OpenSampleWorkbook("sample.xlsx");
            SharedStringsTable sst1 = wb.GetSharedStringSource();

            //Serialize, read back and compare with the original
            SharedStringsTable sst2 = ((XSSFWorkbook)XSSFTestDataSamples.WriteOutAndReadBack(wb)).GetSharedStringSource();

            Assert.AreEqual(sst1.GetCount(), sst2.GetCount());
            Assert.AreEqual(sst1.GetUniqueCount(), sst2.GetUniqueCount());

            List <CT_Rst> items1 = sst1.GetItems();
            List <CT_Rst> items2 = sst2.GetItems();

            Assert.AreEqual(items1.Count, items2.Count);
            for (int i = 0; i < items1.Count; i++)
            {
                CT_Rst st1 = items1[i];
                CT_Rst st2 = items2[i];
                Assert.AreEqual(st1.ToString(), st2.ToString());
            }
        }
Example #17
0
        /**
         * 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>CT_Rst</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(CT_Rst st)
        {
            String s = GetKey(st);

            count++;
            if (stmap.ContainsKey(s))
            {
                return(stmap[s]);
            }

            uniqueCount++;
            //create a CT_Rst bean attached to this SstDocument and copy the argument CT_Rst into it
            CT_Rst newSt = new CT_Rst();

            _sstDoc.GetSst().si.Add(newSt);
            newSt.Set(st);
            int idx = strings.Count;

            stmap[s] = idx;
            strings.Add(newSt);
            return(idx);
        }
Example #18
0
 /**
  * Create empty rich text string and Initialize it with empty string
  */
 public XSSFRichTextString()
 {
     st = new CT_Rst();
 }
Example #19
0
        /**
         * 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>CT_Rst</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(CT_Rst st)
        {
            String s = GetKey(st);
            count++;
            if (stmap.ContainsKey(s))
            {
                return stmap[s];
            }

            uniqueCount++;
            //create a CT_Rst bean attached to this SstDocument and copy the argument CT_Rst into it
            CT_Rst newSt = new CT_Rst();
            _sstDoc.GetSst().si.Add(newSt);
            newSt.Set(st);
            int idx = strings.Count;
            stmap[s] = idx;
            strings.Add(newSt);
            return idx;
        }
Example #20
0
 private String GetKey(CT_Rst st)
 {
     return(st.XmlText);
 }
Example #21
0
        public void TestCreateNew()
        {
            SharedStringsTable sst = new SharedStringsTable();

            CT_Rst st;
            int    idx;

            // Check defaults
            Assert.IsNotNull(sst.Items);
            Assert.AreEqual(0, sst.Items.Count);
            Assert.AreEqual(0, sst.Count);
            Assert.AreEqual(0, sst.UniqueCount);

            st   = new CT_Rst();
            st.t = ("Hello, World!");

            idx = sst.AddEntry(st);
            Assert.AreEqual(0, idx);
            Assert.AreEqual(1, sst.Count);
            Assert.AreEqual(1, sst.UniqueCount);

            //add the same entry again
            idx = sst.AddEntry(st);
            Assert.AreEqual(0, idx);
            Assert.AreEqual(2, sst.Count);
            Assert.AreEqual(1, sst.UniqueCount);

            //and again
            idx = sst.AddEntry(st);
            Assert.AreEqual(0, idx);
            Assert.AreEqual(3, sst.Count);
            Assert.AreEqual(1, sst.UniqueCount);

            st   = new CT_Rst();
            st.t = ("Second string");

            idx = sst.AddEntry(st);
            Assert.AreEqual(1, idx);
            Assert.AreEqual(4, sst.Count);
            Assert.AreEqual(2, sst.UniqueCount);

            //add the same entry again
            idx = sst.AddEntry(st);
            Assert.AreEqual(1, idx);
            Assert.AreEqual(5, sst.Count);
            Assert.AreEqual(2, sst.UniqueCount);

            st = new CT_Rst();
            CT_RElt   r  = st.AddNewR();
            CT_RPrElt pr = r.AddNewRPr();

            pr.AddNewColor().SetRgb(new byte[] { (byte)0xFF, 0, 0 }); //red
            pr.AddNewI().val = (true);                                //bold
            pr.AddNewB().val = (true);                                //italic
            r.t = ("Second string");

            idx = sst.AddEntry(st);
            Assert.AreEqual(2, idx);
            Assert.AreEqual(6, sst.Count);
            Assert.AreEqual(3, sst.UniqueCount);

            idx = sst.AddEntry(st);
            Assert.AreEqual(2, idx);
            Assert.AreEqual(7, sst.Count);
            Assert.AreEqual(3, sst.UniqueCount);

            //OK. the sst table is Filled, check the contents
            Assert.AreEqual(3, sst.Items.Count);
            Assert.AreEqual("Hello, World!", new XSSFRichTextString(sst.GetEntryAt(0)).ToString());
            Assert.AreEqual("Second string", new XSSFRichTextString(sst.GetEntryAt(1)).ToString());
            Assert.AreEqual("Second string", new XSSFRichTextString(sst.GetEntryAt(2)).ToString());
        }
Example #22
0
 private String GetKey(CT_Rst st)
 {
     return st.t==null?string.Empty:st.t; //.xmltext
 }
 public XSSFRichTextString(string str)
 {
     this.st   = new CT_Rst();
     this.st.t = str;
     XSSFRichTextString.PreserveSpaces(this.st.t);
 }
Example #24
0
 private String GetKey(CT_Rst st)
 {
     return(st.t == null?string.Empty:st.t); //.xmltext
 }
Example #25
0
        public void TestApplyFont_lowlevel()
        {
            CT_Rst             st   = new CT_Rst();
            String             text = "Apache Software Foundation";
            XSSFRichTextString str  = new XSSFRichTextString(text);

            Assert.AreEqual(26, text.Length);

            st.AddNewR().t = (text);

            //SortedDictionary<int, CT_RPrElt> formats = str.GetFormatMap(st);
            //Assert.AreEqual(1, formats.Count);
            //Assert.AreEqual(26, (int)formats.Keys[0]);
            //Assert.IsNull(formats.Get(formats.firstKey()));

            //CT_RPrElt fmt1 = new CT_RPrElt();
            //str.ApplyFont(formats, 0, 6, fmt1);
            //Assert.AreEqual(2, formats.Count);
            //Assert.AreEqual("[6, 26]", formats.Keys.ToString());
            //Object[] Runs1 = formats.Values.ToArray();
            //Assert.AreSame(fmt1, Runs1[0]);
            //Assert.AreSame(null, Runs1[1]);

            //CT_RPrElt fmt2 = new CT_RPrElt();
            //str.ApplyFont(formats, 7, 15, fmt2);
            //Assert.AreEqual(4, formats.Count);
            //Assert.AreEqual("[6, 7, 15, 26]", formats.Keys.ToString());
            //Object[] Runs2 = formats.Values.ToArray();
            //Assert.AreSame(fmt1, Runs2[0]);
            //Assert.AreSame(null, Runs2[1]);
            //Assert.AreSame(fmt2, Runs2[2]);
            //Assert.AreSame(null, Runs2[3]);

            //CT_RPrElt fmt3 = new CT_RPrElt();
            //str.ApplyFont(formats, 6, 7, fmt3);
            //Assert.AreEqual(4, formats.Count);
            //Assert.AreEqual("[6, 7, 15, 26]", formats.Keys.ToString());
            //Object[] Runs3 = formats.Values.ToArray();
            //Assert.AreSame(fmt1, Runs3[0]);
            //Assert.AreSame(fmt3, Runs3[1]);
            //Assert.AreSame(fmt2, Runs3[2]);
            //Assert.AreSame(null, Runs3[3]);

            //CT_RPrElt fmt4 = new CT_RPrElt();
            //str.ApplyFont(formats, 0, 7, fmt4);
            //Assert.AreEqual(3, formats.Count);
            //Assert.AreEqual("[7, 15, 26]", formats.Keys.ToString());
            //Object[] Runs4 = formats.Values.ToArray();
            //Assert.AreSame(fmt4, Runs4[0]);
            //Assert.AreSame(fmt2, Runs4[1]);
            //Assert.AreSame(null, Runs4[2]);

            //CT_RPrElt fmt5 = new CT_RPrElt();
            //str.ApplyFont(formats, 0, 26, fmt5);
            //Assert.AreEqual(1, formats.Count);
            //Assert.AreEqual("[26]", formats.Keys.ToString());
            //Object[] Runs5 = formats.Values.ToArray();
            //Assert.AreSame(fmt5, Runs5[0]);

            //CT_RPrElt fmt6 = new CT_RPrElt();
            //str.ApplyFont(formats, 15, 26, fmt6);
            //Assert.AreEqual(2, formats.Count);
            //Assert.AreEqual("[15, 26]", formats.Keys.ToString());
            //Object[] Runs6 = formats.Values.ToArray();
            //Assert.AreSame(fmt5, Runs6[0]);
            //Assert.AreSame(fmt6, Runs6[1]);

            //str.ApplyFont(formats, 0, 26, null);
            //Assert.AreEqual(1, formats.Count);
            //Assert.AreEqual("[26]", formats.Keys.ToString());
            //Object[] Runs7 = formats.Values.ToArray();
            //Assert.AreSame(null, Runs7[0]);

            //str.ApplyFont(formats, 15, 26, fmt6);
            //Assert.AreEqual(2, formats.Count);
            //Assert.AreEqual("[15, 26]", formats.Keys.ToString());
            //Object[] Runs8 = formats.Values.ToArray();
            //Assert.AreSame(null, Runs8[0]);
            //Assert.AreSame(fmt6, Runs8[1]);

            //str.ApplyFont(formats, 15, 26, fmt5);
            //Assert.AreEqual(2, formats.Count);
            //Assert.AreEqual("[15, 26]", formats.Keys.ToString());
            //Object[] Runs9 = formats.Values.ToArray();
            //Assert.AreSame(null, Runs9[0]);
            //Assert.AreSame(fmt5, Runs9[1]);

            //str.ApplyFont(formats, 2, 20, fmt6);
            //Assert.AreEqual(3, formats.Count);
            //Assert.AreEqual("[2, 20, 26]", formats.Keys.ToString());
            //Object[] Runs10 = formats.Values.ToArray();
            //Assert.AreSame(null, Runs10[0]);
            //Assert.AreSame(fmt6, Runs10[1]);
            //Assert.AreSame(fmt5, Runs10[2]);

            //str.ApplyFont(formats, 22, 24, fmt4);
            //Assert.AreEqual(5, formats.Count);
            //Assert.AreEqual("[2, 20, 22, 24, 26]", formats.Keys.ToString());
            //Object[] Runs11 = formats.Values.ToArray();
            //Assert.AreSame(null, Runs11[0]);
            //Assert.AreSame(fmt6, Runs11[1]);
            //Assert.AreSame(fmt5, Runs11[2]);
            //Assert.AreSame(fmt4, Runs11[3]);
            //Assert.AreSame(fmt5, Runs11[4]);

            //str.ApplyFont(formats, 0, 10, fmt1);
            //Assert.AreEqual(5, formats.Count);
            //Assert.AreEqual("[10, 20, 22, 24, 26]", formats.Keys.ToString());
            //Object[] Runs12 = formats.Values.ToArray();
            //Assert.AreSame(fmt1, Runs12[0]);
            //Assert.AreSame(fmt6, Runs12[1]);
            //Assert.AreSame(fmt5, Runs12[2]);
            //Assert.AreSame(fmt4, Runs12[3]);
            //Assert.AreSame(fmt5, Runs12[4]);

            Assert.Fail("implement STXString");
        }
Example #26
0
 public XMLCharacterRunManager(CT_Rst inlineString, XMLPaletteModel palette)
 {
     _inlineString = inlineString;
     _palette      = palette;
 }
Example #27
0
 private String GetKey(CT_Rst st)
 {
     return st.XmlText;
 }
Example #28
0
 /**
  * Create a rich text string
  */
 public XSSFRichTextString(String str)
 {
     st   = new CT_Rst();
     st.t = str;
     PreserveSpaces(st.t);
 }
Example #29
0
 /**
  * Create a rich text string from the supplied XML bean
  */
 public XSSFRichTextString(CT_Rst st)
 {
     this.st = st;
 }