Coverage Report - net.sf.jabref.journals.JournalAbbreviations
 
Classes in this File Line Coverage Branch Coverage Complexity
JournalAbbreviations
39%
53/133
20%
13/62
3.526
JournalAbbreviations$1
31%
5/16
12%
1/8
3.526
JournalAbbreviations$2
0%
0/2
N/A
3.526
 
 1  
 package net.sf.jabref.journals;
 2  
 
 3  
 import java.awt.event.ActionEvent;
 4  
 import java.awt.event.ActionListener;
 5  
 import java.io.*;
 6  
 import java.net.URL;
 7  
 import java.util.*;
 8  
 
 9  
 import javax.swing.JButton;
 10  
 import javax.swing.JComponent;
 11  
 import javax.swing.table.DefaultTableModel;
 12  
 import javax.swing.table.TableModel;
 13  
 import javax.swing.undo.CompoundEdit;
 14  
 import javax.swing.undo.UndoManager;
 15  
 
 16  
 import net.sf.jabref.*;
 17  
 import net.sf.jabref.undo.UndoableFieldChange;
 18  
 import net.sf.jabref.util.CaseChanger;
 19  
 
 20  
 /**
 21  
  * Created by IntelliJ IDEA.
 22  
  * User: alver
 23  
  * Date: Sep 16, 2005
 24  
  * Time: 10:49:08 PM
 25  
  * To browseOld this template use File | Settings | File Templates.
 26  
  */
 27  
 public class JournalAbbreviations {
 28  
 
 29  669552521515
     static String TOOLTIPTEXT = "<HTML>"+Globals.lang("Switches between full and abbreviated journal name "
 30  
         +"if the journal name is known.")
 31  
         +"<BR>"+Globals.lang("To set up, go to <B>Tools -> Manage journal abbreviations</B>")+".</HTML>";
 32  676441174283
     TreeMap<String, String> fullNameKeyed = new TreeMap<String, String>();
 33  676441174283
     HashMap<String, String> abbrNameKeyed = new HashMap<String, String>();
 34  676441174283
     HashMap<String, String> abbrNoDotsToAbbr = new HashMap<String, String>();
 35  676441174283
     TreeMap<String, String> all = new TreeMap<String, String>();
 36  676441174283
     CaseChanger caseChanger = new CaseChanger();
 37  
 
 38  1757584234
     public JournalAbbreviations() {
 39  
         
 40  1757584234
     }
 41  
 
 42  674627760356
     public JournalAbbreviations(String resource) {
 43  674627760356
         readJournalList(resource);
 44  674627760356
     }
 45  
 
 46  55829693
     public JournalAbbreviations(File file) throws FileNotFoundException {
 47  55829693
         readJournalList(file);
 48  0
     }
 49  
 
 50  
     /**
 51  
      * Get an iterator for the known journals in alphabetical order.
 52  
      * @return Iterator for journal full names
 53  
      */
 54  
     public Iterator<String> fullNameIterator() {
 55  0
         return fullNameKeyed.keySet().iterator();
 56  
     }
 57  
 
 58  
     public boolean isKnownName(String journalName) {
 59  326547
         String s = journalName.toLowerCase();
 60  326547
         return ((fullNameKeyed.get(s) != null) || (abbrNameKeyed.get(s) != null) || (abbrNoDotsToAbbr.get(s) != null));
 61  
     }
 62  
 
 63  
     public boolean isAbbreviatedName(String journalName) {
 64  0
         String s = journalName.toLowerCase();
 65  0
         return ((abbrNameKeyed.get(s) != null) || (abbrNoDotsToAbbr.get(s) != null));
 66  
     }
 67  
 
 68  
     public String dotsToNodots(String name) {
 69  141671829674760
         return name.replaceAll("\\.", " ").replaceAll("  ", " ").trim();
 70  
     }
 71  
 
 72  
     /**
 73  
      * Attempts to get the abbreviated name of the journal given. Returns null if no
 74  
      * abbreviated name is known.
 75  
      * @param journalName The journal name to abbreviate.
 76  
      * @param withDots True if the abbreviation should have dots.
 77  
      * if only the first character should be.
 78  
      * @return The abbreviated name, or null if it couldn't be found.
 79  
      */
 80  
     public String getAbbreviatedName(String journalName, boolean withDots) {
 81  0
         String s = journalName.toLowerCase();
 82  
         String abbr;
 83  0
         if (fullNameKeyed.containsKey(s)) {
 84  0
             abbr = fullNameKeyed.get(s);
 85  
         }
 86  0
         else if (abbrNameKeyed.containsKey(s)) {
 87  0
             abbr = journalName;
 88  
         }
 89  0
         else if (abbrNoDotsToAbbr.containsKey(s)) {
 90  0
             abbr = abbrNoDotsToAbbr.get(s);
 91  
         } else
 92  0
             return null;
 93  
 
 94  0
         if (!withDots) {
 95  0
             abbr = dotsToNodots(abbr);
 96  
         }
 97  
 
 98  0
         return abbr;
 99  
     }
 100  
 
 101  
     /**
 102  
      * Attempts to get the full name of the abbreviation given. Returns null if no
 103  
      * full name is known.
 104  
      * @param journalName The abbreviation to resolve.
 105  
      * @return The full name, or null if it couldn't be found.
 106  
      */
 107  
     public String getFullName(String journalName) {
 108  
         // Normalize name first:
 109  0
         String s = getAbbreviatedName(journalName, true);
 110  0
         if (s != null) 
 111  0
             s = s.toLowerCase();
 112  
         else
 113  0
             return null;
 114  0
         Object o = abbrNameKeyed.get(s);
 115  0
         if (o == null) {
 116  0
             if (fullNameKeyed.containsKey(s))
 117  0
                 o = s;
 118  
             else
 119  0
                 return null;
 120  
         }
 121  0
         s = (String)o;
 122  0
         return s;//caseChanger.changeCase(s, CaseChanger.UPPER_EACH_FIRST);
 123  
     }
 124  
 
 125  
     public void readJournalList(String resourceFileName) {
 126  674627760356
         URL url = JournalAbbreviations.class.getResource(resourceFileName);
 127  
         try {
 128  674627760356
             readJournalList(new InputStreamReader(url.openStream()));
 129  0
         } catch (FileNotFoundException e) {
 130  0
             e.printStackTrace();
 131  0
         } catch (IOException e) {
 132  0
             e.printStackTrace();
 133  674627760356
         }
 134  674627760356
     }
 135  
 
 136  
     public void readJournalList(File file) throws FileNotFoundException {
 137  1452243278
         readJournalList(new FileReader(file));
 138  0
     }
 139  
 
 140  
     /**
 141  
      * Read the given file, which should contain a list of journal names and their
 142  
      * abbreviations. Each line should be formatted as: "Full Journal Name=Abbr. Journal Name"
 143  
      * @param in
 144  
      */
 145  
     public void readJournalList(Reader in) throws FileNotFoundException {
 146  674627760356
         BufferedReader reader = new BufferedReader(in);
 147  
         try {
 148  
             String line;
 149  234770460603888
             while ((line = reader.readLine()) != null) {
 150  
                 //System.out.println(line);
 151  234095832843532
                 if (line.startsWith("#"))
 152  0
                     continue;
 153  234095832843532
                 String[] parts = line.split("=");
 154  234095832843532
                 if (parts.length == 2) {
 155  141671829674760
                     String fullName = parts[0].trim();
 156  141671829674760
                     String fullNameLC = fullName.toLowerCase();
 157  141671829674760
                     String abbrName = parts[1].trim();
 158  141671829674760
                     if (abbrName.indexOf(';') >= 0) {
 159  0
                         String[] restParts = abbrName.split(";");
 160  0
                         abbrName = restParts[0];
 161  
                     }
 162  141671829674760
                     String abbrNameLC = abbrName.toLowerCase();
 163  141671829674760
                     String abbrNoDots = dotsToNodots(abbrName);
 164  141671829674760
                     String abbrNoDotsLC = abbrNoDots.toLowerCase();
 165  
                     //System.out.println(abbrNoDots);
 166  141671829674760
                     if ((fullName.length()>0) && (abbrName.length()>0)) {
 167  
                         //System.out.println("'"+fullName+"' : '"+abbrNoDots+"'");
 168  141671829674760
                         fullNameKeyed.put(fullNameLC, abbrName);
 169  141671829674760
                         abbrNameKeyed.put(abbrNameLC, fullName);
 170  141671829674760
                         abbrNoDotsToAbbr.put(abbrNoDotsLC, abbrName);
 171  141671829674760
                         all.put(fullName, abbrName);
 172  
                     }
 173  
                 }
 174  234095832843532
             }
 175  
 
 176  0
         } catch (IOException ex) {
 177  0
             ex.printStackTrace();
 178  
         } finally {
 179  0
             try {
 180  674627760356
                 reader.close();
 181  0
             } catch (IOException ex2) {
 182  0
                 ex2.printStackTrace();
 183  674627760356
             }
 184  0
         }
 185  674627760356
     }
 186  
 
 187  
     /**
 188  
      * Abbreviate the journal name of the given entry.
 189  
      * @param database The database the entry belongs to, or null if no database.
 190  
      * @param entry The entry to be treated.
 191  
      * @param fieldName The field name (e.g. "journal")
 192  
      * @param ce If the entry is changed, add an edit to this compound.
 193  
      * @param withDots True if the abbreviations should have dots.
 194  
      * @return true if the entry was changed, false otherwise.
 195  
      */
 196  
     public boolean abbreviate(BibtexDatabase database, BibtexEntry entry,
 197  
                               String fieldName, CompoundEdit ce, boolean withDots) {
 198  58955
         String text = entry.getField(fieldName);
 199  58955
         if (text == null)
 200  58955
             return false;
 201  0
         String origText = text;
 202  0
         if (database != null)
 203  0
             text = database.resolveForStrings(text);
 204  0
         if (isKnownName(text) && !isAbbreviatedName(text)) {
 205  0
             String newText = getAbbreviatedName(text, withDots);
 206  0
             if (newText == null)
 207  0
                 return false;
 208  0
             entry.setField(fieldName, newText);
 209  0
             ce.addEdit(new UndoableFieldChange(entry, fieldName, origText, newText));
 210  0
             return true;
 211  
         } else {
 212  0
             String unabbr = getFullName(text);
 213  0
             if (unabbr != null) {
 214  0
                 String newText = getAbbreviatedName(unabbr, withDots);
 215  0
                 if (newText == null)
 216  0
                     return false;
 217  0
                 entry.setField(fieldName, newText);
 218  0
                 ce.addEdit(new UndoableFieldChange(entry, fieldName, origText, newText));
 219  0
                 return true;
 220  
             } else
 221  0
                 return false;
 222  
         }
 223  
     }
 224  
 
 225  
     /**
 226  
      * Unabbreviate the journal name of the given entry.
 227  
      * @param entry The entry to be treated.
 228  
      * @param fieldName The field name (e.g. "journal")
 229  
      * @param ce If the entry is changed, add an edit to this compound.
 230  
      * @return true if the entry was changed, false otherwise.
 231  
      */
 232  
     public boolean unabbreviate(BibtexDatabase database, BibtexEntry entry,
 233  
                                 String fieldName, CompoundEdit ce) {
 234  28017
         String text = entry.getField(fieldName);
 235  28017
         if (text == null)
 236  28017
             return false;
 237  0
         String origText = text;
 238  0
         if (database != null)
 239  0
             text = database.resolveForStrings(text);
 240  0
         if (isKnownName(text) && isAbbreviatedName(text)) {
 241  0
             String newText = getFullName(text);
 242  0
             if (newText == null)
 243  0
                 return false;
 244  0
             entry.setField(fieldName, newText);
 245  0
             ce.addEdit(new UndoableFieldChange(entry, fieldName, origText, newText));
 246  0
             return true;
 247  
         } else
 248  0
             return false;
 249  
     }
 250  
 
 251  
 
 252  
     public Map<String, String> getJournals() {
 253  45300061003
         return Collections.unmodifiableMap(all);
 254  
     }
 255  
 
 256  
     /**
 257  
      * Create a control panel for the entry editor's journal field, to toggle
 258  
      * abbreviated/full journal name
 259  
      * @param editor The FieldEditor for the journal field.
 260  
      * @return The control panel for the entry editor.
 261  
      */
 262  
     public static JComponent getNameSwitcher(final EntryEditor entryEditor, final FieldEditor editor,
 263  
                                       final UndoManager undoManager) {
 264  11456665
         JButton button = new JButton(Globals.lang("Toggle abbreviation"));
 265  11456665
         button.setToolTipText(TOOLTIPTEXT);
 266  11456665
         button.addActionListener(new ActionListener() {
 267  11456665
             boolean withDots = true;
 268  
             public void actionPerformed(ActionEvent actionEvent) {
 269  326547
                 String text = editor.getText();
 270  326547
                 if (Globals.journalAbbrev.isKnownName(text)) {
 271  
                     String s;
 272  0
                     if (Globals.journalAbbrev.isAbbreviatedName(text)) {
 273  
                         //System.out.println(withDots);
 274  0
                         if (!withDots) {
 275  0
                             s = Globals.journalAbbrev.getAbbreviatedName(text, false);
 276  0
                             withDots = true;
 277  
                         } else {
 278  0
                             s = Globals.journalAbbrev.getFullName(text);
 279  
                         }
 280  
                     }
 281  
                     else {
 282  0
                         s = Globals.journalAbbrev.getAbbreviatedName(text, true);
 283  0
                         withDots = false;
 284  
                     }
 285  
 
 286  0
                     if (s != null) {
 287  0
                         editor.setText(s);
 288  0
                         entryEditor.storeFieldAction.actionPerformed(new ActionEvent(editor, 0, ""));
 289  0
                         undoManager.addEdit(new UndoableFieldChange(entryEditor.getEntry(), editor.getFieldName(),
 290  
                                 text, s));
 291  
                     }
 292  
                 }
 293  326547
             }
 294  
         });
 295  
 
 296  11456665
         return button;
 297  
     }
 298  
 
 299  
     public TableModel getTableModel() {
 300  0
         Object[][] cells = new Object[fullNameKeyed.size()][2];
 301  0
         int row = 0;
 302  0
         for (Iterator<String> i=fullNameIterator(); i.hasNext();) {
 303  0
             String name = i.next();
 304  0
             cells[row][0] = getFullName(name);
 305  0
             cells[row][1] = getAbbreviatedName(name, true);
 306  0
             row++;
 307  0
         }
 308  0
         DefaultTableModel tableModel = new DefaultTableModel(cells, new Object[] {Globals.lang("Full name"),
 309  0
             Globals.lang("Abbreviation")}) {
 310  
 
 311  
             public boolean isCellEditable(int row, int column) {
 312  0
                 return false;
 313  
             }
 314  
         };
 315  
 
 316  0
         return tableModel;
 317  
     }
 318  
 
 319  
 }
 320