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 // 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/";
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
= "";
57 bool grab_property
= false;
58 bool grab_text
= false;
61 while (reader
.Read ()) {
62 switch (reader
.NodeType
) {
63 case XmlNodeType
.Element
:
66 if (reader
.IsEmptyElement
)
69 if (ArrayFu
.IndexOfString (ignore_strings
, reader
.LocalName
) != -1)
71 else if (reader
.LocalName
== "title") {
73 current_tag_name
= "title";
74 } else if (reader
.LocalName
== "desc") {
76 current_tag_name
= "description";
77 } else if (reader
.LocalName
== "text") {
79 current_tag_name
= "text";
80 } else if (reader
.LocalName
== "RDF") {
81 PullRdfProperties (reader
, reader
.Depth
);
85 case XmlNodeType
.Text
:
87 AddProperty (Property
.New ("dc:" + current_tag_name
, reader
.Value
));
89 sb
.Append (reader
.Value
);
93 case XmlNodeType
.Comment
:
94 AppendText (reader
.Value
.Trim ());
95 AppendStructuralBreak ();
98 case XmlNodeType
.EndElement
:
99 if (reader
.LocalName
!= current_tag_name
)
103 AppendText (sb
.ToString());
104 AppendStructuralBreak ();
107 grab_text
= grab_property
= false;
113 } catch (System
.Xml
.XmlException e
) {
114 Logger
.Log
.Error ("Error parsing xml file {0}", FileInfo
.FullName
);
115 Logger
.Log
.Debug (e
);
120 protected void PullRdfProperties (XmlTextReader reader
, int depth
)
122 string current_tag_name
= "";
123 bool grab_text
= false;
124 bool grab_date
= false;
127 while (reader
.Read ()) {
128 if (depth
== reader
.Depth
)
131 switch (reader
.NodeType
) {
132 case XmlNodeType
.Element
:
136 if (reader
.IsEmptyElement
)
139 if (ArrayFu
.IndexOfString (ignore_strings
, reader
.LocalName
) != -1)
141 else if (reader
.NamespaceURI
== dcnamespace
) {
142 if (reader
.LocalName
== "date")
148 current_tag_name
= reader
.LocalName
;
151 case XmlNodeType
.Text
:
153 AddProperty (Property
.New ("dc:" + current_tag_name
, reader
.Value
));
154 else if (grab_date
) {
156 AddProperty (Property
.NewDate ("dc:date", System
.Convert
.ToDateTime (reader
.Value
.Trim())));
157 } catch (FormatException
) {
158 AddProperty (Property
.New ("dc:date", reader
.Value
));
163 case XmlNodeType
.EndElement
:
164 if(reader
.LocalName
== current_tag_name
)
165 grab_text
= grab_date
= false;
169 } catch (System
.Xml
.XmlException e
) {
170 Logger
.Log
.Error ("error parsing embedded RDF {0}", FileInfo
.FullName
);
171 Logger
.Log
.Debug (e
);