Esempio n. 1
0
 public XWPFSDTContent(CT_SdtContentBlock block, IBody part, IRunBody parent)
 {
     foreach (object o in block.Items)
     {
         if (o is CT_P)
         {
             XWPFParagraph p = new XWPFParagraph((CT_P)o, part);
             bodyElements.Add(p);
             paragraphs.Add(p);
         }
         else if (o is CT_Tbl)
         {
             XWPFTable t = new XWPFTable((CT_Tbl)o, part);
             bodyElements.Add(t);
             tables.Add(t);
         }
         else if (o is CT_SdtBlock)
         {
             XWPFSDT c = new XWPFSDT(((CT_SdtBlock)o), part);
             bodyElements.Add(c);
             contentControls.Add(c);
         }
         else if (o is CT_R)
         {
             XWPFRun run = new XWPFRun((CT_R)o, parent);
             runs.Add(run);
             bodyElements.Add(run);
         }
     }
 }
Esempio n. 2
0
        internal override void OnDocumentRead()
        {
            base.OnDocumentRead();
            FtrDocument ftrDocument = null;

            try {
                XmlDocument xmldoc = ConvertStreamToXml(GetPackagePart().GetInputStream());
                ftrDocument  = FtrDocument.Parse(xmldoc, NamespaceManager);
                headerFooter = ftrDocument.Ftr;
                // parse the document with cursor and add
                // the XmlObject to its lists
                foreach (object o in headerFooter.Items)
                {
                    if (o is CT_P)
                    {
                        XWPFParagraph p = new XWPFParagraph((CT_P)o, this);
                        paragraphs.Add(p);
                        bodyElements.Add(p);
                    }
                    if (o is CT_Tbl)
                    {
                        XWPFTable t = new XWPFTable((CT_Tbl)o, this);
                        tables.Add(t);
                        bodyElements.Add(t);
                    }
                    if (o is CT_SdtBlock)
                    {
                        XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this);
                        bodyElements.Add(c);
                    }
                }
            } catch (Exception e) {
                throw new POIXMLException(e);
            }
        }
Esempio n. 3
0
 public XWPFSDTContent(CT_SdtContentBlock block, IBody part, IRunBody parent)
 {
     
     foreach (object o in block.Items)
     {
         if (o is CT_P)
         {
             XWPFParagraph p = new XWPFParagraph((CT_P)o, part);
             bodyElements.Add(p);
             paragraphs.Add(p);
         }
         else if (o is CT_Tbl)
         {
             XWPFTable t = new XWPFTable((CT_Tbl)o, part);
             bodyElements.Add(t);
             tables.Add(t);
         }
         else if (o is CT_SdtBlock)
         {
             XWPFSDT c = new XWPFSDT(((CT_SdtBlock)o), part);
             bodyElements.Add(c);
             contentControls.Add(c);
         }
         else if (o is CT_R)
         {
             XWPFRun run = new XWPFRun((CT_R)o, parent);
             runs.Add(run);
             bodyElements.Add(run);
         }
     }
 }
Esempio n. 4
0
 private void Init()
 {
     //copied from XWPFDocument...should centralize this code
     //to avoid duplication
     foreach (object o in ctFtnEdn.Items)
     {
         if (o is CT_P)
         {
             XWPFParagraph p = new XWPFParagraph((CT_P)o, this);
             bodyElements.Add(p);
             paragraphs.Add(p);
         }
         else if (o is CT_Tbl)
         {
             XWPFTable t = new XWPFTable((CT_Tbl)o, this);
             bodyElements.Add(t);
             tables.Add(t);
         }
         else if (o is CT_SdtBlock)
         {
             XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this);
             bodyElements.Add(c);
         }
     }
 }
Esempio n. 5
0
        private List <AbstractXWPFSDT> ExtractSDTsFromBodyElements(IList <IBodyElement> elements)
        {
            List <AbstractXWPFSDT> sdts = new List <AbstractXWPFSDT>();

            foreach (IBodyElement e in elements)
            {
                if (e is XWPFSDT)
                {
                    XWPFSDT sdt = (XWPFSDT)e;
                    sdts.Add(sdt);
                }
                else if (e is XWPFParagraph)
                {
                    XWPFParagraph p = (XWPFParagraph)e;
                    foreach (IRunElement e2 in p.IRuns)
                    {
                        if (e2 is XWPFSDT)
                        {
                            XWPFSDT sdt = (XWPFSDT)e2;
                            sdts.Add(sdt);
                        }
                    }
                }
                else if (e is XWPFTable)
                {
                    XWPFTable table = (XWPFTable)e;
                    sdts.AddRange(ExtractSDTsFromTable(table));
                }
            }
            return(sdts);
        }
Esempio n. 6
0
        public void TestGetSDTs()
        {
            String[] contents = new String[] {
                "header_rich_text",
                "Rich_text",
                "Rich_text_pre_table\nRich_text_cell1\t\t\t\n\nRich_text_post_table",
                "Plain_text_no_newlines",
                "Plain_text_with_newlines1\nplain_text_with_newlines2",
                "Watermelon",
                "Dirt",
                "4/16/2013",
                "rich_text_in_paragraph_in_cell",
                "Footer_rich_text",
                "Footnote_sdt",
                "Endnote_sdt"
            };
            XWPFDocument   doc  = XWPFTestDataSamples.OpenSampleDocument("Bug54849.docx");
            List <XWPFSDT> sdts = extractAllSDTs(doc);

            Assert.AreEqual(contents.Length, sdts.Count, "number of sdts");

            for (int i = 0; i < sdts.Count; i++)
            {//contents.Length; i++){
                XWPFSDT sdt = sdts[i];

                Assert.AreEqual(contents[i], sdt.Content.ToString(), i + ": " + contents[i]);
            }
        }
Esempio n. 7
0
 /**
  * Identifies (in order) the parts of the paragraph /
  *  sub-paragraph that correspond to character text
  *  runs, and builds the appropriate runs for these.
  */
 private void BuildRunsInOrderFromXml(ArrayList items)
 {
     foreach (object o in items)
     {
         if (o is CT_R)
         {
             XWPFRun r = new XWPFRun((CT_R)o, this);
             runs.Add(r);
             iRuns.Add(r);
         }
         if (o is CT_Hyperlink1)
         {
             CT_Hyperlink1 link = (CT_Hyperlink1)o;
             foreach (CT_R r in link.GetRList())
             {
                 //runs.Add(new XWPFHyperlinkRun(link, r, this));
                 XWPFHyperlinkRun hr = new XWPFHyperlinkRun(link, r, this);
                 runs.Add(hr);
                 iRuns.Add(hr);
             }
         }
         if (o is CT_SdtBlock)
         {
             XWPFSDT cc = new XWPFSDT((CT_SdtBlock)o, part);
             iRuns.Add(cc);
         }
         if (o is CT_SdtRun)
         {
             XWPFSDT cc = new XWPFSDT((CT_SdtRun)o, part);
             iRuns.Add(cc);
         }
         if (o is CT_RunTrackChange)
         {
             foreach (CT_R r in ((CT_RunTrackChange)o).GetRList())
             {
                 XWPFRun cr = new XWPFRun(r, this);
                 runs.Add(cr);
                 iRuns.Add(cr);
             }
         }
         if (o is CT_SimpleField)
         {
             foreach (CT_R r in ((CT_SimpleField)o).GetRList())
             {
                 XWPFRun cr = new XWPFRun(r, this);
                 runs.Add(cr);
                 iRuns.Add(cr);
             }
         }
         if (o is CT_SmartTagRun)
         {
             // Smart Tags can be nested many times.
             // This implementation does not preserve the tagging information
             BuildRunsInOrderFromXml((o as CT_SmartTagRun).Items);
         }
     }
 }
Esempio n. 8
0
        /// <summary>
        /// Read the document
        /// </summary>
        internal override void OnDocumentRead()
        {
            base.OnDocumentRead();
            HdrDocument hdrDocument = null;
            Stream      is1         = null;

            try
            {
                is1 = GetPackagePart().GetInputStream();
                XmlDocument xmldoc = DocumentHelper.LoadDocument(is1);
                hdrDocument  = HdrDocument.Parse(xmldoc, NamespaceManager);
                headerFooter = hdrDocument.Hdr;
                foreach (object o in headerFooter.Items)
                {
                    if (o is CT_P)
                    {
                        XWPFParagraph p = new XWPFParagraph((CT_P)o, this);
                        paragraphs.Add(p);
                        bodyElements.Add(p);
                    }
                    if (o is CT_Tbl)
                    {
                        XWPFTable t = new XWPFTable((CT_Tbl)o, this);
                        tables.Add(t);
                        bodyElements.Add(t);
                    }
                    if (o is CT_SdtBlock)
                    {
                        XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this);
                        bodyElements.Add(c);
                    }
                }
            }
            catch (Exception e)
            {
                throw new POIXMLException(e);
            }
            finally
            {
                if (is1 != null)
                {
                    is1.Close();
                }
            }
        }
Esempio n. 9
0
 /**
  * If a table cell does not include at least one block-level element, then this document shall be considered corrupt
  */
 public XWPFTableCell(CT_Tc cell, XWPFTableRow tableRow, IBody part)
 {
     this.ctTc     = cell;
     this.part     = part;
     this.tableRow = tableRow;
     // NB: If a table cell does not include at least one block-level element, then this document shall be considered corrupt.
     if (cell.GetPList().Count < 1)
     {
         cell.AddNewP();
     }
     bodyElements = new List <IBodyElement>();
     paragraphs   = new List <XWPFParagraph>();
     tables       = new List <XWPFTable>();
     foreach (object o in ctTc.Items)
     {
         if (o is CT_P)
         {
             XWPFParagraph p = new XWPFParagraph((CT_P)o, this);
             paragraphs.Add(p);
             bodyElements.Add(p);
         }
         if (o is CT_Tbl)
         {
             XWPFTable t = new XWPFTable((CT_Tbl)o, this);
             tables.Add(t);
             bodyElements.Add(t);
         }
         if (o is CT_SdtBlock)
         {
             XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this);
             bodyElements.Add(c);
         }
         if (o is CT_SdtRun)
         {
             XWPFSDT c = new XWPFSDT((CT_SdtRun)o, this);
             bodyElements.Add(c);
         }
     }
 }
Esempio n. 10
0
        public void TestNewLinesBetweenRuns()
        {
            XWPFDocument   doc   = XWPFTestDataSamples.OpenSampleDocument("Bug55142.docx");
            List <XWPFSDT> sdts  = extractAllSDTs(doc);
            List <String>  targs = new List <String>();

            //these test newlines and tabs in paragraphs/body elements
            targs.Add("Rich-text1 abcdefghi");
            targs.Add("Rich-text2 abcd\t\tefgh");
            targs.Add("Rich-text3 abcd\nefg");
            targs.Add("Rich-text4 abcdefg");
            targs.Add("Rich-text5 abcdefg\nhijk");
            targs.Add("Plain-text1 abcdefg");
            targs.Add("Plain-text2 abcdefg\nhijk\nlmnop");
            //this tests consecutive runs within a cell (not a paragraph)
            //this test case was triggered by Tika-1130
            targs.Add("sdt_incell2 abcdefg");

            for (int i = 0; i < sdts.Count; i++)
            {
                XWPFSDT sdt = sdts[i];
                Assert.AreEqual(targs[i], sdt.Content.Text, targs[i]);
            }
        }
Esempio n. 11
0
        internal override void OnDocumentRead()
        {
            try {
                XmlDocument xmldoc = ConvertStreamToXml(GetPackagePart().GetInputStream());
                DocumentDocument doc = DocumentDocument.Parse(xmldoc, NamespaceManager);
                ctDocument = doc.Document;

                InitFootnotes();
                // parse the document with cursor and add
                //    // the XmlObject to its lists

                foreach (object o in ctDocument.body.Items)
                {
                    if (o is CT_P)
                    {
                        XWPFParagraph p = new XWPFParagraph((CT_P)o, this);
                        bodyElements.Add(p);
                        paragraphs.Add(p);
                    }
                    else if (o is CT_Tbl)
                    {
                        XWPFTable t = new XWPFTable((CT_Tbl)o, this);
                        bodyElements.Add(t);
                        tables.Add(t);
                    }
                    else if (o is CT_SdtBlock)
                    {
                        XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this);
                        bodyElements.Add(c);
                        contentControls.Add(c);
                    }
                }
                // Sort out headers and footers
                if (doc.Document.body.sectPr != null)
                    headerFooterPolicy = new XWPFHeaderFooterPolicy(this);

                // Create for each XML-part in the Package a PartClass
                foreach (POIXMLDocumentPart p in GetRelations()) {
                    String relation = p.GetPackageRelationship().RelationshipType;
                    if (relation.Equals(XWPFRelation.STYLES.Relation))
                    {
                        this.styles = (XWPFStyles)p;
                        this.styles.OnDocumentRead();
                    }
                    else if (relation.Equals(XWPFRelation.NUMBERING.Relation))
                    {
                        this.numbering = (XWPFNumbering)p;
                        this.numbering.OnDocumentRead();
                    }
                    else if (relation.Equals(XWPFRelation.FOOTER.Relation))
                    {
                        XWPFFooter footer = (XWPFFooter)p;
                        footers.Add(footer);
                        footer.OnDocumentRead();
                    }
                    else if (relation.Equals(XWPFRelation.HEADER.Relation))
                    {
                        XWPFHeader header = (XWPFHeader)p;
                        headers.Add(header);
                        header.OnDocumentRead();
                    }
                    else if (relation.Equals(XWPFRelation.COMMENT.Relation))
                    {
                        XmlDocument xml = ConvertStreamToXml(p.GetPackagePart().GetInputStream());
                        CommentsDocument cmntdoc = CommentsDocument.Parse(xml ,NamespaceManager);
                        foreach (CT_Comment ctcomment in cmntdoc.Comments.comment)
                        {
                            comments.Add(new XWPFComment(ctcomment, this));
                        }
                    }
                    else if (relation.Equals(XWPFRelation.SETTINGS.Relation))
                    {
                        Settings = (XWPFSettings)p;
                        Settings.OnDocumentRead();
                    }
                    else if (relation.Equals(XWPFRelation.IMAGES.Relation))
                    {
                        XWPFPictureData picData = (XWPFPictureData)p;
                        picData.OnDocumentRead();
                        RegisterPackagePictureData(picData);
                        pictures.Add(picData);
                    }
                    else if (relation.Equals(XWPFRelation.GLOSSARY_DOCUMENT.Relation))
                    {
                        // We don't currently process the glossary itself
                        // Until we do, we do need to load the glossary child parts of it
                        foreach (POIXMLDocumentPart gp in p.GetRelations())
                        {
                            // Trigger the onDocumentRead for all the child parts
                            // Otherwise we'll hit issues on Styles, Settings etc on save
                            try
                            {
                                gp.OnDocumentRead();
                                //Method onDocumentRead = gp.getClass().getDeclaredMethod("onDocumentRead");
                                //onDocumentRead.setAccessible(true);
                                //onDocumentRead.invoke(gp);
                            }
                            catch (Exception e)
                            {
                                throw new POIXMLException(e);
                            }
                        }
                    }
                }
                InitHyperlinks();
            }
            catch (XmlException e)
            {
                throw new POIXMLException(e);
            }
            
        }
Esempio n. 12
0
 /**
  * If a table cell does not include at least one block-level element, then this document shall be considered corrupt
  */
 public XWPFTableCell(CT_Tc cell, XWPFTableRow tableRow, IBody part)
 {
     this.ctTc = cell;
     this.part = part;
     this.tableRow = tableRow;
     // NB: If a table cell does not include at least one block-level element, then this document shall be considered corrupt.
     if(cell.GetPList().Count<1)
         cell.AddNewP();
     bodyElements = new List<IBodyElement>();
     paragraphs = new List<XWPFParagraph>();
     tables = new List<XWPFTable>();
     foreach (object o in ctTc.Items)
     {
         if (o is CT_P)
         {
             XWPFParagraph p = new XWPFParagraph((CT_P)o, this);
             paragraphs.Add(p);
             bodyElements.Add(p);
         }
         if (o is CT_Tbl)
         {
             XWPFTable t = new XWPFTable((CT_Tbl)o, this);
             tables.Add(t);
             bodyElements.Add(t);
         }
         if (o is CT_SdtBlock)
         {
             XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this);
             bodyElements.Add(c);
         }
         if (o is CT_SdtRun)
         {
             XWPFSDT c = new XWPFSDT((CT_SdtRun)o, this);
             bodyElements.Add(c);
         }
     }
 }
Esempio n. 13
0
        /**
         * Identifies (in order) the parts of the paragraph /
         *  sub-paragraph that correspond to character text
         *  runs, and builds the appropriate runs for these.
         */
        private void BuildRunsInOrderFromXml(ArrayList items)
        {
            foreach (object o in items)
            {
                if (o is CT_R)
                {
                    XWPFRun r = new XWPFRun((CT_R)o, this);
                    runs.Add(r);
                    iRuns.Add(r);
                }
                if (o is CT_Hyperlink1)
                {
                    CT_Hyperlink1 link = (CT_Hyperlink1)o;
                    foreach (CT_R r in link.GetRList())
                    {
                        //runs.Add(new XWPFHyperlinkRun(link, r, this));
                        XWPFHyperlinkRun hr = new XWPFHyperlinkRun(link, r, this);
                        runs.Add(hr);
                        iRuns.Add(hr);

                    }
                }
                if (o is CT_SdtBlock)
                {
                    XWPFSDT cc = new XWPFSDT((CT_SdtBlock)o, part);
                    iRuns.Add(cc);
                }
                if (o is CT_SdtRun)
                {
                    XWPFSDT cc = new XWPFSDT((CT_SdtRun)o, part);
                    iRuns.Add(cc);
                }
                if (o is CT_RunTrackChange)
                {
                    foreach (CT_R r in ((CT_RunTrackChange)o).GetRList())
                    {
                        XWPFRun cr = new XWPFRun(r, this);
                        runs.Add(cr);
                        iRuns.Add(cr);
                    }
                }
                if (o is CT_SimpleField)
                {
                    foreach (CT_R r in ((CT_SimpleField)o).GetRList())
                    {
                        XWPFRun cr = new XWPFRun(r, this);
                        runs.Add(cr);
                        iRuns.Add(cr);
                    }
                }
                if (o is CT_SmartTagRun)
                {
                    // Smart Tags can be nested many times. 
                    // This implementation does not preserve the tagging information
                    BuildRunsInOrderFromXml((o as CT_SmartTagRun).Items);
                }
            }
        }
Esempio n. 14
0
 internal override void OnDocumentRead()
 {
     base.OnDocumentRead();
     FtrDocument ftrDocument = null;
     try {
         XmlDocument xmldoc = ConvertStreamToXml(GetPackagePart().GetInputStream());
         ftrDocument = FtrDocument.Parse(xmldoc, NamespaceManager);
         headerFooter = ftrDocument.Ftr;
         // parse the document with cursor and add
         // the XmlObject to its lists
         foreach (object o in headerFooter.Items)
         {
             if (o is CT_P)
             {
                 XWPFParagraph p = new XWPFParagraph((CT_P)o, this);
                 paragraphs.Add(p);
                 bodyElements.Add(p);
             }
             if (o is CT_Tbl)
             {
                 XWPFTable t = new XWPFTable((CT_Tbl)o, this);
                 tables.Add(t);
                 bodyElements.Add(t);
             }
             if (o is CT_SdtBlock)
             {
                 XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this);
                 bodyElements.Add(c);
             }
         }
     } catch (Exception e) {
         throw new POIXMLException(e);
     }
 }
Esempio n. 15
0
 /// <summary>
 /// Read the document
 /// </summary>
 internal override void OnDocumentRead()
 {
     base.OnDocumentRead();
     HdrDocument hdrDocument = null;
     try
     {
         XmlDocument xmldoc = DocumentHelper.LoadDocument(GetPackagePart().GetInputStream());
         hdrDocument = HdrDocument.Parse(xmldoc, NamespaceManager);
         headerFooter = hdrDocument.Hdr;
         foreach (object o in headerFooter.Items)
         {
             if (o is CT_P)
             {
                 XWPFParagraph p = new XWPFParagraph((CT_P)o, this);
                 paragraphs.Add(p);
                 bodyElements.Add(p);
             }
             if (o is CT_Tbl)
             {
                 XWPFTable t = new XWPFTable((CT_Tbl)o, this);
                 tables.Add(t);
                 bodyElements.Add(t);
             }
             if (o is CT_SdtBlock)
             {
                 XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this);
                 bodyElements.Add(c);
             }
         }
     }
     catch (Exception e)
     {
         throw new POIXMLException(e);
     }
 }