BibliographyExporter.cs

00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using Shared;
00005 
00006 namespace Client
00007 {
00008     #region Exporter
00012     class BibliographyExporter
00013     {
00015         private List<LiteratureInfo> literature = new List<LiteratureInfo>();
00017         private string projectTitle;
00019         private string extension;
00020 
00022         public string Extension
00023         {
00024             get { return extension; }
00025         }
00026 
00030         public string ProjectTitle
00031         {
00032             get { return projectTitle; }
00033             set { projectTitle = value; }
00034         }
00035 
00040         public BibliographyExporter(List<LiteratureInfo> literature)
00041         {
00042             this.literature = literature;
00043         }
00044 
00051         public string getBibliography( BibliographyType bibType, out string filename )
00052         {
00053             BibExporter bibExporter;
00054             string renderedBibliography;
00055 
00056             // TODO: Make this prettier... Should really be just checking wether a class name of Exporter + bibType.ToString() exists, and then instantiate that...
00057             switch (bibType)
00058             {
00059                 case BibliographyType.BibTeX:
00060                     bibExporter = new ExporterBibTeX();
00061                     break;
00062                 case BibliographyType.EndNote:
00063                     extension = String.Empty;
00064                     filename = "Sorry, not supported yet";
00065                     return String.Empty;
00066                     //bibExporter = new ExporterEndNote();
00067                     //break;
00068                 case BibliographyType.RTF:
00069                     extension = String.Empty;
00070                     filename = "Sorry, not supported yet";
00071                     return String.Empty;
00072                     //bibExporter = new ExporterRTF();
00073                     //break;
00074                 case BibliographyType.XHTML:
00075                     extension = String.Empty;
00076                     filename = "Sorry, not supported yet";
00077                     return String.Empty;
00078                     //bibExporter = new ExporterXHTML();
00079                     //break;
00080                 default:
00081                     extension = String.Empty;
00082                     filename = String.Empty;
00083                     return String.Empty;
00084             }
00085 
00086             renderedBibliography = bibExporter.Begin();
00087             foreach (LiteratureInfo theItem in this.literature)
00088                 renderedBibliography += bibExporter.GetItem(theItem);
00089             renderedBibliography += bibExporter.End();
00090 
00091             extension = bibExporter.FileExtension;
00092             filename = this.projectTitle + "." + bibExporter.FileExtension;
00093 
00094             return renderedBibliography;
00095         }
00096     }
00097 
00101     public interface BibExporter
00102     {
00104         string FileExtension{ get; }
00109         string Begin();
00115         string GetItem( LiteratureInfo lit );
00120         string End();
00121     }
00122     #endregion
00123 
00124     #region BibTeX handling
00128     public class ExporterBibTeX : BibExporter
00129     {
00130         #region BibExporter Members
00131 
00133         private string fileExtension = "bib";
00134 
00136         public string FileExtension { get { return fileExtension; } }
00137 
00142         public string Begin()
00143         {
00144             return String.Empty;
00145         }
00146 
00152         public string GetItem(LiteratureInfo lit)
00153         {
00154             BibTeXItem item = new BibTeXItem(lit.type, lit.id, lit.title);
00155             item.fields.Add("author", lit.author);
00156             item.fields.Add("year", lit.date.Year.ToString());
00157             item.fields.Add("month", lit.date.Month.ToString());
00158 
00159             if (lit.isbn != String.Empty)
00160                 item.fields.Add("isbn", lit.isbn);
00161 
00162             switch (lit.type)
00163             {
00164                 case LiteratureType.Article:
00165                     item.fields.Add("journal", lit.source);
00166                     break;
00167                 case LiteratureType.Book:
00168                 case LiteratureType.Inbook:
00169                 case LiteratureType.Incollection:
00170                     item.fields.Add("publisher", lit.source);
00171                     break;
00172                 case LiteratureType.BachelorThesis:
00173                 case LiteratureType.MasterThesis:
00174                 case LiteratureType.PhDThesis:
00175                     item.fields.Add("school", lit.source);
00176                     break;
00177                 case LiteratureType.Conference:
00178                 case LiteratureType.Inproceedings:
00179                     item.fields.Add("booktitle", lit.source);
00180                     break;
00181                 case LiteratureType.TechReport:
00182                     item.fields.Add("institution", lit.source);
00183                     break;
00184                 default:
00185                     item.fields.Add("note", lit.source);
00186                     break;
00187             }
00188 
00189             return item.ToString();
00190         }
00191 
00196         public string End()
00197         {
00198             return String.Empty;
00199         }
00200 
00201         #endregion
00202     }
00203 
00207     public class BibTeXItem
00208     {
00215         public LiteratureType theType;
00217         public string citeKey;
00219         public Dictionary<string, string> fields = new Dictionary<string, string>();
00220 
00227         public BibTeXItem(LiteratureType theType, int id, string title)
00228         {
00229             this.theType = theType;
00230             this.citeKey = id.ToString();
00231             this.fields.Add("title", title);
00232         }
00233 
00238         public override string ToString()
00239         {
00240             string citeType = "";
00241             switch (theType)
00242             {
00243                 case LiteratureType.Article:
00244                     citeType = "Article";
00245                     if (!fields.ContainsKey("author"))
00246                         fields.Add("author", "");
00247                     if (!fields.ContainsKey("journal"))
00248                         fields.Add("journal", "");
00249                     if (!fields.ContainsKey("year"))
00250                         fields.Add("year", "");
00251                     break;
00252                 case LiteratureType.Ebook:
00253                     if (!fields.ContainsKey("note"))
00254                         fields.Add("note", "");
00255                     // Would have done a fallthrough here, but docs say goto :P
00256                     goto case LiteratureType.Book;
00257                 case LiteratureType.Book:
00258                     citeType = "Book";
00259                     if (!fields.ContainsKey("author"))
00260                         fields.Add("author", "");
00261                     if (!fields.ContainsKey("editor"))
00262                         fields.Add("editor", "");
00263                     if (!fields.ContainsKey("publisher"))
00264                         fields.Add("publisher", "");
00265                     if (!fields.ContainsKey("year"))
00266                         fields.Add("year", "");
00267                     break;
00268                 case LiteratureType.Booklet:
00269                     citeType = "Booklet";
00270                     // There is only the one required item here!
00271                     break;
00272                 case LiteratureType.Conference:
00273                 case LiteratureType.Inproceedings:
00274                     citeType = "Inproceedings";
00275                     if (!fields.ContainsKey("author"))
00276                         fields.Add("author", "");
00277                     if (!fields.ContainsKey("booktitle"))
00278                         fields.Add("booktitle", "");
00279                     if (!fields.ContainsKey("year"))
00280                         fields.Add("year", "");
00281                     break;
00282                 case LiteratureType.Inbook:
00283                     citeType = "Inbook";
00284                     if (!fields.ContainsKey("author"))
00285                         fields.Add("author", "");
00286                     if (!fields.ContainsKey("editor"))
00287                         fields.Add("editor", "");
00288                     if (!fields.ContainsKey("chapter") || !fields.ContainsKey("pages"))
00289                         fields.Add("pages", "");
00290                     if (!fields.ContainsKey("publisher"))
00291                         fields.Add("publisher", "");
00292                     if (!fields.ContainsKey("year"))
00293                         fields.Add("year", "");
00294                     break;
00295                 case LiteratureType.Incollection:
00296                     citeType = "Incollection";
00297                     if (!fields.ContainsKey("author"))
00298                         fields.Add("author", "");
00299                     if (!fields.ContainsKey("booktitle"))
00300                         fields.Add("booktitle", "");
00301                     if (!fields.ContainsKey("year"))
00302                         fields.Add("year", "");
00303                     break;
00304                 case LiteratureType.Journal:
00305                     citeType = "Journal";
00306                     if (!fields.ContainsKey("volume"))
00307                         fields.Add("volume", "");
00308                     break;
00309                 case LiteratureType.Manual:
00310                     citeType = "Manual";
00311                     // There is only the one required item here!
00312                     break;
00313                 case LiteratureType.BachelorThesis:
00314                     citeType = "Unpublished";
00315                     // Would have done a fallthrough here, but docs say goto :P
00316                     goto case LiteratureType.PhDThesis;
00317                 case LiteratureType.MasterThesis:
00318                     citeType = "MasterThesis";
00319                     // Would have done a fallthrough here, but docs say goto :P
00320                     goto case LiteratureType.PhDThesis;
00321                 case LiteratureType.PhDThesis:
00322                     // Got to check this because of the gotos above...
00323                     if( theType == LiteratureType.PhDThesis)
00324                         citeType = "PhDThesis";
00325                     if (!fields.ContainsKey("author"))
00326                         fields.Add("author", "");
00327                     if (!fields.ContainsKey("school"))
00328                         fields.Add("school", "");
00329                     if (!fields.ContainsKey("year"))
00330                         fields.Add("year", "");
00331                     break;
00332                 case LiteratureType.Proceedings:
00333                     citeType = "Proceedings";
00334                     if (!fields.ContainsKey("year"))
00335                         fields.Add("year", "");
00336                     break;
00337                 case LiteratureType.TechReport:
00338                     citeType = "TechReport";
00339                     if (!fields.ContainsKey("author"))
00340                         fields.Add("author", "");
00341                     if (!fields.ContainsKey("institution"))
00342                         fields.Add("institution", "");
00343                     if (!fields.ContainsKey("year"))
00344                         fields.Add("year", "");
00345                     break;
00346                 case LiteratureType.Unpublished:
00347                     citeType = "Unpublished";
00348                     if (!fields.ContainsKey("author"))
00349                         fields.Add("author", "");
00350                     if (!fields.ContainsKey("note"))
00351                         fields.Add("note", "");
00352                     break;
00353                 case LiteratureType.Misc:
00354                     citeType = "Misc";
00355                     // There is not actually any required items here, but we're going to require a title anyway...
00356                     break;
00357                 case LiteratureType.Movie:
00358                     citeType = "Misc";
00359                     if (!fields.ContainsKey("author"))
00360                         fields.Add("author", "");
00361                     break;
00362                 case LiteratureType.Website:
00363                     citeType = "Misc";
00364                     if (!fields.ContainsKey("author"))
00365                         fields.Add("author", "");
00366                     if (!fields.ContainsKey("year"))
00367                         fields.Add("year", "");
00368                     if (!fields.ContainsKey("note"))
00369                         fields.Add("note", "");
00370                     break;
00371             }
00372 
00373             string escapedValue;
00374             string renderedItem = "@";
00375             renderedItem += citeType;
00376             renderedItem += "{" + citeKey + "," + System.Environment.NewLine;
00377 
00378             foreach (KeyValuePair<string, string> theItem in fields)
00379             {
00380                 escapedValue = theItem.Value.ToString();
00381                 // Is this the correct escape method?
00382                 escapedValue = System.Text.RegularExpressions.Regex.Escape(escapedValue);
00383 
00384                 // Special case - need to put source in special environment...
00385                 // Also, this needs to be here, to avoid too much logic elsewhere (as in, above doing one thing, then here to check if it has been done... so we only check here)
00386                 if (theType == LiteratureType.Website && theItem.Key.ToString() == "note")
00387                     escapedValue = "\\texttt{" + escapedValue + "}";
00388 
00389                 renderedItem += "    " + theItem.Key.ToString() + "= \"" + escapedValue + "\"," + System.Environment.NewLine;
00390             }
00391 
00392             renderedItem += "}" + System.Environment.NewLine;
00393 
00394             return renderedItem;
00395         }
00396     }
00397     #endregion
00398 
00399     #region EndNote handling
00404     public class ExporterEndNote : BibExporter
00405     {
00406         #region BibExporter Members
00407 
00409         private string fileExtension = "enl";
00410 
00412         public string FileExtension { get { return fileExtension; } }
00413 
00418         public string Begin()
00419         {
00420             throw new Exception("The method or operation is not implemented.");
00421         }
00422 
00428         public string GetItem(LiteratureInfo lit)
00429         {
00430             throw new Exception("The method or operation is not implemented.");
00431         }
00432 
00437         public string End()
00438         {
00439             throw new Exception("The method or operation is not implemented.");
00440         }
00441 
00442         #endregion
00443     }
00444     #endregion
00445 
00446     #region RTF handler
00450     public class ExporterRTF : BibExporter
00451     {
00452         #region BibExporter Members
00453 
00455         private string fileExtension = "rtf";
00456 
00458         public string FileExtension { get { return fileExtension; } }
00459         
00464         public string Begin()
00465         {
00466             throw new Exception("The method or operation is not implemented.");
00467         }
00468 
00474         public string GetItem(LiteratureInfo lit)
00475         {
00476             throw new Exception("The method or operation is not implemented.");
00477         }
00478 
00483         public string End()
00484         {
00485             throw new Exception("The method or operation is not implemented.");
00486         }
00487 
00488         #endregion
00489     }
00490     #endregion
00491 
00492     #region XHTML handler
00496     public class ExporterXHTML : BibExporter
00497     {
00498         #region BibExporter Members
00499 
00501         private string fileExtension = "xhtml";
00502 
00504         public string FileExtension { get { return fileExtension; } }
00505 
00510         public string Begin()
00511         {
00512             throw new Exception("The method or operation is not implemented.");
00513         }
00514 
00520         public string GetItem(LiteratureInfo lit)
00521         {
00522             throw new Exception("The method or operation is not implemented.");
00523         }
00524 
00529         public string End()
00530         {
00531             throw new Exception("The method or operation is not implemented.");
00532         }
00533 
00534         #endregion
00535     }
00536     #endregion
00537 }

Generated on Thu Dec 21 06:21:54 2006 for SCRAML by  doxygen 1.5.1-p1