1
2
3
4
5
6 package uk.ac.ebi.intenz.tools.sib.translator.rules;
7
8 import org.apache.log4j.Logger;
9
10 import java.util.*;
11 import java.io.InputStream;
12 import java.io.IOException;
13
14 import uk.ac.ebi.intenz.tools.sib.translator.XCharsASCIITranslator;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class DescriptionLineRules implements RuleGroup {
30
31 private static final Logger LOGGER = Logger.getLogger(ReactionRules.class);
32
33 private static final DescriptionLineRules INSTANCE = new DescriptionLineRules();
34
35 private Map reverseRules;
36
37
38
39
40
41
42 private DescriptionLineRules() {
43 InputStream stream = null;
44 reverseRules = new HashMap();
45 try{
46 stream = XCharsASCIITranslator.class.getClassLoader().getResourceAsStream("deRules_rev.txt");
47 PropertyResourceBundle prb = new PropertyResourceBundle(stream);
48 Enumeration en = prb.getKeys();
49 while (en.hasMoreElements()) {
50 String key = (String) en.nextElement();
51 String value = prb.getString(key);
52 reverseRules.put(key, value);
53 }
54 } catch (IOException e) {
55 LOGGER.error("Error while initialising 'DescriptionLineRules' class.", e);
56 } finally {
57 try {
58 if (stream!=null)
59 stream.close();
60 } catch (IOException e) {
61 LOGGER.error("Error while closing input stream.", e);
62 }
63 }
64 }
65
66
67
68
69
70
71
72
73 public static DescriptionLineRules getInstance() {
74 return INSTANCE;
75 }
76
77
78
79
80
81
82
83
84 public String applyRules(String text) {
85 if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
86 return text;
87 }
88
89
90
91
92
93
94
95
96 public String reverseRules(String text) {
97 if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
98 text = translate(text, reverseRules);
99 return text;
100 }
101
102
103
104
105
106
107
108
109 private String translate(String text, Map rules) {
110 Set rulePatterns = rules.keySet();
111 for (Iterator it = rulePatterns.iterator(); it.hasNext();) {
112 String pattern = (String) it.next();
113 String replacement = (String) rules.get(pattern);
114 text = text.replaceAll(pattern, replacement);
115 }
116 return text;
117 }
118 }