View Javadoc

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   * This class translates <code>ENZYME</code> data into the <code>IntEnz</code> format and vice versa.
16   * <p/>
17   * The rules applied can be found in the <code>rules</code> package (see also: {@link uk.ac.ebi.intenz.tools.sib.translator.rules.RuleGroup}).
18   *
19   * @author Michael Darsow
20   * @version $Revision: 1.2 $ $Date: 2008/11/11 12:01:24 $
21   */
22  public class XCharsASCIITranslator {
23  
24     private static final Logger LOGGER = Logger.getLogger(XCharsASCIITranslator.class);
25  
26  
27    /**
28     * List of rules used to translate strings.
29     */
30    private List ruleGroups;
31  
32    /**
33     * The sole instance of this class.
34     */
35    private static final XCharsASCIITranslator INSTANCE = new XCharsASCIITranslator();
36  
37  
38    /**
39     * Initialises all rules.
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     * Returns the sole instance of this class.
57     *
58     * @return the sole instance of this class.
59     */
60    public static XCharsASCIITranslator getInstance() {
61      return INSTANCE;
62    }
63  
64    /**
65     * Translates a string into the <code>IntEnz</code> format.
66     *
67     * @param text             String to translate.
68     * @param useReactionRules Set to <code>true</code> if a reaction should be translated
69     * @return the translated string.
70     * @throws NullPointerException if <code>text</code> is <code>null</code>.
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++; // skip reaction rules if desired
76      for (; iii < ruleGroups.size(); iii++) {
77        RuleGroup ruleGroup = (RuleGroup) ruleGroups.get(iii);
78         if( ruleGroup instanceof TagRules ) continue;  // skip tag rules
79        text = ruleGroup.applyRules(text);
80      }
81      return text;
82    }
83  
84    /**
85     * Just calls {@link XCharsASCIITranslator#toXCharsFormat(String, boolean)}  method for each item.
86     *
87     * @param texts            List of strings.
88     * @param useReactionRules Set to <code>true</code> if a reaction should be translated
89     * @return a list of translated strings.
90     * @throws NullPointerException if <code>texts</code> is <code>null</code>.
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    * Translates a XChars encoded string into its ASCII representation.
103    *
104    * @deprecated Use toAscii(String text, boolean useReactionRules, boolean useDeLineRules)
105    * @param text             The string to be translated.
106    * @param useReactionRules <code>true</code>, if specific reaction rules should be applied to the translation process.
107    * @return the ASCII representation.
108    * @throws NullPointerException if <code>texts</code> is <code>null</code>.
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     //if (!useReactionRules) iii++; // skip reaction rules if desired
115     for (; iii < ruleGroups.size(); iii++) {
116        RuleGroup ruleGroup = (RuleGroup) ruleGroups.get(iii);
117 
118        // skip reaction rules if desired
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    * Translates a XChars encoded string into its ASCII representation.
131    *
132    * @param text             The string to be translated.
133    * @param useReactionRules <code>true</code>, if specific reaction rules should be applied to the translation process.
134    * @return the ASCII representation.
135    * @throws NullPointerException if <code>texts</code> is <code>null</code>.
136    */
137   public String toASCII(String text, boolean useReactionRules,
138 		  boolean useDeLineRules) {
139     return toASCII(text, useReactionRules, useDeLineRules, true);
140   }
141 
142 	/**
143 	 * Translates a XChars encoded string into its ASCII representation.
144 	 * @param text The string to be translated.
145 	 * @param useReactionRules use the reaction rules?
146 	 * @param useDeLineRules use the DE line rules?
147 	 * @param useGrammarRules use the grammar rules?
148 	 * @return
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     * Initialise the SpecialCharacters package as we can't encode without it.
167     * @return
168     */
169    private SpecialCharacters initEncoding () {
170       SpecialCharacters encoding = SpecialCharacters.getInstance(null);
171       return encoding;
172    }
173 }