View Javadoc
1   package uk.ac.ebi.intenz.tools.sib.translator.rules;
2   
3   import org.apache.log4j.Logger;
4   import uk.ac.ebi.xchars.SpecialCharacters;
5   import uk.ac.ebi.xchars.domain.EncodingType;
6   
7   
8   /**
9    * This singleton class stores regular expression rules to be used to transform text.
10   * <p/>
11   * Each rule consists of a regular expression pattern and a replacement string. The rules will be applied in an
12   * arbitrary order.
13   *
14   * @author Michael Darsow
15   * @version $Revision: 1.2 $ $Date: 2008/11/11 12:01:24 $
16   */
17  public class TagRules implements RuleGroup {
18  
19    private static final Logger LOGGER = Logger.getLogger(TagRules.class);
20  
21    private static TagRules INSTANCE;
22    SpecialCharacters encoding;
23  
24    private final String[][] RULES =
25            {
26           //   {"\\<[^\\s].*?[^\\s]\\>", ""},
27               {"\\<b\\>(.*?)\\<\\/b\\>","$1"},
28               {"\\<i\\>(.*?)\\<\\/i\\>","$1"},
29               {"\\<a\\s(.+?)\\>(.+?)\\<\\/a\\>","$2"},
30               {"\\<ring\\>p\\<\\/ring\\>","\\<ringsugar\\>p\\<\\/ringsugar\\>"},
31               {"\\<ring\\>f\\<\\/ring\\>","\\<ringsugar\\>f\\<\\/ringsugar\\>"},
32            };
33  
34    /**
35     * Nothing special is happening here.
36     */
37    private TagRules(SpecialCharacters encoding) {
38     this.encoding = encoding;
39    }
40  
41  
42    /**
43     * Returns the sole instance of this class.
44     *
45     * If no instance is available yet then it will be created.
46     *
47     * @return the class sole instance.
48     */
49    public static TagRules getInstance(SpecialCharacters encoding) {
50       if( INSTANCE == null){
51          synchronized (TagRules.class){
52             if(INSTANCE == null)
53              INSTANCE = new TagRules(encoding);
54          }
55       }
56      return INSTANCE;
57    }
58  
59    /**
60     * Applies the rules.
61     *
62     * @param text The text to be translated.
63     * @return the translated text.
64     * @throws NullPointerException if <code>text</code> is <code>null</code>.
65     */
66    public String applyRules(String text) {
67      if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
68      return translate(text, RULES);
69    }
70  
71    /**
72     * Calls {@link TagRules#applyRules(String)} only.
73     *
74     * @param text The text to be translated.
75     * @return the translated text.
76     * @throws NullPointerException if <code>text</code> is <code>null</code>.
77     */
78    public String reverseRules(String text) {
79  //    if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
80      return applyRules(text);
81    }
82  
83    /**
84     * This method performs the actual application of the rules.
85     *
86     * @param text The text to be translated.
87     * @param rules Map of rules to be applied.
88     * @return the translated text.
89     */
90    private String translate(String text, String[][] rules) {
91      // Encode the special characters
92      text = this.encoding.xml2Display(text, EncodingType.SWISSPROT_CODE);
93      for (int iii = 0; iii < rules.length; iii++) {
94        String[] rule = rules[iii];
95        text = text.replaceAll(rule[0], rule[1]);
96      }    
97      return text;
98    }
99  
100 }