Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorRenderingGdi/Forms/SvgCacheManager.cs @ 12762

Last change on this file since 12762 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 5.0 KB
Line 
1using System;
2using System.Diagnostics;
3using System.IO;
4using System.Net;
5using System.Xml;
6
7using SharpVectors.Net;
8
9namespace SharpVectors.Renderers.Forms
10{
11  public class SvgCacheManager : ICacheManager
12  {
13    private string cacheDir;
14    private string cacheDocPath;
15    private XmlDocument cacheDoc = new XmlDocument();
16
17    public SvgCacheManager()
18    {
19      cacheDir = Path.Combine(
20        SvgApplicationContext.ExecutableDirectory.FullName,
21        "cache/");
22      cacheDocPath = Path.Combine(cacheDir, "cache.xml");
23
24      loadDoc();
25    }
26
27    public SvgCacheManager(string cacheDir)
28    {
29            this.cacheDir = Path.Combine(
30        SvgApplicationContext.ExecutableDirectory.FullName,
31        cacheDir);
32
33      cacheDocPath = Path.Combine(cacheDir, "cache.xml");
34
35      loadDoc();
36    }
37
38    private void loadDoc()
39    {
40      if(File.Exists(cacheDocPath))
41      {
42        cacheDoc.Load(cacheDocPath);
43      }
44      else
45      {
46        Directory.CreateDirectory(Directory.GetParent(cacheDocPath).FullName);
47        cacheDoc.LoadXml("<cache />");
48      }
49    }
50
51    private void saveDoc()
52    {
53      cacheDoc.Save(cacheDocPath);
54    }
55
56    private XmlElement lastCacheElm;
57    private Uri lastUri;
58    private XmlElement getCacheElm(Uri uri)
59    {
60      if(uri == lastUri && lastCacheElm != null)
61      {
62        return lastCacheElm;
63      }
64      else
65      {
66        //string xpath = "/cache/resource[@url='" + uri.ToString() + "']";
67                string xpath = "/cache/resource[@url='" + uri.ToString().Replace("'", "&apos;") + "']";
68                XmlNode node = cacheDoc.SelectSingleNode(xpath);
69        if(node != null)
70        {
71          lastCacheElm = node as XmlElement;
72        }
73        else
74        {
75          lastCacheElm = cacheDoc.CreateElement("resource");
76          cacheDoc.DocumentElement.AppendChild(lastCacheElm);
77          lastCacheElm.SetAttribute("url", uri.ToString());
78        }
79
80        lastUri = uri;
81        return lastCacheElm;
82      }
83    }
84
85    private Uri getLocalPathUri(XmlElement cacheElm)
86    {
87      if(cacheElm.HasAttribute("local-path"))
88      {
89        string path = Path.Combine(cacheDir, cacheElm.GetAttribute("local-path"));
90        if(File.Exists(path))
91        {
92          path = "file:///" + path.Replace('\\', '/');
93          return new Uri(path);
94        }
95        else
96        {
97          cacheElm.RemoveAttribute("local-path");
98          return null;
99        }
100      }
101      else
102      {
103        return null;
104      }
105    }
106   
107    public long Size
108    {
109      get
110      {
111        DirectoryInfo di = new DirectoryInfo(cacheDir);
112        FileInfo[] files = di.GetFiles();
113        long size = 0;
114        foreach(FileInfo file in files)
115        {
116          size += file.Length;
117        }
118        return size;       
119      }
120    }
121
122    public void Clear()
123    {
124      DirectoryInfo di = new DirectoryInfo(cacheDir);
125      FileInfo[] files = di.GetFiles();
126      foreach(FileInfo file in files)
127      {
128        try
129        {
130          file.Delete();
131        }
132        catch{}
133      }
134
135      cacheDoc = new XmlDocument();
136
137      loadDoc();
138    }
139
140    public CacheInfo GetCacheInfo(Uri uri)
141    {
142      XmlElement cacheElm = getCacheElm(uri);
143
144      DateTime expires = DateTime.MinValue;
145      if(cacheElm.HasAttribute("expires"))
146      {
147        expires = DateTime.Parse(cacheElm.GetAttribute("expires"));
148      }
149
150      DateTime lastModified = DateTime.MinValue;
151      if(cacheElm.HasAttribute("last-modified"))
152      {
153        lastModified = DateTime.Parse(cacheElm.GetAttribute("last-modified"));
154      }
155
156      Uri cachedUri = getLocalPathUri(cacheElm);
157
158            return new CacheInfo(expires, cacheElm.GetAttribute("etag"), lastModified, cachedUri, cacheElm.GetAttribute("content-type"));
159    }
160
161    public void SetCacheInfo(Uri uri, CacheInfo cacheInfo, Stream stream)
162    {
163      XmlElement cacheElm = getCacheElm(uri);
164
165      if(cacheInfo != null)
166      {
167        if(cacheInfo.ETag != null)
168        {
169          cacheElm.SetAttribute("etag", cacheInfo.ETag);
170        }
171        else
172        {
173          cacheElm.RemoveAttribute("etag");
174        }
175
176        if(cacheInfo.ContentType != null)
177        {
178          cacheElm.SetAttribute("content-type", cacheInfo.ContentType);
179        }
180        else
181        {
182          cacheElm.RemoveAttribute("content-type");
183        }
184
185        if(cacheInfo.Expires != DateTime.MinValue)
186        {
187          cacheElm.SetAttribute("expires", cacheInfo.Expires.ToString("s"));
188        }
189        else
190        {
191          cacheElm.RemoveAttribute("expires");
192        }
193
194        if(cacheInfo.LastModified != DateTime.MinValue)
195        {
196          cacheElm.SetAttribute("last-modified", cacheInfo.LastModified.ToString("s"));
197        }
198        else
199        {
200          cacheElm.RemoveAttribute("last-modified");
201        }
202      }
203
204      if(stream != null)
205      {
206        string localPath;
207        if(cacheElm.HasAttribute("local-path"))
208        {
209          localPath = cacheElm.GetAttribute("local-path");
210        }
211        else
212        {
213                    localPath = Guid.NewGuid().ToString() + ".cache";
214          cacheElm.SetAttribute("local-path", localPath);
215        }
216
217        stream.Position = 0;
218        int count;
219        byte[] buffer = new byte[4096];
220
221        FileStream fs = File.OpenWrite(Path.Combine(cacheDir, localPath));
222        while((count = stream.Read(buffer, 0, 4096)) > 0) fs.Write(buffer, 0, count);
223        fs.Flush();
224        fs.Close();
225      }
226      saveDoc();
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.