public static XmlNodeList SelectSubNodes(XmlDocument doc, string xPath, OpenXmlFormat format = OpenXmlFormat.Transitional)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            CommonNamespaces cns = new CommonNamespaces(doc.NameTable, format);
            nsmgr.AddNamespace("w", cns.GetAtomicName(NamespaceId.w));

            XmlNodeList nodes = doc.SelectNodes(xPath, nsmgr);

            return nodes;
        }
Beispiel #2
0
        public CommonNamespaces(XmlNameTable nametable, OpenXmlFormat openXmlFormat)
        {
            m_openXmlFormat = openXmlFormat;
            m_nametable = nametable;

            for (int i = 0; i < OpenXmlFactory.Get(openXmlFormat).NamespaceNames.Length; i++)
            {
                m_atomicStrings.Add(LookupNamespace(OpenXmlFactory.Get(openXmlFormat).NamespaceNames[i]));
            }
        }
Beispiel #3
0
 public static IOpenXmlFormat Get(OpenXmlFormat format)
 { 
     switch (format)
     {
         case OpenXmlFormat.Strict:
             return _strictOpenXml;
         case OpenXmlFormat.Transitional:
         default:
             return _transitionalOpenXml;
     }  
 }
Beispiel #4
0
        public OPCPackage(Stream s, List<DefaultInfo> defaults, OpenXmlFormat openXmlFormat)
        {
            if (s == null)
                throw new ArgumentNullException();

            m_openXmlFormat = openXmlFormat;
            m_bForWriting = true;
            m_stream = s;
            Initialize();
            m_contentTypes.Defaults = defaults;
        }
        //private static readonly string TESTFILE_DIR = Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\Projects\FCS\src\Workshare.Fcs.Lite.OfficeOpenXML.Tests\Test Files\Office 2007\");

        public static string ExtractAllDocText(XmlDocument doc, OpenXmlFormat format = OpenXmlFormat.Transitional)
        {
            XmlNodeList nodes = SelectSubNodes(doc, "//w:r/w:t", format);
            StringBuilder bld = new StringBuilder();
            foreach (XmlNode node in nodes)
            {
                bld.Append(node.InnerText);
            }

            return bld.ToString();
        }
        public Stream Write(OpenXmlFormat openXmlFormat)
        {
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));

            XmlElement typesNode = doc.CreateElement("Types", OpenXmlFactory.Get(openXmlFormat).PackageContentTypes);

            foreach (ContentTypeInfo part in m_contentTypes)
            {
                XmlElement partNode = doc.CreateElement("Override", OpenXmlFactory.Get(openXmlFormat).PackageContentTypes);
                XmlAttribute attr = doc.CreateAttribute("PartName");
                attr.Value = part.Name;
                partNode.Attributes.Append(attr);
                attr = doc.CreateAttribute("ContentType");
                attr.Value = part.ContentType;
                partNode.Attributes.Append(attr);
                typesNode.AppendChild(partNode);
            }

            foreach (DefaultInfo info in m_defaults)
            {
                XmlElement defaultNode = doc.CreateElement("Default", OpenXmlFactory.Get(openXmlFormat).PackageContentTypes);
                XmlAttribute attr = doc.CreateAttribute("Extension");
                attr.Value = info.Extension;
                defaultNode.Attributes.Append(attr);
                attr = doc.CreateAttribute("ContentType");
                attr.Value = info.ContentType;
                defaultNode.Attributes.Append(attr);
                typesNode.AppendChild(defaultNode);
            }

            doc.AppendChild(typesNode);
            MemoryStream ms = new MemoryStream();

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.CloseOutput = false;
            settings.Indent = true;
            using (XmlWriter wri = XmlWriter.Create(ms, settings))
            {
                doc.WriteTo(wri);
            }
            return ms;
        }
Beispiel #7
0
        public void AddContent(PartInfo rel, Stream sourceStream, OpenXmlFormat openXmlFormat)
        {
            PartInfo parentPart = GetPart(rel.RelatedTo);
            PartInfo clonedPart = new PartInfo(this, rel);
            string sContentType;
            if (ContentIsInternalType(rel.Type, openXmlFormat))
            {
                sContentType = rel.GetContentType();
                clonedPart.Target = RemoveLeadingSlash(clonedPart.Target);
                parentPart.AddRelatedItem(clonedPart);
                if (m_partsMap.ContainsKey(clonedPart.AbsolutePath()) && sourceStream == null)
                { // we have already added this content via a relationship elsewhere, don't need to write it
                    return;
                }
                m_partsMap.Add(clonedPart.AbsolutePath(), clonedPart);

                if (sourceStream != null)
                {
                    string partName = clonedPart.AbsolutePath();
                    AddZipEntry(partName, sourceStream);
                    if (rel.ContentTypeOverriden())
                        m_contentTypes.Add("/" + partName, sContentType);
                }
            }
            else
            {//we should only get here for external rels - so there should never be an additional stream to add
                parentPart.AddRelatedItem(clonedPart);
                sContentType = rel.Type;
                if (m_partsMap.ContainsKey(clonedPart.Target))
                { // we have already added this content via a relationship elsewhere, don't need to write it
                    return;
                }
                m_partsMap.Add(clonedPart.Target, clonedPart);
                if (sourceStream != null)
                {
                    throw new System.InvalidOperationException("Sourcestream is not null for supposedly external hyperlink part");
                }
            }
        }
Beispiel #8
0
 public XmlPartFilter(CommonNamespaces commonNamespaces, string ContentType)
 {
     m_commonNamespaces = commonNamespaces;
     m_openXmlFormat = commonNamespaces.OpenXmlFormat;
     m_contentType = ContentType;
 }
Beispiel #9
0
 public XmlPartFilter(CommonNamespaces commonNamespaces)
 {
     m_commonNamespaces = commonNamespaces;
     m_openXmlFormat = commonNamespaces.OpenXmlFormat;
 }
Beispiel #10
0
 public CommonNamespaces(OpenXmlFormat openXmlFormat)
     : this(new NameTable(), openXmlFormat)
 {            
 }
Beispiel #11
0
 private bool ContentIsInternalType(string relationshipType, OpenXmlFormat openXmlFormat)
 {//so far the only one that has occurred
     if (relationshipType.CompareTo(OpenXmlFactory.Get(openXmlFormat).OfficeDocumentRelationshipsHyperlink) == 0)
         return false;
     return true;
 }
Beispiel #12
0
        public Stream Write(OpenXmlFormat openXmlFormat)
        {
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));

            XmlElement relsNode = doc.CreateElement("Relationships", OpenXmlFactory.Get(openXmlFormat).PackageRelationships);

            foreach (PartInfo rel in m_relationships)
            {
                XmlElement relNode = doc.CreateElement("Relationship", OpenXmlFactory.Get(openXmlFormat).PackageRelationships);
                
                XmlAttribute attr = doc.CreateAttribute("Id");
                attr.Value = rel.Id;
                relNode.Attributes.Append(attr);

                attr = doc.CreateAttribute("Type");
                attr.Value = rel.Type;
                relNode.Attributes.Append(attr);

                attr = doc.CreateAttribute("Target");
                attr.Value = rel.Target;
                relNode.Attributes.Append(attr);


                if (rel.External)
                {
                    attr = doc.CreateAttribute("TargetMode");
                    attr.Value = "External";
                    relNode.Attributes.Append(attr);
                  
                }
                
                relsNode.AppendChild(relNode);
            }

            doc.AppendChild(relsNode);
            MemoryStream ms = new MemoryStream();

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.CloseOutput = false;
            settings.Indent = true;
            using (XmlWriter wri = XmlWriter.Create(ms, settings))
            {
                doc.WriteTo(wri);
            }
            return ms;
        }
Beispiel #13
0
        public Stream GetRelationshipXML(OpenXmlFormat openXmlFormat)
        {
            if (m_relationships.Items.Count == 0)
                return null;

            return m_relationships.Write(openXmlFormat);
        }
        private static Dictionary<string, string> ConstructTemplateLookup(PartInfo settingsPart, OpenXmlFormat openXmlFormat)
        {
            Dictionary<string, string> result = new Dictionary<string,string>();

            foreach (PartInfo pi in settingsPart.GetRelatedObjects())
            {
                if (pi.Type == OpenXmlFactory.Get(openXmlFormat).OfficeDocumentRelationshipsAttachedTemplate)
                {
                    result.Add(pi.Id, pi.Target);
                }
            }

            return result;
        }
        public static List<TriggeringNodeDefinition> GetAttachedTemplateDef(PartInfo settingsPart, OpenXmlFormat openXmlFormat)
        {
            List<TriggeringNodeDefinition> results = new List<TriggeringNodeDefinition>();
            EffectDescriptor td = new EffectDescriptor();
            td.ContentType = ContentType.AttachedTemplate;
            td.BlockType = Effect.BlockType.Structure;

            td.DictStringToStringLookup = ConstructTemplateLookup(settingsPart, openXmlFormat);

            AdditionalInfoDescriptor adi = new AdditionalInfoDescriptor("Path", "id", AdditionalInfoDescriptor.MappingType.StringToStringLookup);
            adi.LookupNamespace = NamespaceId.r;
            adi.StringMangler = URLToPath;
            td.AddAdditionalInfoDescriptor(adi);
            adi = new AdditionalInfoDescriptor("Name", "id", AdditionalInfoDescriptor.MappingType.StringToStringLookup);
            adi.LookupNamespace = NamespaceId.r;
            adi.StringMangler = URLToFilename;
            td.AddAdditionalInfoDescriptor(adi);


            results.Add(new TriggeringNodeDefinition(NamespaceId.w, "attachedTemplate", null, null, td));

            return results;
        }