Skip to content

Commit

Permalink
Merge pull request nissl-lab#27 from kekekeks/master
Browse files Browse the repository at this point in the history
Fixed uri-related problems on Mono
  • Loading branch information
tonyqus committed Nov 27, 2014
2 parents d43e72e + bcc2d8e commit cde3412
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion ooxml/openxml4Net/OPC/Internal/ContentTypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ private void ParseContentTypesFile(Stream in1)

// Overriden content types
//iterator.Current.MoveToNext();
Uri uri = new Uri(iterator.Current.GetAttribute(PART_NAME_ATTRIBUTE_NAME, xpathnav.NamespaceURI),UriKind.RelativeOrAbsolute);
Uri uri = PackagingUriHelper.ParseUri(iterator.Current.GetAttribute(PART_NAME_ATTRIBUTE_NAME, xpathnav.NamespaceURI), UriKind.RelativeOrAbsolute);
PackagePartName partName = PackagingUriHelper
.CreatePartName(uri);
String contentType = iterator.Current.GetAttribute(CONTENT_TYPE_ATTRIBUTE_NAME, xpathnav.NamespaceURI);
Expand Down
2 changes: 1 addition & 1 deletion ooxml/openxml4Net/OPC/Internal/ZipHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static Uri GetZipURIFromOPCName(String opcItemName)
retVal = retVal.Substring(1);
try
{
return new Uri(retVal,UriKind.RelativeOrAbsolute);
return PackagingUriHelper.ParseUri(retVal, UriKind.RelativeOrAbsolute);
}
catch (UriFormatException)
{
Expand Down
2 changes: 1 addition & 1 deletion ooxml/openxml4Net/OPC/OPCPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ public PackageRelationship AddExternalRelationship(String target,
Uri targetURI;
try
{
targetURI = new Uri(target);
targetURI = PackagingUriHelper.ParseUri(target);
}
catch (UriFormatException e)
{
Expand Down
4 changes: 2 additions & 2 deletions ooxml/openxml4Net/OPC/PackagePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public PackageRelationship AddExternalRelationship(String target,
Uri targetURI;
try
{
targetURI = new Uri(target,UriKind.RelativeOrAbsolute);
targetURI = PackagingUriHelper.ParseUri(target,UriKind.RelativeOrAbsolute);
}
catch (UriFormatException e)
{
Expand Down Expand Up @@ -501,7 +501,7 @@ public PackagePart GetRelatedPart(PackageRelationship rel)
String t = target.ToString();
try
{
target = new Uri(t.Substring(0, t.IndexOf('#')));
target = PackagingUriHelper.ParseUri(t.Substring(0, t.IndexOf('#')));
}
catch (UriFormatException e)
{
Expand Down
2 changes: 1 addition & 1 deletion ooxml/openxml4Net/OPC/PackagePartName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ internal PackagePartName(String partName, bool checkConformance)
Uri partURI;
try
{
partURI = new Uri(partName,UriKind.RelativeOrAbsolute);
partURI = PackagingUriHelper.ParseUri(partName, UriKind.RelativeOrAbsolute);
}
catch (UriFormatException)
{
Expand Down
2 changes: 1 addition & 1 deletion ooxml/openxml4Net/OPC/PackageRelationship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace NPOI.OpenXml4Net.OPC
public class PackageRelationship
{

private static Uri containerRelationshipPart = new Uri("/_rels/.rels",UriKind.RelativeOrAbsolute);
private static Uri containerRelationshipPart = PackagingUriHelper.ParseUri("/_rels/.rels", UriKind.RelativeOrAbsolute);

/* XML markup */

Expand Down
42 changes: 28 additions & 14 deletions ooxml/openxml4Net/OPC/PackagingUriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ static PackagingUriHelper()
Uri uriPACKAGE_RELATIONSHIPS_ROOT_URI = null;
Uri uriPACKAGE_PROPERTIES_URI = null;

uriPACKAGE_ROOT_URI = new Uri("/",UriKind.Relative);
uriPACKAGE_RELATIONSHIPS_ROOT_URI = new Uri(FORWARD_SLASH_CHAR
uriPACKAGE_ROOT_URI = ParseUri("/",UriKind.Relative);
uriPACKAGE_RELATIONSHIPS_ROOT_URI = ParseUri(FORWARD_SLASH_CHAR
+ RELATIONSHIP_PART_SEGMENT_NAME + FORWARD_SLASH_CHAR
+ RELATIONSHIP_PART_EXTENSION_NAME, UriKind.Relative);
packageRootUri = new Uri("/", UriKind.Relative);
uriPACKAGE_PROPERTIES_URI = new Uri(FORWARD_SLASH_CHAR
packageRootUri = ParseUri("/", UriKind.Relative);
uriPACKAGE_PROPERTIES_URI = ParseUri(FORWARD_SLASH_CHAR
+ PACKAGE_PROPERTIES_SEGMENT_NAME + FORWARD_SLASH_CHAR
+ PACKAGE_CORE_PROPERTIES_NAME, UriKind.Relative);

Expand Down Expand Up @@ -141,6 +141,20 @@ public static Uri PackageRootUri
}
}

private static readonly bool IsMono = Type.GetType("Mono.Runtime") != null;
public static Uri ParseUri(string s, UriKind kind = UriKind.Absolute)
{
if (IsMono)
{
if (kind == UriKind.Absolute)
throw new UriFormatException();
if (kind == UriKind.RelativeOrAbsolute && s.StartsWith("/"))
kind = UriKind.Relative;
}
return new Uri(s, kind);
}


/**
* Know if the specified Uri is a relationship part name.
*
Expand Down Expand Up @@ -207,7 +221,7 @@ public static Uri GetPath(Uri uri)
{
try
{
return new Uri(path.Substring(0, num2));
return ParseUri(path.Substring(0, num2));
}
catch (UriFormatException)
{
Expand All @@ -232,7 +246,7 @@ public static Uri Combine(Uri prefix, Uri suffix)
Uri retUri = null;
try
{
retUri = new Uri(Combine(prefix.OriginalString, suffix.OriginalString));
retUri = ParseUri(Combine(prefix.OriginalString, suffix.OriginalString));
}
catch (UriFormatException)
{
Expand Down Expand Up @@ -299,7 +313,7 @@ public static Uri RelativizeUri(Uri sourceURI, Uri targetURI, bool msCompatible)
{
try
{
targetURI = new Uri(path.Substring(1), UriKind.RelativeOrAbsolute);
targetURI = ParseUri(path.Substring(1), UriKind.RelativeOrAbsolute);
}
catch (Exception)
{
Expand Down Expand Up @@ -346,7 +360,7 @@ public static Uri RelativizeUri(Uri sourceURI, Uri targetURI, bool msCompatible)

try
{
return new Uri(retVal.ToString(),UriKind.RelativeOrAbsolute);
return ParseUri(retVal.ToString(),UriKind.RelativeOrAbsolute);
}
catch (Exception)
{
Expand Down Expand Up @@ -405,7 +419,7 @@ public static Uri RelativizeUri(Uri sourceURI, Uri targetURI, bool msCompatible)

try
{
return new Uri(retVal.ToString(), UriKind.RelativeOrAbsolute);
return ParseUri(retVal.ToString(), UriKind.RelativeOrAbsolute);
}
catch (Exception)
{
Expand Down Expand Up @@ -483,7 +497,7 @@ public static Uri ResolvePartUri(Uri sourcePartUri, Uri targetUri)
{
path = Path.Combine(path, targetUri.OriginalString).Replace("\\", "/");
}
return new Uri(path, UriKind.RelativeOrAbsolute);
return ParseUri(path, UriKind.RelativeOrAbsolute);
}

/**
Expand All @@ -494,7 +508,7 @@ public static Uri GetURIFromPath(String path)
Uri retUri = null;
try
{
retUri = new Uri(path,UriKind.RelativeOrAbsolute);
retUri = ParseUri(path,UriKind.RelativeOrAbsolute);
}
catch (UriFormatException)
{
Expand Down Expand Up @@ -569,7 +583,7 @@ public static PackagePartName CreatePartName(String partName)
try
{
partName = partName.Replace("\\","/"); //tolerate backslash - poi test49609
partNameURI = new Uri(partName,UriKind.Relative);
partNameURI = ParseUri(partName,UriKind.Relative);
}
catch (UriFormatException e)
{
Expand Down Expand Up @@ -597,7 +611,7 @@ public static PackagePartName CreatePartName(String partName,
try
{
newPartNameURI = ResolvePartUri(
relativePart.PartName.URI, new Uri(partName,UriKind.RelativeOrAbsolute));
relativePart.PartName.URI, ParseUri(partName,UriKind.RelativeOrAbsolute));
}
catch (UriFormatException e)
{
Expand Down Expand Up @@ -776,7 +790,7 @@ public static Uri ToUri(String value)
{
value += "/";
}
return new Uri(value, UriKind.RelativeOrAbsolute); //unicode character is not allowed in Uri class before .NET4.5
return ParseUri(value, UriKind.RelativeOrAbsolute); //unicode character is not allowed in Uri class before .NET4.5
}

/**
Expand Down

0 comments on commit cde3412

Please sign in to comment.