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   * These rules should only be applied on enzyme reaction data.
17   *
18   * @author Michael Darsow
19   * @version $Revision: 1.3 $ $Date: 2009/05/14 15:02:29 $
20   */
21  public class ReactionRules implements RuleGroup {
22  
23    private static final Logger LOGGER = Logger.getLogger(ReactionRules.class);
24  
25    private static final ReactionRules INSTANCE = new ReactionRules();
26  
27    private Map reverseRules;
28  
29    private Map rules;
30  
31    /**
32     * Initialises the class sole instance.
33     */ 
34    private ReactionRules() {
35      InputStream stream = XCharsASCIITranslator.class.getClassLoader().getResourceAsStream("reactionRules.txt");
36  //    FileInputStream stream = null;
37      rules = new HashMap();
38      reverseRules = new HashMap();
39      try {
40  //      File file = new File("rules/reactionRules.txt");
41  //      stream = new FileInputStream(file);
42        PropertyResourceBundle prb = new PropertyResourceBundle(stream);
43        Enumeration en = prb.getKeys();
44        while (en.hasMoreElements()) {
45          String key = (String) en.nextElement();
46          String value = prb.getString(key);
47          rules.put(key, value);
48        }
49        stream.close();
50  
51  //      file = new File("rules/reactionRules_rev.txt");
52  //      stream = new FileInputStream(file);
53        stream = XCharsASCIITranslator.class.getClassLoader().getResourceAsStream("reactionRules_rev.txt");
54        prb = new PropertyResourceBundle(stream);
55        en = prb.getKeys();
56        while (en.hasMoreElements()) {
57          String key = (String) en.nextElement();
58          String value = prb.getString(key);
59          reverseRules.put(key, value);
60        }
61      } catch (IOException e) {
62        LOGGER.error("Error while initialising 'ReactionRules' class.", e);
63      } finally {
64        try {
65          stream.close();
66        } catch (IOException e) {
67          LOGGER.error("Error while closing input stream.", e);
68        }
69      }
70    }
71  
72    /**
73     * Returns the sole instance of this class.
74     * 
75     * If no instance is available yet then it will be created.
76     * 
77     * @return the class's sole instance.
78     */ 
79    public static ReactionRules getInstance() {
80      return INSTANCE;
81    }
82  
83    /**
84     * Applies the rules.
85     * 
86     * @param text The text to be translated.
87     * @return the translated text.
88     * @throws NullPointerException if <code>text</code> is <code>null</code>.
89     */ 
90    public String applyRules(String text) {
91      if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
92      text = translate(text, rules);
93      return text;
94    }
95  
96    /**
97     * Applies the reverse rules.
98     * 
99     * @param text The text to be translated.
100    * @return the translated text.
101    * @throws NullPointerException if <code>text</code> is <code>null</code>.
102    */
103   public String reverseRules(String text) {
104     if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
105     text = translate(text, reverseRules);
106     return text;
107   }
108 
109   /**
110    * This method performs the actual application of the rules.
111    *
112    * @param text The text to be translated.
113    * @param rules Map of rules to be applied.
114    * @return the translated text.
115    */
116   private String translate(String text, Map rules) {
117     Set rulePatterns = rules.keySet();
118     for (Iterator it = rulePatterns.iterator(); it.hasNext();) {
119       //LOGGER.debug("IN: " + text);
120       String pattern = (String) it.next();
121       //LOGGER.debug("REACTION RULE: " + pattern);
122       String replacement = (String) rules.get(pattern);
123       text = text.replaceAll(pattern, replacement);
124       //LOGGER.debug("OUT: " + text);
125     }
126     return text;
127   }
128 
129 }