a9facc0b27a3e3504f704973c8fa5d131264bbfb
[debian/jabref.git] / src / java / net / sf / jabref / BibtexEntryType.java
1 /*
2 Copyright (C) 2003 David Weitzman, Morten O. Alver
3
4 All programs in this directory and
5 subdirectories are published under the GNU General Public License as
6 described below.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at
11 your option) any later version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA
22
23 Further information about the GNU GPL is available at:
24 http://www.gnu.org/copyleft/gpl.ja.html
25
26 Note:
27 Modified for use in JabRef.
28
29 */
30 package net.sf.jabref;
31
32 import java.io.*;
33 import java.util.*;
34
35 public abstract class BibtexEntryType implements Comparable
36 {
37
38     public static final BibtexEntryType OTHER =
39         new BibtexEntryType()
40         {
41             public String getName()
42             {
43                 return "Other";
44             }
45
46             public String[] getOptionalFields()
47             {
48                 return new String[0];
49             }
50
51             public String[] getRequiredFields()
52             {
53                 return new String[0];
54             }
55
56
57             public String describeRequiredFields()
58             {
59                 return "";
60             }
61
62             public boolean hasAllRequiredFields(BibtexEntry entry)
63             {
64                 return true;
65             }
66         };
67
68
69     public static final BibtexEntryType ARTICLE =
70         new BibtexEntryType()
71         {
72             public String getName()
73             {
74                 return "Article";
75             }
76
77             public String[] getOptionalFields()
78             {
79                 return new String[]
80                 {
81                     "number", "month", "eid", "note"
82                 };
83             }
84
85             public String[] getRequiredFields()
86             {
87                 return new String[]
88                 {
89                     "author", "title", "journal", "year", "volume", "pages"
90                 };
91             }
92
93             public String describeRequiredFields()
94             {
95                 return "AUTHOR, TITLE, JOURNAL and YEAR";
96             }
97
98             public boolean hasAllRequiredFields(BibtexEntry entry)
99             {
100                 return entry.allFieldsPresent(new String[]
101                     {
102                         "author", "title", "journal", "year", "bibtexkey", "volume", "pages"
103                     });
104             }
105         };
106
107     public static final BibtexEntryType BOOKLET =
108         new BibtexEntryType()
109         {
110             public String getName()
111             {
112                 return "Booklet";
113             }
114
115             public String[] getOptionalFields()
116             {
117                 return new String[]
118                 {
119                     "author", "howpublished", "lastchecked", "address", "month", "year", "note"
120                 };
121             }
122
123             public String[] getRequiredFields()
124             {
125                 return new String[]
126                 {
127                     "title"
128                 };
129             }
130
131             public String describeRequiredFields()
132             {
133                 return "TITLE";
134             }
135
136             public boolean hasAllRequiredFields(BibtexEntry entry)
137             {
138                 return entry.allFieldsPresent(new String[]
139                     {
140                         "title", "bibtexkey"
141                     });
142             }
143         };
144
145
146    public static final BibtexEntryType INBOOK =
147         new BibtexEntryType()
148         {
149             public String getName()
150             {
151                 return "Inbook";
152             }
153
154             public String[] getOptionalFields()
155             {
156                 return new String[]
157                 {
158                     "volume", "number", "series", "type", "address", "edition",
159                     "month", "note"
160                 };
161             }
162
163             public String[] getRequiredFields()
164             {
165                 return new String[]
166                 {
167                     "chapter", "pages", "title", "publisher", "year", "editor",
168                     "author"
169                 };
170             }
171
172             public String describeRequiredFields()
173             {
174                 return "TITLE, CHAPTER and/or PAGES, PUBLISHER, YEAR, and an "
175                     +"EDITOR and/or AUTHOR";
176             }
177
178             public boolean hasAllRequiredFields(BibtexEntry entry)
179             {
180                 return entry.allFieldsPresent(new String[]
181                     {
182                         "title", "publisher", "year", "bibtexkey"
183                     }) &&
184                     (((entry.getField("author") != null) ||
185                       (entry.getField("editor") != null)) &&
186                      ((entry.getField("chapter") != null) ||
187                       (entry.getField("pages") != null)));
188             }
189         };
190
191     public static final BibtexEntryType BOOK =
192         new BibtexEntryType()
193         {
194             public String getName()
195             {
196                 return "Book";
197             }
198
199             public String[] getOptionalFields()
200             {
201                 return new String[]
202                 {
203                     "volume", "number", "series", "address", "edition", "month",
204                     "note"
205                 };
206             }
207
208             public String[] getRequiredFields()
209             {
210                 return new String[]
211                 {
212                     "title", "publisher", "year", "editor", "author"
213                 };
214             }
215
216             public String describeRequiredFields()
217             {
218                 return "TITLE, PUBLISHER, YEAR, and an EDITOR and/or AUTHOR";
219             }
220
221             public boolean hasAllRequiredFields(BibtexEntry entry)
222             {
223                 return entry.allFieldsPresent(new String[]
224                     {
225                         "title", "publisher", "year", "bibtexkey"
226                     }) &&
227                 ((entry.getField("author") != null) ||
228                 (entry.getField("editor") != null));
229             }
230         };
231
232
233     public static final BibtexEntryType INCOLLECTION =
234         new BibtexEntryType()
235         {
236             public String getName()
237             {
238                 return "Incollection";
239             }
240
241             public String[] getOptionalFields()
242             {
243                 return new String[]
244                 {
245                     "editor", "volume", "number", "series", "type", "chapter",
246                     "pages", "address", "edition", "month", "note"
247                 };
248             }
249
250             public String[] getRequiredFields()
251             {
252                 return new String[]
253                 {
254                     "author", "title", "booktitle", "publisher", "year"
255                 };
256             }
257
258             public String describeRequiredFields()
259             {
260                 return "AUTHOR, TITLE, BOOKTITLE, PUBLISHER and YEAR";
261             }
262
263             public boolean hasAllRequiredFields(BibtexEntry entry)
264             {
265                 return entry.allFieldsPresent(new String[]
266                     {
267                         "author", "title", "booktitle", "publisher", "year",
268                         "bibtexkey"
269
270                     });
271             }
272         };
273
274     public static final BibtexEntryType CONFERENCE =
275         new BibtexEntryType()
276         {
277             public String getName()
278             {
279                 return "Conference";
280             }
281
282             public String[] getOptionalFields()
283             {
284                 return new String[]
285                 {
286                     "editor", "volume", "number", "series", "pages",
287                     "address", "month", "organization", "publisher", "note"
288                 };
289             }
290
291             public String[] getRequiredFields()
292             {
293                 return new String[]
294                 {
295                     "author", "title", "booktitle", "year"
296                 };
297             }
298
299             public String describeRequiredFields()
300             {
301                 return "AUTHOR, TITLE, BOOKTITLE and YEAR";
302             }
303
304             public boolean hasAllRequiredFields(BibtexEntry entry)
305             {
306                 return entry.allFieldsPresent(new String[]
307                     {
308                         "author", "title", "booktitle", "year" , "bibtexkey"
309                     });
310             }
311         };
312
313     public static final BibtexEntryType INPROCEEDINGS =
314         new BibtexEntryType()
315         {
316             public String getName()
317             {
318                 return "Inproceedings";
319             }
320
321             public String[] getOptionalFields()
322             {
323                 return new String[]
324                 {
325                     "editor", "volume", "number", "series", "pages",
326                     "address", "month", "organization", "publisher", "note"
327                 };
328             }
329
330             public String[] getRequiredFields()
331             {
332                 return new String[]
333                 {
334                     "author", "title", "booktitle", "year"
335                 };
336             }
337
338             public String describeRequiredFields()
339             {
340                 return "AUTHOR, TITLE, BOOKTITLE and YEAR";
341             }
342
343             public boolean hasAllRequiredFields(BibtexEntry entry)
344             {
345                 return entry.allFieldsPresent(new String[]
346                     {
347                         "author", "title", "booktitle", "year" , "bibtexkey"
348                     });
349             }
350         };
351
352     public static final BibtexEntryType PROCEEDINGS =
353         new BibtexEntryType()
354         {
355             public String getName()
356             {
357                 return "Proceedings";
358             }
359
360             public String[] getOptionalFields()
361             {
362                 return new String[]
363                 {
364                     "editor", "volume", "number", "series", "address",
365                     "publisher", "note", "month", "organization"
366                 };
367             }
368
369             public String[] getRequiredFields()
370             {
371                 return new String[]
372                 {
373                     "title", "year"
374                 };
375             }
376
377             public String describeRequiredFields()
378             {
379                 return "TITLE and YEAR";
380             }
381
382             public boolean hasAllRequiredFields(BibtexEntry entry)
383             {
384                 return entry.allFieldsPresent(new String[]
385                     {
386                         "title", "year", "bibtexkey"
387                     });
388             }
389         };
390
391
392     public static final BibtexEntryType MANUAL =
393         new BibtexEntryType()
394         {
395             public String getName()
396             {
397                 return "Manual";
398             }
399
400             public String[] getOptionalFields()
401             {
402                 return new String[]
403                 {
404                     "author", "organization", "address", "edition",
405                     "month", "year", "note"
406                 };
407             }
408
409             public String[] getRequiredFields()
410             {
411                 return new String[]
412                 {
413                     "title"
414                 };
415             }
416
417             public String describeRequiredFields()
418             {
419                 return "TITLE";
420             }
421
422             public boolean hasAllRequiredFields(BibtexEntry entry)
423             {
424                 return entry.allFieldsPresent(new String[]
425                     {
426                         "title", "bibtexkey"
427                     });
428             }
429         };
430
431     public static final BibtexEntryType TECHREPORT =
432         new BibtexEntryType()
433         {
434             public String getName()
435             {
436                 return "Techreport";
437             }
438
439             public String[] getOptionalFields()
440             {
441                 return new String[]
442                 {
443                     "type", "number", "address", "month", "note"
444                 };
445             }
446
447             public String[] getRequiredFields()
448             {
449                 return new String[]
450                 {
451                     "author", "title", "institution", "year"
452                 };
453             }
454
455             public String describeRequiredFields()
456             {
457                 return "AUTHOR, TITLE, INSTITUTION and YEAR";
458             }
459
460             public boolean hasAllRequiredFields(BibtexEntry entry)
461             {
462                 return entry.allFieldsPresent(new String[]
463                     {
464                         "author", "title", "institution", "year",
465                         "bibtexkey"
466                     });
467             }
468         };
469
470
471     public static final BibtexEntryType MASTERSTHESIS =
472         new BibtexEntryType()
473         {
474             public String getName()
475             {
476                 return "Mastersthesis";
477             }
478
479             public String[] getOptionalFields()
480             {
481                 return new String[]
482                 {
483                     "type", "address", "month", "note"
484                 };
485             }
486
487             public String[] getRequiredFields()
488             {
489                 return new String[]
490                 {
491                     "author", "title", "school", "year"
492                 };
493             }
494
495             public String describeRequiredFields()
496             {
497                 return "AUTHOR, TITLE, SCHOOL and YEAR";
498             }
499
500             public boolean hasAllRequiredFields(BibtexEntry entry)
501             {
502                 return entry.allFieldsPresent(new String[]
503                     {
504                         "author", "title", "school", "year", "bibtexkey"
505                     });
506             }
507         };
508
509
510     public static final BibtexEntryType PHDTHESIS =
511         new BibtexEntryType()
512         {
513             public String getName()
514             {
515                 return "Phdthesis";
516             }
517
518             public String[] getOptionalFields()
519             {
520                 return new String[]
521                 {
522                     "type", "address", "month", "note"
523                 };
524             }
525
526             public String[] getRequiredFields()
527             {
528                 return new String[]
529                 {
530                     "author", "title", "school", "year"
531                 };
532             }
533
534             public String describeRequiredFields()
535             {
536                 return "AUTHOR, TITLE, SCHOOL and YEAR";
537             }
538
539             public boolean hasAllRequiredFields(BibtexEntry entry)
540             {
541                 return entry.allFieldsPresent(new String[]
542                     {
543                         "author", "title", "school", "year", "bibtexkey"
544                     });
545             }
546         };
547
548     public static final BibtexEntryType UNPUBLISHED =
549         new BibtexEntryType()
550         {
551             public String getName()
552             {
553                 return "Unpublished";
554             }
555
556             public String[] getOptionalFields()
557             {
558                 return new String[]
559                 {
560                     "month", "year"
561                 };
562             }
563
564             public String[] getRequiredFields()
565             {
566                 return new String[]
567                 {
568                     "author", "title", "note"
569                 };
570             }
571
572             public String describeRequiredFields()
573             {
574                 return "AUTHOR, TITLE and NOTE";
575             }
576
577             public boolean hasAllRequiredFields(BibtexEntry entry)
578             {
579                 return entry.allFieldsPresent(new String[]
580                     {
581                         "author", "title", "note", "bibtexkey"
582                     });
583             }
584         };
585
586
587     public static final BibtexEntryType MISC =
588         new BibtexEntryType()
589         {
590             public String getName()
591             {
592                 return "Misc";
593             }
594
595             public String[] getOptionalFields()
596             {
597                 return new String[]
598                 {
599                     "author", "title", "howpublished", "month", "year", "note"
600                 };
601             }
602
603             public String[] getRequiredFields()
604             {
605                 return null;
606             }
607
608             public String describeRequiredFields()
609             {
610                 return "None";
611             }
612
613             public boolean hasAllRequiredFields(BibtexEntry entry)
614             {
615                 return entry.allFieldsPresent(new String[]
616                     {
617                         "bibtexkey"
618                     });
619             }
620         };
621
622     /**
623      * This type is provided as an emergency choice if the user makes
624      * customization changes that remove the type of an entry.
625      */
626     public static final BibtexEntryType TYPELESS =
627         new BibtexEntryType()
628         {
629             public String getName()
630             {
631                 return "Typeless";
632             }
633
634             public String[] getOptionalFields()
635             {
636                 return null;
637             }
638
639             public String[] getRequiredFields()
640             {
641                 return null;
642             }
643
644             public String describeRequiredFields()
645             {
646                 return "None";
647             }
648
649             public boolean hasAllRequiredFields(BibtexEntry entry)
650             {
651                 return false;
652            }
653         };
654
655
656     public abstract String getName();
657
658     public int compareTo(Object o) {
659         return getName().compareTo(((BibtexEntryType)o).getName());
660     }
661
662     public abstract String[] getOptionalFields();
663
664     public abstract String[] getRequiredFields();
665
666     public String[] getGeneralFields() {
667         return new String[]
668             {"crossref", "keywords", "doi", "url",
669              "citeseerurl", "pdf", "abstract", "comment"};
670     }
671
672     public abstract String describeRequiredFields();
673
674     public abstract boolean hasAllRequiredFields(BibtexEntry entry);
675
676
677     public String[] getUtilityFields(){
678         return new String[] {"search" } ;
679     }
680
681
682     public boolean isRequired(String field) {
683         String[] req = getRequiredFields();
684         if (req == null) return false;
685         for (int i=0; i<req.length; i++)
686             if (req[i].equals(field)) return true;
687         return false;
688     }
689
690     public boolean isOptional(String field) {
691         String[] opt = getOptionalFields();
692         if (opt == null) return false;
693         for (int i=0; i<opt.length; i++)
694             if (opt[i].equals(field)) return true;
695         return false;
696     }
697
698     public static TreeMap ALL_TYPES = new TreeMap();
699     public static TreeMap STANDARD_TYPES = new TreeMap();
700     static {
701         // Put the standard entry types into the type map.
702         ALL_TYPES.put("article", ARTICLE);
703         ALL_TYPES.put("inbook", INBOOK);
704         ALL_TYPES.put("book", BOOK);
705         ALL_TYPES.put("booklet", BOOKLET);
706         ALL_TYPES.put("incollection", INCOLLECTION);
707         ALL_TYPES.put("conference", CONFERENCE);
708         ALL_TYPES.put("inproceedings", INPROCEEDINGS);
709         ALL_TYPES.put("proceedings", PROCEEDINGS);
710         ALL_TYPES.put("manual", MANUAL);
711         ALL_TYPES.put("mastersthesis", MASTERSTHESIS);
712         ALL_TYPES.put("phdthesis", PHDTHESIS);
713         ALL_TYPES.put("techreport", TECHREPORT);
714         ALL_TYPES.put("unpublished", UNPUBLISHED);
715         ALL_TYPES.put("misc", MISC);
716         ALL_TYPES.put("other", OTHER);
717
718         // We need a record of the standard types, in case the user wants
719         // to remove a customized version. Therefore we clone the map.
720         STANDARD_TYPES = (TreeMap)ALL_TYPES.clone();
721     }
722
723     /**
724      * This method returns the BibtexEntryType for the name of a type,
725      * or null if it does not exist.
726      */
727     public static BibtexEntryType getType(String name) {
728         //Util.pr("'"+name+"'");
729         Object o = ALL_TYPES.get(name.toLowerCase());
730         if (o == null)
731             return null;
732         else return (BibtexEntryType)o;
733     }
734
735     /**
736      * This method returns the standard BibtexEntryType for the
737      * name of a type, or null if it does not exist.
738      */
739     public static BibtexEntryType getStandardType(String name) {
740         //Util.pr("'"+name+"'");
741         Object o = STANDARD_TYPES.get(name.toLowerCase());
742         if (o == null)
743             return null;
744         else return (BibtexEntryType)o;
745     }
746
747     /**
748      * Removes a customized entry type from the type map. If this type
749      * overrode a standard type, we reinstate the standard one.
750      *
751      * @param name The customized entry type to remove.
752      */
753     public static void removeType(String name) {
754         //BibtexEntryType type = getType(name);
755         String nm = name.toLowerCase();
756         //System.out.println(ALL_TYPES.size());
757         ALL_TYPES.remove(nm);
758         //System.out.println(ALL_TYPES.size());
759         if (STANDARD_TYPES.get(nm) != null) {
760             // In this case the user has removed a customized version
761             // of a standard type. We reinstate the standard type.
762             ALL_TYPES.put(nm, STANDARD_TYPES.get(nm));
763         }
764
765     }
766
767     /**
768      * Load all custom entry types from preferences. This method is
769      * called from JabRef when the program starts.
770      */
771     public static void loadCustomEntryTypes(JabRefPreferences prefs) {
772         int number = 0;
773         CustomEntryType type;
774         while ((type = prefs.getCustomEntryType(number)) != null) {
775             ALL_TYPES.put(type.getName().toLowerCase(), type);
776             number++;
777         }
778     }
779
780     /**
781      * Iterate through all entry types, and store those that are
782      * custom defined to preferences. This method is called from
783      * JabRefFrame when the program closes.
784      */
785     public static void saveCustomEntryTypes(JabRefPreferences prefs) {
786         Iterator i=ALL_TYPES.keySet().iterator();
787         int number = 0;
788         //Vector customTypes = new Vector(10, 10);
789         while (i.hasNext()) {
790             Object o=ALL_TYPES.get(i.next());
791             if (o instanceof CustomEntryType) {
792                 // Store this entry type.
793                 prefs.storeCustomEntryType((CustomEntryType)o, number);
794                 number++;
795             }
796         }
797         // Then, if there are more 'old' custom types defined, remove these
798         // from preferences. This is necessary if the number of custom types
799         // has decreased.
800         prefs.purgeCustomEntryTypes(number);
801     }
802
803 }