Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PausableBasicAlgorithm/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Packaging/ZipPackageRelationshipBase.cs @ 13371

Last change on this file since 13371 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 5.2 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 *******************************************************************************
29 * Jan Källman    Added   25-Oct-2012
30 *******************************************************************************/
31using System;
32using System.Collections.Generic;
33using System.Linq;
34using System.Text;
35using Ionic.Zip;
36using System.IO;
37using System.Xml;
38using OfficeOpenXml.Packaging.Ionic.Zlib;
39using System.Web;
40namespace OfficeOpenXml.Packaging
41{
42    public abstract class ZipPackageRelationshipBase
43    {
44        protected ZipPackageRelationshipCollection _rels = new ZipPackageRelationshipCollection();
45        protected internal
46        int maxRId = 1;
47        internal void DeleteRelationship(string id)
48        {
49            _rels.Remove(id);
50            UpdateMaxRId(id, ref maxRId);
51        }
52        protected void UpdateMaxRId(string id, ref int maxRId)
53        {
54            if (id.StartsWith("rId"))
55            {
56                int num;
57                if (int.TryParse(id.Substring(3), out num))
58                {
59                    if (num == maxRId - 1)
60                    {
61                        maxRId--;
62                    }
63                }
64            }
65        }
66        internal virtual ZipPackageRelationship CreateRelationship(Uri targetUri, TargetMode targetMode, string relationshipType)
67        {
68            var rel = new ZipPackageRelationship();
69            rel.TargetUri = targetUri;
70            rel.TargetMode = targetMode;
71            rel.RelationshipType = relationshipType;
72            rel.Id = "rId" + (maxRId++).ToString();
73            _rels.Add(rel);
74            return rel;
75        }
76        internal bool RelationshipExists(string id)
77        {
78            return _rels.ContainsKey(id);
79        }
80        internal ZipPackageRelationshipCollection GetRelationshipsByType(string schema)
81        {
82            return _rels.GetRelationshipsByType(schema);
83        }
84        internal ZipPackageRelationshipCollection GetRelationships()
85        {
86            return _rels;
87        }
88        internal ZipPackageRelationship GetRelationship(string id)
89        {
90            return _rels[id];
91        }
92        internal void ReadRelation(string xml, string source)
93        {
94            var doc = new XmlDocument();
95            XmlHelper.LoadXmlSafe(doc, xml, Encoding.UTF8);
96
97            foreach (XmlElement c in doc.DocumentElement.ChildNodes)
98            {
99                var rel = new ZipPackageRelationship();
100                rel.Id = c.GetAttribute("Id");
101                rel.RelationshipType = c.GetAttribute("Type");
102                rel.TargetMode = c.GetAttribute("TargetMode").Equals("external",StringComparison.InvariantCultureIgnoreCase) ? TargetMode.External : TargetMode.Internal;
103                try
104                {
105                    rel.TargetUri = new Uri(c.GetAttribute("Target"), UriKind.RelativeOrAbsolute);
106                }
107                catch
108                {
109                    //The URI is not a valid URI. Encode it to make i valid.
110                    rel.TargetUri = new Uri(Uri.EscapeUriString("Invalid:URI "+c.GetAttribute("Target")), UriKind.RelativeOrAbsolute);
111                }
112                if (!string.IsNullOrEmpty(source))
113                {
114                    rel.SourceUri = new Uri(source, UriKind.Relative);
115                }
116                if (rel.Id.StartsWith("rid", StringComparison.InvariantCultureIgnoreCase))
117                {
118                    int id;
119                    if (int.TryParse(rel.Id.Substring(3), out id))
120                    {
121                        if (id >= maxRId && id < int.MaxValue - 10000) //Not likly to have this high id's but make sure we have space to avoid overflow.
122                        {
123                            maxRId = id + 1;
124                        }
125                    }
126                }
127                _rels.Add(rel);
128            }
129        }
130    }
131}
Note: See TracBrowser for help on using the repository browser.