1 | /** | | 1 | /** |
2 | * Implements grammer rule "Authors" | | 2 | * Parse the entries in the source, and return a List of BibtexEntry |
3 | * | | 3 | * objects. |
4 | * @param be | | 4 | */ |
5 | * @throws IOException | | 5 | public List<BibtexEntry> importEntries(InputStream stream, OutputPrinter status) throws IOException { |
6 | */ | | 6 | ArrayList<BibtexEntry> bibitems = new ArrayList<BibtexEntry>(); |
7 | private void parseAuthors(BibtexEntry be) throws IOException { | | 7 | StringBuffer sb = new StringBuffer(); |
8 | // read authors and institutions | | 8 | BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream)); |
9 | String authors = ""; | | 9 | String ENDOFRECORD = "__EOREOR__"; |
10 | String institutions = ""; | | 10 | |
11 | while (this.lastLine != null && !this.lastLine.equals("") && !startsWithKeyword(recognizedFields)) { | | 11 | String str; |
12 | | | 12 | boolean first = true; |
13 | // read single author | | 13 | while ((str = in.readLine()) != null){ |
14 | String author = null; | | 14 | str = str.trim(); |
15 | String institution = null; | | 15 | // if(str.equals("")) continue; |
16 | boolean institutionDone = false; | | 16 | if (str.indexOf("%0") == 0){ |
17 | if (this.lastLine.indexOf('(') >= 0) { | | 17 | if (first){ |
18 | author = this.lastLine.substring(0, this.lastLine.indexOf('(')).trim(); | | 18 | first = false; |
19 | institutionDone = this.lastLine.indexOf(')') > 0; | | 19 | }else{ |
20 | institution = this.lastLine.substring(this.lastLine.indexOf('(') + 1, institutionDone && this.lastLine.indexOf(')') > this.lastLine.indexOf('(') + 1 ? this.lastLine.indexOf(')') : this.lastLine.length()).trim(); | | 20 | sb.append(ENDOFRECORD); |
21 | } else { | | 21 | } |
22 | author = this.lastLine.substring(0, this.lastLine.length()).trim(); | | 22 | sb.append(str); |
23 | institutionDone = true; | | 23 | }else sb.append(str); |
24 | } | | 24 | sb.append("\n"); |
25 | | | 25 | } |
26 | readLine(); | | 26 | |
27 | while (!institutionDone && this.lastLine!= null) { | | 27 | String[] entries = sb.toString().split(ENDOFRECORD); |
28 | institutionDone = this.lastLine.indexOf(')') > 0; | | 28 | HashMap<String, String> hm = new HashMap<String, String>(); |
29 | institution += this.lastLine.substring(0, institutionDone ? this.lastLine.indexOf(')') : this.lastLine.length()).trim(); | | 29 | String author = "", Type = "", editor = "", artnum = ""; |
30 | readLine(); | | 30 | for (int i = 0; i < entries.length; i++){ |
31 | } | | 31 | hm.clear(); |
32 | | | 32 | author = ""; |
33 | if (author != null) { | | 33 | Type = ""; |
34 | authors += !authors.equals("") ? " and " + author : "" + author; | | 34 | editor = ""; |
35 | } | | 35 | artnum = ""; |
36 | if (institution != null) { | | 36 | |
37 | institutions += !institutions.equals("") ? " and " + institution : "" + institution; | | 37 | boolean IsEditedBook = false; |
38 | } | | 38 | String[] fields = entries[i].trim().substring(1).split("\n%"); |
39 | } | | 39 | //String lastPrefix = ""; |
40 | | | 40 | for (int j = 0; j < fields.length; j++){ |
41 | if (!authors.equals("")) { | | 41 | |
42 | be.setField("author", authors); | | 42 | if (fields[j].length() < 3) continue; |
43 | } | | 43 | |
44 | if (!institutions.equals("")) { | | 44 | /* |
45 | be.setField("institution", institutions); | | 45 | * Details of Refer format for Journal Article and Book: |
46 | } | | 46 | * |
47 | } | | 47 | * Generic Ref Journal Article Book Code Author %A Author Author Year %D |
| | | 48 | * Year Year Title %T Title Title Secondary Author %E Series Editor |
| | | 49 | * Secondary Title %B Journal Series Title Place Published %C City |
| | | 50 | * Publisher %I Publisher Volume %V Volume Volume Number of Volumes %6 |
| | | 51 | * Number of Volumes Number %N Issue Pages %P Pages Number of Pages |
| | | 52 | * Edition %7 Edition Subsidiary Author %? Translator Alternate Title %J |
| | | 53 | * Alternate Journal Label %F Label Label Keywords %K Keywords Keywords |
| | | 54 | * Abstract %X Abstract Abstract Notes %O Notes Notes |
| | | 55 | */ |
| | | 56 | |
| | | 57 | String prefix = fields[j].substring(0, 1); |
| | | 58 | |
| | | 59 | String val = fields[j].substring(2); |
| | | 60 | |
| | | 61 | if (prefix.equals("A")){ |
| | | 62 | if (author.equals("")) author = val; |
| | | 63 | else author += " and " + val; |
| | | 64 | }else if (prefix.equals("E")){ |
| | | 65 | if (editor.equals("")) editor = val; |
| | | 66 | else editor += " and " + val; |
| | | 67 | }else if (prefix.equals("T")) hm.put("title", val); |
| | | 68 | else if (prefix.equals("0")){ |
| | | 69 | if (val.indexOf("Journal") == 0) Type = "article"; |
| | | 70 | else if ((val.indexOf("Book Section") == 0)) Type = "incollection"; |
| | | 71 | else if ((val.indexOf("Book") == 0)) Type = "book"; |
| | | 72 | else if (val.indexOf("Edited Book") == 0) { |
| | | 73 | Type = "book"; |
| | | 74 | IsEditedBook = true; |
| | | 75 | }else if (val.indexOf("Conference") == 0) // Proceedings |
| | | 76 | Type = "inproceedings"; |
| | | 77 | else if (val.indexOf("Report") == 0) // Techreport |
| | | 78 | Type = "techreport"; |
| | | 79 | else if (val.indexOf("Review") == 0) |
| | | 80 | Type = "article"; |
| | | 81 | else if (val.indexOf("Thesis") == 0) |
| | | 82 | Type = "phdthesis"; |
| | | 83 | else Type = "misc"; // |
| | | 84 | }else if (prefix.equals("7")) hm.put("edition", val); |
| | | 85 | else if (prefix.equals("C")) hm.put("address", val); |
| | | 86 | else if (prefix.equals("D")) hm.put("year", val); |
| | | 87 | else if (prefix.equals("8")) hm.put("date", val); |
| | | 88 | else if (prefix.equals("J")){ |
| | | 89 | // "Alternate journal. Let's set it only if no journal |
| | | 90 | // has been set with %B. |
| | | 91 | if (hm.get("journal") == null) hm.put("journal", val); |
| | | 92 | }else if (prefix.equals("B")){ |
| | | 93 | // This prefix stands for "journal" in a journal entry, and |
| | | 94 | // "series" in a book entry. |
| | | 95 | if (Type.equals("article")) hm.put("journal", val); |
| | | 96 | else if (Type.equals("book") || Type.equals("inbook")) hm.put( |
| | | 97 | "series", val); |
| | | 98 | else /* if (Type.equals("inproceedings")) */ |
| | | 99 | hm.put("booktitle", val); |
| | | 100 | }else if (prefix.equals("I")) { |
| | | 101 | if (Type.equals("phdthesis")) |
| | | 102 | hm.put("school", val); |
| | | 103 | else |
| | | 104 | hm.put("publisher", val); |
| | | 105 | } |
| | | 106 | // replace single dash page ranges (23-45) with double dashes (23--45): |
| | | 107 | else if (prefix.equals("P")) hm.put("pages", val.replaceAll("([0-9]) *- *([0-9])","$1--$2")); |
| | | 108 | else if (prefix.equals("V")) hm.put("volume", val); |
| | | 109 | else if (prefix.equals("N")) hm.put("number", val); |
| | | 110 | else if (prefix.equals("U")) hm.put("url", val); |
| | | 111 | else if (prefix.equals("R")) { |
| | | 112 | String doi = val; |
| | | 113 | if (doi.startsWith("doi:")) |
| | | 114 | doi = doi.substring(4); |
| | | 115 | hm.put("doi", doi); |
| | | 116 | } |
| | | 117 | else if (prefix.equals("O")) { |
| | | 118 | // Notes may contain Article number |
| | | 119 | if (val.startsWith("Artn")) { |
| | | 120 | String[] tokens = val.split("\\s"); |
| | | 121 | artnum = tokens[1]; |
| | | 122 | } |
| | | 123 | else { |
| | | 124 | hm.put("note", val); |
| | | 125 | } |
| | | 126 | } |
| | | 127 | else if (prefix.equals("K")) hm.put("keywords", val); |
| | | 128 | else if (prefix.equals("X")) hm.put("abstract", val); |
| | | 129 | else if (prefix.equals("9")){ |
| | | 130 | //Util.pr(val); |
| | | 131 | if (val.indexOf("Ph.D.") == 0) Type = "phdthesis"; |
| | | 132 | if (val.indexOf("Masters") == 0) Type = "mastersthesis"; |
| | | 133 | }else if (prefix.equals("F")) hm.put(BibtexFields.KEY_FIELD, Util |
| | | 134 | .checkLegalKey(val)); |
| | | 135 | } |
| | | 136 | |
| | | 137 | // For Edited Book, EndNote puts the editors in the author field. |
| | | 138 | // We want them in the editor field so that bibtex knows it's an edited book |
| | | 139 | if (IsEditedBook && editor.equals("")) { |
| | | 140 | editor = author; |
| | | 141 | author = ""; |
| | | 142 | } |
| | | 143 | |
| | | 144 | //fixauthorscomma |
| | | 145 | if (!author.equals("")) hm.put("author", fixAuthor(author)); |
| | | 146 | if (!editor.equals("")) hm.put("editor", fixAuthor(editor)); |
| | | 147 | //if pages missing and article number given, use the article number |
| | | 148 | if (((hm.get("pages") == null) || hm.get("pages").equals("-")) && !artnum.equals("")) |
| | | 149 | hm.put("pages", artnum); |
| | | 150 | |
| | | 151 | BibtexEntry b = new BibtexEntry(BibtexFields.DEFAULT_BIBTEXENTRY_ID, Globals |
| | | 152 | .getEntryType(Type)); // id assumes an existing database so don't |
| | | 153 | // create one here |
| | | 154 | b.setField(hm); |
| | | 155 | //if (hm.isEmpty()) |
| | | 156 | if (b.getAllFields().size() > 0) |
| | | 157 | bibitems.add(b); |
| | | 158 | |
| | | 159 | } |
| | | 160 | return bibitems; |
| | | 161 | |
| | | 162 | } |