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  
12  
13  
14  
15  
16  
17  
18  
19  
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  
33   
34    private ReactionRules() {
35      InputStream stream = XCharsASCIITranslator.class.getClassLoader().getResourceAsStream("reactionRules.txt");
36  
37      rules = new HashMap();
38      reverseRules = new HashMap();
39      try {
40  
41  
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  
52  
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  
74  
75  
76  
77  
78   
79    public static ReactionRules getInstance() {
80      return INSTANCE;
81    }
82  
83    
84  
85  
86  
87  
88  
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  
98  
99  
100 
101 
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 
111 
112 
113 
114 
115 
116   private String translate(String text, Map rules) {
117     Set rulePatterns = rules.keySet();
118     for (Iterator it = rulePatterns.iterator(); it.hasNext();) {
119       
120       String pattern = (String) it.next();
121       
122       String replacement = (String) rules.get(pattern);
123       text = text.replaceAll(pattern, replacement);
124       
125     }
126     return text;
127   }
128 
129 }