ICreateDocumentTask IDocumentService.CreateCreateDocumentTask(Uri uri, Guid guid, string persistentInfo) { return(CreateDocumentTask.FromFactory(() => { if (uri.Scheme == Uri.UriSchemeFile) { var path = uri.LocalPath; if (path == null || !UnifiedFile.Exists(path)) { return null; } var fileViewer = this.GetFileViewerService(UnifiedPath.GetExtension(path)); if (fileViewer == null) { return null; } return this.CreateDocument(guid, uri, fileViewer); } else if (uri.Scheme == StreamScheme) { var streamId = uri.Host + uri.PathAndQuery; StreamInfo streamInfo; if (!_registeredStreams.TryGetValue(streamId, out streamInfo)) { this.LogWarning("no registered stream with ID '{0}' found", streamId); return null; } _registeredStreams.Remove(streamId); if (!streamInfo.StreamReference.IsAlive) { this.LogWarning("the registered stream with ID '{0}' is no more alive", streamId); return null; } var fileViewer = this.GetFileViewerServiceOrNotifyMissing(streamInfo.FileType); if (fileViewer == null) { return null; } IFeature[] features; var viewer = fileViewer.CreateViewer(streamInfo.StreamReference.Target, out features); var document = DocumentInfo.CreateTemporary(streamInfo.OwnerRepository.ID, streamInfo.Title, uri, viewer, features); document.Description = streamInfo.Description; return document; } else { throw new NotSupportedException(); } })); }
public static string Decode(string path, out EncodeType encodeType, bool wellFormatted = false) { var packedFileName = UnifiedPath.GetFileName(path).ToLower(); using (var stream = UnifiedFile.OpenRead(path)) { using (BinaryReader reader = new BinaryReader(stream)) { Int32 header = reader.ReadInt32(); if (header == PackedSection.PackedHeader) { reader.ReadSByte(); var document = new XmlDocument(); var packedSection = new PackedSection(); var dictionary = packedSection.readDictionary(reader); var xmlroot = document.CreateNode(XmlNodeType.Element, packedFileName, ""); packedSection.readElement(reader, xmlroot, document, dictionary); document.AppendChild(xmlroot); encodeType = EncodeType.Packed; if (wellFormatted) { return(XmlDecoder.FormatXml(document.OuterXml)); } else { return(document.OuterXml); } } else if (header == BinaryHeader) { stream.Seek(0, SeekOrigin.Begin); var document = new XmlDocument(); var xmlPrimitives = document.CreateNode(XmlNodeType.Element, "primitives", ""); var primitiveReader = new PrimitiveReader(); primitiveReader.ReadPrimitives(reader, xmlPrimitives, document); document.AppendChild(xmlPrimitives); encodeType = EncodeType.Binary; if (wellFormatted) { return(XmlDecoder.FormatXml(document.OuterXml)); } else { return(document.OuterXml); } } else { var extension = UnifiedPath.GetExtension(path); if (extension == ".xml" || extension == ".def" || extension == ".visual" || extension == ".chunk" || extension == ".settings" || extension == ".model") { encodeType = EncodeType.Plain; return(File.ReadAllText(path)); } else { throw new NotSupportedException(); } } } } }