added SSCLI 1.0
[windows-sources.git] / sdk / samples / WPFSamples / XpsFlatFile / csharp / filehelper.cs
blob53b50d9d139c00915295e87b46a90e84fb4749ce
1 // XpsFlatFile SDK Sample - FileHelper.cs
2 // Copyright (c) Microsoft Corporation. All rights reserved.
4 using System;
5 using System.Collections.Generic;
6 using System.Text;
7 using System.IO;
10 namespace XpsFlatFile
12 /// <summary>
13 /// This is a basic file helper class for the Packaging Policy example.
14 /// This class is used to generate file names based on type, creating the
15 /// files, and getting a file extension based on type. This class is not
16 /// required in order to override the BasePackagingPolicy. It is used as
17 /// a resource for the implementation of this sample.</summary>
18 public class FileHelper
20 /// <summary>
21 /// Here we generate the file name based on the Xps File Type. We also
22 /// create the directory where the file will end up being saved.</summary>
23 /// <param name="fileType">Type of Xps part</param>
24 /// <param name="outputDirectory">Directory where the file will be stored</param>
25 /// <returns>Name of file</returns>
26 public static String GenerateFileName(XpsFileType fileType, String outputDirectory)
28 string fileName;
29 string path;
30 switch (fileType)
32 case XpsFileType.FixedDocumentSequence:
33 //There will be only 1 FixedDocumentSequence and it will be at the root
34 path = null;
35 fileName = _documentSequenceName + "." + _documentSequenceExtension;
36 break;
37 case XpsFileType.FixedDocument:
38 path = "Documents/";
39 fileName = path + _fixedDocumentName + "_" + _fixedDocumentCount + "." + _fixedDocumentExtension;
40 _fixedDocumentCount++;
41 break;
42 case XpsFileType.FixedPage:
43 path = "Pages/";
44 fileName = path + _fixedPageName + "_" + _fixedPageCount + "." + _fixedPageExtension;
45 _fixedPageCount++;
46 break;
47 case XpsFileType.ImagePng:
48 path = "Resources/Images/";
49 fileName = path + _imageName + "_" + _imageCount + "." + _imagePngExtension;
50 _imageCount++;
51 break;
52 case XpsFileType.Font:
53 Guid fontguid = Guid.NewGuid();
54 String fontString = fontguid.ToString();
56 path = "Resources/Fonts/";
57 fileName = path + fontString + "." + _obfuscatedFontExtension;
58 break;
59 case XpsFileType.ColorContext:
60 Guid colorContextGuid = Guid.NewGuid();
61 String colorContextString = colorContextGuid.ToString();
63 path = "Resources/ColorContexts/";
64 fileName = path + colorContextString + "." + _colorContextExtension;
65 break;
66 case XpsFileType.ResourceDictionary:
67 Guid resourceDictionaryGuid = Guid.NewGuid();
68 String resourceDictionaryString = resourceDictionaryGuid.ToString();
70 path = "Resources/ResourceDictionaries/";
71 fileName = path + resourceDictionaryString + "." + _resourceDictionaryExtension;
72 break;
73 case XpsFileType.Relationship:
74 fileName = null;
75 path = null;
76 break;
77 default:
78 throw new ArgumentException("File Type is not valid");
81 if (path != null)
83 String fullPath = outputDirectory + "/" + path;
84 if (Directory.Exists(fullPath) != true)
86 Directory.CreateDirectory(fullPath);
89 return fileName;
90 }// end:GenerateFileName
93 /// <summary>
94 /// Creates a file given the filename and path.</summary>
95 /// <param name="fileName">File name of the file to be created</param>
96 /// <param name="outputPath">Path of the file to be created</param>
97 /// <returns></returns>
98 public static FileStream CreateFile(String fileName, String outputPath)
100 int lastChar = outputPath.Length - 1;
101 StringBuilder tmpPath = new StringBuilder(outputPath);
102 if (outputPath[lastChar] != '/')
104 tmpPath.Append("/");
106 tmpPath.Append(fileName);
107 string path = tmpPath.ToString();
108 return File.Create(path);
112 /// <summary>
113 /// Returns the extension of the XpsFileType
114 /// </summary>
115 /// <param name="fileType">The type of extension returned</param>
116 /// <returns></returns>
117 public static String GetExtension(XpsFileType fileType)
119 string extension;
120 switch (fileType)
122 case XpsFileType.FixedDocumentSequence:
123 extension = _documentSequenceExtension;
124 break;
125 case XpsFileType.FixedDocument:
126 extension = _fixedDocumentExtension;
127 break;
128 case XpsFileType.FixedPage:
129 extension = _fixedPageExtension;
130 break;
131 case XpsFileType.Relationship:
132 extension = _relationshipExtension;
133 break;
134 case XpsFileType.ImagePng:
135 extension = _imagePngExtension;
136 break;
137 case XpsFileType.ImageTif:
138 extension = _imageTifExtension;
139 break;
140 case XpsFileType.ImageJpg:
141 extension = _imageJpgExtension;
142 break;
143 case XpsFileType.ImageWmp:
144 extension = _imageWmpExtension;
145 break;
146 case XpsFileType.Font:
147 extension = _fontExtension;
148 break;
149 case XpsFileType.ObfuscatedFont:
150 extension = _obfuscatedFontExtension;
151 break;
152 case XpsFileType.ResourceDictionary:
153 extension = _resourceDictionaryExtension;
154 break;
155 case XpsFileType.ColorContext:
156 extension = _colorContextExtension;
157 break;
158 default:
159 throw new ArgumentException("Invalid XpsFileType");
161 return extension;
165 #region Private Members
167 //FileNames
169 static private string _documentSequenceName = "FixedDocumentSequence";
170 static private string _fixedDocumentName = "FixedDocument";
171 static private string _fixedPageName = "FixedPage";
172 static private string _imageName = "Image";
174 //File Extensions
176 static private string _documentSequenceExtension = "fdseq";
177 static private string _fixedDocumentExtension = "fdoc";
178 static private string _fixedPageExtension = "fpage";
179 static private string _imagePngExtension = "png";
180 static private string _imageJpgExtension = "jpg";
181 static private string _imageWmpExtension = "wdp";
182 static private string _imageTifExtension = "tif";
183 static private string _fontExtension = "ttf";
184 static private string _obfuscatedFontExtension = "ODTTF";
185 static private string _resourceDictionaryExtension = "dict";
186 static private string _colorContextExtension = "icc";
187 static private string _relationshipExtension = "rels";
189 private static int _fixedDocumentCount = 1;
190 private static int _fixedPageCount = 1;
191 private static int _imageCount = 1;
193 #endregion Private Members
195 }// end:class FileHelper
197 }// end:namespace XpsFlatFile