View Javadoc

1   package uk.ac.ebi.intenz.tools.sib.translator.rules;
2   
3   import org.apache.log4j.Logger;
4   import uk.ac.ebi.intenz.tools.sib.translator.XCharsASCIITranslator;
5   
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.util.*;
9   
10  /**
11   * This singleton class stores regular expression rules to be used to transform text.
12   * <p/>
13   * Each rule consists of a regular expression pattern and a replacement string. The rules will be applied in an
14   * arbitrary order.
15   *
16   * @author Michael Darsow
17   * @version $Revision: 1.3 $ $Date: 2009/05/14 15:02:28 $
18   */
19  public class UnorderedRules implements RuleGroup {
20  
21    private static final Logger LOGGER = Logger.getLogger(UnorderedRules.class);
22  
23    private static final UnorderedRules INSTANCE = new UnorderedRules();
24  
25    private Map reverseRules;
26  
27    private Map rules;
28  
29    private Map spellingRules;
30  
31    /**
32     * Initialises the class's sole instance.
33     */
34    private UnorderedRules() {
35      InputStream stream = XCharsASCIITranslator.class.getClassLoader().getResourceAsStream("unorderedRules.txt");
36  //    FileInputStream stream = null;
37      rules = new HashMap();
38      reverseRules = new HashMap();
39      spellingRules = new HashMap();
40      try {
41  //      File file = new File("rules/unorderedRules.txt");
42  //      stream = new FileInputStream(file);
43        PropertyResourceBundle prb = new PropertyResourceBundle(stream);
44        Enumeration en = prb.getKeys();
45        while (en.hasMoreElements()) {
46          String key = (String) en.nextElement();
47          String value = prb.getString(key);
48          rules.put(key, value);
49        }
50        stream.close();
51  
52  //      file = new File("rules/unorderedRules_rev.txt");
53  //      stream = new FileInputStream(file);
54        stream = XCharsASCIITranslator.class.getClassLoader().getResourceAsStream("unorderedRules_rev.txt");
55        prb = new PropertyResourceBundle(stream);
56        en = prb.getKeys();
57        while (en.hasMoreElements()) {
58          String key = (String) en.nextElement();
59          String value = prb.getString(key);
60          reverseRules.put(key, value);
61        }
62  
63        stream = XCharsASCIITranslator.class.getClassLoader().getResourceAsStream("spellingRules.txt");
64        prb = new PropertyResourceBundle(stream);
65        en = prb.getKeys();
66        while (en.hasMoreElements()) {
67          String key = (String) en.nextElement();
68          String value = prb.getString(key);
69          spellingRules.put(key, value);
70        }
71  
72      } catch (IOException e) {
73        LOGGER.error("Error while initialising 'UnorderedRules' class.", e);
74      } finally {
75        try {
76          stream.close();
77        } catch (IOException e) {
78          LOGGER.error("Error while closing input stream.", e);
79        }
80      }
81    }
82  
83    /**
84     * Returns the sole instance of this class.
85     * <p/>
86     * If no instance is available yet then it will be created.
87     *
88     * @return the class's sole instance.
89     */
90    public static UnorderedRules getInstance() {
91      return INSTANCE;
92    }
93  
94    /**
95     * Applies the rules.
96     *
97     * @param text The text to be translated.
98     * @return the translated text.
99     * @throws NullPointerException if <code>text</code> is <code>null</code>.
100    */
101   public String applyRules(String text) {
102     if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
103     text = translate(text, rules);
104     return text;
105   }
106 
107   /**
108    * Applies the reverse rules.
109    *
110    * This rule also capitalises the first letter of the given text since all affected line
111    * types start with a capital letter.
112    *
113    * @param text The text to be translated.
114    * @return the translated text.
115    * @throws NullPointerException if <code>text</code> is <code>null</code>.
116    */
117   public String reverseRules(String text) {
118     if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
119     text = translate(text, reverseRules);
120     text = translate(text, spellingRules);
121 
122     return text;
123   }
124 
125   /**
126    * This method performs the actual application of the rules.
127    *
128    * @param text  The text to be translated.
129    * @param rules Map of rules to be applied.
130    * @return the translated text.
131    */
132   private String translate(String text, Map rules) {
133     //LOGGER.debug("> " + text);
134     Set rulePatterns = rules.keySet();
135     for (Iterator it = rulePatterns.iterator(); it.hasNext();) {
136       String pattern = (String) it.next();
137       String replacement = (String) rules.get(pattern);
138       text = text.replaceAll(pattern, replacement);
139     }
140     //LOGGER.debug("< " + text);
141     return text;
142   }
143 
144 }