Skip to content

Commit c155bce

Browse files
authored
fixing DrtXpsApi failures (#1301)
* fixing xps code * responding to PR feedback * undo line ending change
1 parent adf7f3e commit c155bce

File tree

4 files changed

+1322
-1309
lines changed

4 files changed

+1322
-1309
lines changed

src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsDocument.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -325,26 +325,28 @@ public ReadOnlyCollection<XpsDigitalSignature> Signatures
325325

326326
foreach( PackagePart part in xmlPartList )
327327
{
328-
Stream stream = part.GetStream(FileMode.Open, FileAccess.Read );
329-
//
330-
// An empty stream contains not version extensibility thus is valid
331-
// We do create empty parts for print tickets
332-
//
333-
if( stream.Length == 0 )
334-
continue;
335-
try
328+
using (Stream stream = part.GetStream(FileMode.Open, FileAccess.Read))
336329
{
337-
if( StreamContainsVersionExtensiblity(stream) )
330+
//
331+
// An empty stream contains not version extensibility thus is valid
332+
// We do create empty parts for print tickets
333+
//
334+
if (stream.Length == 0)
335+
continue;
336+
try
337+
{
338+
if (StreamContainsVersionExtensiblity(stream))
339+
{
340+
isSignable = false;
341+
break;
342+
}
343+
}
344+
catch (XmlException)
338345
{
339346
isSignable = false;
340347
break;
341348
}
342349
}
343-
catch( XmlException )
344-
{
345-
isSignable = false;
346-
break;
347-
}
348350
}
349351
return isSignable;
350352
}

src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentReaderWriter.cs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -950,23 +950,25 @@ IList<String> linkTargetStream
950950
void
951951
ParsePages()
952952
{
953-
Stream stream = _metroPart.GetStream(FileMode.Open);
954-
//
955-
// If the stream is empty there are no pages to parse
956-
//
957-
if( stream.Length > 0 )
953+
using (Stream stream = _metroPart.GetStream(FileMode.Open))
958954
{
959-
XmlTextReader reader = new XmlTextReader(stream);
960-
961-
while( reader.Read() )
955+
//
956+
// If the stream is empty there are no pages to parse
957+
//
958+
if (stream.Length > 0)
962959
{
963-
if( reader.NodeType == XmlNodeType.Element && reader.Name == XpsS0Markup.PageContent)
960+
XmlTextReader reader = new XmlTextReader(stream);
961+
962+
while (reader.Read())
964963
{
965-
string attribute = reader.GetAttribute(XmlTags.Source);
966-
if( attribute != null )
964+
if (reader.NodeType == XmlNodeType.Element && reader.Name == XpsS0Markup.PageContent)
967965
{
968-
Uri relativeUri = new Uri(attribute, UriKind.Relative);
969-
AddPageToCache(PackUriHelper.ResolvePartUri( Uri, relativeUri));
966+
string attribute = reader.GetAttribute(XmlTags.Source);
967+
if (attribute != null)
968+
{
969+
Uri relativeUri = new Uri(attribute, UriKind.Relative);
970+
AddPageToCache(PackUriHelper.ResolvePartUri(Uri, relativeUri));
971+
}
970972
}
971973
}
972974
}
@@ -1074,14 +1076,16 @@ IList<String> linkTargetStream
10741076
Collection<XpsSignatureDefinition> sigDefCollection
10751077
)
10761078
{
1077-
XmlTextReader reader = new XmlTextReader( sigDefPart.GetStream(FileMode.Open) );
1078-
while( reader.Read() )
1079+
using (XmlTextReader reader = new XmlTextReader(sigDefPart.GetStream(FileMode.Open)))
10791080
{
1080-
if( reader.NodeType == XmlNodeType.Element &&
1081-
reader.Name == XpsS0Markup.SignatureDefinitions
1082-
)
1081+
while (reader.Read())
10831082
{
1084-
ParseSignatureDefinitions( reader, sigDefCollection );
1083+
if (reader.NodeType == XmlNodeType.Element &&
1084+
reader.Name == XpsS0Markup.SignatureDefinitions
1085+
)
1086+
{
1087+
ParseSignatureDefinitions(reader, sigDefCollection);
1088+
}
10851089
}
10861090
}
10871091
}
@@ -1124,21 +1128,20 @@ Collection<XpsSignatureDefinition> sigDefCollection
11241128
{
11251129
sigDefPart = CurrentXpsManager.AddSignatureDefinitionPart( _metroPart );
11261130
}
1127-
Stream stream = sigDefPart.GetStream(FileMode.Create);
1128-
XmlTextWriter writer = new XmlTextWriter(
1129-
stream,
1130-
System.Text.Encoding.UTF8
1131-
);
1132-
writer.WriteStartDocument();
1133-
writer.WriteStartElement( XpsS0Markup.SignatureDefinitions,
1134-
XpsS0Markup.SignatureDefinitionNamespace);
1135-
foreach( XpsSignatureDefinition sigDef in _signatureDefinitions )
1131+
1132+
using (Stream stream = sigDefPart.GetStream(FileMode.Create))
1133+
using (XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8))
11361134
{
1137-
sigDef.WriteXML( writer );
1135+
writer.WriteStartDocument();
1136+
writer.WriteStartElement(XpsS0Markup.SignatureDefinitions,
1137+
XpsS0Markup.SignatureDefinitionNamespace);
1138+
foreach (XpsSignatureDefinition sigDef in _signatureDefinitions)
1139+
{
1140+
sigDef.WriteXML(writer);
1141+
}
1142+
writer.WriteEndElement();
11381143
}
1139-
writer.WriteEndElement();
1140-
writer.Close();
1141-
stream.Close();
1144+
11421145
_sigCollectionDirty = false;
11431146
}
11441147

src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentSequenceReaderWriter.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -546,28 +546,31 @@ Uri partUri
546546
void
547547
ParseDocuments()
548548
{
549-
Stream stream = _metroPart.GetStream(FileMode.Open);
550-
//
551-
// If the stream is empty there are no documents to parse
552-
//
553-
if( stream.Length > 0 )
554-
{
555-
XmlTextReader reader = new XmlTextReader(stream);
556-
557-
while( reader.Read() )
558-
{
559-
if( reader.NodeType == XmlNodeType.Element && reader.Name == XpsS0Markup.DocumentReference)
560-
{
561-
string attribute = reader.GetAttribute(XmlTags.Source);
562-
if( attribute != null )
563-
{
564-
Uri relativeUri = new Uri(attribute, UriKind.Relative);
565-
//This routine properly adds DocumentReaderWriter to the _documentCache
566-
AddDocumentToCache(PackUriHelper.ResolvePartUri( Uri, relativeUri));
567-
}
568-
}
569-
}
570-
}
549+
using (Stream stream = _metroPart.GetStream(FileMode.Open))
550+
{
551+
//
552+
// If the stream is empty there are no documents to parse
553+
//
554+
if (stream.Length > 0)
555+
{
556+
XmlTextReader reader = new XmlTextReader(stream);
557+
558+
while (reader.Read())
559+
{
560+
if (reader.NodeType == XmlNodeType.Element && reader.Name == XpsS0Markup.DocumentReference)
561+
{
562+
string attribute = reader.GetAttribute(XmlTags.Source);
563+
if (attribute != null)
564+
{
565+
Uri relativeUri = new Uri(attribute, UriKind.Relative);
566+
//This routine properly adds DocumentReaderWriter to the _documentCache
567+
AddDocumentToCache(PackUriHelper.ResolvePartUri(Uri, relativeUri));
568+
}
569+
}
570+
}
571+
}
572+
}
573+
571574
}
572575

573576
/// <summary>

0 commit comments

Comments
 (0)