ParsePages() { Stream stream = _metroPart.GetStream(FileMode.Open); // // If the stream is empty there are no pages to parse // if (stream.Length > 0) { XmlTextReader reader = new XmlTextReader(stream); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == XpsS0Markup.PageContent) { string attribute = reader.GetAttribute(XmlTags.Source); if (attribute != null) { Uri relativeUri = new Uri(attribute, UriKind.Relative); AddPageToCache(PackUriHelper.ResolvePartUri(Uri, relativeUri)); } } } } }
EnsureDoucmentStructure() { // if _xpsSignaturs is not null we have already initialized this // if (null != _documentStructure) { return; } PackageRelationship documentStructureRelationship = null; PackagePart documentStructurePart = null; foreach (PackageRelationship rel in _metroPart.GetRelationshipsByType(XpsS0Markup.StructureRelationshipName)) { if (documentStructureRelationship != null) { throw new InvalidDataException(SR.Get(SRID.ReachPackaging_MoreThanOneDocStructure)); } documentStructureRelationship = rel; } if (documentStructureRelationship != null) { Uri documentStructureUri = PackUriHelper.ResolvePartUri(documentStructureRelationship.SourceUri, documentStructureRelationship.TargetUri); if (CurrentXpsManager.MetroPackage.PartExists(documentStructureUri)) { documentStructurePart = CurrentXpsManager.MetroPackage.GetPart(documentStructureUri); _documentStructure = new XpsStructure(CurrentXpsManager, this, documentStructurePart); } } }
ParseDocuments() { Stream stream = _metroPart.GetStream(FileMode.Open); // // If the stream is empty there are no documents to parse // if (stream.Length > 0) { XmlTextReader reader = new XmlTextReader(stream); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == XpsS0Markup.DocumentReference) { string attribute = reader.GetAttribute(XmlTags.Source); if (attribute != null) { Uri relativeUri = new Uri(attribute, UriKind.Relative); //This routine properly adds DocumentReaderWriter to the _documentCache AddDocumentToCache(PackUriHelper.ResolvePartUri(Uri, relativeUri)); } } } } }
// Locate core properties part using the package relationship that points to it. private PackagePart GetPropertyPart() { // Find a package-wide relationship of type CoreDocumentPropertiesRelationshipType. PackageRelationship corePropertiesRelationship = GetCorePropertiesRelationship(); if (corePropertiesRelationship == null) { return(null); } // Retrieve the part referenced by its target URI. if (corePropertiesRelationship.TargetMode != TargetMode.Internal) { throw new FileFormatException(SR.NoExternalTargetForMetadataRelationship); } PackagePart propertiesPart = null; Uri propertiesPartUri = PackUriHelper.ResolvePartUri( PackUriHelper.PackageRootUri, corePropertiesRelationship.TargetUri); if (!_package.PartExists(propertiesPartUri)) { throw new FileFormatException(SR.DanglingMetadataRelationship); } propertiesPart = _package.GetPart(propertiesPartUri); if (!propertiesPart.ValidatedContentType.AreTypeAndSubTypeEqual(s_coreDocumentPropertiesContentType)) { throw new FileFormatException(SR.WrongContentTypeForPropertyPart); } return(propertiesPart); }
//------------------------------------------------------ // // Internal Properties // //------------------------------------------------------ /// <summary> /// Get certificate part - null if none /// </summary> internal CertificatePart GetCertificatePart() { // lazy init if (_certificatePart == null && !_alreadyLookedForCertPart) { PackageRelationshipCollection relationships = SignaturePart.GetRelationshipsByType( CertificatePart.RelationshipType); foreach (PackageRelationship relationship in relationships) { // don't resolve if external if (relationship.TargetMode != TargetMode.Internal) { throw new FileFormatException(SR.Get(SRID.PackageSignatureCorruption)); } Uri resolvedUri = PackUriHelper.ResolvePartUri(SignaturePart.Uri, relationship.TargetUri); // don't create if it doesn't exist if (!_manager.Package.PartExists(resolvedUri)) { continue; } // find the cert _certificatePart = new CertificatePart(_manager.Package, resolvedUri); break; } _alreadyLookedForCertPart = true; } return(_certificatePart); }
/// <summary> /// Resolves the target uri in the relationship against the source part or the /// package root. This resolved Uri is then used by the Add method to figure /// out if a relationship is being created to another relationship part. /// </summary> /// <param name="target">PackageRelationship target uri</param> /// <param name="targetMode"> Enum value specifying the interpretation of the base uri /// for the relationship target uri</param> /// <returns>Resolved Uri</returns> private Uri GetResolvedTargetUri(Uri target, TargetMode targetMode) { if (targetMode == TargetMode.Internal) { Debug.Assert(!target.IsAbsoluteUri, "Uri should be relative at this stage"); if (_sourcePart == null) //indicates that the source is the package root { return(PackUriHelper.ResolvePartUri(PackUriHelper.PackageRootUri, target)); } else { return(PackUriHelper.ResolvePartUri(_sourcePart.Uri, target)); } } else { if (target.IsAbsoluteUri) { if (string.Equals(target.Scheme, PackUriHelper.UriSchemePack)) { return(PackUriHelper.GetPartUri(target)); } } else { Debug.Fail("Uri should not be relative at this stage"); } } // relative to the location of the package. return(target); }
void LoadRelationships() { relationships = new Dictionary <string, PackageRelationship> (); 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); } 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()); } } } }
/// <summary> /// Resolves the target uri in the relationship against the source part or the /// package root. This resolved Uri is then used by the Add method to figure /// out if a relationship is being created to another relationship part. /// </summary> /// <param name="target">PackageRelationship target uri</param> /// <param name="targetMode"> Enum value specifying the interpretation of the base uri /// for the relationship target uri</param> /// <returns>Resolved Uri</returns> private Uri GetResolvedTargetUri(Uri target, TargetMode targetMode) { Debug.Assert(targetMode == TargetMode.Internal); Debug.Assert(!target.IsAbsoluteUri, "Uri should be relative at this stage"); if (_sourcePart == null) //indicates that the source is the package root { return(PackUriHelper.ResolvePartUri(PackUriHelper.PackageRootUri, target)); } else { return(PackUriHelper.ResolvePartUri(_sourcePart.Uri, target)); } }
/// <summary> /// rootElement == null: Load elements, validation of root element will occur in caller by checking object type or casting /// rootElement != null: Only perform validation, and expect rootElement at root of markup /// </summary> /// <param name="stream"></param> /// <param name="parentUri"></param> /// <param name="pc"></param> /// <param name="mimeType"></param> /// <param name="rootElement"></param> /// <returns></returns> private object Load(Stream stream, Uri parentUri, ParserContext pc, ContentType mimeType, string rootElement) { object obj = null; if (!DocumentMode) { // Loose XAML, just check against schema, don't check content type if (rootElement == null) { obj = XamlReader.Load(stream, pc); } } else { // inside an XPS Document. Perform maximum validation XpsSchema schema = XpsSchema.GetSchema(mimeType); Uri uri = pc.BaseUri; Uri packageUri = PackUriHelper.GetPackageUri(uri); Uri partUri = PackUriHelper.GetPartUri(uri); Package package = PreloadedPackages.GetPackage(packageUri); Uri parentPackageUri = null; if (parentUri != null) { parentPackageUri = PackUriHelper.GetPackageUri(parentUri); if (!parentPackageUri.Equals(packageUri)) { throw new FileFormatException(SR.Get(SRID.XpsValidatingLoaderUriNotInSamePackage)); } } schema.ValidateRelationships(new SecurityCriticalData <Package>(package), packageUri, partUri, mimeType); if (schema.AllowsMultipleReferencesToSameUri(mimeType)) { _uniqueUriRef = null; } else { _uniqueUriRef = new Hashtable(11); } Hashtable validResources = (_validResources.Count > 0 ? _validResources.Peek() : null); if (schema.HasRequiredResources(mimeType)) { validResources = new Hashtable(11); PackagePart part = package.GetPart(partUri); PackageRelationshipCollection requiredResources = part.GetRelationshipsByType(_requiredResourceRel); foreach (PackageRelationship relationShip in requiredResources) { Uri targetUri = PackUriHelper.ResolvePartUri(partUri, relationShip.TargetUri); Uri absTargetUri = PackUriHelper.Create(packageUri, targetUri); PackagePart targetPart = package.GetPart(targetUri); if (schema.IsValidRequiredResourceMimeType(targetPart.ValidatedContentType())) { if (!validResources.ContainsKey(absTargetUri)) { validResources.Add(absTargetUri, true); } } else { if (!validResources.ContainsKey(absTargetUri)) { validResources.Add(absTargetUri, false); } } } } XpsSchemaValidator xpsSchemaValidator = new XpsSchemaValidator(this, schema, mimeType, stream, packageUri, partUri); _validResources.Push(validResources); if (rootElement != null) { xpsSchemaValidator.XmlReader.MoveToContent(); if (!rootElement.Equals(xpsSchemaValidator.XmlReader.Name)) { throw new FileFormatException(SR.Get(SRID.XpsValidatingLoaderUnsupportedMimeType)); } while (xpsSchemaValidator.XmlReader.Read()) { ; } } else { obj = XamlReader.Load(xpsSchemaValidator.XmlReader, pc, XamlParseMode.Synchronous); } _validResources.Pop(); } return(obj); }