diff --git a/OpenXmlFormats/Wordprocessing/Document.cs b/OpenXmlFormats/Wordprocessing/Document.cs index 96dbee52a..69fb9110c 100644 --- a/OpenXmlFormats/Wordprocessing/Document.cs +++ b/OpenXmlFormats/Wordprocessing/Document.cs @@ -75,14 +75,14 @@ public class CT_Document : CT_DocumentBase { public static CT_Document Parse(XmlNode node, XmlNamespaceManager namespaceManager) { - if (node == null) + if(node == null) return null; CT_Document ctObj = new CT_Document(); - foreach (XmlNode childNode in node.ChildNodes) + foreach(XmlNode childNode in node.ChildNodes) { - if (childNode.LocalName == "body") + if(childNode.LocalName == "body") ctObj.body = CT_Body.Parse(childNode, namespaceManager); - else if (childNode.LocalName == "background") + else if(childNode.LocalName == "background") ctObj.background = CT_Background.Parse(childNode, namespaceManager); } return ctObj; @@ -108,9 +108,9 @@ internal void Write(StreamWriter sw) sw.Write("xmlns:w16se=\"http://schemas.microsoft.com/office/word/2015/wordml/symex\" xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\" xmlns:wpi=\"http://schemas.microsoft.com/office/word/2010/wordprocessingInk\" "); sw.Write("xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" "); sw.Write("mc:Ignorable=\"w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14\">"); - if (this.body != null) + if(this.body != null) this.body.Write(sw, "body"); - if (this.background != null) + if(this.background != null) this.background.Write(sw, "background"); sw.Write(""); } @@ -162,161 +162,161 @@ public CT_Body() } public static CT_Body Parse(XmlNode node, XmlNamespaceManager namespaceManager) { - if (node == null) + if(node == null) return null; CT_Body ctObj = new CT_Body(); - foreach (XmlNode childNode in node.ChildNodes) + foreach(XmlNode childNode in node.ChildNodes) { - if (childNode.LocalName == "moveTo") + if(childNode.LocalName == "moveTo") { ctObj.Items.Add(CT_RunTrackChange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveTo); } - else if (childNode.LocalName == "sectPr") + else if(childNode.LocalName == "sectPr") { ctObj.sectPr = CT_SectPr.Parse(childNode, namespaceManager); } - else if (childNode.LocalName == "oMathPara") + else if(childNode.LocalName == "oMathPara") { ctObj.Items.Add(CT_OMathPara.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.oMathPara); } - else if (childNode.LocalName == "customXml") + else if(childNode.LocalName == "customXml") { ctObj.Items.Add(CT_CustomXmlBlock.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXml); } - else if (childNode.LocalName == "oMath") + else if(childNode.LocalName == "oMath") { ctObj.Items.Add(CT_OMath.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.oMath); } - else if (childNode.LocalName == "altChunk") + else if(childNode.LocalName == "altChunk") { ctObj.Items.Add(CT_AltChunk.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.altChunk); } - else if (childNode.LocalName == "bookmarkEnd") + else if(childNode.LocalName == "bookmarkEnd") { ctObj.Items.Add(CT_MarkupRange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.bookmarkEnd); } - else if (childNode.LocalName == "bookmarkStart") + else if(childNode.LocalName == "bookmarkStart") { ctObj.Items.Add(CT_Bookmark.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.bookmarkStart); } - else if (childNode.LocalName == "commentRangeEnd") + else if(childNode.LocalName == "commentRangeEnd") { ctObj.Items.Add(CT_MarkupRange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.commentRangeEnd); } - else if (childNode.LocalName == "commentRangeStart") + else if(childNode.LocalName == "commentRangeStart") { ctObj.Items.Add(CT_MarkupRange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.commentRangeStart); } - else if (childNode.LocalName == "customXmlDelRangeEnd") + else if(childNode.LocalName == "customXmlDelRangeEnd") { ctObj.Items.Add(CT_Markup.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlDelRangeEnd); } - else if (childNode.LocalName == "customXmlDelRangeStart") + else if(childNode.LocalName == "customXmlDelRangeStart") { ctObj.Items.Add(CT_TrackChange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlDelRangeStart); } - else if (childNode.LocalName == "customXmlInsRangeEnd") + else if(childNode.LocalName == "customXmlInsRangeEnd") { ctObj.Items.Add(CT_Markup.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlInsRangeEnd); } - else if (childNode.LocalName == "customXmlInsRangeStart") + else if(childNode.LocalName == "customXmlInsRangeStart") { ctObj.Items.Add(CT_TrackChange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlInsRangeStart); } - else if (childNode.LocalName == "customXmlMoveFromRangeEnd") + else if(childNode.LocalName == "customXmlMoveFromRangeEnd") { ctObj.Items.Add(CT_Markup.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlMoveFromRangeEnd); } - else if (childNode.LocalName == "customXmlMoveFromRangeStart") + else if(childNode.LocalName == "customXmlMoveFromRangeStart") { ctObj.Items.Add(CT_TrackChange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlMoveFromRangeStart); } - else if (childNode.LocalName == "customXmlMoveToRangeEnd") + else if(childNode.LocalName == "customXmlMoveToRangeEnd") { ctObj.Items.Add(CT_Markup.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlMoveToRangeEnd); } - else if (childNode.LocalName == "customXmlMoveToRangeStart") + else if(childNode.LocalName == "customXmlMoveToRangeStart") { ctObj.Items.Add(CT_TrackChange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlMoveToRangeStart); } - else if (childNode.LocalName == "del") + else if(childNode.LocalName == "del") { ctObj.Items.Add(CT_RunTrackChange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.del); } - else if (childNode.LocalName == "ins") + else if(childNode.LocalName == "ins") { ctObj.Items.Add(CT_RunTrackChange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.ins); } - else if (childNode.LocalName == "moveFrom") + else if(childNode.LocalName == "moveFrom") { ctObj.Items.Add(CT_RunTrackChange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveFrom); } - else if (childNode.LocalName == "moveFromRangeEnd") + else if(childNode.LocalName == "moveFromRangeEnd") { ctObj.Items.Add(CT_MarkupRange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveFromRangeEnd); } - else if (childNode.LocalName == "moveFromRangeStart") + else if(childNode.LocalName == "moveFromRangeStart") { ctObj.Items.Add(CT_MoveBookmark.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveFromRangeStart); } - else if (childNode.LocalName == "moveToRangeEnd") + else if(childNode.LocalName == "moveToRangeEnd") { ctObj.Items.Add(CT_MarkupRange.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveToRangeEnd); } - else if (childNode.LocalName == "moveToRangeStart") + else if(childNode.LocalName == "moveToRangeStart") { ctObj.Items.Add(CT_MoveBookmark.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveToRangeStart); } - else if (childNode.LocalName == "p") + else if(childNode.LocalName == "p") { ctObj.Items.Add(CT_P.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.p); } - else if (childNode.LocalName == "permEnd") + else if(childNode.LocalName == "permEnd") { ctObj.Items.Add(CT_Perm.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.permEnd); } - else if (childNode.LocalName == "permStart") + else if(childNode.LocalName == "permStart") { ctObj.Items.Add(CT_PermStart.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.permStart); } - else if (childNode.LocalName == "proofErr") + else if(childNode.LocalName == "proofErr") { ctObj.Items.Add(CT_ProofErr.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.proofErr); } - else if (childNode.LocalName == "sdt") + else if(childNode.LocalName == "sdt") { ctObj.Items.Add(CT_SdtBlock.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.sdt); } - else if (childNode.LocalName == "tbl") + else if(childNode.LocalName == "tbl") { ctObj.Items.Add(CT_Tbl.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.tbl); @@ -335,71 +335,71 @@ internal void Write(StreamWriter sw, string nodeName) sw.Write(string.Format(""); int i=0; - foreach (object o in this.Items) - { - if (o is CT_RunTrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveTo) - ((CT_RunTrackChange)o).Write(sw, "moveTo"); - else if (o is CT_OMathPara) - ((CT_OMathPara)o).Write(sw, "oMathPara"); - else if (o is CT_CustomXmlBlock) - ((CT_CustomXmlBlock)o).Write(sw, "customXml"); - else if (o is CT_OMath) - ((CT_OMath)o).Write(sw, "oMath"); - else if (o is CT_AltChunk) - ((CT_AltChunk)o).Write(sw, "altChunk"); - else if ((o is CT_MarkupRange)&&this.itemsElementNameField[i]== DocumentBodyItemChoiceType.bookmarkEnd) - ((CT_MarkupRange)o).Write(sw, "bookmarkEnd"); - else if (o is CT_Bookmark && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.bookmarkStart) - ((CT_Bookmark)o).Write(sw, "bookmarkStart"); - else if (o is CT_MarkupRange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.commentRangeEnd) - ((CT_MarkupRange)o).Write(sw, "commentRangeEnd"); - else if (o is CT_MarkupRange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.commentRangeStart) - ((CT_MarkupRange)o).Write(sw, "commentRangeStart"); - else if (o is CT_Markup && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlDelRangeEnd) - ((CT_Markup)o).Write(sw, "customXmlDelRangeEnd"); - else if (o is CT_TrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlDelRangeStart) - ((CT_TrackChange)o).Write(sw, "customXmlDelRangeStart"); - else if (o is CT_Markup && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlInsRangeEnd) - ((CT_Markup)o).Write(sw, "customXmlInsRangeEnd"); - else if (o is CT_TrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlInsRangeStart) - ((CT_TrackChange)o).Write(sw, "customXmlInsRangeStart"); - else if (o is CT_Markup && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlMoveFromRangeEnd) - ((CT_Markup)o).Write(sw, "customXmlMoveFromRangeEnd"); - else if (o is CT_TrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlMoveFromRangeStart) - ((CT_TrackChange)o).Write(sw, "customXmlMoveFromRangeStart"); - else if (o is CT_Markup && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlMoveToRangeEnd) - ((CT_Markup)o).Write(sw, "customXmlMoveToRangeEnd"); - else if (o is CT_TrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlMoveToRangeStart) - ((CT_TrackChange)o).Write(sw, "customXmlMoveToRangeStart"); - else if (o is CT_RunTrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.del) - ((CT_RunTrackChange)o).Write(sw, "del"); - else if (o is CT_RunTrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.ins) - ((CT_RunTrackChange)o).Write(sw, "ins"); - else if (o is CT_RunTrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveFrom) - ((CT_RunTrackChange)o).Write(sw, "moveFrom"); - else if (o is CT_MarkupRange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveFromRangeEnd) - ((CT_MarkupRange)o).Write(sw, "moveFromRangeEnd"); - else if (o is CT_MoveBookmark && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveFromRangeStart) - ((CT_MoveBookmark)o).Write(sw, "moveFromRangeStart"); - else if (o is CT_MarkupRange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveToRangeEnd) - ((CT_MarkupRange)o).Write(sw, "moveToRangeEnd"); - else if (o is CT_MoveBookmark && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveToRangeStart) - ((CT_MoveBookmark)o).Write(sw, "moveToRangeStart"); - else if (o is CT_P) - ((CT_P)o).Write(sw, "p"); - else if (o is CT_Perm) - ((CT_Perm)o).Write(sw, "permEnd"); - else if (o is CT_PermStart) - ((CT_PermStart)o).Write(sw, "permStart"); - else if (o is CT_ProofErr) - ((CT_ProofErr)o).Write(sw, "proofErr"); - else if (o is CT_SdtBlock) - ((CT_SdtBlock)o).Write(sw, "sdt"); - else if (o is CT_Tbl) - ((CT_Tbl)o).Write(sw, "tbl"); + foreach(object o in this.Items) + { + if(o is CT_RunTrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveTo) + ((CT_RunTrackChange) o).Write(sw, "moveTo"); + else if(o is CT_OMathPara) + ((CT_OMathPara) o).Write(sw, "oMathPara"); + else if(o is CT_CustomXmlBlock) + ((CT_CustomXmlBlock) o).Write(sw, "customXml"); + else if(o is CT_OMath) + ((CT_OMath) o).Write(sw, "oMath"); + else if(o is CT_AltChunk) + ((CT_AltChunk) o).Write(sw, "altChunk"); + else if((o is CT_MarkupRange)&&this.itemsElementNameField[i]== DocumentBodyItemChoiceType.bookmarkEnd) + ((CT_MarkupRange) o).Write(sw, "bookmarkEnd"); + else if(o is CT_Bookmark && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.bookmarkStart) + ((CT_Bookmark) o).Write(sw, "bookmarkStart"); + else if(o is CT_MarkupRange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.commentRangeEnd) + ((CT_MarkupRange) o).Write(sw, "commentRangeEnd"); + else if(o is CT_MarkupRange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.commentRangeStart) + ((CT_MarkupRange) o).Write(sw, "commentRangeStart"); + else if(o is CT_Markup && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlDelRangeEnd) + ((CT_Markup) o).Write(sw, "customXmlDelRangeEnd"); + else if(o is CT_TrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlDelRangeStart) + ((CT_TrackChange) o).Write(sw, "customXmlDelRangeStart"); + else if(o is CT_Markup && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlInsRangeEnd) + ((CT_Markup) o).Write(sw, "customXmlInsRangeEnd"); + else if(o is CT_TrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlInsRangeStart) + ((CT_TrackChange) o).Write(sw, "customXmlInsRangeStart"); + else if(o is CT_Markup && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlMoveFromRangeEnd) + ((CT_Markup) o).Write(sw, "customXmlMoveFromRangeEnd"); + else if(o is CT_TrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlMoveFromRangeStart) + ((CT_TrackChange) o).Write(sw, "customXmlMoveFromRangeStart"); + else if(o is CT_Markup && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlMoveToRangeEnd) + ((CT_Markup) o).Write(sw, "customXmlMoveToRangeEnd"); + else if(o is CT_TrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.customXmlMoveToRangeStart) + ((CT_TrackChange) o).Write(sw, "customXmlMoveToRangeStart"); + else if(o is CT_RunTrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.del) + ((CT_RunTrackChange) o).Write(sw, "del"); + else if(o is CT_RunTrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.ins) + ((CT_RunTrackChange) o).Write(sw, "ins"); + else if(o is CT_RunTrackChange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveFrom) + ((CT_RunTrackChange) o).Write(sw, "moveFrom"); + else if(o is CT_MarkupRange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveFromRangeEnd) + ((CT_MarkupRange) o).Write(sw, "moveFromRangeEnd"); + else if(o is CT_MoveBookmark && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveFromRangeStart) + ((CT_MoveBookmark) o).Write(sw, "moveFromRangeStart"); + else if(o is CT_MarkupRange && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveToRangeEnd) + ((CT_MarkupRange) o).Write(sw, "moveToRangeEnd"); + else if(o is CT_MoveBookmark && this.itemsElementNameField[i] == DocumentBodyItemChoiceType.moveToRangeStart) + ((CT_MoveBookmark) o).Write(sw, "moveToRangeStart"); + else if(o is CT_P) + ((CT_P) o).Write(sw, "p"); + else if(o is CT_Perm) + ((CT_Perm) o).Write(sw, "permEnd"); + else if(o is CT_PermStart) + ((CT_PermStart) o).Write(sw, "permStart"); + else if(o is CT_ProofErr) + ((CT_ProofErr) o).Write(sw, "proofErr"); + else if(o is CT_SdtBlock) + ((CT_SdtBlock) o).Write(sw, "sdt"); + else if(o is CT_Tbl) + ((CT_Tbl) o).Write(sw, "tbl"); i++; } - if (this.sectPr != null) + if(this.sectPr != null) this.sectPr.Write(sw, "sectPr"); sw.WriteEndW(nodeName); } @@ -461,14 +461,14 @@ public ArrayList Items public CT_P AddNewP() { CT_P p = new CT_P(); - lock (this) + lock(this) { this.itemsField.Add(p); this.itemsElementNameField.Add(DocumentBodyItemChoiceType.p); } return p; } - + [XmlElement("ItemsElementName", Order = 1)] [XmlIgnore] public List ItemsElementName @@ -498,7 +498,7 @@ public CT_SectPr sectPr public CT_Tbl AddNewTbl(int? pos = null) { CT_Tbl tbl = new CT_Tbl(); - lock (this) + lock(this) { if(pos.HasValue) { @@ -562,15 +562,15 @@ public CT_SdtBlock AddNewSdt() return AddNewObject(DocumentBodyItemChoiceType.sdt); } #region Generic methods for object operation - + private List GetObjectList(DocumentBodyItemChoiceType type) where T : class { - lock (this) + lock(this) { List list = new List(); - for (int i = 0; i < itemsElementNameField.Count; i++) + for(int i = 0; i < itemsElementNameField.Count; i++) { - if (itemsElementNameField[i] == type) + if(itemsElementNameField[i] == type) list.Add(itemsField[i] as T); } return list; @@ -578,12 +578,12 @@ private List GetObjectList(DocumentBodyItemChoiceType type) where T : clas } private int SizeOfArray(DocumentBodyItemChoiceType type) { - lock (this) + lock(this) { int size = 0; - for (int i = 0; i < itemsElementNameField.Count; i++) + for(int i = 0; i < itemsElementNameField.Count; i++) { - if (itemsElementNameField[i] == type) + if(itemsElementNameField[i] == type) size++; } return size; @@ -591,10 +591,10 @@ private int SizeOfArray(DocumentBodyItemChoiceType type) } private T GetObjectArray(int p, DocumentBodyItemChoiceType type) where T : class { - lock (this) + lock(this) { int pos = GetObjectIndex(type, p); - if (pos < 0 || pos >= this.itemsField.Count) + if(pos < 0 || pos >= this.itemsField.Count) return null; return itemsField[pos] as T; } @@ -602,24 +602,24 @@ private T GetObjectArray(int p, DocumentBodyItemChoiceType type) where T : cl private T AddNewObject(DocumentBodyItemChoiceType type) where T : class, new() { T t = new T(); - lock (this) + lock(this) { this.itemsElementNameField.Add(type); this.itemsField.Add(t); } return t; } - private void SetObject (DocumentBodyItemChoiceType type, int p, T obj) where T : class + private void SetObject(DocumentBodyItemChoiceType type, int p, T obj) where T : class { - lock (this) + lock(this) { int pos = GetObjectIndex(type, p); - if (pos < 0 || pos >= this.itemsField.Count) + if(pos < 0 || pos >= this.itemsField.Count) return; - if (this.itemsField[pos] is T) + if(this.itemsField[pos] is T) this.itemsField[pos] = obj; else - throw new Exception(string.Format(@"object types are difference, itemsField[{0}] is {1}, and parameter obj is {2}", + throw new Exception(string.Format(@"object types are difference, itemsField[{0}] is {1}, and parameter obj is {2}", pos, this.itemsField[pos].GetType().Name, typeof(T).Name)); } } @@ -627,11 +627,11 @@ private int GetObjectIndex(DocumentBodyItemChoiceType type, int p) { int index = -1; int pos = 0; - for (int i = 0; i < itemsElementNameField.Count; i++) + for(int i = 0; i < itemsElementNameField.Count; i++) { - if (itemsElementNameField[i] == type) + if(itemsElementNameField[i] == type) { - if (pos == p) + if(pos == p) { index = i; break; @@ -644,10 +644,10 @@ private int GetObjectIndex(DocumentBodyItemChoiceType type, int p) } private void RemoveObject(DocumentBodyItemChoiceType type, int p) { - lock (this) + lock(this) { int pos = GetObjectIndex(type, p); - if (pos < 0 || pos >= this.itemsField.Count) + if(pos < 0 || pos >= this.itemsField.Count) return; itemsElementNameField.RemoveAt(pos); itemsField.RemoveAt(pos); @@ -670,78 +670,78 @@ public void SetPArray(int pos, CT_P cT_P) public enum DocumentBodyItemChoiceType { - + [XmlEnum("http://schemas.openxmlformats.org/officeDocument/2006/math:oMath")] oMath, - + [XmlEnum("http://schemas.openxmlformats.org/officeDocument/2006/math:oMathPara")] oMathPara, - + altChunk, - + bookmarkEnd, - + bookmarkStart, - + commentRangeEnd, - + commentRangeStart, - + customXml, - + customXmlDelRangeEnd, - + customXmlDelRangeStart, - + customXmlInsRangeEnd, - + customXmlInsRangeStart, - + customXmlMoveFromRangeEnd, - + customXmlMoveFromRangeStart, - + customXmlMoveToRangeEnd, - + customXmlMoveToRangeStart, - + del, - + ins, - + moveFrom, - + moveFromRangeEnd, - + moveFromRangeStart, - + moveTo, - + moveToRangeEnd, - + moveToRangeStart, /// @@ -749,19 +749,19 @@ public enum DocumentBodyItemChoiceType /// p, - + permEnd, - + permStart, - + proofErr, - + sdt, - + tbl, } @@ -951,13 +951,13 @@ public ST_DocPartBehavior val public enum ST_DocPartBehavior { - + content, - + p, - + pg, } @@ -1035,118 +1035,118 @@ public ST_DocPartGallery val public enum ST_DocPartGallery { - + placeholder, - + any, - + @default, - + docParts, - + coverPg, - + eq, - + ftrs, - + hdrs, - + pgNum, - + tbls, - + watermarks, - + autoTxt, - + txtBox, - + pgNumT, - + pgNumB, - + pgNumMargins, - + tblOfContents, - + bib, - + custQuickParts, - + custCoverPg, - + custEq, - + custFtrs, - + custHdrs, - + custPgNum, - + custTbls, - + custWatermarks, - + custAutoTxt, - + custTxtBox, - + custPgNumT, - + custPgNumB, - + custPgNumMargins, - + custTblOfContents, - + custBib, - + custom1, - + custom2, - + custom3, - + custom4, - + custom5, } @@ -1289,25 +1289,25 @@ public ST_DocPartType val public enum ST_DocPartType { - + none, - + normal, - + autoExp, - + toolbar, - + speller, - + formFld, - + bbPlcHdr, } @@ -1316,25 +1316,25 @@ public enum ST_DocPartType public enum ItemsChoiceType11 { - + behaviors, - + category, - + description, - + guid, - + name, - + style, - + types, } @@ -1359,11 +1359,11 @@ public CT_DocGrid() public static CT_DocGrid Parse(XmlNode node, XmlNamespaceManager namespaceManager) { - if (node == null) + if(node == null) return null; CT_DocGrid ctObj = new CT_DocGrid(); - if (node.Attributes["w:type"] != null) - ctObj.type = (ST_DocGrid)Enum.Parse(typeof(ST_DocGrid), node.Attributes["w:type"].Value); + if(node.Attributes["w:type"] != null) + ctObj.type = (ST_DocGrid) Enum.Parse(typeof(ST_DocGrid), node.Attributes["w:type"].Value); ctObj.linePitch = XmlHelper.ReadString(node.Attributes["w:linePitch"]); ctObj.charSpace = XmlHelper.ReadString(node.Attributes["w:charSpace"]); return ctObj; @@ -1440,16 +1440,16 @@ public string charSpace public enum ST_DocGrid { - + @default, - + lines, - + linesAndChars, - + snapToChars, } @@ -1479,6 +1479,33 @@ public List docVar this.docVarField = value; } } + + public static CT_DocVars Parse(XmlNode node, XmlNamespaceManager namespaceManager) + { + if(node == null) + return null; + + CT_DocVars ctObj = new CT_DocVars(); + ctObj.docVar = new List(); + foreach(XmlNode childNode in node.ChildNodes) + ctObj.docVar.Add(CT_DocVar.Parse(childNode, namespaceManager)); + + return ctObj; + } + + internal void Write(StreamWriter sw, string nodeName) + { + sw.Write(string.Format(""); + if(this.docVar != null) + { + foreach(CT_DocVar x in this.docVar) + { + x.Write(sw, "docVar"); + } + } + sw.WriteEndW(nodeName); + } } } diff --git a/OpenXmlFormats/Wordprocessing/Settings.cs b/OpenXmlFormats/Wordprocessing/Settings.cs index e7c5e224d..ca4f35c3f 100644 --- a/OpenXmlFormats/Wordprocessing/Settings.cs +++ b/OpenXmlFormats/Wordprocessing/Settings.cs @@ -22,7 +22,6 @@ public static CT_Settings Parse(XmlNode node, XmlNamespaceManager namespaceManag return null; CT_Settings ctObj = new CT_Settings(); ctObj.activeWritingStyle = new List(); - ctObj.docVars = new List(); ctObj.attachedSchema = new List(); ctObj.smartTagType = new List(); ctObj.schemaLibrary = new List(); @@ -219,7 +218,7 @@ public static CT_Settings Parse(XmlNode node, XmlNamespaceManager namespaceManag else if (childNode.LocalName == "activeWritingStyle") ctObj.activeWritingStyle.Add(CT_WritingStyle.Parse(childNode, namespaceManager)); else if (childNode.LocalName == "docVars") - ctObj.docVars.Add(CT_DocVar.Parse(childNode, namespaceManager)); + ctObj.docVars = CT_DocVars.Parse(childNode, namespaceManager); else if (childNode.LocalName == "attachedSchema") ctObj.attachedSchema.Add(CT_String.Parse(childNode, namespaceManager)); else if (childNode.LocalName == "smartTagType") @@ -433,12 +432,7 @@ internal void Write(StreamWriter sw) } } if (this.docVars != null) - { - foreach (CT_DocVar x in this.docVars) - { - x.Write(sw, "docVars"); - } - } + this.docVars.Write(sw, "docVars"); if (this.attachedSchema != null) { foreach (CT_String x in this.attachedSchema) @@ -625,7 +619,7 @@ internal void Write(StreamWriter sw) private CT_Compat compatField; - private List docVarsField; + private CT_DocVars docVarsField; private CT_DocRsids rsidsField; @@ -1840,9 +1834,8 @@ public CT_Compat compat } } - [XmlArray(Order = 81)] - [XmlArrayItem("docVar", IsNullable = false)] - public List docVars + [XmlElement(Order = 81)] + public CT_DocVars docVars { get { @@ -6105,8 +6098,10 @@ public static CT_DocVar Parse(XmlNode node, XmlNamespaceManager namespaceManager if (node == null) return null; CT_DocVar ctObj = new CT_DocVar(); + ctObj.name = XmlHelper.ReadString(node.Attributes["w:name"]); ctObj.val = XmlHelper.ReadString(node.Attributes["w:val"]); + return ctObj; } diff --git a/testcases/ooxml/XWPF/UserModel/TestXWPFDocument.cs b/testcases/ooxml/XWPF/UserModel/TestXWPFDocument.cs index c8ddeadbf..af7a95dc8 100644 --- a/testcases/ooxml/XWPF/UserModel/TestXWPFDocument.cs +++ b/testcases/ooxml/XWPF/UserModel/TestXWPFDocument.cs @@ -19,6 +19,7 @@ namespace TestCases.XWPF.UserModel { using NPOI; using NPOI.OpenXml4Net.OPC; + using NPOI.OpenXmlFormats.Wordprocessing; using NPOI.Util; using NPOI.XWPF; using NPOI.XWPF.Extractor; @@ -40,13 +41,13 @@ public void TestContainsMainContentType() OPCPackage pack = doc.Package; bool found = false; - foreach (PackagePart part in pack.GetParts()) + foreach(PackagePart part in pack.GetParts()) { - if (part.ContentType.Equals(XWPFRelation.DOCUMENT.ContentType)) + if(part.ContentType.Equals(XWPFRelation.DOCUMENT.ContentType)) { found = true; } - if (false == found) + if(false == found) { // successful tests should be silent System.Console.WriteLine(part); @@ -143,7 +144,7 @@ public void TestAddPicture() byte[] newJpeg = ((XWPFPictureData)doc.GetRelationById(relationId)).Data; Assert.AreEqual(newJpeg.Length, jpeg.Length); - for (int i = 0; i < jpeg.Length; i++) + for(int i = 0; i < jpeg.Length; i++) { Assert.AreEqual(newJpeg[i], jpeg[i]); } @@ -153,17 +154,17 @@ public void TestAllPictureFormats() { XWPFDocument doc = new XWPFDocument(); - doc.AddPictureData(new byte[10], (int)PictureType.EMF); - doc.AddPictureData(new byte[11], (int)PictureType.WMF); - doc.AddPictureData(new byte[12], (int)PictureType.PICT); - doc.AddPictureData(new byte[13], (int)PictureType.JPEG); - doc.AddPictureData(new byte[14], (int)PictureType.PNG); - doc.AddPictureData(new byte[15], (int)PictureType.DIB); - doc.AddPictureData(new byte[16], (int)PictureType.GIF); - doc.AddPictureData(new byte[17], (int)PictureType.TIFF); - doc.AddPictureData(new byte[18], (int)PictureType.EPS); - doc.AddPictureData(new byte[19], (int)PictureType.BMP); - doc.AddPictureData(new byte[20], (int)PictureType.WPG); + doc.AddPictureData(new byte[10], (int) PictureType.EMF); + doc.AddPictureData(new byte[11], (int) PictureType.WMF); + doc.AddPictureData(new byte[12], (int) PictureType.PICT); + doc.AddPictureData(new byte[13], (int) PictureType.JPEG); + doc.AddPictureData(new byte[14], (int) PictureType.PNG); + doc.AddPictureData(new byte[15], (int) PictureType.DIB); + doc.AddPictureData(new byte[16], (int) PictureType.GIF); + doc.AddPictureData(new byte[17], (int) PictureType.TIFF); + doc.AddPictureData(new byte[18], (int) PictureType.EPS); + doc.AddPictureData(new byte[19], (int) PictureType.BMP); + doc.AddPictureData(new byte[20], (int) PictureType.WPG); Assert.AreEqual(11, doc.AllPictures.Count); @@ -287,7 +288,7 @@ public void TestGetAllPictures() Assert.IsNotNull(allPictures); Assert.AreEqual(3, allPictures.Count); - foreach (XWPFPictureData xwpfPictureData in allPictures) + foreach(XWPFPictureData xwpfPictureData in allPictures) { Assert.IsTrue(allPackagePictures.Contains(xwpfPictureData)); } @@ -297,7 +298,7 @@ public void TestGetAllPictures() allPictures.Add(allPictures[0]); Assert.Fail("This list must be unmodifiable!"); } - catch (NotSupportedException) + catch(NotSupportedException) { // all ok } @@ -319,7 +320,7 @@ public void TestGetAllPackagePictures() allPackagePictures.Add(allPackagePictures[0]); Assert.Fail("This list must be unmodifiable!"); } - catch (NotSupportedException) + catch(NotSupportedException) { // all ok } @@ -406,22 +407,49 @@ public void TestEnforcedWith() } [Test] - [Ignore("XWPF should be able to write to a new Stream when opened Read-Only")] - public void TestWriteFromReadOnlyOPC() { - OPCPackage opc = OPCPackage.Open( - POIDataSamples.GetDocumentInstance().GetFileInfo("SampleDoc.docx"), - PackageAccess.READ - ); - XWPFDocument doc = new XWPFDocument(opc); - XWPFWordExtractor ext = new XWPFWordExtractor(doc); - String origText = ext.Text; - - doc = XWPFTestDataSamples.WriteOutAndReadBack(doc); - ext = new XWPFWordExtractor(doc); - - Assert.AreEqual(origText, ext.Text); - } + [Ignore("XWPF should be able to write to a new Stream when opened Read-Only")] + public void TestWriteFromReadOnlyOPC() + { + OPCPackage opc = OPCPackage.Open( + POIDataSamples.GetDocumentInstance().GetFileInfo("SampleDoc.docx"), + PackageAccess.READ + ); + XWPFDocument doc = new XWPFDocument(opc); + XWPFWordExtractor ext = new XWPFWordExtractor(doc); + String origText = ext.Text; - } + doc = XWPFTestDataSamples.WriteOutAndReadBack(doc); + ext = new XWPFWordExtractor(doc); + + Assert.AreEqual(origText, ext.Text); + } + [Test] + public void TestDocVars() + { + XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("docVars.docx"); + + foreach(var part in doc.RelationParts) + { + var relationType = part.Relationship.RelationshipType; + if(relationType == XWPFRelation.SETTINGS.Relation) + { + var settingsPart = (XWPFSettings)part.DocumentPart; + + using(var stream = settingsPart.GetPackagePart().GetInputStream()) + { + var xmldoc = POIXMLDocumentPart.ConvertStreamToXml(stream); + var ctSettings = SettingsDocument.Parse(xmldoc, POIXMLDocumentPart.NamespaceManager).Settings; + var variables = ctSettings.docVars; + + Assert.IsNotNull(variables); + Assert.AreEqual(5, variables.docVar.Count); + + for(int i = 0; i