Пример #1
0
        public static CopyBuffer ExtractCopyBufferFromClipboardStream(Stream clipboardStream)
        {
            CopyBuffer result;

            using (Package package = Package.Open(clipboardStream, FileMode.Open, FileAccess.Read))
            {
                result = new CopyBuffer();
                foreach (PackageRelationship packageRelationship in package.GetRelationshipsByType("http://schemas.microsoft.com/cfxaml/2006/06/relationships/metadata/core-properties"))
                {
                    Uri         partUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), packageRelationship.TargetUri);
                    PackagePart part    = package.GetPart(partUri);
                    Container.ParseCorePropertyPart(result, part);
                }
                foreach (PackageRelationship packageRelationship in package.GetRelationshipsByType("http://schemas.microsoft.com/cfxaml/2006/06/selected-item"))
                {
                    Uri      partUri     = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), packageRelationship.TargetUri);
                    CopyItem itemToPaste = (CopyItem) new MemoryCopyItem(Container.GetBytes(package.GetPart(partUri)));
                    result.AddSelectedItem(itemToPaste);
                }
                foreach (PackageRelationship packageRelationship1 in package.GetRelationshipsByType("http://schemas.microsoft.com/cfxaml/2006/06/referenced-item"))
                {
                    Uri         partUri1 = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), packageRelationship1.TargetUri);
                    PackagePart part     = package.GetPart(partUri1);
                    CopyItem    copyItem = (CopyItem) new MemoryCopyItem(Container.GetBytes(part));
                    result.AddReferencedItem(copyItem);
                    foreach (PackageRelationship packageRelationship2 in part.GetRelationshipsByType("http://schemas.microsoft.com/cfxaml/2006/06/item-attributes"))
                    {
                        Uri partUri2 = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), packageRelationship2.TargetUri);
                        Container.ReadAttributes(package.GetPart(partUri2), copyItem);
                    }
                }
            }
            return(result);
        }
Пример #2
0
        private static string GetMimeType(CopyItem referencedItem)
        {
            string str = "text/plain";

            if (referencedItem.FilenameExtension == ".gif")
            {
                str = "image/gif";
            }
            else if (referencedItem.FilenameExtension == ".jpg" || referencedItem.FilenameExtension == ".jpeg")
            {
                str = "image/jpeg";
            }
            else if (referencedItem.FilenameExtension == ".png")
            {
                str = "image/png";
            }
            else if (referencedItem.FilenameExtension == ".tiff" || referencedItem.FilenameExtension == ".tif")
            {
                str = "image/tiff";
            }
            else if (referencedItem.FilenameExtension == ".xaml")
            {
                str = "text/xml";
            }
            return(str);
        }
Пример #3
0
 public static void ReadAttributes(PackagePart part, CopyItem item)
 {
     using (Stream stream = part.GetStream())
     {
         using (XmlReader xmlReader = XmlReader.Create(stream))
         {
             try
             {
                 if (!xmlReader.IsStartElement("ItemAttributes"))
                 {
                     return;
                 }
                 double result;
                 double.TryParse(xmlReader.GetAttribute("Version"), NumberStyles.Any, (IFormatProvider)CultureInfo.InvariantCulture, out result);
                 if (result != 0.71)
                 {
                     return;
                 }
                 xmlReader.ReadStartElement("ItemAttributes");
                 if (xmlReader.IsStartElement("ContentType"))
                 {
                     string attribute = xmlReader.GetAttribute("Value");
                     item.ContentType = attribute;
                     xmlReader.ReadStartElement("ContentType");
                 }
                 if (xmlReader.IsStartElement("FileExtension"))
                 {
                     string attribute = xmlReader.GetAttribute("Value");
                     item.FilenameExtension = attribute;
                     xmlReader.ReadStartElement("FileExtension");
                 }
                 if (xmlReader.IsStartElement("OriginalUri"))
                 {
                     string attribute = xmlReader.GetAttribute("Value");
                     item.OriginalUri = new Uri(attribute, UriKind.RelativeOrAbsolute);
                     xmlReader.ReadStartElement("OriginalUri");
                 }
                 if (xmlReader.IsStartElement("LocalPath"))
                 {
                     item.LocalPath = xmlReader.GetAttribute("Value");
                     xmlReader.ReadStartElement("LocalPath");
                 }
                 if (xmlReader.IsStartElement("Key"))
                 {
                     item.Key = xmlReader.GetAttribute("Value");
                     xmlReader.ReadStartElement("Key");
                 }
                 xmlReader.ReadEndElement();
             }
             catch (XmlException ex)
             {
             }
         }
     }
 }
Пример #4
0
 private static void WriteAttributes(PackagePart part, CopyItem item)
 {
     using (Stream stream = part.GetStream())
     {
         using (XmlWriter xmlWriter = XmlWriter.Create(stream, new XmlWriterSettings()
         {
             Indent = true
         }))
         {
             try
             {
                 xmlWriter.WriteStartElement("ItemAttributes");
                 xmlWriter.WriteAttributeString("Version", 0.71.ToString((IFormatProvider)CultureInfo.InvariantCulture));
                 xmlWriter.WriteStartElement("ContentType");
                 xmlWriter.WriteAttributeString("Value", item.ContentType);
                 xmlWriter.WriteEndElement();
                 xmlWriter.WriteStartElement("FileExtension");
                 xmlWriter.WriteAttributeString("Value", item.FilenameExtension);
                 xmlWriter.WriteEndElement();
                 xmlWriter.WriteStartElement("OriginalUri");
                 xmlWriter.WriteAttributeString("Value", item.OriginalUri.OriginalString);
                 xmlWriter.WriteEndElement();
                 xmlWriter.WriteStartElement("LocalPath");
                 xmlWriter.WriteAttributeString("Value", item.LocalPath);
                 xmlWriter.WriteEndElement();
                 xmlWriter.WriteStartElement("Key");
                 xmlWriter.WriteAttributeString("Value", item.Key);
                 xmlWriter.WriteEndElement();
                 xmlWriter.WriteEndElement();
             }
             catch (UnauthorizedAccessException ex)
             {
             }
             catch (IOException ex)
             {
             }
         }
     }
 }
Пример #5
0
        public static MemoryStream CreateClipboardStream(CopyBuffer itemsToPaste)
        {
            Uri          partUri      = PackUriHelper.CreatePartUri(new Uri("CoreProperties", UriKind.Relative));
            MemoryStream memoryStream = new MemoryStream();

            using (Package package = Package.Open((Stream)memoryStream, FileMode.Create))
            {
                PackagePart part = package.CreatePart(partUri, "text/Xml");
                using (Stream stream = part.GetStream())
                {
                    using (StreamWriter streamWriter = new StreamWriter(stream, Encoding.UTF8))
                        streamWriter.Write("<?xml version='1.0' encoding='utf-8'?>\r\n<coreProperties\r\n  xmlns='http://schemas.microsoft.com/cfxaml/2006/06/metadata/core-properties'\r\n  xmlns:dc='http://purl.org/dc/elements/1.1'>\r\n  <contentType>cfXaml</contentType>\r\n  <dc:creator>Expression</dc:creator>\r\n  <version>0.71</version>\r\n</coreProperties>");
                }
                package.CreateRelationship(part.Uri, TargetMode.Internal, "http://schemas.microsoft.com/cfxaml/2006/06/relationships/metadata/core-properties");
                List <PackagePart> itemPackageParts1 = new List <PackagePart>();
                for (int index = 0; index < itemsToPaste.SelectedItemCount; ++index)
                {
                    CopyItem copyItem = itemsToPaste.SelectedItem(index);
                    Container.CreatePackageItem(package, itemPackageParts1, "/CopiedItem" + (object)index, "http://schemas.microsoft.com/cfxaml/2006/06/selected-item", copyItem);
                }
                List <PackagePart> itemPackageParts2 = new List <PackagePart>();
                for (int index = 0; index < itemsToPaste.ReferencedItemCount; ++index)
                {
                    CopyItem copyItem = itemsToPaste.ReferencedItem(index);
                    Container.CreatePackageItem(package, itemPackageParts2, "/ReferencedItem" + (object)index, "http://schemas.microsoft.com/cfxaml/2006/06/referenced-item", copyItem);
                }
            }
            if (Container.SaveClipboardContentsToFile)
            {
                using (FileStream fileStream = new FileStream("clipboardContents.zip", FileMode.Create, FileAccess.ReadWrite))
                {
                    memoryStream.Seek(0L, SeekOrigin.Begin);
                    Container.CopyStream((Stream)memoryStream, (Stream)fileStream);
                }
            }
            return(memoryStream);
        }
Пример #6
0
 public void AddReferencedItem(CopyItem item)
 {
     this.referencedItems.Add(item);
 }
Пример #7
0
 public void AddSelectedItem(CopyItem itemToPaste)
 {
     this.selectedItems.Add(itemToPaste);
 }
Пример #8
0
        private static void CreatePackageItem(Package package, List <PackagePart> itemPackageParts, string itemPartRoot, string packageRelationship, CopyItem item)
        {
            Stream stream1 = item.GetStream();

            if (stream1 == null)
            {
                return;
            }
            using (stream1)
            {
                Uri         partUri1 = PackUriHelper.CreatePartUri(new Uri(itemPartRoot + "/content" + item.FilenameExtension, UriKind.Relative));
                string      mimeType = Container.GetMimeType(item);
                PackagePart part1    = package.CreatePart(partUri1, mimeType);
                itemPackageParts.Add(part1);
                using (Stream stream2 = part1.GetStream())
                    Container.CopyStream(stream1, stream2);
                package.CreateRelationship(partUri1, TargetMode.Internal, packageRelationship);
                if (item.Key == null)
                {
                    return;
                }
                Uri         partUri2 = PackUriHelper.CreatePartUri(new Uri(itemPartRoot + "/Attributes.xml", UriKind.Relative));
                PackagePart part2    = package.CreatePart(partUri2, "text/Xml");
                itemPackageParts.Add(part2);
                Container.WriteAttributes(part2, item);
                part1.CreateRelationship(partUri2, TargetMode.Internal, "http://schemas.microsoft.com/cfxaml/2006/06/item-attributes");
            }
        }