View Javadoc

1   /*
2   Copyright (c) 2005 The European Bioinformatics Institute, and others.
3   All rights reserved. Please see the file LICENSE
4   in the root directory of this distribution.
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   * DescriptionLineRules are specific to the description line.
19   * They should be applied before other rules.
20   * Only reverse rules are applicable to this DE line currently.
21   *
22   * @author pmatos
23   * @version $id 09-May-2005 17:33:27
24   *          <p/>
25   *          History:
26   *          Developer          Date              Description<br>
27   *          pmatos            09-May-2005         Created class<br>
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    //private Map rules;
38  
39    /**
40     * Initialises the class's sole instance.
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     * Returns the sole instance of this class.
68     *
69     * If no instance is available yet then it will be created.
70     *
71     * @return the class's sole instance.
72     */
73    public static DescriptionLineRules getInstance() {
74      return INSTANCE;
75    }
76  
77    /**
78     * Not implemented
79     *
80     * @param text The text to be translated.
81     * @return the translated text.
82     * @throws NullPointerException if <code>text</code> is <code>null</code>.
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     * Applies the reverse rules.
91     *
92     * @param text The text to be translated.
93     * @return the translated text.
94     * @throws NullPointerException if <code>text</code> is <code>null</code>.
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    * This method performs the actual application of the rules.
104    *
105    * @param text The text to be translated.
106    * @param rules Map of rules to be applied.
107    * @return the translated text.
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 }