Merging from head
[beagle.git] / Filters / FilterScribus.cs
blob045526db0de04f06a9ef4398848262ad537c2e6a
1 //
2 // FilterScribus.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 FilterScribus : Beagle.Daemon.Filter {
37 // These two arrays contain the mapping from attributes in a scribus
38 // file to actual beagle property names
39 private static string[] scribus_attributes = { "TITLE", "KEYWORDS", "COMMENTS", "DOCSOURCE", "DOCCONTRIB", "DOCRELATION", "DOCIDENT", "PUBLISHER", "AUTHOR", "DOCCOVER", "DOCRIGHTS" };
40 private static string[] scribus_properties = { "dc:title", "dc:keywords", "dc:description", "fixme:source", "dc:contributers", "fixme:relation", "dc:identifier", "dc:publisher", "dc:author", "fixme:coverage", "dc:rights" };
42 private StringBuilder sb = new StringBuilder();
44 public FilterScribus ()
46 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("application/x-scribus"));
47 AddSupportedFlavor (FilterFlavor.NewFromExtension (".sla"));
50 override protected void DoPullProperties ()
52 XmlTextReader reader = new XmlTextReader (Stream);
54 string text = String.Empty;
55 int pagecount = 0;
57 bool join_text_mode = false;
59 try {
60 while (reader.Read ()) {
61 switch (reader.NodeType) {
62 case XmlNodeType.Element:
63 switch (reader.LocalName) {
64 case "SCRIBUSUTF8":
65 join_text_mode = true;
66 AddProperty (Property.New ("fixme:scribus-version" , reader.GetAttribute("Version")));
67 break;
68 case "SCRIBUSUTF8NEW":
69 AddProperty (Property.New ("fixme:scribus-version" , reader.GetAttribute("Version")));
70 break;
71 case "DOCUMENT":
72 for (int i=0; i<scribus_attributes.Length; i++) {
73 text = reader.GetAttribute(scribus_attributes[i]);
74 AddProperty (Property.New (scribus_properties[i] , text));
77 // treat the DOCDATE attribute in a special way
78 text = reader.GetAttribute("DOCDATE");
79 if (text != null) {
80 try {
81 AddProperty (Property.NewDate ("dc:date", System.Convert.ToDateTime (text)));
82 } catch (FormatException) {
83 AddProperty (Property.New ("dc:date", text));
86 break;
87 case "ITEXT":
88 text = reader.GetAttribute("CH");
89 if (text == null)
90 break;
92 if (join_text_mode) {
93 sb.Append(text);
94 } else {
95 AppendText (text);
96 AppendStructuralBreak ();
98 break;
99 case "PAGE":
100 pagecount++;
101 break;
103 break;
104 case XmlNodeType.Comment:
105 AppendText (reader.Value.Trim ());
106 AppendStructuralBreak ();
107 break;
111 AddProperty (Property.New ("fixme:pagecount", pagecount.ToString ()));
113 if (join_text_mode)
114 AppendText (sb.ToString());
116 Finished ();
117 } catch (System.Xml.XmlException e) {
118 Logger.Log.Error ("Error parsing xml file {0}", FileInfo.FullName);
119 Logger.Log.Debug (e);
120 Error ();