beea81daa414a119fa83908e8bb5b0d25dedbef6
[jalview.git] / forester / java / src / org / forester / io / parsers / phyloxml / XmlElement.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/forester
25
26 package org.forester.io.parsers.phyloxml;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30
31 import org.forester.io.parsers.util.PhylogenyParserException;
32 import org.forester.util.ForesterUtil;
33 import org.xml.sax.Attributes;
34
35 public class XmlElement {
36
37     public final static boolean           DEBUG = false;
38     private final String                  _namespaceUri;
39     private final String                  _localName;
40     private final String                  _qualifiedName;
41     private String                        _value;
42     private final HashMap<String, String> _attributes;
43     private final ArrayList<XmlElement>   _childElements;
44     private XmlElement                    _parent;
45
46     public XmlElement( final String namespaceUri,
47                        final String localName,
48                        final String qualifiedName,
49                        final Attributes attributes ) {
50         _namespaceUri = namespaceUri;
51         _localName = localName;
52         _qualifiedName = qualifiedName;
53         if ( attributes != null ) {
54             _attributes = new HashMap<String, String>( attributes.getLength() );
55             for( int i = 0; i < attributes.getLength(); ++i ) {
56                 getAttributes().put( new String( attributes.getQName( i ) ), new String( attributes.getValue( i ) ) );
57             }
58         }
59         else {
60             _attributes = new HashMap<String, String>();
61         }
62         _childElements = new ArrayList<XmlElement>();
63         _parent = null;
64     }
65
66     public void addChildElement( final XmlElement element ) {
67         element.setParent( this );
68         getChildElements().add( element );
69     }
70
71     public void appendValue( final String value ) {
72         _value = _value + value;
73     }
74
75     public String getAttribute( final String attribute_name ) {
76         if ( !isHasAttribute( attribute_name ) ) {
77             throw new IllegalArgumentException( "no attribute named [" + attribute_name + "] present in element ["
78                     + getQualifiedName() + "]" );
79         }
80         return getAttributes().get( attribute_name );
81     }
82
83     public HashMap<String, String> getAttributes() {
84         return _attributes;
85     }
86
87     public XmlElement getChildElement( final int i ) {
88         if ( ( i < 0 ) || ( i >= getNumberOfChildElements() ) ) {
89             throw new IllegalArgumentException( "attempt to get child element with index " + i + " for element with "
90                     + getNumberOfChildElements() + " child elements" );
91         }
92         return getChildElements().get( i );
93     }
94
95     ArrayList<XmlElement> getChildElements() {
96         return _childElements;
97     }
98
99     String getLocalName() {
100         return _localName;
101     }
102
103     String getNamespaceUri() {
104         return _namespaceUri;
105     }
106
107     public int getNumberOfChildElements() {
108         return getChildElements().size();
109     }
110
111     public XmlElement getParent() {
112         return _parent;
113     }
114
115     public String getQualifiedName() {
116         return _qualifiedName;
117     }
118
119     XmlElement getRoot() {
120         XmlElement e = this;
121         while ( e.getParent() != null ) {
122             e = e.getParent();
123         }
124         return e;
125     }
126
127     public boolean getValueAsBoolean() throws PhylogenyParserException {
128         boolean b = false;
129         try {
130             b = ( new Boolean( getValueAsString() ) ).booleanValue();
131         }
132         catch ( final NumberFormatException ex ) {
133             throw new PhylogenyParserException( "attempt to parse [" + getValueAsString() + "] into boolean, in "
134                     + toString() );
135         }
136         return b;
137     }
138
139     public double getValueAsDouble() throws PhylogenyParserException {
140         double d = 0.0;
141         try {
142             d = Double.parseDouble( getValueAsString() );
143         }
144         catch ( final NumberFormatException ex ) {
145             throw new PhylogenyParserException( "attempt to parse [" + getValueAsString() + "] into double, in "
146                     + toString() );
147         }
148         return d;
149     }
150
151     public int getValueAsInt() throws PhylogenyParserException {
152         int i = 0;
153         try {
154             i = Integer.parseInt( getValueAsString() );
155         }
156         catch ( final NumberFormatException ex ) {
157             throw new PhylogenyParserException( "attempt to parse [" + getValueAsString() + "] into integer, in "
158                     + toString() );
159         }
160         return i;
161     }
162
163     public String getValueAsString() {
164         if ( _value == null ) {
165             return "";
166         }
167         return _value.replaceAll( "\\s+", " " ).trim();
168     }
169
170     public boolean isHasAttribute( final String attribute_name ) {
171         return getAttributes().containsKey( attribute_name );
172     }
173
174     public boolean isHasValue() {
175         return !ForesterUtil.isEmpty( _value );
176     }
177
178     void setParent( final XmlElement parent ) {
179         _parent = parent;
180     }
181
182     /**
183      * [Careful, this does not call "new String(...)"]
184      * 
185      * @param value
186      */
187     public void setValue( final String value ) {
188         _value = value;
189         if ( XmlElement.DEBUG ) {
190             System.out.println();
191             System.out.println( "Value is \"" + value + "\" for" );
192             System.out.println( "Local name     = " + getLocalName() );
193             System.out.println( "Qualified name = " + getQualifiedName() );
194             System.out.println( "Namespace URI  = " + getNamespaceUri() );
195             System.out.print( "Attributes     : " );
196             for( final String string : getAttributes().keySet() ) {
197                 final String key = string;
198                 System.out.print( key + " = \"" + getAttributes().get( key ) + "\"  " );
199             }
200             System.out.println();
201             System.out.println();
202         }
203     }
204
205     @Override
206     public String toString() {
207         if ( getParent() != null ) {
208             return "\"" + getQualifiedName() + "\" [value: " + getValueAsString() + ", parent element: \""
209                     + getParent().getQualifiedName() + "\"]";
210         }
211         return "\"" + getQualifiedName() + "\" [value: " + getValueAsString() + "]";
212     }
213 }