cvsimport
[beagle.git] / Filters / FilterSvg.cs
blob90cad8657eff8d6b1f3bb2883579077fdfe852f6
1 //
2 // FilterSVG.cs
3 //
4 // Copyright (C) 2006 Alexander Macdonald <alex@alexmac.cc>
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Xml;
30 using System.Text;
32 using Beagle.Util;
33 using Beagle.Daemon;
35 namespace Beagle.Filters {
36 public class FilterSvg : Beagle.Daemon.Filter {
37 private StringBuilder sb = new StringBuilder ();
39 // List of keys that should be ignored when adding to content.
40 // For example, dc:format is the mime type, so it's not interesting text.
41 static private string [] ignore_strings = { "format"};
43 static private string dcnamespace = "http://purl.org/dc/elements/1.1/";
45 public FilterSvg ()
47 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("image/svg+xml"));
48 AddSupportedFlavor (FilterFlavor.NewFromExtension (".svg"));
51 override protected void DoPullProperties ()
53 XmlTextReader reader = new XmlTextReader (Stream);
54 reader.XmlResolver = null;
56 string current_tag_name = String.Empty;
57 bool grab_property = false;
58 bool grab_text = false;
60 try {
61 while (reader.Read ()) {
62 switch (reader.NodeType) {
63 case XmlNodeType.Element:
64 if (grab_text)
65 break;
66 if (reader.IsEmptyElement)
67 break;
69 if (ArrayFu.IndexOfString (ignore_strings, reader.LocalName) != -1)
70 break;
71 else if (reader.LocalName == "title") {
72 grab_property = true;
73 current_tag_name = "title";
74 } else if (reader.LocalName == "desc") {
75 grab_property = true;
76 current_tag_name = "description";
77 } else if (reader.LocalName == "text") {
78 grab_text = true;
79 current_tag_name = "text";
80 } else if (reader.LocalName == "RDF") {
81 PullRdfProperties (reader, reader.Depth);
83 break;
85 case XmlNodeType.Text:
86 if (grab_property)
87 AddProperty (Property.New ("dc:" + current_tag_name , reader.Value));
88 else if (grab_text) {
89 sb.Append (reader.Value);
91 break;
93 case XmlNodeType.Comment:
94 AppendText (reader.Value.Trim ());
95 AppendStructuralBreak ();
96 break;
98 case XmlNodeType.EndElement:
99 if (reader.LocalName != current_tag_name)
100 break;
102 if(grab_text) {
103 AppendText (sb.ToString());
104 AppendStructuralBreak ();
107 grab_text = grab_property = false;
108 break;
112 Finished ();
113 } catch (System.Xml.XmlException e) {
114 Logger.Log.Error ("Error parsing xml file {0}", FileInfo.FullName);
115 Logger.Log.Debug (e);
116 Error ();
120 protected void PullRdfProperties (XmlTextReader reader, int depth)
122 string current_tag_name = String.Empty;
123 bool grab_text = false;
124 bool grab_date = false;
126 try {
127 while (reader.Read ()) {
128 if (depth == reader.Depth)
129 return;
131 switch (reader.NodeType) {
132 case XmlNodeType.Element:
133 if (grab_text)
134 break;
136 if (reader.IsEmptyElement)
137 break;
139 if (ArrayFu.IndexOfString (ignore_strings, reader.LocalName) != -1)
140 break;
141 else if (reader.NamespaceURI == dcnamespace) {
142 if (reader.LocalName == "date")
143 grab_date = true;
144 else
145 grab_text = true;
148 current_tag_name = reader.LocalName;
149 break;
151 case XmlNodeType.Text:
152 if (grab_text)
153 AddProperty (Property.New ("dc:" + current_tag_name, reader.Value));
154 else if (grab_date) {
155 try {
156 AddProperty (Property.NewDate ("dc:date", System.Convert.ToDateTime (reader.Value.Trim())));
157 } catch (FormatException) {
158 AddProperty (Property.New ("dc:date", reader.Value));
161 break;
163 case XmlNodeType.EndElement:
164 if(reader.LocalName == current_tag_name)
165 grab_text = grab_date = false;
166 break;
169 } catch (System.Xml.XmlException e) {
170 Logger.Log.Error ("error parsing embedded RDF {0}", FileInfo.FullName);
171 Logger.Log.Debug (e);
172 Error ();