1 package uk.ac.ebi.intenz.tools.sib.translator;
2
3 import uk.ac.ebi.intenz.tools.sib.translator.rules.*;
4 import uk.ac.ebi.xchars.SpecialCharacters;
5
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.PropertyResourceBundle;
9 import java.io.FileInputStream;
10 import java.io.IOException;
11
12 import org.apache.log4j.Logger;
13
14
15
16
17
18
19
20
21
22 public class XCharsASCIITranslator {
23
24 private static final Logger LOGGER = Logger.getLogger(XCharsASCIITranslator.class);
25
26
27
28
29
30 private List ruleGroups;
31
32
33
34
35 private static final XCharsASCIITranslator INSTANCE = new XCharsASCIITranslator();
36
37
38
39
40
41 private XCharsASCIITranslator() {
42 SpecialCharacters encoding = initEncoding();
43
44 ruleGroups = new ArrayList();
45 ruleGroups.add(ReactionRules.getInstance());
46 ruleGroups.add(DescriptionLineRules.getInstance());
47 ruleGroups.add(UnorderedRules.getInstance());
48 ruleGroups.add(OrderedRules.getInstance());
49 ruleGroups.add(TagRules.getInstance(encoding));
50 ruleGroups.add(GrammarRules.getInstance());
51 }
52
53
54
55
56
57
58
59
60 public static XCharsASCIITranslator getInstance() {
61 return INSTANCE;
62 }
63
64
65
66
67
68
69
70
71
72 public String toXCharsFormat(String text, boolean useReactionRules) {
73 if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
74 int iii = 0;
75 if (!useReactionRules) iii++;
76 for (; iii < ruleGroups.size(); iii++) {
77 RuleGroup ruleGroup = (RuleGroup) ruleGroups.get(iii);
78 if( ruleGroup instanceof TagRules ) continue;
79 text = ruleGroup.applyRules(text);
80 }
81 return text;
82 }
83
84
85
86
87
88
89
90
91
92 public List toXCharsFormat(List texts, boolean useReactionRules) {
93 if (texts == null) throw new NullPointerException("Parameter 'texts' must not be null.");
94 List translatedTexts = new ArrayList();
95 for (int iii = 0; iii < texts.size(); iii++) {
96 translatedTexts.add(this.toXCharsFormat((String) texts.get(iii), useReactionRules));
97 }
98 return translatedTexts;
99 }
100
101
102
103
104
105
106
107
108
109
110 public String toASCII(String text, boolean useReactionRules) {
111 if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
112
113 int iii = 0;
114
115 for (; iii < ruleGroups.size(); iii++) {
116 RuleGroup ruleGroup = (RuleGroup) ruleGroups.get(iii);
117
118
119 if (ruleGroup instanceof ReactionRules && !useReactionRules)
120 continue;
121 if (ruleGroup instanceof DescriptionLineRules)
122 continue;
123 text = ruleGroup.reverseRules(text);
124
125 }
126 return text;
127 }
128
129
130
131
132
133
134
135
136
137 public String toASCII(String text, boolean useReactionRules,
138 boolean useDeLineRules) {
139 return toASCII(text, useReactionRules, useDeLineRules, true);
140 }
141
142
143
144
145
146
147
148
149
150 public String toASCII(String text, boolean useReactionRules,
151 boolean useDeLineRules, boolean useGrammarRules) {
152 for (int iii = 0; iii < ruleGroups.size(); iii++) {
153 RuleGroup ruleGroup = (RuleGroup) ruleGroups.get(iii);
154 if (ruleGroup instanceof ReactionRules && !useReactionRules)
155 continue;
156 if (ruleGroup instanceof DescriptionLineRules && !useDeLineRules)
157 continue;
158 if (ruleGroup instanceof GrammarRules && !useGrammarRules)
159 continue;
160 text = ruleGroup.reverseRules(text);
161 }
162 return text;
163 }
164
165
166
167
168
169 private SpecialCharacters initEncoding () {
170 SpecialCharacters encoding = SpecialCharacters.getInstance(null);
171 return encoding;
172 }
173 }