initial commit
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Reference.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 // 
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 // 
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org
25
26 package org.forester.phylogeny.data;
27
28 import java.io.IOException;
29 import java.io.Writer;
30
31 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
32 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
33 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
34 import org.forester.util.ForesterUtil;
35
36 public class Reference implements PhylogenyData {
37
38     String _desc;
39     String _doi;
40
41     public Reference( final String desc ) {
42         _desc = desc;
43         _doi = "";
44     }
45
46     public Reference( final String desc, final String doi ) {
47         _desc = desc;
48         _doi = doi;
49     }
50
51     public StringBuffer asSimpleText() {
52         return new StringBuffer( getDescription() );
53     }
54
55     public StringBuffer asText() {
56         final StringBuffer sb = new StringBuffer();
57         if ( !ForesterUtil.isEmpty( getDoi() ) ) {
58             sb.append( "[doi:" );
59             sb.append( getDoi() );
60             sb.append( "] " );
61         }
62         sb.append( getDescription() );
63         return sb;
64     }
65
66     public PhylogenyData copy() {
67         return new Reference( getDescription(), getDoi() );
68     }
69
70     public String getDoi() {
71         return _doi;
72     }
73
74     public String getDescription() {
75         return _desc;
76     }
77
78     public boolean isEqual( final PhylogenyData data ) {
79         if ( ( data == null ) || ( getDescription() == null ) ) {
80             return false;
81         }
82         return ( ( Reference ) data ).getDescription().equals( getDescription() )
83                 && ( ( Reference ) data ).getDoi().equals( getDoi() );
84     }
85
86     public void setDoi( final String doi ) {
87         if ( !ForesterUtil.isEmpty( doi ) && !PhyloXmlUtil.LIT_REF_DOI_PATTERN.matcher( doi ).matches() ) {
88             throw new PhyloXmlDataFormatException( "illegal doi: [" + doi + "]" );
89         }
90         _doi = doi;
91     }
92
93     public void setValue( final String value ) {
94         _desc = value;
95     }
96
97     public StringBuffer toNHX() {
98         throw new UnsupportedOperationException();
99     }
100
101     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
102         writer.write( ForesterUtil.LINE_SEPARATOR );
103         writer.write( indentation );
104         PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.REFERENCE, PhyloXmlMapping.REFERENCE_DOI_ATTR, getDoi() );
105         if ( !ForesterUtil.isEmpty( getDescription() ) ) {
106             PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.REFERENCE_DESC, getDescription(), indentation );
107         }
108         writer.write( ForesterUtil.LINE_SEPARATOR );
109         writer.write( indentation );
110         PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.REFERENCE );
111     }
112
113     @Override
114     public String toString() {
115         return asText().toString();
116     }
117 }