View Javadoc

1   package uk.ac.ebi.intenz.domain.reference;
2   
3   import uk.ac.ebi.xchars.SpecialCharacters;
4   import uk.ac.ebi.xchars.domain.EncodingType;
5   import uk.ac.ebi.intenz.domain.constants.EnzymeSourceConstant;
6   import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
7   
8   import java.util.regex.Matcher;
9   import java.util.regex.Pattern;
10  
11  /**
12   * This class represents a reference and serves as the super class for all types of references.
13   *
14   * @author Michael Darsow
15   * @version $Revision: 1.2 $ $Date: 2008/01/28 12:33:10 $
16   */
17  public class Reference {
18    Long pubId;
19  
20    String authors;
21  
22    String title;
23  
24    String year;
25  
26    EnzymeViewConstant view;
27  
28    EnzymeSourceConstant source;
29  
30    /**
31     * Object cannot be created outside this class.
32     *
33     * @param id      The pubId.
34     * @param authors The string containing all author names.
35     * @param title   The title of the publication.
36     * @param year    The year of the publication.
37     */
38    Reference(Long id, String authors, String title, String year, EnzymeViewConstant enzymeViewConstant, EnzymeSourceConstant source) {
39      this.pubId = id;
40      if (authors == null || title == null || year == null) throw new NullPointerException("No parameter must be 'null'");
41      this.authors = authors;
42      this.title = title;
43      this.year = year;
44      this.view = enzymeViewConstant;
45      this.source = source;
46    }
47  
48    /**
49     * Standard <code>equals()</code> method.
50     *
51     * @param o The object to be compared to this one.
52     * @return <code>true</code> if <code>o</code> is equal to this object.
53     */
54    public boolean equals(Object o) {
55      if (this == o) return true;
56      if (!(o instanceof Reference)) return false;
57  
58      final Reference reference = (Reference) o;
59  
60      if (authors != null ? !authors.equals(reference.authors) : reference.authors != null) return false;
61      if (view != null ? !view.equals(reference.view) : reference.view !=
62              null) return false;
63      if (pubId != null ? !pubId.equals(reference.pubId) : reference.pubId != null) return false;
64      if (source != null ? !source.equals(reference.source) : reference.source != null) return false;
65      if (title != null ? !title.equals(reference.title) : reference.title != null) return false;
66      if (year != null ? !year.equals(reference.year) : reference.year != null) return false;
67  
68      return true;
69    }
70  
71    /**
72     * Generates a hash code for this object.
73     *
74     * @return the hash code.
75     */
76    public int hashCode() {
77      int result;
78      result = (pubId != null ? pubId.hashCode() : 0);
79      result = 29 * result + (authors != null ? authors.hashCode() : 0);
80      result = 29 * result + (title != null ? title.hashCode() : 0);
81      result = 29 * result + (year != null ? year.hashCode() : 0);
82      result = 29 * result + (view != null ? view.hashCode() : 0);
83      result = 29 * result + (source != null ? source.hashCode() : 0);
84      return result;
85    }
86  
87    // --------------------  GETTER -----------------------
88  
89    public Long getPubId() {
90        return pubId;
91      }
92  
93    public String getAuthors() {
94      return authors;
95    }
96  
97    public String getTitle() {
98      return title;
99    }
100 
101   public String getYear() {
102     return year;
103   }
104 
105   public EnzymeViewConstant getView() {
106     return view;
107   }
108 
109   public EnzymeSourceConstant getSource() {
110     return source;
111   }
112 
113   // TODO: Put this method's content somewhere else.
114   public String toXML(int number, boolean intenzTextXML) {
115     StringBuffer xmlStringBuffer = new StringBuffer();
116     if (intenzTextXML) {
117       SpecialCharacters encoding = SpecialCharacters.getInstance(null);
118 
119       xmlStringBuffer.append("<number>");
120       xmlStringBuffer.append(number + ".");
121       xmlStringBuffer.append("</number>");
122       xmlStringBuffer.append("<authors>");
123       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(authors)));
124       xmlStringBuffer.append("</authors>");
125 //      if (encoding.isSpecialCharactersElement(authors)) { // Store text only version as well.
126       xmlStringBuffer.append("<authors>");
127       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(authors, EncodingType.SWISSPROT_CODE)));
128       xmlStringBuffer.append("</authors>");
129 //      }
130       xmlStringBuffer.append("<title>");
131       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(title)));
132       xmlStringBuffer.append("</title>");
133 //      if (encoding.isSpecialCharactersElement(title)) { // Store text only version as well.
134       xmlStringBuffer.append("<title>");
135       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(title, EncodingType.SWISSPROT_CODE)));
136       xmlStringBuffer.append("</title>");
137 //      }
138       xmlStringBuffer.append("<year>");
139       xmlStringBuffer.append(year);
140       xmlStringBuffer.append("</year>");
141     } else {
142       xmlStringBuffer.append("      <number>");
143       xmlStringBuffer.append(number + ".");
144       xmlStringBuffer.append("</number>\n");
145       xmlStringBuffer.append("      <authors>");
146       xmlStringBuffer.append(encodeTags(removeFormatting(authors)));
147       xmlStringBuffer.append("</authors>\n");
148       xmlStringBuffer.append("      <title>");
149       xmlStringBuffer.append(encodeTags(removeFormatting(title)));
150       xmlStringBuffer.append("</title>\n");
151       xmlStringBuffer.append("      <year>");
152       xmlStringBuffer.append(year);
153       xmlStringBuffer.append("</year>\n");
154     }
155     return xmlStringBuffer.toString();
156   }
157 
158   private String encodeTags(String name) {
159     Pattern p = Pattern.compile("<");
160     Matcher m = p.matcher(name);
161 
162     Pattern p2 = Pattern.compile(">");
163     Matcher m2 = p2.matcher(m.replaceAll("&lt;"));
164 
165     return m2.replaceAll("&gt;");
166   }
167 
168   private String removeFormatting(String text) {
169     text = text.replaceAll("\\<small\\>", "");
170     text = text.replaceAll("\\<\\/small\\>", "");
171     text = text.replaceAll("\\<b\\>", "");
172     text = text.replaceAll("\\<\\/b\\>", "");
173     text = text.replaceAll("\\<i\\>", "");
174     text = text.replaceAll("\\<\\/i\\>", "");
175     return text;
176   }
177 }