Merge pull request #2735 from xmcclure/early-lookup-addr
[mono.git] / mcs / class / WindowsBase / System.IO.Packaging / Package.cs
index 3e81f8d54811520cefc7fa7e2f4739af5755547d..96ea9a5403b45bdae370068ca187d7763be6b3d8 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
+// Copyright (c) 2011 Xamarin Inc. (http://www.xamarin.com)
 //
 // Authors:
 //     Chris Toshok (toshok@ximian.com)
 //     Alan McGovern (amcgovern@novell.com)
+//     Rolf Bjarne Kvinge (rolf@xamarin.com)
 //
 
 using System;
@@ -37,11 +39,14 @@ namespace System.IO.Packaging {
                internal static readonly Uri RelationshipUri = new Uri ("/_rels/.rels", UriKind.Relative);
 
                PackageProperties packageProperties;
-               PackagePartCollection partsCollection = new PackagePartCollection ();
+               PackagePartCollection partsCollection;
                Dictionary<string, PackageRelationship> relationships;
                PackageRelationshipCollection relationshipsCollection = new PackageRelationshipCollection ();
                Uri Uri = new Uri ("/", UriKind.Relative);
                
+               bool Disposed {
+                       get; set;
+               }
 
                public FileAccess FileOpenAccess {
                        get; private set;
@@ -49,6 +54,10 @@ namespace System.IO.Packaging {
 
                public PackageProperties PackageProperties {
                        get {
+                               // PackageProperties are loaded when the relationships are loaded.
+                               // Therefore ensure we've already loaded the relationships.
+                               int count = Relationships.Count;
+                               
                                if (packageProperties == null) {
                                        packageProperties = new PackagePropertiesPart ();
                                        packageProperties.Package = this;
@@ -57,6 +66,16 @@ namespace System.IO.Packaging {
                        }
                }
 
+               PackagePartCollection PartsCollection {
+                       get {
+                               if (partsCollection == null) {
+                                       partsCollection = new PackagePartCollection ();
+                                       partsCollection.Parts.AddRange (GetPartsCore ());
+                               }
+                               return partsCollection;
+                       }
+               }
+               
                int RelationshipId {
                        get; set;
                }
@@ -64,11 +83,7 @@ namespace System.IO.Packaging {
                Dictionary<string, PackageRelationship> Relationships {
                        get {
                                if (relationships == null) {
-                                       relationships = new Dictionary<string, PackageRelationship> ();
-                                       
-                                       if (PartExists (RelationshipUri))
-                                               using (Stream stream = GetPart (RelationshipUri).GetStream ())
-                                                       LoadRelationships (relationships, stream);
+                                       LoadRelationships ();
                                }
                                return relationships;
                        }
@@ -79,15 +94,15 @@ namespace System.IO.Packaging {
                }
                
                
-               protected Package (FileAccess fileOpenAccess)
-                       : this (fileOpenAccess, false)
+               protected Package (FileAccess openFileAccess)
+                       : this (openFileAccess, false)
                {
                        
                }
 
-               protected Package (FileAccess fileOpenAccess, bool streaming)
+               protected Package (FileAccess openFileAccess, bool streaming)
                {
-                       FileOpenAccess = fileOpenAccess;
+                       FileOpenAccess = openFileAccess;
                        Streaming = streaming;
                }
 
@@ -101,8 +116,7 @@ namespace System.IO.Packaging {
                public void Close ()
                {
                        // FIXME: Ensure that Flush is actually called before dispose
-                       Flush ();
-                       Dispose (true);
+                       ((IDisposable) this).Dispose ();
                }
 
                public PackagePart CreatePart (Uri partUri, string contentType)
@@ -120,11 +134,11 @@ namespace System.IO.Packaging {
                                throw new InvalidOperationException ("This partUri is already contained in the package");
                        
                        PackagePart part = CreatePartCore (partUri, contentType, compressionOption);
-                       partsCollection.Parts.Add (part);
+                       PartsCollection.Parts.Add (part);
                        return part;
                }
                
-               protected abstract PackagePart CreatePartCore (Uri parentUri, string contentType, CompressionOption compressionOption);
+               protected abstract PackagePart CreatePartCore (Uri partUri, string contentType, CompressionOption compressionOption);
 
                public PackageRelationship CreateRelationship (Uri targetUri, TargetMode targetMode, string relationshipType)
                {
@@ -138,8 +152,12 @@ namespace System.IO.Packaging {
 
                internal PackageRelationship CreateRelationship (Uri targetUri, TargetMode targetMode, string relationshipType, string id, bool loading)
                {
-                       CheckIsReadOnly ();
+                       if (!loading)
+                               CheckIsReadOnly ();
+                       
                        Check.TargetUri (targetUri);
+                       if (targetUri.IsAbsoluteUri && targetMode == TargetMode.Internal)
+                               throw new ArgumentException ("TargetUri cannot be absolute for an internal relationship");
                        
                        Check.RelationshipTypeIsValid (relationshipType);
                        Check.IdIsValid (id);
@@ -150,7 +168,7 @@ namespace System.IO.Packaging {
                        PackageRelationship r = new PackageRelationship (id, this, relationshipType, Uri, targetMode, targetUri);
 
                        if (!PartExists (RelationshipUri))
-                               CreatePart (RelationshipUri, RelationshipContentType).IsRelationship = true;
+                               CreatePartCore (RelationshipUri, RelationshipContentType, CompressionOption.NotCompressed).IsRelationship = true;
                        
                        Relationships.Add (r.Id, r);
                        relationshipsCollection.Relationships.Add (r);
@@ -182,7 +200,7 @@ namespace System.IO.Packaging {
 
                                part.Package = null;
                                DeletePartCore (partUri);
-                               partsCollection.Parts.RemoveAll (p => p.Uri == partUri);
+                               PartsCollection.Parts.RemoveAll (p => p.Uri == partUri);
                        }
                }
                
@@ -205,8 +223,11 @@ namespace System.IO.Packaging {
                
                void IDisposable.Dispose ()
                {
-                       Flush ();
-                       Dispose (true);
+                       if (!Disposed) {
+                               Flush ();
+                               Dispose (true);
+                               Disposed = true;
+                       }
                }
 
                protected virtual void Dispose (bool disposing)
@@ -221,6 +242,9 @@ namespace System.IO.Packaging {
                                return;
 
                        flushing = true;
+
+                       // Ensure we've loaded the relationships, parts and properties
+                       int count = Relationships.Count;
                        
                        if (packageProperties != null)
                                packageProperties.Flush ();
@@ -242,9 +266,9 @@ namespace System.IO.Packaging {
 
                public PackagePartCollection GetParts ()
                {
-                       partsCollection.Parts.Clear ();
-                       partsCollection.Parts.AddRange (GetPartsCore());
-                       return partsCollection;
+                       PartsCollection.Parts.Clear ();
+                       PartsCollection.Parts.AddRange (GetPartsCore());
+                       return PartsCollection;
                }
 
                protected abstract PackagePart [] GetPartsCore ();
@@ -256,8 +280,10 @@ namespace System.IO.Packaging {
 
                public PackageRelationshipCollection GetRelationships ()
                {
+                       // Ensure the Relationships dict is instantiated first.
+                       ICollection <PackageRelationship> rels = Relationships.Values;
                        relationshipsCollection.Relationships.Clear ();
-                       relationshipsCollection.Relationships.AddRange (Relationships.Values);
+                       relationshipsCollection.Relationships.AddRange (rels);
                        return relationshipsCollection;
                }
 
@@ -271,34 +297,46 @@ namespace System.IO.Packaging {
                        return collection;
                }
 
-               void LoadRelationships (Dictionary<string, PackageRelationship> relationships, Stream stream)
+               void LoadRelationships ()
                {
-                       XmlDocument doc = new XmlDocument ();
-                       doc.Load (stream);
-                       XmlNamespaceManager manager = new XmlNamespaceManager (doc.NameTable);
-                       manager.AddNamespace ("rel", RelationshipNamespace);
+                       relationships = new Dictionary<string, PackageRelationship> ();
 
-                       foreach (XmlNode node in doc.SelectNodes ("/rel:Relationships/*", manager))
-                       {
-                               
-                               TargetMode mode = TargetMode.Internal;
-                               if (node.Attributes["TargetMode"] != null)
-                                       mode = (TargetMode) Enum.Parse (typeof(TargetMode), node.Attributes ["TargetMode"].Value);
+                       if (!PartExists (RelationshipUri))
+                               return;
+                       
+                       using (Stream stream = GetPart (RelationshipUri).GetStream ()) {
+                               XmlDocument doc = new XmlDocument ();
+                               doc.Load (stream);
+                               XmlNamespaceManager manager = new XmlNamespaceManager (doc.NameTable);
+                               manager.AddNamespace ("rel", RelationshipNamespace);
+
+                               foreach (XmlNode node in doc.SelectNodes ("/rel:Relationships/*", manager))
+                               {
+                                       TargetMode mode = TargetMode.Internal;
+                                       if (node.Attributes["TargetMode"] != null)
+                                               mode = (TargetMode) Enum.Parse (typeof(TargetMode), node.Attributes ["TargetMode"].Value);
+
+                                       Uri uri;
+                                       try {
+                                               uri = new Uri (node.Attributes ["Target"].Value.ToString(), UriKind.Relative);
+                                       } catch {
+                                               uri = new Uri (node.Attributes ["Target"].Value.ToString(), UriKind.Absolute);
+                                       }
+                                       CreateRelationship (uri,
+                                                           mode,
+                                                           node.Attributes["Type"].Value.ToString (),
+                                                           node.Attributes["Id"].Value.ToString (),
+                                                           true);
+                               }
                                
-                               CreateRelationship (new Uri (node.Attributes ["Target"].Value.ToString(), UriKind.Relative),
-                                                   mode,
-                                                   node.Attributes["Type"].Value.ToString (),
-                                                   node.Attributes["Id"].Value.ToString (),
-                                                   true);
-                       }
-
-                       foreach (PackageRelationship r in Relationships.Values) {
-                               if (r.RelationshipType == System.IO.Packaging.PackageProperties.NSPackageProperties) {
-                                       PackagePart part = GetPart (r.TargetUri);
-                                       packageProperties = new PackagePropertiesPart ();
-                                       packageProperties.Package = this;
-                                       packageProperties.Part = part;
-                                       packageProperties.LoadFrom (part.GetStream ());
+                               foreach (PackageRelationship r in relationships.Values) {
+                                       if (r.RelationshipType == System.IO.Packaging.PackageProperties.NSPackagePropertiesRelation) {
+                                               PackagePart part = GetPart (PackUriHelper.ResolvePartUri (Uri, r.TargetUri));
+                                               packageProperties = new PackagePropertiesPart ();
+                                               packageProperties.Package = this;
+                                               packageProperties.Part = part;
+                                               packageProperties.LoadFrom (part.GetStream ());
+                                       }
                                }
                        }
                }
@@ -306,7 +344,7 @@ namespace System.IO.Packaging {
                string NextId ()
                {
                        while (true) {
-                               string s = RelationshipId.ToString ();
+                               string s = "Re" + RelationshipId.ToString ();
                                if (!Relationships.ContainsKey (s))
                                        return s;
                                
@@ -337,7 +375,12 @@ namespace System.IO.Packaging {
 
                public static Package Open (Stream stream, FileMode packageMode, FileAccess packageAccess)
                {
-                       return OpenCore (stream, packageMode, packageAccess);
+                       return Open (stream, packageMode, packageAccess, false);
+               }
+               
+               static Package Open (Stream stream, FileMode packageMode, FileAccess packageAccess, bool ownsStream)
+               {
+                       return OpenCore (stream, packageMode, packageAccess, ownsStream);
                }
 
                public static Package Open (string path, FileMode packageMode, FileAccess packageAccess)
@@ -361,10 +404,10 @@ namespace System.IO.Packaging {
                                throw new FileFormatException ("Stream length cannot be zero with FileMode.Open");
 
                        Stream s = File.Open (path, packageMode, packageAccess, packageShare);
-                       return Open (s, packageMode, packageAccess);
+                       return Open (s, packageMode, packageAccess, true);
                }
 
-               static Package OpenCore (Stream stream, FileMode packageMode, FileAccess packageAccess)
+               static Package OpenCore (Stream stream, FileMode packageMode, FileAccess packageAccess, bool ownsStream)
                {
                        if ((packageAccess & FileAccess.Read) == FileAccess.Read && !stream.CanRead)
                                throw new IOException ("Stream does not support reading");
@@ -399,19 +442,7 @@ namespace System.IO.Packaging {
                                }
                        }
                        
-                       // FIXME: MS docs say that a ZipPackage is returned by default.
-                       // It looks like if you create a custom package, you cannot use Package.Open.
-
-                       // FIXME: This is a horrible hack. If a package is opened in readonly mode, i
-                       // need to pretend i have read/write mode so i can load the PackageParts
-                       // and PackageRelations. I think the 'streaming' boolean might be used for this.
-                       ZipPackage package = new ZipPackage (packageAccess);
-                       package.PackageStream = stream;
-                       package.FileOpenAccess = FileAccess.ReadWrite;
-                       package.GetParts ();
-                       package.GetRelationships();
-                       package.FileOpenAccess = packageAccess;
-                       return package;
+                       return new ZipPackage (packageAccess, ownsStream, stream);
                }
                
                public virtual bool PartExists (Uri partUri)
@@ -446,7 +477,12 @@ namespace System.IO.Packaging {
                                XmlAttribute targetAtt = doc.CreateAttribute ("Target");
                                targetAtt.Value = relationship.TargetUri.ToString ();
                                node.Attributes.Append(targetAtt);
-                               
+
+                               if (relationship.TargetMode != TargetMode.Internal) {
+                                       XmlAttribute modeAtt = doc.CreateAttribute ("TargetMode");
+                                       modeAtt.Value = relationship.TargetMode.ToString ();
+                                       node.Attributes.Append (modeAtt);
+                               }
                                XmlAttribute typeAtt = doc.CreateAttribute ("Type");
                                typeAtt.Value = relationship.RelationshipType;
                                node.Attributes.Append(typeAtt);