4 // Copyright (C) 2006 Alexander Macdonald <alex@alexmac.cc>
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.
35 namespace Beagle
.Filters
{
36 public class FilterSvg
: Beagle
.Daemon
.Filter
{
37 private StringBuilder sb
= new StringBuilder ();
39 private enum RdfGrabModes
{
46 static private string [] rdf_grab_strings
= { "title", "description", "date" }
;
47 static private string [] rdf_nongrab_strings
= { "creator", "contributor", "publisher", "rights" }
;
49 // List of keys that should be ignored when adding to content.
50 // For example, dc:format is the mime type, so it's not interesting text.
51 static private string [] ignore_strings
= { "format" }
;
55 AddSupportedFlavor (FilterFlavor
.NewFromMimeType ("image/svg+xml"));
56 AddSupportedFlavor (FilterFlavor
.NewFromExtension (".svg"));
59 override protected void DoPullProperties ()
61 XmlTextReader reader
= new XmlTextReader (Stream
);
62 reader
.XmlResolver
= null;
65 bool grab_text
= false, ignore_text
= false;;
69 while (reader
.Read ()) {
70 switch (reader
.NodeType
) {
71 case XmlNodeType
.Element
:
75 if (ArrayFu
.IndexOfString (ignore_strings
, reader
.LocalName
) != -1)
77 else if (reader
.LocalName
== "title" || reader
.LocalName
== "desc") {
80 } else if (reader
.LocalName
== "RDF")
81 PullRdfProperties (reader
, reader
.Depth
);
85 case XmlNodeType
.Text
:
86 text
= reader
.Value
.Trim ();
87 if (text
.Length
== 0 || ignore_text
)
94 AppendStructuralBreak ();
98 case XmlNodeType
.Comment
:
99 AppendText (reader
.Value
.Trim ());
100 AppendStructuralBreak ();
103 case XmlNodeType
.EndElement
:
104 if (! (grab_text
&& depth
== reader
.Depth
))
110 if (reader
.LocalName
== "title") {
111 AddProperty (Property
.New ("dc:title", sb
.ToString ()));
113 } else if (reader
.LocalName
== "desc") {
114 AddProperty (Property
.New ("dc:description", sb
.ToString ()));
122 } catch (System
.Xml
.XmlException e
) {
123 Logger
.Log
.Error ("error parsing xml file {0}", FileInfo
.FullName
);
124 Logger
.Log
.Debug (e
);
129 protected void PullRdfProperties (XmlTextReader reader
, int depth
)
131 int grab_mode
= -1, nongrab_mode
= -1;
132 bool grab_text
= false, ignore_text
= false, agent_mode
= false;
137 while (reader
.Read ()) {
138 if (depth
== reader
.Depth
)
141 switch (reader
.NodeType
) {
142 case XmlNodeType
.Element
:
146 if (ArrayFu
.IndexOfString (ignore_strings
, reader
.LocalName
) != -1)
148 else if (reader
.LocalName
== "Agent")
149 grab_text
= agent_mode
= true;
151 for (int i
= 0; i
< (int) RdfGrabModes
.Num
; i
++) {
152 if (reader
.LocalName
== rdf_grab_strings
[i
]) {
159 for (int i
= 0; i
< (int) RdfGrabModes
.Num
; i
++) {
160 if (reader
.LocalName
== rdf_nongrab_strings
[i
]) {
169 case XmlNodeType
.Text
:
170 text
= reader
.Value
.Trim ();
171 if (text
.Length
== 0 || ignore_text
)
178 AppendStructuralBreak ();
182 case XmlNodeType
.EndElement
:
186 if (reader
.LocalName
== "Agent") {
187 agent_mode
= grab_text
= false;
188 AddProperty (Property
.New ("dc:" + rdf_nongrab_strings
[nongrab_mode
], sb
.ToString ()));
191 } else if (grab_mode
>= 0 && reader
.LocalName
== rdf_grab_strings
[grab_mode
]) {
192 if (grab_mode
== (int) RdfGrabModes
.DateMode
) {
194 AddProperty (Property
.NewDate ("dc:date", System
.Convert
.ToDateTime (sb
.ToString ())));
195 } catch (FormatException
) {
196 AddProperty (Property
.New ("dc:date", sb
.ToString ()));
199 AddProperty (Property
.New ("dc:" + rdf_grab_strings
[grab_mode
], sb
.ToString ()));
204 } else if (nongrab_mode
>= 0 && reader
.LocalName
== rdf_nongrab_strings
[nongrab_mode
]) {
211 } catch (System
.Xml
.XmlException e
) {
212 Logger
.Log
.Error ("error parsing embedded RDF {0}", FileInfo
.FullName
);
213 Logger
.Log
.Debug (e
);